Add InvalidD2DPixelShaderSourceAttributeUseAnalyzer
ff fix
このコミットが含まれているのは:
1 親
71b2835d6c
コミット
50c73839bd
4ファイル変更
+74
-19
検索条件に一致するファイルはありません
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
@@ -39,25 +38,19 @@ partial class D2DPixelShaderSourceGenerator
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the HLSL source from a method with the <see cref="D2DPixelShaderSourceAttribute"/> annotation.
|
||||
/// Extracts the HLSL source from an annotated method.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
|
||||
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
|
||||
/// <returns>The HLSL source to compile, if present.</returns>
|
||||
public static string GetHlslSource(ImmutableArrayBuilder<DiagnosticInfo> diagnostics, IMethodSymbol methodSymbol)
|
||||
public static string? GetHlslSource(IMethodSymbol methodSymbol)
|
||||
{
|
||||
_ = methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute", out AttributeData? attributeData);
|
||||
|
||||
if (!attributeData!.TryGetConstructorArgument(0, out string? hlslSource))
|
||||
if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute", out AttributeData? attributeData) &&
|
||||
attributeData!.TryGetConstructorArgument(0, out string? hlslSource))
|
||||
{
|
||||
diagnostics.Add(
|
||||
InvalidD2DPixelShaderSource,
|
||||
methodSymbol,
|
||||
methodSymbol.Name,
|
||||
methodSymbol.ContainingType);
|
||||
return hlslSource;
|
||||
}
|
||||
|
||||
return hlslSource ?? "";
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -41,19 +41,21 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
|
||||
ImmutableArray<ushort> modifiers = methodDeclaration.Modifiers.Select(token => (ushort)token.Kind()).ToImmutableArray();
|
||||
string methodName = methodSymbol.Name;
|
||||
string? invalidReturnType = Execute.GetInvalidReturnType(methodSymbol);
|
||||
string hlslSource = Execute.GetHlslSource(diagnostics, methodSymbol);
|
||||
string? requestedHlslSource = Execute.GetHlslSource(methodSymbol);
|
||||
string effectiveHlslSource = requestedHlslSource ?? "";
|
||||
D2D1CompileOptions compileOptions = Execute.GetCompileOptions(methodSymbol);
|
||||
|
||||
// For the shader profile, reuse the same logic as in D2D1PixelShaderDescriptorGenerator
|
||||
// For the shader profile, reuse the same logic as in D2D1PixelShaderDescriptorGenerator.
|
||||
// The only difference is that in this case, we also skip if the source is null.
|
||||
D2D1ShaderProfile? requestedShaderProfile = Execute.GetShaderProfile(methodSymbol);
|
||||
D2D1ShaderProfile effectiveShaderProfile = requestedShaderProfile ?? D2D1ShaderProfile.PixelShader50;
|
||||
bool isCompilationEnabled = requestedShaderProfile is not null;
|
||||
bool isCompilationEnabled = requestedHlslSource is not null && requestedShaderProfile is not null;
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Prepare the key to cache the bytecode (just like the main D2D1 generator)
|
||||
HlslBytecodeInfoKey hlslInfoKey = new(
|
||||
hlslSource,
|
||||
effectiveHlslSource,
|
||||
effectiveShaderProfile,
|
||||
compileOptions,
|
||||
isCompilationEnabled);
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
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 diagnostics when a method is using [D2DPixelShaderSource] with an invalid argument.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class InvalidD2DPixelShaderSourceAttributeUseAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DPixelShaderSource);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
context.RegisterCompilationStartAction(static context =>
|
||||
{
|
||||
// Get the [D2DPixelShaderSource] symbol
|
||||
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute") is not { } d2DPixelShaderSourceAttributeSymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
context.RegisterSymbolAction(context =>
|
||||
{
|
||||
// Only partial definition methods are possible targets
|
||||
if (context.Symbol is not IMethodSymbol { IsPartialDefinition: true } methodSymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore methods without [D2DPixelShaderSource]
|
||||
if (!methodSymbol.TryGetAttributeWithType(d2DPixelShaderSourceAttributeSymbol, out AttributeData? attributeData))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Emit a diagnostic if there is no valid HLSL source argument
|
||||
if (!attributeData.TryGetConstructorArgument(0, out string? hlslSource))
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(
|
||||
InvalidD2DPixelShaderSource,
|
||||
methodSymbol.Locations.First(),
|
||||
methodSymbol.Name,
|
||||
methodSymbol.ContainingType));
|
||||
}
|
||||
}, SymbolKind.Method);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Extensions;
|
||||
@@ -31,9 +32,9 @@ internal static class AttributeDataExtensions
|
||||
/// <param name="index">The index of the argument to try to retrieve.</param>
|
||||
/// <param name="result">The resulting argument, if it was found.</param>
|
||||
/// <returns>Whether or not an argument of type <typeparamref name="T"/> at position <paramref name="index"/> was found.</returns>
|
||||
public static bool TryGetConstructorArgument<T>(this AttributeData attributeData, int index, out T? result)
|
||||
public static bool TryGetConstructorArgument<T>(this AttributeData attributeData, int index, [NotNullWhen(true)] out T? result)
|
||||
{
|
||||
if (attributeData.ConstructorArguments.Length >= index + 1 &&
|
||||
if (attributeData.ConstructorArguments.Length > index &&
|
||||
attributeData.ConstructorArguments[index].Value is T argument)
|
||||
{
|
||||
result = argument;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする