From f57bf4252a35446c62469953f75d6344ff4868f2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 26 Aug 2024 20:01:33 -0700 Subject: [PATCH] Add 'RequireCSharpLanguageVersionPreviewAnalyzer' --- .../Diagnostics/DiagnosticDescriptors.cs | 4 +- .../AnalyzerReleases.Shipped.md | 1 + .../CanvasEffectPropertyGenerator.cs | 6 ++ ...ireCSharpLanguageVersionPreviewAnalyzer.cs | 57 +++++++++++++++++++ .../Diagnostics/DiagnosticDescriptors.cs | 16 ++++++ .../Extensions/CompilationExtensions.cs | 10 ++++ .../Diagnostics/DiagnosticDescriptors.cs | 4 +- 7 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/Analyzers/RequireCSharpLanguageVersionPreviewAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 83836ddd..6e8dd5b9 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -944,13 +944,13 @@ partial class DiagnosticDescriptors /// /// Gets a for when the AllowUnsafeBlocks option is not set. /// - /// Format: "Using [D2DGeneratedPixelShaderDescriptor] requires unsafe blocks being enabled, as they needed by the source generators to emit valid code (add <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your .csproj/.props file)". + /// Format: "Using [D2DGeneratedPixelShaderDescriptor] requires unsafe blocks being enabled, as they are needed by the source generators to emit valid code (add <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your .csproj/.props file)". /// /// public static readonly DiagnosticDescriptor MissingAllowUnsafeBlocksOption = new( id: "CMPSD2D0064", title: "Missing 'AllowUnsafeBlocks' compilation option", - messageFormat: "Using [D2DGeneratedPixelShaderDescriptor] requires unsafe blocks being enabled, as they needed by the source generators to emit valid code (add true to your .csproj/.props file)", + messageFormat: "Using [D2DGeneratedPixelShaderDescriptor] requires unsafe blocks being enabled, as they are needed by the source generators to emit valid code (add true to your .csproj/.props file)", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md index 3449845d..a67085fd 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md @@ -13,3 +13,4 @@ CMPSD2DWINUI0003 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](htt 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) +CMPSD2DWINUI0007 | 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 a63a7152..a9f20c6e 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs @@ -29,6 +29,12 @@ public sealed partial class CanvasEffectPropertyGenerator : IIncrementalGenerato Execute.IsCandidatePropertyDeclaration, static (context, token) => { + // This generator requires C# preview to be used (due to the use of the 'field' keyword) + if (!context.SemanticModel.Compilation.IsLanguageVersionPreview()) + { + return default; + } + // Ensure we do have a property with a valid containing type if (context.TargetSymbol is not IPropertySymbol { ContainingType: INamedTypeSymbol typeSymbol } propertySymbol) { diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/Analyzers/RequireCSharpLanguageVersionPreviewAnalyzer.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/Analyzers/RequireCSharpLanguageVersionPreviewAnalyzer.cs new file mode 100644 index 00000000..d9bc6943 --- /dev/null +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/Analyzers/RequireCSharpLanguageVersionPreviewAnalyzer.cs @@ -0,0 +1,57 @@ +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] is in a project with the C# language version not set to preview. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class RequireCSharpLanguageVersionPreviewAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = [CSharpLanguageVersionIsNotPreview]; + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // If the language version is set to preview, we'll never emit diagnostics + if (context.Compilation.IsLanguageVersionPreview()) + { + return; + } + + // Get the [GeneratedCanvasEffectProperty] symbols + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.WinUI.GeneratedCanvasEffectPropertyAttribute") is not { } generatedCanvasEffectPropertyAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // We only want to target partial property definitions (also include non-partial ones for diagnostics) + if (context.Symbol is not IPropertySymbol { PartialDefinitionPart: null }) + { + return; + } + + // If the property is using [GeneratedCanvasEffectProperty], emit the diagnostic + if (context.Symbol.TryGetAttributeWithType(generatedCanvasEffectPropertyAttributeSymbol, out AttributeData? generatedCanvasEffectPropertyAttribute)) + { + context.ReportDiagnostic(Diagnostic.Create( + CSharpLanguageVersionIsNotPreview, + 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 05a7daa4..3dc9d5fd 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -102,4 +102,20 @@ internal static class DiagnosticDescriptors 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"); + + /// + /// Gets a for a CanvasEffect property with invalid accessors. + /// + /// Format: "Using [GeneratedCanvasEffectProperty] requires the C# language version to be set to 'preview', as support for the 'field' keyword is needed by the source generators to emit valid code (add <LangVersion>preview</LangVersion> to your .csproj/.props file)". + /// + /// + public static readonly DiagnosticDescriptor CSharpLanguageVersionIsNotPreview = new( + id: "CMPSD2DWINUI0007", + title: "C# language version is not 'preview'", + messageFormat: """Using [GeneratedCanvasEffectProperty] requires the C# language version to be set to 'preview', as support for the 'field' keyword is needed by the source generators to emit valid code (add preview to your .csproj/.props file)""", + category: "ComputeSharp.D2D1.WinUI.Effects", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The C# language version must be set to 'preview' when using [GeneratedComputeShaderDescriptor] for the source generators to emit valid code (the preview option must be set in the .csproj/.props file).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs index 364a639a..ec2db073 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs @@ -46,6 +46,16 @@ internal static class CompilationExtensions return true; } + /// + /// Checks whether a given compilation (assumed to be for C#) is using the preview language version. + /// + /// The to consider for analysis. + /// Whether is using the preview language version. + public static bool IsLanguageVersionPreview(this Compilation compilation) + { + return ((CSharpCompilation)compilation).LanguageVersion == LanguageVersion.Preview; + } + /// /// Checks whether the AllowUnsafeBlocks option is set for a given compilation. /// diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index e55d2a4d..b71dfda1 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -720,13 +720,13 @@ partial class DiagnosticDescriptors /// /// Gets a for when the AllowUnsafeBlocks option is not set. /// - /// Format: "Using [GeneratedComputeShaderDescriptor] requires unsafe blocks being enabled, as they needed by the source generators to emit valid code (add <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your .csproj/.props file)". + /// Format: "Using [GeneratedComputeShaderDescriptor] requires unsafe blocks being enabled, as they are needed by the source generators to emit valid code (add <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your .csproj/.props file)". /// /// public static readonly DiagnosticDescriptor MissingAllowUnsafeBlocksOption = new( id: "CMPS0052", title: "Missing 'AllowUnsafeBlocks' compilation option", - messageFormat: "Using [GeneratedComputeShaderDescriptor] requires unsafe blocks being enabled, as they needed by the source generators to emit valid code (add true to your .csproj/.props file)", + messageFormat: "Using [GeneratedComputeShaderDescriptor] requires unsafe blocks being enabled, as they are needed by the source generators to emit valid code (add true to your .csproj/.props file)", category: "ComputeSharp.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true,