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;
+16
ファイルの表示
@@ -873,4 +873,20 @@ partial class DiagnosticDescriptors
isEnabledByDefault: true,
description: "The [D2DEffectId] attribute must use a valid GUID expression as value.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectDisplayName]</c> attribute is being used with an invalid value.
/// <para>
/// Format: <c>"The [D2DEffectDisplayName] attribute is being used with an invalid value (the input text must be a valid GUID)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidD2DEffectDisplayNameAttributeValue = new DiagnosticDescriptor(
id: "CMPSD2D0060",
title: "Invalid [D2DEffectDisplayName] attribute value",
messageFormat: "The [D2DEffectDisplayName] attribute is being used with an invalid value",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The [D2DEffectDisplayName] attribute must contain valid text.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}