Add InvalidEffectDisplayNameValueAnalyzer

このコミットが含まれているのは:
Sergio Pedri committed 2023-09-27 14:04:04 +02:00
1 親 8cbb3c958e
コミット 9d5e412f7d
6ファイル変更
+155 -28

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

@@ -0,0 +1,62 @@
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;
/// <summary>
/// A diagnostic analyzer that generates an error whenever [D2DEffectName] is used with an invalid value.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InvalidEffectDisplayNameValueAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DEffectDisplayNameAttributeValue);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the [D2DEffectDisplayName] symbol
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DEffectDisplayNameAttribute") is not { } d2DEffectIdAttributeSymbol)
{
return;
}
context.RegisterSymbolAction(context =>
{
// Only struct types are possible targets
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol)
{
return;
}
foreach (AttributeData attributeData in typeSymbol.GetAttributes())
{
// Look for the [D2DEffectDisplayName] use
if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, d2DEffectIdAttributeSymbol))
{
continue;
}
// Validate the effect display name
if (!D2D1EffectMetadataParser.IsValidEffectDisplayName(attributeData))
{
context.ReportDiagnostic(Diagnostic.Create(
InvalidD2DEffectDisplayNameAttributeValue,
attributeData.GetLocation()));
}
return;
}
}, SymbolKind.NamedType);
});
}
}
@@ -38,21 +38,21 @@ public sealed class InvalidEffectIdValueAnalyzer : DiagnosticAnalyzer
return;
}
foreach (AttributeData attributeDate in typeSymbol.GetAttributes())
foreach (AttributeData attributeData in typeSymbol.GetAttributes())
{
// Look for the [D2DEffectId] use
if (!SymbolEqualityComparer.Default.Equals(attributeDate.AttributeClass, d2DEffectIdAttributeSymbol))
if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, d2DEffectIdAttributeSymbol))
{
continue;
}
// Validate the GUID text and emit a diagnostic if needed
if (attributeDate.ConstructorArguments is not [{ Value: string value }] ||
if (attributeData.ConstructorArguments is not [{ Value: string value }] ||
!Guid.TryParse(value, out _))
{
context.ReportDiagnostic(Diagnostic.Create(
InvalidD2DEffectIdAttributeValue,
attributeDate.GetLocation()));
attributeData.GetLocation()));
}
return;