Add MissingComputeShaderDescriptorOnComputeShaderAnalyzer

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-15 23:00:14 +02:00
1 親 6fe1ee5743
コミット 6baf3339fe
3ファイル変更
+86

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

+1
ファイルの表示
@@ -58,3 +58,4 @@ CMPS0049 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
CMPS0050 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
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)
@@ -0,0 +1,63 @@
using System.Collections.Immutable;
using System.Linq;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <summary>
/// A diagnostic analyzer that generates a warning whenever a compute shader type does not have an associated descriptor.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class MissingComputeShaderDescriptorOnComputeShaderAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(MissingComputeShaderDescriptorOnComputeShaderType);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the IComputeShader, IComputeShader<TPixel>, IComputeShaderDescriptor<T> 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.Descriptors.IComputeShaderDescriptor`1") is not { } computeShaderDescriptorSymbol ||
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;
}
// If the type is not a compute shader type, immediately bail out
if (!typeSymbol.HasInterfaceWithType(computeShaderSymbol) &&
!typeSymbol.HasInterfaceWithType(pixelShaderSymbol))
{
return;
}
// Emit a diagnostic if the descriptor is missing for the shader type
if (!typeSymbol.HasInterfaceWithType(computeShaderDescriptorSymbol) &&
!typeSymbol.HasAttributeWithType(generatedComputeShaderDescriptorAttributeSymbol))
{
context.ReportDiagnostic(Diagnostic.Create(
MissingComputeShaderDescriptorOnComputeShaderType,
typeSymbol.Locations.FirstOrDefault(),
typeSymbol));
}
}, SymbolKind.NamedType);
});
}
}
+22
ファイルの表示
@@ -7,6 +7,11 @@ namespace ComputeSharp.SourceGeneration.Diagnostics;
/// <inheritdoc/>
partial class DiagnosticDescriptors
{
/// <summary>
/// The diagnostic id for <see cref="MissingComputeShaderDescriptorOnComputeShaderType"/>.
/// </summary>
public const string MissingComputeShaderDescriptorOnComputeShaderTypeId = "CMPS0053";
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid shader field.
/// <para>
@@ -730,4 +735,21 @@ partial class DiagnosticDescriptors
description: "Unsafe blocks must be enabled for the source generators to emit valid code (the <AllowUnsafeBlocks>true</AllowUnsafeBlocks> option must be set in the .csproj/.props file).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp",
customTags: WellKnownDiagnosticTags.CompilationEnd);
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when a compute shader type doesn't have an associated descriptor.
/// <para>
/// Format: <c>"The compute shader of type {0} does not have an associated descriptor (it can be autogenerated via the [GeneratedComputeShaderDescriptor] attribute)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor MissingComputeShaderDescriptorOnComputeShaderType = new DiagnosticDescriptor(
id: MissingComputeShaderDescriptorOnComputeShaderTypeId,
title: "Missing descriptor for compute pixel shader type",
messageFormat: "The compute shader of type {0} does not have an associated descriptor (it can be autogenerated via the [GeneratedComputeShaderDescriptor] attribute)",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
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);
}