Add InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-15 23:05:35 +02:00
1 親 6baf3339fe
コミット 876e261f6b
4ファイル変更
+73 -1

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

+1
ファイルの表示
@@ -59,3 +59,4 @@ CMPS0050 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
CMPS0051 | ComputeSharp | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0052 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0053 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0054 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
@@ -0,0 +1,55 @@
using System.Collections.Immutable;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators;
/// <summary>
/// A diagnostic analyzer that generates an error whenever the [GeneratedComputeShaderDescriptor] attribute is used on an invalid target type.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DGeneratedPixelShaderDescriptorAttributeTarget);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the IComputeShader, IComputeShader<TPixel> and [GeneratedComputeShaderDescriptor] symbols
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader") is not { } computeShaderSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader`1") is not { } pixelShaderSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.GeneratedComputeShaderDescriptorAttribute") is not { } generatedComputeShaderDescriptorAttributeSymbol)
{
return;
}
context.RegisterSymbolAction(context =>
{
// Only struct types are possible targets
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol)
{
return;
}
// Emit a diagnostic if the target type is using [GeneratedComputeShaderDescriptor] but does not implement IComputeShader nor IComputeShader<TPixel>
if (typeSymbol.TryGetAttributeWithType(generatedComputeShaderDescriptorAttributeSymbol, out AttributeData? attribute) &&
!typeSymbol.HasInterfaceWithType(computeShaderSymbol) &&
!typeSymbol.HasInterfaceWithType(pixelShaderSymbol))
{
context.ReportDiagnostic(Diagnostic.Create(
InvalidD2DGeneratedPixelShaderDescriptorAttributeTarget,
attribute.GetLocation(),
typeSymbol));
}
}, SymbolKind.NamedType);
});
}
}
@@ -5,7 +5,7 @@ using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1.SourceGenerators;
namespace ComputeSharp.SourceGenerators;
/// <summary>
/// A diagnostic analyzer that generates a warning whenever a compute shader type does not have an associated descriptor.
+16
ファイルの表示
@@ -752,4 +752,20 @@ partial class DiagnosticDescriptors
description: "All compute shader types must have an associated descriptor (it can be autogenerated via the [GeneratedComputeShaderDescriptor] attribute).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp",
customTags: WellKnownDiagnosticTags.CompilationEnd);
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[GeneratedComputeShaderDescriptor]</c> attribute is being used on an invalid target type.
/// <para>
/// Format: <c>"The type {0} is not a valid target for the [GeneratedComputeShaderDescriptor] attribute (only types implementing the IComputeShader or IComputeShader&lt;TPixel&gt; interface are valid)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidD2DGeneratedPixelShaderDescriptorAttributeTarget = new DiagnosticDescriptor(
id: "CMPS0054",
title: "Invalid [GeneratedComputeShaderDescriptor] attribute target",
messageFormat: "The type {0} is not a valid target for the [GeneratedComputeShaderDescriptor] attribute (only types implementing the IComputeShader or IComputeShader<TPixel> interface are valid)",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The [GeneratedComputeShaderDescriptor] attribute must be used on types that implement the IComputeShader or IComputeShader<TPixel> interfaces.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}