From 0bb328f14b9a1ea56e3d918c17fa2d1352225227 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2024 19:32:18 -0700 Subject: [PATCH] Add 'InvalidGeneratedCanvasEffectPropertyDeclarationAnalyzer' --- .../AnalyzerReleases.Shipped.md | 4 + .../CanvasEffectPropertyGenerator.cs | 6 ++ ...CanvasEffectPropertyDeclarationAnalyzer.cs | 90 +++++++++++++++++++ .../Diagnostics/DiagnosticDescriptors.cs | 66 +++++++++++++- 4 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedCanvasEffectPropertyDeclarationAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md index 6d5cfa5f..3449845d 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md @@ -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) diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs index 18b41a58..a63a7152 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs @@ -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")) { diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedCanvasEffectPropertyDeclarationAnalyzer.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedCanvasEffectPropertyDeclarationAnalyzer.cs new file mode 100644 index 00000000..94482d0a --- /dev/null +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedCanvasEffectPropertyDeclarationAnalyzer.cs @@ -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; + +/// +/// A diagnostic analyzer that generates errors when a property using [GeneratedCanvasEffectProperty] has invalid accessors. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class InvalidGeneratedCanvasEffectPropertyDeclarationAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = + [ + InvalidGeneratedCanvasEffectPropertyDeclarationIsStatic, + InvalidGeneratedCanvasEffectPropertyDeclarationIsNotIncompletePartialDefinition, + InvalidGeneratedCanvasEffectPropertyDeclarationReturnsByRef, + InvalidGeneratedCanvasEffectPropertyDeclarationReturnsRefLikeType + ]; + + /// + 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); + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 21218efb..05a7daa4 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -31,11 +31,75 @@ internal static class DiagnosticDescriptors /// 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"); + + /// + /// Gets a for a static CanvasEffect property. + /// + /// Format: "The property "{0}" is static ([GeneratedCanvasEffectProperty] must be used on instance properties)". + /// + /// + 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"); + + /// + /// Gets a for a CanvasEffect property with invalid accessors. + /// + /// Format: "The property "{0}" is not an incomplete partial definition ([GeneratedCanvasEffectProperty] must be used on partial property definitions with no implementation part)". + /// + /// + 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"); + + /// + /// Gets a for a CanvasEffect property with invalid accessors. + /// + /// Format: "The property "{0}" returns a value by reference ([GeneratedCanvasEffectProperty] must be used on properties returning a type by value)". + /// + /// + 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"); + + /// + /// Gets a for a CanvasEffect property with invalid accessors. + /// + /// Format: "The property "{0}" returns a ref struct value ([GeneratedCanvasEffectProperty] must be used on properties with a type that is not a ref struct)". + /// + /// + 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"); } \ No newline at end of file