Add draft 'InvalidD2DInputArgumentAnalyzer'
このコミットが含まれているのは:
1 親
a9a5b164d3
コミット
4044cff3e8
2ファイル変更
+166
-1
検索条件に一致するファイルはありません
+1
-1
@@ -13,7 +13,7 @@ partial class D2DPixelShaderDescriptorGenerator
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the <c>InputTypes</c> properties.
|
||||
/// </summary>
|
||||
private static partial class InputTypes
|
||||
internal static partial class InputTypes
|
||||
{
|
||||
/// <summary>
|
||||
/// Extracts the input info for the current shader.
|
||||
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Operations;
|
||||
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
|
||||
/// <summary>
|
||||
/// A diagnostic analyzer that generates an error whenever an invocation to a D2D intrinsic references an out of range or invalid shader input.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [InvalidD2DEffectIdAttributeValue];
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
context.RegisterCompilationStartAction(static context =>
|
||||
{
|
||||
// If we can't get the D2D methods map, we have to stop right away
|
||||
if (!TryBuildMethodSymbolMap(context.Compilation, out ImmutableDictionary<IMethodSymbol, D2D1PixelShaderInputType>? methodSymbols))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
context.CancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
// Get the '[D2DInputCount]' symbol, which we need to validate the source input arguments
|
||||
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputCountAttribute") is not { } d2DInputCountAttributeSymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// We want to register a callback for each method invocation, as all 'D2D' intrinsics would map to one
|
||||
context.RegisterOperationAction(context =>
|
||||
{
|
||||
IInvocationOperation operation = (IInvocationOperation)context.Operation;
|
||||
|
||||
// Cheap initial filter: we only care about static methods from the 'D2D' type
|
||||
if (operation.TargetMethod is not { IsStatic: false, ContainingType.Name: "D2D" } targetMethodSymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Second cheap inital filter: we only care about invocations with a constant 'int' argument in first position.
|
||||
// While we're validating this, let's also get the 'index' parameter, since we need to validate it anyway.
|
||||
if (operation.Arguments is not [ILiteralOperation { Type.SpecialType: SpecialType.System_Int32, ConstantValue: { HasValue: true, Value: int index } }])
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// We found a containing type, make sure it has '[D2DInputCount]'
|
||||
if (!typeSymbol.HasAttributeWithType(d2DInputCountAttributeSymbol))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// At this point we can assume the shader type is mostly valid: let's get the actual input counts and types
|
||||
D2DPixelShaderDescriptorGenerator.InputTypes.GetInfo(
|
||||
typeSymbol,
|
||||
out int inputCount,
|
||||
out _,
|
||||
out _,
|
||||
out ImmutableArray<uint> inputTypes);
|
||||
|
||||
// First validation: the index must be in range
|
||||
if ((uint)index >= (uint)inputCount)
|
||||
{
|
||||
// TODO: emit diagnostic
|
||||
}
|
||||
|
||||
// Second validation: the input type must match
|
||||
if ((D2D1PixelShaderInputType)inputTypes[index] != targetInputType)
|
||||
{
|
||||
// TODO: emit diagnostic
|
||||
}
|
||||
}, OperationKind.Invocation);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to build a map of <see cref="IMethodSymbol"/> instances for all D2D intrinsics and their associated D2D input type.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
|
||||
/// <param name="methodSymbols">The resulting mapping of resolved <see cref="IMethodSymbol"/> instances.</param>
|
||||
/// <returns>Whether all requested <see cref="IMethodSymbol"/> instances could be resolved.</returns>
|
||||
private static bool TryBuildMethodSymbolMap(Compilation compilation, [NotNullWhen(true)] out ImmutableDictionary<IMethodSymbol, D2D1PixelShaderInputType>? methodSymbols)
|
||||
{
|
||||
// Get the 'D2D' symbol, to get methods from it
|
||||
if (compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2D") is not { } d2DSymbol)
|
||||
{
|
||||
methodSymbols = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the symbols for all relevant 'D2D' methods
|
||||
List<IMethodSymbol?> d2DMethodSymbols =
|
||||
[
|
||||
d2DSymbol.GetMethod(nameof(D2D.GetInput)),
|
||||
d2DSymbol.GetMethod(nameof(D2D.GetInputCoordinate)),
|
||||
d2DSymbol.GetMethod(nameof(D2D.SampleInput)),
|
||||
d2DSymbol.GetMethod(nameof(D2D.SampleInputAtOffset)),
|
||||
d2DSymbol.GetMethod(nameof(D2D.GetInput))
|
||||
];
|
||||
|
||||
ImmutableDictionary<IMethodSymbol, D2D1PixelShaderInputType>.Builder inputTypeMethodMap = ImmutableDictionary.CreateBuilder<IMethodSymbol, D2D1PixelShaderInputType>(SymbolEqualityComparer.Default);
|
||||
|
||||
// Validate all methods and build the map
|
||||
foreach (IMethodSymbol? d2DMethodSymbol in d2DMethodSymbols)
|
||||
{
|
||||
// We failed to find a symbol (shouldn't really ever happen), just stop here
|
||||
if (d2DMethodSymbol is null)
|
||||
{
|
||||
methodSymbols = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Lookup the attribute to get the D2D input type
|
||||
if (!d2DMethodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.Intrinsics.HlslD2DIntrinsicInputTypeAttribute", out AttributeData? attributeData))
|
||||
{
|
||||
methodSymbols = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Verify we can extract the D2D input type
|
||||
if (attributeData.ConstructorArguments is not [{ Kind: TypedConstantKind.Primitive, Type.TypeKind: TypeKind.Enum, Value: int inputType }])
|
||||
{
|
||||
methodSymbols = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
inputTypeMethodMap.Add(d2DMethodSymbol, (D2D1PixelShaderInputType)inputType);
|
||||
}
|
||||
|
||||
methodSymbols = inputTypeMethodMap.ToImmutable();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする