Add 'RequireCSharpLanguageVersionPreviewAnalyzer'

このコミットが含まれているのは:
Sergio Pedri committed 2024-10-17 21:41:43 -07:00
1 親 afe8e1d93a
コミット f57bf4252a
7ファイル変更
+94 -4

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

+2 -2
ファイルの表示
@@ -944,13 +944,13 @@ partial class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>AllowUnsafeBlocks</c> option is not set.
/// <para>
/// Format: <c>"Using [D2DGeneratedPixelShaderDescriptor] requires unsafe blocks being enabled, as they needed by the source generators to emit valid code (add &lt;AllowUnsafeBlocks&gt;true&lt;/AllowUnsafeBlocks&gt; to your .csproj/.props file)"</c>.
/// Format: <c>"Using [D2DGeneratedPixelShaderDescriptor] requires unsafe blocks being enabled, as they are needed by the source generators to emit valid code (add &lt;AllowUnsafeBlocks&gt;true&lt;/AllowUnsafeBlocks&gt; to your .csproj/.props file)"</c>.
/// </para>
/// </summary>
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 <AllowUnsafeBlocks>true</AllowUnsafeBlocks> 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 <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your .csproj/.props file)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
+1
ファイルの表示
@@ -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)
+6
ファイルの表示
@@ -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)
{
@@ -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;
/// <summary>
/// A diagnostic analyzer that generates errors when a property using [GeneratedCanvasEffectProperty] is in a project with the C# language version not set to preview.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class RequireCSharpLanguageVersionPreviewAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [CSharpLanguageVersionIsNotPreview];
/// <inheritdoc/>
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);
});
}
}
+16
ファイルの表示
@@ -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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a CanvasEffect property with invalid accessors.
/// <para>
/// Format: <c>"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 &lt;LangVersion&gt;preview&lt;/LangVersion&gt; to your .csproj/.props file)"</c>.
/// </para>
/// </summary>
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 <LangVersion>preview</LangVersion> 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 <LangVersion>preview</LangVersion> option must be set in the .csproj/.props file).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+10
ファイルの表示
@@ -46,6 +46,16 @@ internal static class CompilationExtensions
return true;
}
/// <summary>
/// Checks whether a given compilation (assumed to be for C#) is using the preview language version.
/// </summary>
/// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
/// <returns>Whether <paramref name="compilation"/> is using the preview language version.</returns>
public static bool IsLanguageVersionPreview(this Compilation compilation)
{
return ((CSharpCompilation)compilation).LanguageVersion == LanguageVersion.Preview;
}
/// <summary>
/// Checks whether the <c>AllowUnsafeBlocks</c> option is set for a given compilation.
/// </summary>
+2 -2
ファイルの表示
@@ -720,13 +720,13 @@ partial class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>AllowUnsafeBlocks</c> option is not set.
/// <para>
/// Format: <c>"Using [GeneratedComputeShaderDescriptor] requires unsafe blocks being enabled, as they needed by the source generators to emit valid code (add &lt;AllowUnsafeBlocks&gt;true&lt;/AllowUnsafeBlocks&gt; to your .csproj/.props file)"</c>.
/// Format: <c>"Using [GeneratedComputeShaderDescriptor] requires unsafe blocks being enabled, as they are needed by the source generators to emit valid code (add &lt;AllowUnsafeBlocks&gt;true&lt;/AllowUnsafeBlocks&gt; to your .csproj/.props file)"</c>.
/// </para>
/// </summary>
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 <AllowUnsafeBlocks>true</AllowUnsafeBlocks> 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 <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your .csproj/.props file)",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,