using System.Collections.Generic;
using System.Collections.Immutable;
using ComputeSharp.D2D1.SourceGenerators.Helpers;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1.SourceGenerators;
///
/// A diagnostic analyzer that generates an error whenever any shader metadata value attributes are used with an invalid value.
///
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InvalidEffectMetadataValueAnalyzer : DiagnosticAnalyzer
{
///
/// The set of type names for all metadata attributes.
///
private static readonly ImmutableArray MetadataAttributeTypeNames =
[
"ComputeSharp.D2D1.D2DEffectDisplayNameAttribute",
"ComputeSharp.D2D1.D2DEffectDescriptionAttribute",
"ComputeSharp.D2D1.D2DEffectCategoryAttribute",
"ComputeSharp.D2D1.D2DEffectAuthorAttribute",
];
///
/// The mapping of diagnostics to their attribute type names.
///
private static readonly ImmutableDictionary AttributeTypeNameToDiagnosticMapping = ImmutableDictionary.CreateRange(
[
new KeyValuePair("D2DEffectDisplayNameAttribute", InvalidD2DEffectDisplayNameAttributeValue),
new KeyValuePair("D2DEffectDescriptionAttribute", InvalidD2DEffectDescriptionAttributeValue),
new KeyValuePair("D2DEffectCategoryAttribute", InvalidD2DEffectCategoryAttributeValue),
new KeyValuePair("D2DEffectAuthorAttribute", InvalidD2DEffectAuthorAttributeValue)
]);
///
public override ImmutableArray SupportedDiagnostics { get; } =
[
InvalidD2DEffectDisplayNameAttributeValue,
InvalidD2DEffectDescriptionAttributeValue,
InvalidD2DEffectCategoryAttributeValue,
InvalidD2DEffectAuthorAttributeValue,
];
///
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the metadata attribute symbols
if (!context.Compilation.TryBuildNamedTypeSymbolSet(MetadataAttributeTypeNames, out ImmutableHashSet? typeSymbols))
{
return;
}
context.RegisterSymbolAction(context =>
{
// Only struct types and assemblies are possible targets
if (context.Symbol is not (INamedTypeSymbol { TypeKind: TypeKind.Struct } or IAssemblySymbol))
{
return;
}
foreach (AttributeData attributeData in context.Symbol.GetAttributes())
{
// Only check attributes we care about
if (attributeData.AttributeClass is not INamedTypeSymbol attributeSymbol ||
!typeSymbols.Contains(attributeSymbol))
{
continue;
}
// Validate the effect display name
if (!D2D1EffectMetadataParser.IsValidEffectMetadataName(attributeData))
{
context.ReportDiagnostic(Diagnostic.Create(
AttributeTypeNameToDiagnosticMapping[attributeSymbol.MetadataName],
attributeData.GetLocation()));
}
return;
}
}, SymbolKind.NamedType | SymbolKind.Assembly);
});
}
}