diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DResourceTextureIndexUseAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DResourceTextureIndexUseAnalyzer.cs index e78c4591..fc924003 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DResourceTextureIndexUseAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DResourceTextureIndexUseAnalyzer.cs @@ -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); + }); } } \ No newline at end of file