Make 'AllowsUnsafeBlocks' analyzer target attributes

このコミットが含まれているのは:
Sergio Pedri committed 2024-06-16 19:32:08 -07:00
1 親 ebee50d72d
コミット b6bcc1c6cc
5ファイル変更
+43 -17

検索条件に一致するファイルはありません

@@ -9,7 +9,10 @@ namespace ComputeSharp.SourceGeneration.Diagnostics;
/// A diagnostic analyzer that generates an error if the <c>AllowUnsafeBlocks</c> compilation option is not set.
/// </summary>
/// <param name="diagnosticDescriptor">The <see cref="DiagnosticDescriptor"/> instance to use.</param>
public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor) : DiagnosticAnalyzer
/// <param name="generatedShaderDescriptorFullyQualifiedTypeName">The fully qualified type name of the target attribute.</param>
public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(
DiagnosticDescriptor diagnosticDescriptor,
string generatedShaderDescriptorFullyQualifiedTypeName) : DiagnosticAnalyzer
{
/// <inheritdoc/>
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [diagnosticDescriptor];
@@ -20,13 +23,38 @@ public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(Diag
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationAction(context =>
context.RegisterCompilationStartAction(context =>
{
// Check whether unsafe blocks are available, and emit an error if they are not
if (!context.Compilation.IsAllowUnsafeBlocksEnabled())
// If unsafe blocks are allowed, we'll never need to emit a diagnostic
if (context.Compilation.IsAllowUnsafeBlocksEnabled())
{
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, location: null));
return;
}
// Get the symbol for the target attribute type
if (context.Compilation.GetTypeByMetadataName(generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol)
{
return;
}
// Check if any types in the compilation are using the trigger attribute
context.RegisterSymbolAction(context =>
{
// Only struct types are possible targets
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol)
{
return;
}
// If the target type is not using the attribute, there's nothing left to do
if (!typeSymbol.TryGetAttributeWithType(generatedShaderDescriptorAttributeSymbol, out AttributeData? attribute))
{
return;
}
// Emit the error on the attribute use
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, attribute.GetLocation()));
}, SymbolKind.NamedType);
});
}
}