diff --git a/src/ComputeSharp.Core.SourceGenerators/EmbeddedResources/GlobalUsingDirectives.cs b/src/ComputeSharp.Core.SourceGenerators/EmbeddedResources/GlobalUsingDirectives.cs index 8c405107..979d3122 100644 --- a/src/ComputeSharp.Core.SourceGenerators/EmbeddedResources/GlobalUsingDirectives.cs +++ b/src/ComputeSharp.Core.SourceGenerators/EmbeddedResources/GlobalUsingDirectives.cs @@ -1,4 +1,6 @@ -global using bool2 = global::ComputeSharp.Bool2; +// +#pragma warning disable +global using bool2 = global::ComputeSharp.Bool2; global using bool3 = global::ComputeSharp.Bool3; global using bool4 = global::ComputeSharp.Bool4; global using bool1x1 = global::ComputeSharp.Bool1x1; diff --git a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs index 68ee9fa4..a093c45b 100644 --- a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs @@ -74,40 +74,40 @@ internal sealed partial record HierarchyInfo(string FilenameHint, string FullyQu .AddMembers(typeDeclarationSyntax); } + // Prepare the leading trivia for the generated compilation unit. + // This will produce code as follows: + // + // // + // #pragma warning disable + SyntaxTriviaList syntaxTriviaList = TriviaList( + Comment("// "), + Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))); + if (Namespace is "") { // If there is no namespace, attach the pragma directly to the declared type, // and skip the namespace declaration. This will produce code as follows: // - // #pragma warning disable - // + // // return - CompilationUnit().AddMembers( - typeDeclarationSyntax - .WithModifiers(TokenList(Token( - TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), - SyntaxKind.PartialKeyword, - TriviaList())))) + CompilationUnit() + .AddMembers(typeDeclarationSyntax.WithLeadingTrivia(syntaxTriviaList)) .NormalizeWhitespace(eol: "\n"); } // Create the compilation unit with disabled warnings, target namespace and generated type. // This will produce code as follows: // - // #pragma warning disable - // + // // namespace ; // // return CompilationUnit().AddMembers( FileScopedNamespaceDeclaration(IdentifierName(Namespace)) - .AddMembers(typeDeclarationSyntax) - .WithNamespaceKeyword(Token(TriviaList( - Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), - SyntaxKind.NamespaceKeyword, - TriviaList()))) + .WithLeadingTrivia(syntaxTriviaList) + .AddMembers(typeDeclarationSyntax)) .NormalizeWhitespace(eol: "\n"); } diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs index 19d9a0bd..5b2d8520 100644 --- a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs @@ -174,6 +174,11 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator /// The input instance to use. public static CompilationUnitSyntax GetSyntax(HlslMethodSourceInfo methodSourceInfo) { + // This produces an assembly attribute declaration as follows: + // + // // + // #pragma warning disable + // [assembly: global::ComputeSharp.ShaderMethodSourceAttribute()] return CompilationUnit().AddAttributeLists( AttributeList(SingletonSeparatedList( @@ -184,6 +189,11 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator AttributeArgument(NestedArrayExpression(methodSourceInfo.DependentMethods)), AttributeArgument(NestedArrayExpression(methodSourceInfo.DefinedConstants))))) .WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList())) + .WithOpenBracketToken(Token(TriviaList( + Comment("// "), + Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), + SyntaxKind.OpenBracketToken, + TriviaList())) .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))) .NormalizeWhitespace(eol: "\n"); } diff --git a/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs index cbef7c62..f42439f5 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs @@ -45,6 +45,7 @@ public class AutoConstructorGeneratorTests }"; string expected = $@" + // #pragma warning disable namespace MyFancyApp.Sample; partial class IFoo