diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index 0f3f97f3..975059a8 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -10,6 +10,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadonlyShaderTypeAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadonlyShaderTypeAnalyzerBase.cs new file mode 100644 index 00000000..fcdafa96 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadonlyShaderTypeAnalyzerBase.cs @@ -0,0 +1,75 @@ +using System.Collections.Immutable; +using System.Linq; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace ComputeSharp.SourceGeneration.Diagnostics; + +/// +/// A diagnostic analyzer that generates a warning if a shader type is not readonly. +/// +/// The instance to use. +/// The fully qualified metadata names of all candidate shader interface types. +public abstract class NotReadonlyShaderTypeAnalyzerBase( + DiagnosticDescriptor diagnosticDescriptor, + ImmutableArray shaderInterfaceTypeFullyQualifiedMetadataNames) : DiagnosticAnalyzer +{ + /// + public sealed override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor); + + /// + public sealed override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(context => + { + // Get the candidate type symbols + if (!context.Compilation.TryBuildNamedTypeSymbolSet(shaderInterfaceTypeFullyQualifiedMetadataNames, out ImmutableHashSet? typeSymbols)) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types that are not readonly are possible targets + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct, IsReadOnly: false } typeSymbol) + { + return; + } + + // Emit a diagnostic if the type is a shader type + if (IsShaderType(typeSymbol, typeSymbols)) + { + context.ReportDiagnostic(Diagnostic.Create( + diagnosticDescriptor, + typeSymbol.Locations.FirstOrDefault(), + typeSymbol)); + } + }, SymbolKind.NamedType); + }); + } + + /// + /// Checks whether a given type is a shader type. + /// + /// The type to check. + /// The list of candidate type symbols for shader interface types. + /// + private static bool IsShaderType(INamedTypeSymbol typeSymbol, ImmutableHashSet typeSymbols) + { + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) + { + // Look for either the interface itself, if not generic, or the unbounded type if it's generic + if ((!interfaceSymbol.IsGenericType && typeSymbols.Contains(interfaceSymbol)) || + (interfaceSymbol.IsGenericType && typeSymbols.Contains(interfaceSymbol.ConstructedFrom))) + { + return true; + } + } + + return false; + } +} \ No newline at end of file