Add diagnostic for invalid method calls from shaders

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:33 +02:00
1 親 0c0d97e475
コミット 7318f5bd54
7ファイル変更
+76 -2

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -46,3 +46,4 @@ CMPSD2D0036 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0037 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0038 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0039 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0040 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+16
ファイルの表示
@@ -551,4 +551,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "A D2D1 shader must have all the indices of its simple and complex inputs in the valid range (between 0 and the number of declared inputs).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a method invocation that is not valid from a shader.
/// <para>
/// Format: <c>"The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidMethodCall = new DiagnosticDescriptor(
id: "CMPSD2D0040",
title: "Invalid method invocation from a D2D1 shader",
messageFormat: "The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A D2D1 shader can only invoke methods that are either HLSL intrinsics or with source available for analysis.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+7 -1
ファイルの表示
@@ -305,8 +305,14 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter
if (!this.staticMethods.ContainsKey(method))
{
if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode)
{
Diagnostics.Add(InvalidMethodCall, node, method);
return updatedNode;
}
ShaderSourceRewriter shaderSourceRewriter = new(SemanticModel, DiscoveredTypes, this.staticMethods, ConstantDefinitions, Diagnostics);
MethodDeclarationSyntax methodNode = (MethodDeclarationSyntax)method.DeclaringSyntaxReferences[0].GetSyntax();
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace(eol: "\n").WithoutTrivia();
this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
+1
ファイルの表示
@@ -55,3 +55,4 @@ CMPS0045 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
CMPS0046 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0047 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0048 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0049 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+16
ファイルの表示
@@ -680,4 +680,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "The dispatch axis value for a shader marked as embedded bytecode have to be valid (flags are not supported).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a method invocation that is not valid from a shader.
/// <para>
/// Format: <c>"The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidMethodCall = new DiagnosticDescriptor(
id: "CMPS0049",
title: "Invalid method invocation from a shader",
messageFormat: "The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Shaders can only invoke methods that are either HLSL intrinsics or with source available for analysis.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+7 -1
ファイルの表示
@@ -416,8 +416,14 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter
if (!this.staticMethods.ContainsKey(method))
{
if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode)
{
Diagnostics.Add(InvalidMethodCall, node, method);
return updatedNode;
}
ShaderSourceRewriter shaderSourceRewriter = new(SemanticModel, DiscoveredTypes, this.staticMethods, ConstantDefinitions, Diagnostics);
MethodDeclarationSyntax methodNode = (MethodDeclarationSyntax)method.DeclaringSyntaxReferences[0].GetSyntax();
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace(eol: "\n").WithoutTrivia();
this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
+28
ファイルの表示
@@ -1574,6 +1574,34 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0048");
}
[TestMethod]
public void InvalidMethodCall()
{
string source = @"
using System;
using ComputeSharp;
namespace ComputeSharp
{
public class ReadWriteBuffer<T> { }
}
namespace MyFancyApp.Sample
{
public struct MyShader : IComputeShader
{
public readonly ReadWriteBuffer<float> buffer;
public void Execute()
{
buffer[0] = Convert.ToSingle(42);
}
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0049", "CMPS0047");
}
/// <summary>
/// Verifies the output of a source generator.
/// </summary>