From 707f0be5c4f33615a9eea147d1c2a8465da7785a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 18 Feb 2021 00:18:27 +0100 Subject: [PATCH] Added error for non static group shared buffer --- .../AnalyzerReleases.Unshipped.md | 3 ++- .../Diagnostics/DiagnosticDescriptors.cs | 18 +++++++++++++++++- .../IComputeShaderSourceGenerator.cs | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md index bb77ae76..f42b3e64 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md @@ -7,4 +7,5 @@ Rule ID | Category | Severity | Notes --------|----------|----------|------- CMPS0001 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0002 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) -CMPS0003 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) \ No newline at end of file +CMPS0003 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0004 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index e54e2496..0468bcb3 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -47,12 +47,28 @@ namespace ComputeSharp.SourceGenerators.Diagnostics /// public static DiagnosticDescriptor InvalidGroupSharedFieldElementType { get; } = new( id: "CMPS0003", - title: "Invalid group shared field type", + title: "Invalid group shared field element type", messageFormat: "The compute shader of type {0} contains a group shared field \"{1}\" of an invalid type {2} (it must be a primitive or unmanaged type)", category: typeof(IComputeShader).FullName, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, description: "A group shared field element must be of a primitive or unmanaged type.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invalid group shared field declaration. + /// + /// Format: "The compute shader of type {0} contains a group shared field \"{1}\" that is not static.". + /// + /// + public static DiagnosticDescriptor InvalidGroupSharedFieldDeclaration { get; } = new( + id: "CMPS0004", + title: "Invalid group shared field declaration", + messageFormat: "The compute shader of type {0} contains a group shared field \"{1}\" that is not static.", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A group shared field must be static.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index db8e5ccd..fa64fbf7 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -97,6 +97,19 @@ namespace ComputeSharp.SourceGenerators { if (fieldSymbol.IsStatic) continue; + AttributeData? attribute = fieldSymbol.GetAttributes().FirstOrDefault(static a => a.AttributeClass is { Name: nameof(GroupSharedAttribute) }); + + // Group shared fields must be static + if (attribute is not null) + { + context.ReportDiagnostic(Diagnostic.Create( + DiagnosticDescriptors.InvalidGroupSharedFieldDeclaration, + fieldSymbol.Locations.FirstOrDefault(), + structDeclarationSymbol, fieldSymbol.Name)); + + continue; + } + // Captured fields must be named type symbols if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol) { @@ -121,6 +134,8 @@ namespace ComputeSharp.SourceGenerators DiagnosticDescriptors.InvalidShaderField, fieldSymbol.Locations.FirstOrDefault(), structDeclarationSymbol, fieldSymbol.Name, typeSymbol)); + + continue; } string typeName = HlslKnownTypes.GetMappedName(typeSymbol);