Add 'InvalidGeneratedCanvasEffectPropertyAnalyzer'
このコミットが含まれているのは:
1 親
9f789d1bb3
コミット
d37001d45d
4ファイル変更
+107
検索条件に一致するファイルはありません
@@ -7,3 +7,4 @@
|
||||
|
||||
Rule ID | Category | Severity | Notes
|
||||
--------|----------|----------|-------
|
||||
CMPSD2DWINUI0001 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
using System.Collections.Immutable;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using static ComputeSharp.D2D1.WinUI.SourceGenerators.DiagnosticDescriptors;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.SourceGenerators;
|
||||
|
||||
/// <summary>
|
||||
/// A diagnostic analyzer that generates errors when a property using [GeneratedCanvasEffectProperty] is in an invalid type.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class InvalidGeneratedCanvasEffectPropertyContainingTypeAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [InvalidGeneratedCanvasEffectPropertyContainingType];
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
context.RegisterCompilationStartAction(static context =>
|
||||
{
|
||||
// Get the [GeneratedCanvasEffectProperty] and CanvasEffect symbols
|
||||
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.WinUI.GeneratedCanvasEffectPropertyAttribute") is not { } generatedCanvasEffectPropertyAttributeSymbol ||
|
||||
context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.WinUI.CanvasEffect") is not { } canvasEffectSymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
context.RegisterSymbolAction(context =>
|
||||
{
|
||||
// Validate that we have a valid containing type
|
||||
if (context.Symbol is not IPropertySymbol { ContainingType: { } containingTypeSymbol })
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// If the property is not using [GeneratedCanvasEffectProperty], there's nothing to do
|
||||
if (!context.Symbol.TryGetAttributeWithType(generatedCanvasEffectPropertyAttributeSymbol, out AttributeData? generatedCanvasEffectPropertyAttribute))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Issue a diagnostic if the type doesn't derive from CanvasEffect
|
||||
if (!containingTypeSymbol.InheritsFromType(canvasEffectSymbol))
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(
|
||||
InvalidGeneratedCanvasEffectPropertyContainingType,
|
||||
generatedCanvasEffectPropertyAttribute.GetLocation(),
|
||||
context.Symbol));
|
||||
}
|
||||
}, SymbolKind.Property);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.SourceGenerators;
|
||||
|
||||
/// <summary>
|
||||
/// A container for all <see cref="DiagnosticDescriptor"/> instances for errors reported by analyzers in this project.
|
||||
/// </summary>
|
||||
internal static class DiagnosticDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid CanvasEffect property.
|
||||
/// <para>
|
||||
/// Format: <c>"The property "{0}" is not in a type that derives from CanvasEffect ([GeneratedCanvasEffectProperty] must be used in effect types that extend CanvasEffect)"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor InvalidGeneratedCanvasEffectPropertyContainingType = new(
|
||||
id: "CMPSD2DWINUI0001",
|
||||
title: "Invalid generated CanvasEffect property containing type",
|
||||
messageFormat: """The property "{0}" is not in a type that derives from CanvasEffect ([GeneratedCanvasEffectProperty] must be used in effect types that extend CanvasEffect)""",
|
||||
category: "ComputeSharp.D2D1.WinUI.Effects",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "A property using [GeneratedCanvasEffectProperty] is in a type that does not derive from CanvasEffect ([GeneratedCanvasEffectProperty] must be used in effect types that extend CanvasEffect).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
}
|
||||
@@ -67,6 +67,29 @@ internal static class ITypeSymbolExtensions
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
|
||||
/// </summary>
|
||||
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
|
||||
/// <param name="baseTypeSymbol">The <see cref="ITypeSymbol"/> instane to check for inheritance from.</param>
|
||||
/// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="baseTypeSymbol"/>.</returns>
|
||||
public static bool InheritsFromType(this ITypeSymbol typeSymbol, ITypeSymbol baseTypeSymbol)
|
||||
{
|
||||
INamedTypeSymbol? currentBaseTypeSymbol = typeSymbol.BaseType;
|
||||
|
||||
while (currentBaseTypeSymbol is not null)
|
||||
{
|
||||
if (SymbolEqualityComparer.Default.Equals(currentBaseTypeSymbol, baseTypeSymbol))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
currentBaseTypeSymbol = currentBaseTypeSymbol.BaseType;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given <see cref="ITypeSymbol"/> implements an interface of a specified type.
|
||||
/// </summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする