From 7e7b57cd675deb5e2d6b563d49d7e9dc0255bc33 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 2 Dec 2023 22:58:52 +0100 Subject: [PATCH] Add a diagnostic for constructors with base constructors --- .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 16 +++++++++ .../SyntaxRewriters/ShaderSourceRewriter.cs | 8 +++++ .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 16 +++++++++ .../DiagnosticsTests.cs | 33 +++++++++++++++++++ 6 files changed, 75 insertions(+) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 9378f60c..053cf98a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -79,3 +79,4 @@ CMPSD2D0069 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://gith CMPSD2D0070 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0071 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0072 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0073 | 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 825ce3f9..503a5bf3 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1080,4 +1080,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "A collection expression cannot be used in a pixel shader.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a constructor with a base constructor declaration. + /// + /// Format: "The constructor {0} has a base constructor declaration, which cannot be used in a D2D1 shader (only standalone constructors are allowed)". + /// + /// + public static readonly DiagnosticDescriptor InvalidBaseConstructorDeclaration = new( + id: "CMPSD2D0073", + title: "Invalid base constructor declaration", + messageFormat: "The constructor {0} has a base constructor declaration, which cannot be used in a D2D1 shader (only standalone constructors are allowed)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Only standalone constructors (with no base constructor declaration) can be used in a D2D1 shader.", + 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 a001c26e..10baf557 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -662,6 +662,14 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter return updatedNode; } + // Chaining constructors is not supported, so emit a diagnostic to inform the user. + // The rest of the code will work as usual, but the semantics of the other chained + // constructor being invoked is not preserved, so the resulting code is not the same. + if (constructorNode.Initializer is not null) + { + Diagnostics.Add(InvalidBaseConstructorDeclaration, node, constructor); + } + ShaderSourceRewriter shaderSourceRewriter = new( SemanticModel, DiscoveredTypes, diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index 35b0fc9a..044b323e 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -66,3 +66,4 @@ CMPS0057 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/S CMPS0058 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0059 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0060 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0061 | 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 c4992e5b..7e952d64 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -856,4 +856,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "A collection expression cannot be used in a compute shader.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a constructor with a base constructor declaration. + /// + /// Format: "The constructor {0} has a base constructor declaration, which cannot be used in a shader (only standalone constructors are allowed)". + /// + /// + public static readonly DiagnosticDescriptor InvalidBaseConstructorDeclaration = new( + id: "CMPS0061", + title: "Invalid base constructor declaration", + messageFormat: "The constructor {0} has a base constructor declaration, which cannot be used in a shader (only standalone constructors are allowed)", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Only standalone constructors (with no base constructor declaration) can be used in a shader.", + 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 e42ca2c2..33a36fb3 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -1419,6 +1419,39 @@ public class DiagnosticsTests VerifyGeneratedDiagnostics(source, "CMPS0031", "CMPS0047", "CMPS0060"); } + [TestMethod] + public void InvalidBaseConstructorDeclaration() + { + const string source = """ + using ComputeSharp; + + public struct MyStruct + { + public MyStruct(int x) + { + } + + public MyStruct(float x) + : this((int)x) + { + } + } + + [GeneratedComputeShaderDescriptor] + public partial struct MyShader : IComputeShader + { + public ReadWriteBuffer buffer; + + public void Execute() + { + MyStruct value = new(3.14f); + } + } + """; + + VerifyGeneratedDiagnostics(source, "CMPS0047", "CMPS0061"); + } + /// /// Verifies the output of a source generator. ///