Optimize InvalidD2DResourceTextureIndexUseAnalyzer

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-30 22:49:28 +01:00
1 親 88ae6c12d3
コミット 4cf19792a5
1ファイル変更
+35 -19
@@ -1,7 +1,6 @@
using System.Collections.Immutable;
using System.Linq;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Mappings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
@@ -23,26 +22,43 @@ public sealed class InvalidD2DResourceTextureIndexUseAnalyzer : DiagnosticAnalyz
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterSymbolAction(static context =>
context.RegisterCompilationStartAction(static context =>
{
IFieldSymbol fieldSymbol = (IFieldSymbol)context.Symbol;
if (fieldSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DResourceTextureIndexAttribute", out _))
// Get the [D2DResourceTextureIndex] symbol, and the ones for the D2D resource texture types
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DResourceTextureIndexAttribute") is not { } d2DResourceTextureIndexAttributeSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2D1ResourceTexture1D`1") is not { } d2D1ResourceTexture1DSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2D1ResourceTexture2D`1") is not { } d2D1ResourceTexture2DSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2D1ResourceTexture3D`1") is not { } d2D1ResourceTexture3DSymbol)
{
string metadataName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
if (!HlslKnownTypes.IsResourceTextureType(metadataName))
{
Diagnostic diagnostic = Diagnostic.Create(
InvalidD2DResourceTextureIndexAttributeUse,
fieldSymbol.Locations.First(),
fieldSymbol.Name,
fieldSymbol.ContainingType,
fieldSymbol.Type);
context.ReportDiagnostic(diagnostic);
}
return;
}
}, SymbolKind.Field);
// Inspect all fields to make sure they're not using the attribute incorrectly
context.RegisterSymbolAction(context =>
{
IFieldSymbol fieldSymbol = (IFieldSymbol)context.Symbol;
if (fieldSymbol.HasAttributeWithType(d2DResourceTextureIndexAttributeSymbol))
{
// If the field is using [D2DResourceTextureIndex], it must be a D2D resource texture. We can first
// just check that the field type is a generic named type (since that's the case if it's indeed of
// a D2D resource texture type). Past that, we just check that it's one of the three possible types.
if (fieldSymbol.Type is not INamedTypeSymbol { IsGenericType: true } typeSymbol ||
!(SymbolEqualityComparer.Default.Equals(typeSymbol.ConstructedFrom, d2D1ResourceTexture1DSymbol) ||
SymbolEqualityComparer.Default.Equals(typeSymbol.ConstructedFrom, d2D1ResourceTexture2DSymbol) ||
SymbolEqualityComparer.Default.Equals(typeSymbol.ConstructedFrom, d2D1ResourceTexture3DSymbol)))
{
Diagnostic diagnostic = Diagnostic.Create(
InvalidD2DResourceTextureIndexAttributeUse,
fieldSymbol.Locations.First(),
fieldSymbol.Name,
fieldSymbol.ContainingType,
fieldSymbol.Type);
context.ReportDiagnostic(diagnostic);
}
}
}, SymbolKind.Field);
});
}
}