using System.Collections.Immutable; 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 if the AllowUnsafeBlocks compilation option is not set. /// [DiagnosticAnalyzer(LanguageNames.CSharp)] public sealed class MissingAllowUnsafeBlocksCompilationOptionAnalyzer : DiagnosticAnalyzer { /// public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MissingAllowUnsafeBlocksOption); /// public override void Initialize(AnalysisContext context) { context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); context.EnableConcurrentExecution(); context.RegisterCompilationAction(static context => { // Check whether unsafe blocks are available, and emit an error if they are not if (!context.Compilation.IsAllowUnsafeBlocksEnabled()) { context.ReportDiagnostic(Diagnostic.Create(MissingAllowUnsafeBlocksOption, location: null)); } }); } }