Add dedicated diagnostic for Math/MathF calls

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-09 18:38:20 +01:00
1 親 3b92f3d595
コミット 2b1b864c8d
6ファイル変更
+70 -1

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

+1
ファイルの表示
@@ -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)
+16
ファイルの表示
@@ -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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invocation of a <c>Math</c> or <c>MathF</c> API.
/// <para>
/// Format: <c>"The method {0} cannot be used in a shader, use equivalent APIs from the Hlsl type instead"</c>.
/// </para>
/// </summary>
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");
}
+12 -1
ファイルの表示
@@ -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;
}
+1
ファイルの表示
@@ -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)
+16
ファイルの表示
@@ -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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invocation of a <c>Math</c> or <c>MathF</c> API.
/// <para>
/// Format: <c>"The method {0} cannot be used in a shader, use equivalent APIs from the Hlsl type instead"</c>.
/// </para>
/// </summary>
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");
}
+24
ファイルの表示
@@ -1241,6 +1241,30 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics<ComputeShaderDescriptorGenerator>(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<float> buffer;
public void Execute()
{
buffer[0] = Math.Abs(42);
}
}
""";
VerifyGeneratedDiagnostics<ComputeShaderDescriptorGenerator>(source, "CMPS0063", "CMPS0047");
}
[TestMethod]
public void InvalidDiscoveredType_Primitive()
{