33 行
1.3 KiB
C#
33 行
1.3 KiB
C#
using System.Collections.Immutable;
|
|
using ComputeSharp.SourceGeneration.Extensions;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.Diagnostics;
|
|
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
|
|
|
|
namespace ComputeSharp.SourceGenerators;
|
|
|
|
/// <summary>
|
|
/// A diagnostic analyzer that generates an error if the <c>AllowUnsafeBlocks</c> compilation option is not set.
|
|
/// </summary>
|
|
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
|
public sealed class MissingAllowUnsafeBlocksCompilationOptionAnalyzer : DiagnosticAnalyzer
|
|
{
|
|
/// <inheritdoc/>
|
|
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(MissingAllowUnsafeBlocksOption);
|
|
|
|
/// <inheritdoc/>
|
|
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));
|
|
}
|
|
});
|
|
}
|
|
} |