From 2b1b864c8db7879c9de37c02f7ddb5bdeac9672e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 9 Dec 2023 18:38:20 +0100 Subject: [PATCH] Add dedicated diagnostic for Math/MathF calls --- .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 16 +++++++++++++ .../SyntaxRewriters/ShaderSourceRewriter.cs | 13 +++++++++- .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 16 +++++++++++++ .../DiagnosticsTests.cs | 24 +++++++++++++++++++ 6 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index ced0f1a7..6fe7a3a5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -81,3 +81,4 @@ CMPSD2D0071 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0072 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0073 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0074 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0075 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 9bc919f4..ec8921e5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1109,4 +1109,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "A pixel shader cannot use a 'this' expression outside of member accesses (such as 'this.field').", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invocation of a Math or MathF API. + /// + /// Format: "The method {0} cannot be used in a shader, use equivalent APIs from the Hlsl type instead". + /// + /// + public static readonly DiagnosticDescriptor InvalidMathOrMathFCall = new( + id: "CMPSD2D0075", + title: "Invalid Math or MathF invocation from a shader", + messageFormat: "The method {0} cannot be used in a shader, use equivalent APIs from the Hlsl type instead", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Methods from the Math and MathF types cannot be used in a shader, and equivalent APIs from the Hlsl type should be used instead.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index a0d66410..ac5fb1b2 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -531,7 +531,18 @@ internal sealed partial class ShaderSourceRewriter( { if (!method.TryGetSyntaxNode(CancellationToken, out MethodDeclarationSyntax? methodNode)) { - Diagnostics.Add(InvalidMethodOrConstructorCall, node, method); + // Special cases method calls to Math and MathF APIs. In this case, we also want to emit a specific + // diagnostic to guide developers towards using the intrinsic APIs on the Hlsl class instead. This + // check is only done when the current method is already invalid anyway, so it adds no overhead. + if (method.ContainingType is { Name: "Math" or "MathF", ContainingType: null, ContainingNamespace.Name: "System" }) + { + Diagnostics.Add(InvalidMathOrMathFCall, node, method); + } + else + { + // Otherwise just emit the generic error diagnostic + Diagnostics.Add(InvalidMethodOrConstructorCall, node, method); + } return updatedNode; } diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index c3ff60f2..0dbb22ff 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -68,3 +68,4 @@ CMPS0059 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser CMPS0060 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0061 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0062 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0063 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 202d812e..99aab81a 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -885,4 +885,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "A compute shader cannot use a 'this' expression outside of member accesses (such as 'this.field').", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invocation of a Math or MathF API. + /// + /// Format: "The method {0} cannot be used in a shader, use equivalent APIs from the Hlsl type instead". + /// + /// + public static readonly DiagnosticDescriptor InvalidMathOrMathFCall = new( + id: "CMPS0063", + title: "Invalid Math or MathF invocation from a shader", + messageFormat: "The method {0} cannot be used in a shader, use equivalent APIs from the Hlsl type instead", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Methods from the Math and MathF types cannot be used in a shader, and equivalent APIs from the Hlsl type should be used instead.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs index ffbd74c8..3cf768d2 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -1241,6 +1241,30 @@ public class DiagnosticsTests VerifyGeneratedDiagnostics(source, "CMPS0049", "CMPS0047"); } + [TestMethod] + public void InvalidMethodCall_SystemMath() + { + const string source = """ + using System; + using ComputeSharp; + + namespace MyFancyApp.Sample; + + [GeneratedComputeShaderDescriptor] + public partial struct MyShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + public void Execute() + { + buffer[0] = Math.Abs(42); + } + } + """; + + VerifyGeneratedDiagnostics(source, "CMPS0063", "CMPS0047"); + } + [TestMethod] public void InvalidDiscoveredType_Primitive() {