From d55d5009c6cc2c81a8211f12dfa75f4fe463da1a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 18 Feb 2021 14:34:35 +0100 Subject: [PATCH] Added error for declarations of invalid type --- .../AnalyzerReleases.Unshipped.md | 3 ++- .../Diagnostics/DiagnosticDescriptors.cs | 16 ++++++++++++++++ .../SyntaxRewriters/ShaderSourceRewriter.cs | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md index f9fd1d07..a1d77353 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md @@ -34,4 +34,5 @@ CMPS0026 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, CMPS0027 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0028 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0029 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) -CMPS0030 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) \ No newline at end of file +CMPS0030 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0031 | 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 d5447612..1a60fbce 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -426,5 +426,21 @@ namespace ComputeSharp.SourceGenerators.Diagnostics isEnabledByDefault: true, description: "A yield statement cannot be used in a compute shader.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invalid object declaration. + /// + /// Format: "A variable of type {0} cannot be declared in a compute shader". + /// + /// + public static DiagnosticDescriptor InvalidObjectDeclaration { get; } = new( + id: "CMPS0031", + title: "Invalid object declaration", + messageFormat: "A variable of type {0} cannot be declared in a compute shader (only unmanaged types are supported)", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Only unmanaged value type objects can be declared in a compute shader.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 9a52a64a..06db18f5 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -787,6 +787,24 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters return base.VisitUsingStatement(node); } + /// + public override SyntaxNode? VisitVariableDeclarator(VariableDeclaratorSyntax node) + { + var updatedNode = (VariableDeclaratorSyntax)base.VisitVariableDeclarator(node)!; + + if (node.Initializer is null && + node.Parent is VariableDeclarationSyntax declaration && + this.semanticModel.GetTypeInfo(declaration.Type).Type is ITypeSymbol { IsUnmanagedType: false } type) + { + this.context.ReportDiagnostic(Diagnostic.Create( + DiagnosticDescriptors.InvalidObjectDeclaration, + node.GetLocation(), + type)); + } + + return updatedNode; + } + /// public override SyntaxNode? VisitYieldStatement(YieldStatementSyntax node) {