Add 'InvalidGeneratedCanvasEffectPropertyDeclarationAnalyzer'
このコミットが含まれているのは:
1 親
d8f9b35c9e
コミット
0bb328f14b
4ファイル変更
+165
-1
検索条件に一致するファイルはありません
@@ -9,3 +9,7 @@ Rule ID | Category | Severity | Notes
|
||||
--------|----------|----------|-------
|
||||
CMPSD2DWINUI0001 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2DWINUI0002 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2DWINUI0003 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2DWINUI0004 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2DWINUI0005 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2DWINUI0006 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
@@ -41,6 +41,12 @@ public sealed partial class CanvasEffectPropertyGenerator : IIncrementalGenerato
|
||||
return default;
|
||||
}
|
||||
|
||||
// Also ignore all properties that have an invalid declaration
|
||||
if (propertySymbol.IsStatic || propertySymbol.ReturnsByRef || propertySymbol.ReturnsByRefReadonly || propertySymbol.Type.IsRefLikeType)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
// Ensure that the containing type derives from CanvasEffect
|
||||
if (!typeSymbol.InheritsFromFullyQualifiedMetadataName("ComputeSharp.D2D1.WinUI.CanvasEffect"))
|
||||
{
|
||||
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
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] has invalid accessors.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class InvalidGeneratedCanvasEffectPropertyDeclarationAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
|
||||
[
|
||||
InvalidGeneratedCanvasEffectPropertyDeclarationIsStatic,
|
||||
InvalidGeneratedCanvasEffectPropertyDeclarationIsNotIncompletePartialDefinition,
|
||||
InvalidGeneratedCanvasEffectPropertyDeclarationReturnsByRef,
|
||||
InvalidGeneratedCanvasEffectPropertyDeclarationReturnsRefLikeType
|
||||
];
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
context.RegisterCompilationStartAction(static context =>
|
||||
{
|
||||
// Get the [GeneratedCanvasEffectProperty] symbols
|
||||
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.WinUI.GeneratedCanvasEffectPropertyAttribute") is not { } generatedCanvasEffectPropertyAttributeSymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
context.RegisterSymbolAction(context =>
|
||||
{
|
||||
// Ensure that we have some target property to analyze
|
||||
if (context.Symbol is not IPropertySymbol propertySymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// If the property is not using [GeneratedCanvasEffectProperty], there's nothing to do
|
||||
if (!context.Symbol.TryGetAttributeWithType(generatedCanvasEffectPropertyAttributeSymbol, out AttributeData? generatedCanvasEffectPropertyAttribute))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Emit an error if the property is static
|
||||
if (propertySymbol.IsStatic)
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(
|
||||
InvalidGeneratedCanvasEffectPropertyDeclarationIsStatic,
|
||||
generatedCanvasEffectPropertyAttribute.GetLocation(),
|
||||
context.Symbol));
|
||||
}
|
||||
|
||||
// Emit an error if the property is not a partial definition with no implementation
|
||||
if (propertySymbol is not { IsPartialDefinition: true, PartialImplementationPart: null })
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(
|
||||
InvalidGeneratedCanvasEffectPropertyDeclarationIsNotIncompletePartialDefinition,
|
||||
generatedCanvasEffectPropertyAttribute.GetLocation(),
|
||||
context.Symbol));
|
||||
}
|
||||
|
||||
// Emit an error if the property returns a value by ref
|
||||
if (propertySymbol.ReturnsByRef || propertySymbol.ReturnsByRefReadonly)
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(
|
||||
InvalidGeneratedCanvasEffectPropertyDeclarationReturnsByRef,
|
||||
generatedCanvasEffectPropertyAttribute.GetLocation(),
|
||||
context.Symbol));
|
||||
}
|
||||
|
||||
// Emit an error if the property type is a ref struct
|
||||
if (propertySymbol.Type.IsRefLikeType)
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(
|
||||
InvalidGeneratedCanvasEffectPropertyDeclarationReturnsRefLikeType,
|
||||
generatedCanvasEffectPropertyAttribute.GetLocation(),
|
||||
context.Symbol));
|
||||
}
|
||||
}, SymbolKind.Property);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -31,11 +31,75 @@ internal static class DiagnosticDescriptors
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor InvalidGeneratedCanvasEffectPropertyAccessors = new(
|
||||
id: "CMPSD2DWINUI0002",
|
||||
title: "Invalid generated CanvasEffect property declaration",
|
||||
title: "Invalid generated CanvasEffect property accessors",
|
||||
messageFormat: """The property "{0}" does not have a getter and a setter ([GeneratedCanvasEffectProperty] must be used on properties with get and set accessors)""",
|
||||
category: "ComputeSharp.D2D1.WinUI.Effects",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "A property using [GeneratedCanvasEffectProperty] does not have a getter and a setter ([GeneratedCanvasEffectProperty] must be used on properties with get and set accessors).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for a static CanvasEffect property.
|
||||
/// <para>
|
||||
/// Format: <c>"The property "{0}" is static ([GeneratedCanvasEffectProperty] must be used on instance properties)"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor InvalidGeneratedCanvasEffectPropertyDeclarationIsStatic = new(
|
||||
id: "CMPSD2DWINUI0003",
|
||||
title: "Invalid generated CanvasEffect property declaration (static member)",
|
||||
messageFormat: """The property "{0}" is static ([GeneratedCanvasEffectProperty] must be used on instance properties)""",
|
||||
category: "ComputeSharp.D2D1.WinUI.Effects",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "A property using [GeneratedCanvasEffectProperty] cannot be static ([GeneratedCanvasEffectProperty] must be used on instance properties).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for a CanvasEffect property with invalid accessors.
|
||||
/// <para>
|
||||
/// Format: <c>"The property "{0}" is not an incomplete partial definition ([GeneratedCanvasEffectProperty] must be used on partial property definitions with no implementation part)"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor InvalidGeneratedCanvasEffectPropertyDeclarationIsNotIncompletePartialDefinition = new(
|
||||
id: "CMPSD2DWINUI0004",
|
||||
title: "Invalid generated CanvasEffect property declaration (not incomplete partial definition)",
|
||||
messageFormat: """The property "{0}" is not an incomplete partial definition ([GeneratedCanvasEffectProperty] must be used on partial property definitions with no implementation part)""",
|
||||
category: "ComputeSharp.D2D1.WinUI.Effects",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "A property using [GeneratedCanvasEffectProperty] is either not partial, or a partial implementation part ([GeneratedCanvasEffectProperty] must be used on partial property definitions with no implementation par).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for a CanvasEffect property with invalid accessors.
|
||||
/// <para>
|
||||
/// Format: <c>"The property "{0}" returns a value by reference ([GeneratedCanvasEffectProperty] must be used on properties returning a type by value)"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor InvalidGeneratedCanvasEffectPropertyDeclarationReturnsByRef = new(
|
||||
id: "CMPSD2DWINUI0005",
|
||||
title: "Invalid generated CanvasEffect property declaration (returns byref)",
|
||||
messageFormat: """The property "{0}" returns a value by reference ([GeneratedCanvasEffectProperty] must be used on properties returning a type by value)""",
|
||||
category: "ComputeSharp.D2D1.WinUI.Effects",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "A property using [GeneratedCanvasEffectProperty] returns a value by reference ([GeneratedCanvasEffectProperty] must be used on properties returning a type by value).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for a CanvasEffect property with invalid accessors.
|
||||
/// <para>
|
||||
/// Format: <c>"The property "{0}" returns a ref struct value ([GeneratedCanvasEffectProperty] must be used on properties with a type that is not a ref struct)"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor InvalidGeneratedCanvasEffectPropertyDeclarationReturnsRefLikeType = new(
|
||||
id: "CMPSD2DWINUI0006",
|
||||
title: "Invalid generated CanvasEffect property declaration (returns ref struct type)",
|
||||
messageFormat: """The property "{0}" returns a ref struct value ([GeneratedCanvasEffectProperty] must be used on properties with a type that is not a ref struct)""",
|
||||
category: "ComputeSharp.D2D1.WinUI.Effects",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "A property using [GeneratedCanvasEffectProperty] has a ref struct type ([GeneratedCanvasEffectProperty] must be used on properties with a type that is not a ref struct).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする