diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index 84f4186f..b26dbb1c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -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 } /// - /// Extracts the HLSL source from a method with the annotation. + /// Extracts the HLSL source from an annotated method. /// - /// The collection of produced instances. /// The input instance to process. /// The HLSL source to compile, if present. - public static string GetHlslSource(ImmutableArrayBuilder 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; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index d0205095..5906a970 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -41,19 +41,21 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato ImmutableArray 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); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceAttributeUseAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceAttributeUseAnalyzer.cs new file mode 100644 index 00000000..7b4464e3 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceAttributeUseAnalyzer.cs @@ -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; + +/// +/// A diagnostic analyzer that generates diagnostics when a method is using [D2DPixelShaderSource] with an invalid argument. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class InvalidD2DPixelShaderSourceAttributeUseAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DPixelShaderSource); + + /// + 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); + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/AttributeDataExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/AttributeDataExtensions.cs index 6450f6bf..c1c13fe8 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/AttributeDataExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/AttributeDataExtensions.cs @@ -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 /// The index of the argument to try to retrieve. /// The resulting argument, if it was found. /// Whether or not an argument of type at position was found. - public static bool TryGetConstructorArgument(this AttributeData attributeData, int index, out T? result) + public static bool TryGetConstructorArgument(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;