From 53e2b85d42132b9cd7924b11e6128adfe13bc13f Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Tue, 6 Sep 2022 17:48:42 -0600 Subject: [PATCH] Fix visibility of macros --- src/Microsoft.Windows.CsWin32/Generator.cs | 2 +- .../GeneratorTests.cs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Windows.CsWin32/Generator.cs b/src/Microsoft.Windows.CsWin32/Generator.cs index 6707e226..efdcac75 100644 --- a/src/Microsoft.Windows.CsWin32/Generator.cs +++ b/src/Microsoft.Windows.CsWin32/Generator.cs @@ -1605,7 +1605,7 @@ internal void RequestMacro(MethodDeclarationSyntax macro) { this.volatileCode.GenerateMacro(macro.Identifier.ValueText, delegate { - this.volatileCode.AddMacro(macro.Identifier.ValueText, macro); + this.volatileCode.AddMacro(macro.Identifier.ValueText, (MethodDeclarationSyntax)ElevateVisibility(macro)); // Generate any additional types that this macro relies on. foreach (QualifiedNameSyntax identifier in macro.DescendantNodes().OfType()) diff --git a/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs b/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs index 6baca68e..0aefdded 100644 --- a/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs +++ b/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs @@ -607,6 +607,18 @@ public void HandleStructsHaveStaticNullMember(string handleName) this.AssertGeneratedMember(handleName, "Null", $"internal static {handleName} Null => default;"); } + [Theory, PairwiseData] + public void MacroAPIsGenerateWithAppropriateVisibility(bool publicVisibility) + { + this.generator = this.CreateGenerator(DefaultTestGeneratorOptions with { Public = publicVisibility }); + Assert.True(this.generator.TryGenerate("HRESULT_FROM_WIN32", CancellationToken.None)); + this.CollectGeneratedCode(this.generator); + this.AssertNoDiagnostics(); + var method = Assert.Single(this.FindGeneratedMethod("HRESULT_FROM_WIN32")); + + Assert.True(method.Modifiers.Any(publicVisibility ? SyntaxKind.PublicKeyword : SyntaxKind.InternalKeyword)); + } + [Theory] [InlineData("HRESULT_FROM_WIN32")] public void MacroAPIsGenerate(string macro)