Add InvalidEffectIdValueAnalyzer

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

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

+2 -1
ファイルの表示
@@ -1,4 +1,4 @@
; Shipped analyzer releases
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
## Release 1.0
@@ -65,3 +65,4 @@ CMPSD2D0055 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0056 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0057 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0058 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0059 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
@@ -0,0 +1,63 @@
using System;
using System.Collections.Immutable;
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 [D2DEffectId] is used with an invalid value.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InvalidEffectIdValueAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DEffectIdAttributeValue);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the [D2DEffectId] symbol
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DEffectIdAttribute") 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 attributeDate in typeSymbol.GetAttributes())
{
// Look for the [D2DEffectId] use
if (!SymbolEqualityComparer.Default.Equals(attributeDate.AttributeClass, d2DEffectIdAttributeSymbol))
{
continue;
}
// Validate the GUID text and emit a diagnostic if needed
if (attributeDate.ConstructorArguments is not [{ Value: string value }] ||
!Guid.TryParse(value, out _))
{
context.ReportDiagnostic(Diagnostic.Create(
InvalidD2DEffectIdAttributeValue,
attributeDate.GetLocation()));
}
return;
}
}, SymbolKind.NamedType);
});
}
}
+16
ファイルの表示
@@ -857,4 +857,20 @@ partial class DiagnosticDescriptors
isEnabledByDefault: true,
description: "A D2D1 shader must be annotated with the [D2DInputCount] attribute.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectId]</c> attribute is being used with an invalid value.
/// <para>
/// Format: <c>"The [D2DEffectId] attribute is being used with an invalid value (the input text must be a valid GUID)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidD2DEffectIdAttributeValue = new DiagnosticDescriptor(
id: "CMPSD2D0059",
title: "Invalid [D2DEffectId] attribute value",
messageFormat: "The [D2DEffectId] attribute is being used with an invalid value (the input text must be a valid GUID)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The [D2DEffectId] attribute must use a valid GUID expression as value.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+15
ファイルの表示
@@ -8,6 +8,21 @@ namespace ComputeSharp.SourceGeneration.Extensions;
/// </summary>
internal static class AttributeDataExtensions
{
/// <summary>
/// Tries to get the location of the input <see cref="AttributeData"/> instance.
/// </summary>
/// <param name="attributeData">The input <see cref="AttributeData"/> instance to get the location for.</param>
/// <returns>The resulting location for <paramref name="attributeData"/>, if a syntax reference is available.</returns>
public static Location? GetLocation(this AttributeData attributeData)
{
if (attributeData.ApplicationSyntaxReference is { } syntaxReference)
{
return syntaxReference.SyntaxTree.GetLocation(syntaxReference.Span);
}
return null;
}
/// <summary>
/// Tries to get a constructor argument at a given index from the input <see cref="AttributeData"/> instance.
/// </summary>