Skip to content

Commit

Permalink
Merge pull request #743 from microsoft/fix742
Browse files Browse the repository at this point in the history
Be careful about using internal attributes declared in other assemblies
  • Loading branch information
AArnott committed Oct 31, 2022
2 parents 6d86f35 + 2579898 commit 4be534f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,10 @@ public Generator(string metadataLibraryPath, Docs? docs, GeneratorOptions option
this.canCallCreateSpan = this.compilation?.GetTypeByMetadataName(typeof(MemoryMarshal).FullName)?.GetMembers("CreateSpan").Any() is true;
this.canUseUnsafeAsRef = this.compilation?.GetTypeByMetadataName(typeof(Unsafe).FullName)?.GetMembers("AsRef").Any() is true;
this.canUseUnsafeNullRef = this.compilation?.GetTypeByMetadataName(typeof(Unsafe).FullName)?.GetMembers("NullRef").Any() is true;
this.unscopedRefAttributePredefined = this.compilation?.GetTypeByMetadataName("System.Diagnostics.CodeAnalysis.UnscopedRefAttribute") is not null;
this.unscopedRefAttributePredefined = this.FindTypeSymbolIfAlreadyAvailable("System.Diagnostics.CodeAnalysis.UnscopedRefAttribute") is not null;
this.getDelegateForFunctionPointerGenericExists = this.compilation?.GetTypeByMetadataName(typeof(Marshal).FullName)?.GetMembers(nameof(Marshal.GetDelegateForFunctionPointer)).Any(m => m is IMethodSymbol { IsGenericMethod: true }) is true;
this.generateDefaultDllImportSearchPathsAttribute = this.compilation?.GetTypeByMetadataName(typeof(DefaultDllImportSearchPathsAttribute).FullName) is object;
if (this.compilation?.GetTypeByMetadataName("System.Runtime.Versioning.SupportedOSPlatformAttribute") is { } attribute
&& (attribute.DeclaredAccessibility == Accessibility.Public || attribute.ContainingAssembly.GivesAccessTo(this.compilation.Assembly)))
if (this.FindTypeSymbolIfAlreadyAvailable("System.Runtime.Versioning.SupportedOSPlatformAttribute") is { } attribute)
{
this.generateSupportedOSPlatformAttributes = true;
AttributeData usageAttribute = attribute.GetAttributes().Single(att => att.AttributeClass?.Name == nameof(AttributeUsageAttribute));
Expand Down
2 changes: 2 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1511,13 +1511,15 @@ public void ProjectReferenceBetweenTwoGeneratingProjects(bool internalsVisibleTo
using var referencedGenerator = this.CreateGenerator(new GeneratorOptions { ClassName = "P1" }, referencedProject);
Assert.True(referencedGenerator.TryGenerate("LockWorkStation", CancellationToken.None));
Assert.True(referencedGenerator.TryGenerate("CreateFile", CancellationToken.None));
Assert.True(referencedGenerator.TryGenerate("RAWHID", CancellationToken.None));
referencedProject = this.AddGeneratedCode(referencedProject, referencedGenerator);
this.AssertNoDiagnostics(referencedProject);

// Now produce more code in a referencing project that includes at least one of the same types as generated in the referenced project.
this.compilation = this.compilation.AddReferences(referencedProject.ToMetadataReference());
this.generator = this.CreateGenerator(new GeneratorOptions { ClassName = "P2" });
Assert.True(this.generator.TryGenerate("HidD_GetAttributes", CancellationToken.None));
Assert.True(this.generator.TryGenerate("RAWHID", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
}
Expand Down

0 comments on commit 4be534f

Please sign in to comment.