From 28a458630e25dcf5e5cc6bf1cfc85b3c8a75b5f4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Nov 2023 21:20:22 +0100 Subject: [PATCH] Fix interface detection in descriptor interface analyzers --- ...elShaderDescriptorOnPixelShaderAnalyzer.cs | 21 ++++++++++++++++++- ...ShaderDescriptorOnComputeShaderAnalyzer.cs | 21 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingPixelShaderDescriptorOnPixelShaderAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingPixelShaderDescriptorOnPixelShaderAnalyzer.cs index 2f125cf0..0014a534 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingPixelShaderDescriptorOnPixelShaderAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingPixelShaderDescriptorOnPixelShaderAnalyzer.cs @@ -47,7 +47,7 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderAnalyzer : Diagnost } // Emit a diagnostic if the descriptor is missing for the shader type - if (!typeSymbol.HasInterfaceWithType(d2D1PixelShaderDescriptorSymbol) && + if (!HasD2D1PixelShaderDescriptorInterface(typeSymbol, d2D1PixelShaderDescriptorSymbol) && !typeSymbol.HasAttributeWithType(d2DGeneratedPixelShaderDescriptorAttributeSymbol)) { context.ReportDiagnostic(Diagnostic.Create( @@ -58,4 +58,23 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderAnalyzer : Diagnost }, SymbolKind.NamedType); }); } + + /// + /// Checks whether a given type implements the shader descriptor interface. + /// + /// The type to check. + /// The type symbol for ID2D1PixelShaderDescriptor<T>. + /// + private static bool HasD2D1PixelShaderDescriptorInterface(INamedTypeSymbol typeSymbol, INamedTypeSymbol d2D1PixelShaderDescriptorSymbol) + { + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) + { + if (SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, d2D1PixelShaderDescriptorSymbol)) + { + return true; + } + } + + return false; + } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingComputeShaderDescriptorOnComputeShaderAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingComputeShaderDescriptorOnComputeShaderAnalyzer.cs index 61db689f..632c4ea2 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingComputeShaderDescriptorOnComputeShaderAnalyzer.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingComputeShaderDescriptorOnComputeShaderAnalyzer.cs @@ -48,7 +48,7 @@ public sealed class MissingComputeShaderDescriptorOnComputeShaderAnalyzer : Diag } // Emit a diagnostic if the descriptor is missing for the shader type - if (!typeSymbol.HasInterfaceWithType(computeShaderDescriptorSymbol) && + if (!HasComputeShaderDescriptorInterface(typeSymbol, computeShaderDescriptorSymbol) && !typeSymbol.HasAttributeWithType(generatedComputeShaderDescriptorAttributeSymbol)) { context.ReportDiagnostic(Diagnostic.Create( @@ -83,4 +83,23 @@ public sealed class MissingComputeShaderDescriptorOnComputeShaderAnalyzer : Diag return false; } + + /// + /// Checks whether a given type implements the shader descriptor interface. + /// + /// The type to check. + /// The type symbol for IComputeShaderDescriptor<T>. + /// + private static bool HasComputeShaderDescriptorInterface(INamedTypeSymbol typeSymbol, INamedTypeSymbol computeShaderDescriptorSymbol) + { + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) + { + if (SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, computeShaderDescriptorSymbol)) + { + return true; + } + } + + return false; + } } \ No newline at end of file