From 7a630204efc159cc0d890bedee84402778407586 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Nov 2023 22:58:13 +0100 Subject: [PATCH] Add MultipleComputeShaderInterfacesOnShaderTypeAnalyzer --- ...uteShaderInterfacesOnShaderTypeAnalyzer.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MultipleComputeShaderInterfacesOnShaderTypeAnalyzer.cs diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MultipleComputeShaderInterfacesOnShaderTypeAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MultipleComputeShaderInterfacesOnShaderTypeAnalyzer.cs new file mode 100644 index 00000000..de85a567 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MultipleComputeShaderInterfacesOnShaderTypeAnalyzer.cs @@ -0,0 +1,72 @@ +using System.Collections.Immutable; +using System.Linq; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.SourceGenerators; + +/// +/// A diagnostic analyzer that generates an error when a shader type using [GeneratedComputeShaderDescriptor] is implementing multiple shader interfaces. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class MultipleComputeShaderInterfacesOnShaderTypeAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MultipleShaderTypesImplemented); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // Get the IComputeShader, IComputeShader, [GeneratedComputeShaderDescriptor] symbols + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader") is not { } computeShaderSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader`1") is not { } pixelShaderSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.GeneratedComputeShaderDescriptorAttribute") is not { } generatedComputeShaderDescriptorAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types are possible targets + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) + { + return; + } + + // We only want to check types using [GeneratedComputeShaderDescriptor] + if (!typeSymbol.HasAttributeWithType(generatedComputeShaderDescriptorAttributeSymbol)) + { + return; + } + + int shaderInferfaceCount = 0; + + // Count how many shader interfaces are implemented + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) + { + if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, computeShaderSymbol) || + SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, pixelShaderSymbol)) + { + shaderInferfaceCount++; + } + } + + // Emit a diagnostic if there's more than one + if (shaderInferfaceCount > 1) + { + context.ReportDiagnostic(Diagnostic.Create( + MultipleShaderTypesImplemented, + typeSymbol.Locations.FirstOrDefault(), + typeSymbol)); + } + }, SymbolKind.NamedType); + }); + } +} \ No newline at end of file