From 1b531f1dd733eec18f126b934b3564f087f42338 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 14:38:35 +0100 Subject: [PATCH] Added diagnostics for invalid DispatchSize usage --- .../AnalyzerReleases.Unshipped.md | 3 +- .../Diagnostics/DiagnosticDescriptors.cs | 13 +++++++ .../ConstantPropertyRewriter.cs | 1 + .../SyntaxRewriters/ShaderSourceRewriter.cs | 1 + .../DiagnosticsTests.cs | 37 +++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md index 6bbf251f..97be482d 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md @@ -50,4 +50,5 @@ CMPS0042 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, CMPS0043 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) 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 +CMPS0046 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0047 | 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 3edab2e8..2b3f33ba 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -652,5 +652,18 @@ namespace ComputeSharp.SourceGenerators.Diagnostics isEnabledByDefault: true, description: "A type representing a compute shader contains an instance property declaration.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invalid usage. + /// + public static readonly DiagnosticDescriptor InvalidDispatchSizeUsage = new( + id: "CMPS0047", + title: "Invalid DispatchSize usage", + messageFormat: "The DispatchSize type can only be used within the main body of a compute shader", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The DispatchSize type can only be used within the main body of a compute shader.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs index 4d2449e4..6e221561 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs @@ -87,6 +87,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters _ when typeName == typeof(GroupIds).FullName => InvalidGroupIdsUsage, _ when typeName == typeof(GroupSize).FullName => InvalidGroupSizeUsage, _ when typeName == typeof(GridIds).FullName => InvalidGridIdsUsage, + _ when typeName == typeof(DispatchSize).FullName => InvalidDispatchSizeUsage, _ => null }; diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index e35144d5..69a7c9fd 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -288,6 +288,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters _ when typeName == typeof(GroupIds).FullName => InvalidGroupIdsUsage, _ when typeName == typeof(GroupSize).FullName => InvalidGroupSizeUsage, _ when typeName == typeof(GridIds).FullName => InvalidGridIdsUsage, + _ when typeName == typeof(DispatchSize).FullName => InvalidDispatchSizeUsage, _ => null }; diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs index 6b020e63..429d4afd 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -142,6 +142,7 @@ namespace ComputeSharp.Tests.SourceGenerators [DataRow(nameof(GroupIds), "CMPS0007")] [DataRow(nameof(GroupSize), "CMPS0008")] [DataRow(nameof(GridIds), "CMPS0009")] + [DataRow(nameof(DispatchSize), "CMPS0047")] public void InvalidDispatchInfoUsage_LocalFunction(string typeName, string diagnosticsId) { string source = $@" @@ -176,6 +177,7 @@ namespace ComputeSharp.Tests.SourceGenerators [DataRow(nameof(GroupIds), "CMPS0007")] [DataRow(nameof(GroupSize), "CMPS0008")] [DataRow(nameof(GridIds), "CMPS0009")] + [DataRow(nameof(DispatchSize), "CMPS0047")] public void InvalidDispatchInfoUsage_StaticMethod(string typeName, string diagnosticsId) { string source = $@" @@ -202,6 +204,41 @@ namespace ComputeSharp.Tests.SourceGenerators VerifyGeneratedDiagnostics(source, diagnosticsId); } + [TestMethod] + [DataRow(nameof(ThreadIds), "CMPS0006")] + [DataRow(nameof(GroupIds), "CMPS0007")] + [DataRow(nameof(GroupSize), "CMPS0008")] + [DataRow(nameof(GridIds), "CMPS0009")] + [DataRow(nameof(DispatchSize), "CMPS0047")] + public void InvalidDispatchInfoUsage_ConstantProperty(string typeName, string diagnosticsId) + { + string source = $@" + using ComputeSharp; + + namespace ComputeSharp + {{ + public class ReadWriteBuffer {{ }} + public static class {typeName} {{ public static int X => 0; }} + }} + + namespace MyFancyApp.Sample + {{ + public struct MyShader : IComputeShader + {{ + public ReadWriteBuffer buffer; + + private static int Foo => {typeName}.X; + + public void Execute() + {{ + buffer[0] = Foo; + }} + }} + }}"; + + VerifyGeneratedDiagnostics(source, diagnosticsId); + } + [TestMethod] public void InvalidObjectCreationExpression_Explicit() {