diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 2576182d..e9637e0e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -92,3 +92,4 @@ CMPSD2D0082 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://gith CMPSD2D0083 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0084 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0085 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0086 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs index 078c9360..24631108 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs @@ -5,6 +5,7 @@ using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Intrinsics; using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Operations; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; @@ -21,7 +22,8 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer public override ImmutableArray SupportedDiagnostics { get; } = [ IndexOutOfRangeForD2DIntrinsic, - InvalidInputTypeForD2DIntrinsic + InvalidInputTypeForD2DIntrinsic, + InvalidIndexSyntaxForD2DIntrinsic ]; /// @@ -64,12 +66,39 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer return; } + // We only want to kick in when the target parameter is an 'int' (same as in 'NonConstantD2DInputArgumentAnalyzer') + if (firstArgument.Parameter is not { Type.SpecialType: SpecialType.System_Int32 } parameterSymbol) + { + return; + } + // Validate that the target method is one of the ones we care about, and get the target input type if (!methodSymbols.TryGetValue(targetMethodSymbol, out D2D1PixelShaderInputType? targetInputType)) { return; } + // Also check that the actual syntax for the index argument is valid (only literals and direct constant field references + // are valid). For parenthesized expressions, we need to check the parent syntax node (the operation just ignores it). + if (firstArgument.Value is not (ILiteralOperation or IFieldReferenceOperation)) + { + context.ReportDiagnostic(Diagnostic.Create( + InvalidIndexSyntaxForD2DIntrinsic, + firstArgument.Syntax.GetLocation(), + parameterSymbol.Name, + targetMethodSymbol.Name)); + } + else if (firstArgument.Syntax.Parent.IsKind(SyntaxKind.ParenthesizedExpression)) + { + // In this case, we pass the parent syntax node for the location, so that we can + // include the parentheses as well. Otherwise we'd just underline the inner value. + context.ReportDiagnostic(Diagnostic.Create( + InvalidIndexSyntaxForD2DIntrinsic, + firstArgument.Syntax.Parent.GetLocation(), + parameterSymbol.Name, + targetMethodSymbol.Name)); + } + // We have matched a target symbol, so let's try to get the parent shader if (context.ContainingSymbol.FirstAncestorOrSelf() is not { TypeKind: TypeKind.Struct } typeSymbol) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 72b8a204..80df85c4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1285,4 +1285,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, 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"); + + /// + /// Gets a for a D2D intrinsic call with a source input index using invalid syntax. + /// + /// Format: "The argument for parameter '{0}' in the call to the D2D intrinsic '{1}' must be a literal or a constant field reference (for instance, 'D2D.GetInput(0)' and 'D2D.GetInput(TextureIndex)' are valid, whereas 'D2D.GetInput(BaseIndex + 1)' and 'D2D.GetInput(1 + 2)' are not)". + /// + /// + public static readonly DiagnosticDescriptor InvalidIndexSyntaxForD2DIntrinsic = new( + id: "CMPSD2D0086", + title: "Invalid input index syntax for D2D intrinsic", + messageFormat: "The argument for parameter '{0}' in the call to the D2D intrinsic '{1}' must be a literal or a constant field reference (for instance, 'D2D.GetInput(0)' and 'D2D.GetInput(TextureIndex)' are valid, whereas 'D2D.GetInput(BaseIndex + 1)' and 'D2D.GetInput(1 + 2)' are 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 a literal or constant field references (for instance, 'D2D.GetInput(0)' and 'D2D.GetInput(TextureIndex)' are valid, whereas 'D2D.GetInput(BaseIndex + 1)' and 'D2D.GetInput(1 + 2)' are not).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file