using System.Collections.Immutable; using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; namespace ComputeSharp.SourceGeneration.Diagnostics; /// /// A diagnostic analyzer that generates an error if a target shader type is not accessible from its containing assembly. /// /// The instance to use. /// The fully qualified type name of the target attribute. public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase( DiagnosticDescriptor diagnosticDescriptor, string generatedShaderDescriptorFullyQualifiedTypeName) : 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 symbol for the target attribute type if (context.Compilation.GetTypeByMetadataName(generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) { return; } context.RegisterSymbolAction(context => { // Only struct types are possible targets if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) { return; } // Emit a diagnostic if the target type is using the attribute but is not accessible if (typeSymbol.TryGetAttributeWithType(generatedShaderDescriptorAttributeSymbol, out AttributeData? attribute) && !typeSymbol.IsAccessibleFromContainingAssembly(context.Compilation)) { context.ReportDiagnostic(Diagnostic.Create( diagnosticDescriptor, attribute.GetLocation(), typeSymbol)); } }, SymbolKind.NamedType); }); } }