Add MissingPixelShaderDescriptorOnPixelShaderAnalyzer

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-01 15:40:18 +02:00
1 親 933dca22ed
コミット 821bc6ba0d
5ファイル変更
+114

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

+1
ファイルの表示
@@ -71,3 +71,4 @@ CMPSD2D0061 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0062 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0063 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0064 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0065 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp)
@@ -0,0 +1,61 @@
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 D2D1 shader type does not have an associated descriptor.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class MissingPixelShaderDescriptorOnPixelShaderAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(MissingPixelShaderDescriptorOnPixelShaderType);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the ID2D1PixelShader, ID2D1PixelShaderDescriptor<T> and [D2DGeneratedPixelShaderDescriptor] symbols
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.ID2D1PixelShader") is not { } d2D1PixelShaderSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor`1") is not { } d2D1PixelShaderDescriptorSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute") is not { } d2DGeneratedPixelShaderDescriptorAttributeSymbol)
{
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 pixel shader type, immediately bail out
if (!typeSymbol.HasInterfaceWithType(d2D1PixelShaderSymbol))
{
return;
}
// Emit a diagnostic if the descriptor is missing for the shader type
if (!typeSymbol.HasInterfaceWithType(d2D1PixelShaderDescriptorSymbol) &&
!typeSymbol.HasAttributeWithType(d2DGeneratedPixelShaderDescriptorAttributeSymbol))
{
context.ReportDiagnostic(Diagnostic.Create(
MissingPixelShaderDescriptorOnPixelShaderType,
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="MissingPixelShaderDescriptorOnPixelShaderType"/>.
/// </summary>
public const string MissingPixelShaderDescriptorOnPixelShaderTypeId = "CMPSD2D0065";
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid shader field.
/// <para>
@@ -954,4 +959,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 pixel shader type doesn't have an associated descriptor.
/// <para>
/// Format: <c>"The D2D1 shader of type {0} does not have an associated descriptor (it can be autogenerated via the [D2DGeneratedPixelShaderDescriptor] attribute)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor MissingPixelShaderDescriptorOnPixelShaderType = new DiagnosticDescriptor(
id: MissingPixelShaderDescriptorOnPixelShaderTypeId,
title: "Missing descriptor for D2D1 pixel shader type",
messageFormat: "The D2D1 shader of type {0} does not have an associated descriptor (it can be autogenerated via the [D2DGeneratedPixelShaderDescriptor] attribute)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "All D2D1 shader types must have an associated descriptor (it can be autogenerated via the [D2DGeneratedPixelShaderDescriptor] attribute).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp",
customTags: WellKnownDiagnosticTags.CompilationEnd);
}
+11
ファイルの表示
@@ -27,6 +27,17 @@ internal static class ISymbolExtensions
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
}
/// <summary>
/// Checks whether or not a given symbol has an attribute with the specified type.
/// </summary>
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
/// <param name="typeSymbol">The <see cref="ITypeSymbol"/> instance for the attribute type to look for.</param>
/// <returns>Whether or not <paramref name="symbol"/> has an attribute with the specified type.</returns>
public static bool HasAttributeWithType(this ISymbol symbol, ITypeSymbol typeSymbol)
{
return TryGetAttributeWithType(symbol, typeSymbol, out _);
}
/// <summary>
/// Tries to get an attribute with the specified type.
/// </summary>
+19
ファイルの表示
@@ -24,6 +24,25 @@ internal static class ITypeSymbolExtensions
return builder.WrittenSpan.SequenceEqual(name.AsSpan());
}
/// <summary>
/// Checks whether or not a given <see cref="ITypeSymbol"/> implements an interface of a specified type.
/// </summary>
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
/// <param name="interfaceSymbol">The <see cref="ITypeSymbol"/> instane to check for inheritance from.</param>
/// <returns>Whether or not <paramref name="typeSymbol"/> has an interface of type <paramref name="interfaceSymbol"/>.</returns>
public static bool HasInterfaceWithType(this ITypeSymbol typeSymbol, ITypeSymbol interfaceSymbol)
{
foreach (INamedTypeSymbol interfaceType in typeSymbol.AllInterfaces)
{
if (SymbolEqualityComparer.Default.Equals(interfaceType, interfaceSymbol))
{
return true;
}
}
return false;
}
/// <summary>
/// Gets the fully qualified metadata name for a given <see cref="ITypeSymbol"/> instance.
/// </summary>