diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NonConstantD2DInputArgumentAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NonConstantD2DInputArgumentAnalyzer.cs index fbc4c86f..6e16e46e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NonConstantD2DInputArgumentAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NonConstantD2DInputArgumentAnalyzer.cs @@ -62,8 +62,8 @@ public sealed class NonConstantD2DInputArgumentAnalyzer : DiagnosticAnalyzer return; } - // This analyzer should only kick in when the target parameter is an 'int', and the argument is not constant - if (operation.Value is ILiteralOperation { ConstantValue.HasValue: true } or IUnaryOperation { ConstantValue.HasValue: true, Operand: ILiteralOperation }) + // This analyzer should also only kick in when the argument is not constant + if (operation.Value.ConstantValue.HasValue) { return; } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 43409107..72b8a204 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1273,16 +1273,16 @@ partial class DiagnosticDescriptors /// /// Gets a for a D2D intrinsic call with a source input index that is not a constant. /// - /// Format: "The argument for parameter '{0}' in the call to the D2D intrinsic '{1}' must be a constant value with a literal expression (for instance, 'D2D.GetInput(0)' is valid, whereas 'D2D.GetInput(x)' or 'D2D.GetInput(ConstantIndex)' are not)". + /// Format: "The argument for parameter '{0}' in the call to the D2D intrinsic '{1}' must be a constant value (for instance, 'D2D.GetInput(0)' is valid, whereas 'D2D.GetInput(x)' is not)". /// /// public static readonly DiagnosticDescriptor NonConstantSourceInputIndexForD2DIntrinsic = new( id: "CMPSD2D0085", title: "Non constant input index for D2D intrinsic", - messageFormat: "The argument for parameter '{0}' in the call to the D2D intrinsic '{1}' must be a constant value with a literal expression (for instance, 'D2D.GetInput(0)' is valid, whereas 'D2D.GetInput(x)' or 'D2D.GetInput(ConstantIndex)' are not)", + messageFormat: "The argument for parameter '{0}' in the call to the D2D intrinsic '{1}' must be a constant value (for instance, 'D2D.GetInput(0)' is valid, whereas 'D2D.GetInput(x)' is not)", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "Arguments for parameters indicating the source shader input index in calls to D2D intrinsics must be constant values with a literal expression (for instance, 'D2D.GetInput(0)' is valid, whereas 'D2D.GetInput(x)' or 'D2D.GetInput(ConstantIndex)' are not).", + description: "Arguments for parameters indicating the source shader input index in calls to D2D intrinsics must be constant values (for instance, 'D2D.GetInput(0)' is valid, whereas 'D2D.GetInput(x)' is not).", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs index 153b1f81..778129b9 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs @@ -1443,6 +1443,34 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + [TestMethod] + public async Task NonConstantSourceInputIndexForD2DIntrinsic_NonLiteralConstant_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(1)] + internal readonly partial struct MyType(int x) : ID2D1PixelShader + { + public const int SourceIndex = 0; + + public float4 Execute() + { + D2D.GetInput(SourceIndex); + D2D.GetInputCoordinate(SourceIndex); + D2D.SampleInput(SourceIndex, 0); + D2D.SampleInputAtOffset(SourceIndex, 0); + D2D.SampleInputAtPosition(SourceIndex, 0); + + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + [TestMethod] public async Task NonConstantSourceInputIndexForD2DIntrinsic_NonConstantExpression_Warns() { @@ -1468,32 +1496,4 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } - - [TestMethod] - public async Task NonConstantSourceInputIndexForD2DIntrinsic_NonLiteralConstant_Warns() - { - const string source = """ - using ComputeSharp.D2D1; - using float4 = ComputeSharp.Float4; - - [D2DInputCount(1)] - internal readonly partial struct MyType(int x) : ID2D1PixelShader - { - public const int SourceIndex = 0; - - public float4 Execute() - { - D2D.GetInput({|CMPSD2D0085:SourceIndex|}); - D2D.GetInputCoordinate({|CMPSD2D0085:SourceIndex|}); - D2D.SampleInput({|CMPSD2D0085:SourceIndex|}, 0); - D2D.SampleInputAtOffset({|CMPSD2D0085:SourceIndex|}, 0); - D2D.SampleInputAtPosition({|CMPSD2D0085:SourceIndex|}, 0); - - return 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } } \ No newline at end of file