Add 'InvalidIndexSyntaxForD2DIntrinsic' diagnostic

このコミットが含まれているのは:
Sergio Pedri committed 2024-11-25 21:42:20 -08:00
1 親 7a3dd640b1
コミット 1facaba03a
3ファイル変更
+47 -1

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

+1
ファイルの表示
@@ -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)
@@ -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<DiagnosticDescriptor> SupportedDiagnostics { get; } =
[
IndexOutOfRangeForD2DIntrinsic,
InvalidInputTypeForD2DIntrinsic
InvalidInputTypeForD2DIntrinsic,
InvalidIndexSyntaxForD2DIntrinsic
];
/// <inheritdoc/>
@@ -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<INamedTypeSymbol>() is not { TypeKind: TypeKind.Struct } typeSymbol)
{
+16
ファイルの表示
@@ -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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a D2D intrinsic call with a source input index using invalid syntax.
/// <para>
/// Format: <c>"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)"</c>.
/// </para>
/// </summary>
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");
}