From 7c02100df1e7cc21242a735a9193a0b0da3d9d1c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 7 Mar 2021 20:28:52 +0100 Subject: [PATCH] Added more shader constant property diagnostics --- .../AnalyzerReleases.Unshipped.md | 4 ++- .../Diagnostics/DiagnosticDescriptors.cs | 32 +++++++++++++++++++ .../IComputeShaderSourceGenerator.cs | 11 ++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md index 55dfd8b3..6bbf251f 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md @@ -48,4 +48,6 @@ CMPS0040 | ComputeSharp.SourceGenerators.IComputeShaderSourceGenerator | Warning CMPS0041 | ComputeSharp.SourceGenerators.ShaderMethodSourceGenerator | Warning | ShaderMethodSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0042 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0043 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) -CMPS0044 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) \ No newline at end of file +CMPS0044 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0045 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0046 | 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 6b444b2a..9dc7506e 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -620,5 +620,37 @@ namespace ComputeSharp.SourceGenerators.Diagnostics isEnabledByDefault: true, description: "A type representing a compute shader contains a constant property with an invalid declaration.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invalid shader constant property declaration. + /// + /// Format: "The compute shader of type {0} contains a mutable property \"{1}\" (only readonly properties are supported)". + /// + /// + public static readonly DiagnosticDescriptor NonReadonlyShaderPropertyDeclaration = new( + id: "CMPS0045", + title: "Invalid shader constant property", + messageFormat: "The compute shader of type {0} contains a mutable property \"{1}\" (only readonly properties are supported)", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A type representing a compute shader contains a non readonly property declaration.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invalid shader constant property declaration. + /// + /// Format: "The compute shader of type {0} contains an instance property \"{1}\" (only static properties are supported)". + /// + /// + public static readonly DiagnosticDescriptor InstanceShaderPropertyDeclaration = new( + id: "CMPS0046", + title: "Invalid shader constant property", + messageFormat: "The compute shader of type {0} contains an instance property \"{1}\" (only static properties are supported)", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A type representing a compute shader contains an instance property declaration.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index fd5a11a2..1f8dff73 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -196,8 +196,17 @@ namespace ComputeSharp.SourceGenerators { IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration)!; - if (!propertySymbol.IsStatic || !propertySymbol.IsReadOnly) + if (!propertySymbol.IsStatic) { + context.ReportDiagnostic(InstanceShaderPropertyDeclaration, propertyDeclaration, structDeclarationSymbol, propertySymbol.Name); + + continue; + } + + if (!propertySymbol.IsReadOnly) + { + context.ReportDiagnostic(NonReadonlyShaderPropertyDeclaration, propertyDeclaration, structDeclarationSymbol, propertySymbol.Name); + continue; }