diff --git a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs index f04c3e04..7c3422f8 100644 --- a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs @@ -129,6 +129,24 @@ public sealed partial class AutoConstructorGenerator : IIncrementalGenerator .AddMembers(typeDeclarationSyntax); } + if (hierarchyInfo.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())))) + .NormalizeWhitespace(eol: "\n"); + } + // Create the compilation unit with disabled warnings, target namespace and generated type. // This will produce code as follows: // diff --git a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs index 4f19441b..0974e9b6 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs @@ -43,8 +43,10 @@ internal static class ISymbolExtensions INamespaceSymbol ns when ns.IsGlobalNamespace => builder, INamespaceSymbol ns when ns.ContainingNamespace is { IsGlobalNamespace: false } => BuildFrom(ns.ContainingNamespace, builder.Insert(0, $".{ns.MetadataName}")), - ITypeSymbol ts when ts.ContainingType is ISymbol pt => BuildFrom(pt, builder.Insert(0, $"+{ts.MetadataName}")), - ITypeSymbol ts when ts.ContainingNamespace is ISymbol pn => BuildFrom(pn, builder.Insert(0, $".{ts.MetadataName}")), + ITypeSymbol ts when ts.ContainingType is ISymbol containingType + => BuildFrom(containingType, builder.Insert(0, $"+{ts.MetadataName}")), + ITypeSymbol ts when ts.ContainingNamespace is ISymbol containingNamespace and not INamespaceSymbol { IsGlobalNamespace: true } + => BuildFrom(containingNamespace, builder.Insert(0, $".{ts.MetadataName}")), ISymbol => BuildFrom(symbol.ContainingSymbol, builder.Insert(0, symbol.MetadataName)), _ => builder }; diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs index 4a869b46..1352edac 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs @@ -102,6 +102,25 @@ public sealed partial class IShaderGenerator .AddMembers(typeDeclarationSyntax); } + if (hierarchyInfo.Namespace is "") + { + // If there is no namespace, attach the pragma directly to the declared type, + // and skip the namespace declaration. This is the same additional path also + // used when generating custom constructors. 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())))) + .NormalizeWhitespace(eol: "\n"); + } + // Create the compilation unit with disabled warnings, target namespace and generated type. // This will produce code as follows: //