From d90cd39d66eb27a11efeb4d5f5c24d714787c733 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Jan 2024 22:48:24 +0100 Subject: [PATCH 01/10] Add InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer --- .../D2DPixelShaderSourceGenerator.Execute.cs | 12 +--- .../D2DPixelShaderSourceGenerator.cs | 2 +- ...xelShaderSourceMethodReturnTypeAnalyzer.cs | 58 +++++++++++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index 0de6514d..973c044b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -20,10 +20,9 @@ partial class D2DPixelShaderSourceGenerator /// /// Validates that the return type of the annotated method is valid and returns the type name. /// - /// The collection of produced instances. /// The input instance to process. - /// The HLSL source to compile, if present. - public static string? GetInvalidReturnType(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) + /// The fully qualified type name, if invalid. + public static string? GetInvalidReturnType(IMethodSymbol methodSymbol) { if (methodSymbol.ReturnType is not INamedTypeSymbol { @@ -33,13 +32,6 @@ partial class D2DPixelShaderSourceGenerator TypeArguments: [{ SpecialType: SpecialType.System_Byte }] }) { - diagnostics.Add( - InvalidD2DPixelShaderSourceMethodReturnType, - methodSymbol, - methodSymbol.Name, - methodSymbol.ContainingType, - methodSymbol.ReturnType); - return methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index c26dfc4a..57082fdd 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -40,7 +40,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato // Get the remaining info for the current shader ImmutableArray modifiers = methodDeclaration.Modifiers.Select(token => (ushort)token.Kind()).ToImmutableArray(); string methodName = methodSymbol.Name; - string? invalidReturnType = Execute.GetInvalidReturnType(diagnostics, methodSymbol); + string? invalidReturnType = Execute.GetInvalidReturnType(methodSymbol); string hlslSource = Execute.GetHlslSource(diagnostics, methodSymbol); D2D1ShaderProfile shaderProfile = Execute.GetShaderProfile(diagnostics, methodSymbol); D2D1CompileOptions compileOptions = Execute.GetCompileOptions(diagnostics, methodSymbol); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs new file mode 100644 index 00000000..f14c2949 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs @@ -0,0 +1,58 @@ +using System.Collections.Immutable; +using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +/// A diagnostic analyzer that generates diagnostics when [D2DPixelShaderSource] is used on a method with an incorrect return type. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DPixelShaderSourceMethodReturnType); + + /// + 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; + } + + // Also get the constructed ReadOnlySpan type + INamedTypeSymbol readOnlySpanSymbol = context.Compilation.GetTypeByMetadataName("System.ReadOnlySpan`1")!; + INamedTypeSymbol byteSymbol = context.Compilation.GetSpecialType(SpecialType.System_Byte); + INamedTypeSymbol readOnlySpanOfByteSymbol = readOnlySpanSymbol.Construct(byteSymbol); + + context.RegisterSymbolAction(context => + { + // Only partial definition methods are possible targets + if (context.Symbol is not IMethodSymbol { IsPartialDefinition: true } methodSymbol) + { + return; + } + + // Emit a diagnostic if the return type is not ReadOnlySpan + if (!SymbolEqualityComparer.Default.Equals(methodSymbol.ReturnType, readOnlySpanOfByteSymbol)) + { + context.ReportDiagnostic(Diagnostic.Create( + InvalidD2DPixelShaderSourceMethodReturnType, + methodSymbol.Locations.First(), + methodSymbol.Name, + methodSymbol.ContainingType, + methodSymbol.ReturnType)); + } + }, SymbolKind.Method); + }); + } +} \ No newline at end of file From 0b9f9b304ec034b97e39d1c4329849a1c0d17b07 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Jan 2024 23:01:14 +0100 Subject: [PATCH 02/10] Add InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer tests --- ...xelShaderDescriptorGenerator_Analyzers.cs} | 2 +- ...D2DPixelShaderSourceGenerator_Analyzers.cs | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) rename tests/ComputeSharp.D2D1.Tests.SourceGenerators/{Test_Analyzers.cs => Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs} (99%) create mode 100644 tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs similarity index 99% rename from tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs rename to tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs index 548bf50f..c834aab6 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs @@ -6,7 +6,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.D2D1.Tests.SourceGenerators; [TestClass] -public class Test_Analyzers +public class Test_D2DPixelShaderDescriptorGenerator_Analyzers { [TestMethod] public async Task MissingD2DPixelShaderDescriptor_ComputeShader() diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs new file mode 100644 index 00000000..12450fcc --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs @@ -0,0 +1,74 @@ +using System.Threading.Tasks; +using ComputeSharp.D2D1.SourceGenerators; +using ComputeSharp.Tests.SourceGenerators.Helpers; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.D2D1.Tests.SourceGenerators; + +[TestClass] +public class Test_D2DPixelShaderSourceGenerator_Analyzers +{ + [TestMethod] + public async Task InvalidD2DPixelShaderSourceMethodReturnType_ReadOnlySpanOfByte_DoesNotWarn() + { + const string source = """" + using System; + using ComputeSharp.D2D1; + + public partial class MyClass + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + return 0; + } + """)] + public static partial ReadOnlySpan InvertEffect(); + + public static partial ReadOnlySpan InvertEffect() => default; + } + """"; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + [DataRow("TimeSpan")] + [DataRow("ReadOnlyMemory")] + [DataRow("ReadOnlySpan")] + [DataRow("string")] + [DataRow("object")] + [DataRow("byte")] + [DataRow("dynamic")] + [DataRow("byte*")] + public async Task InvalidD2DPixelShaderSourceMethodReturnType_InvalidType_Warns(string returnType) + { + string source = $$"""" + using System; + using ComputeSharp.D2D1; + + public unsafe partial class MyClass + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + return 0; + } + """)] + public static partial {{returnType}} {|CMPSD2D0057:InvertEffect|}(); + + public static partial {{returnType}} InvertEffect() => default; + } + """"; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } +} \ No newline at end of file From 29d0ccca5cebb3f80a49827ed0022842951bd084 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Jan 2024 23:20:55 +0100 Subject: [PATCH 03/10] Add MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer --- .../D2DPixelShaderSourceGenerator.Execute.cs | 11 +--- .../D2DPixelShaderSourceGenerator.cs | 9 ++- ...ileOnD2DPixelShaderSourceMethodAnalyzer.cs | 55 +++++++++++++++++++ 3 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index 973c044b..7c6f5154 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -63,10 +63,9 @@ partial class D2DPixelShaderSourceGenerator /// /// Extracts the shader profile for a target method, if present. /// - /// The collection of produced instances. /// The input instance to process. /// The shader profile to use to compile the shader, if present. - public static D2D1ShaderProfile GetShaderProfile(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) + public static D2D1ShaderProfile? GetShaderProfile(IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out AttributeData? attributeData)) { @@ -78,13 +77,7 @@ partial class D2DPixelShaderSourceGenerator return (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!; } - diagnostics.Add( - MissingShaderProfileForD2DPixelShaderSource, - methodSymbol, - methodSymbol.Name, - methodSymbol.ContainingType); - - return D2D1ShaderProfile.PixelShader50; + return null; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index 57082fdd..78e3eef2 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -42,16 +42,19 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato string methodName = methodSymbol.Name; string? invalidReturnType = Execute.GetInvalidReturnType(methodSymbol); string hlslSource = Execute.GetHlslSource(diagnostics, methodSymbol); - D2D1ShaderProfile shaderProfile = Execute.GetShaderProfile(diagnostics, methodSymbol); D2D1CompileOptions compileOptions = Execute.GetCompileOptions(diagnostics, methodSymbol); - bool isCompilationEnabled = diagnostics.Count == 0; + + // For the shader profile, reuse the same logic as in D2D1PixelShaderDescriptorGenerator + D2D1ShaderProfile? requestedShaderProfile = Execute.GetShaderProfile(methodSymbol); + D2D1ShaderProfile effectiveShaderProfile = requestedShaderProfile ?? D2D1ShaderProfile.PixelShader50; + bool isCompilationEnabled = requestedShaderProfile is not null; token.ThrowIfCancellationRequested(); // Prepare the key to cache the bytecode (just like the main D2D1 generator) HlslBytecodeInfoKey hlslInfoKey = new( hlslSource, - shaderProfile, + effectiveShaderProfile, compileOptions, isCompilationEnabled); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs new file mode 100644 index 00000000..411c7afc --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs @@ -0,0 +1,55 @@ +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 with [D2DPixelShaderSource] isn't using [D2DShaderProfile]. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MissingShaderProfileForD2DPixelShaderSource); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // Get the [D2DPixelShaderSource] and [D2DShaderProfile] symbols + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute") is not { } d2DPixelShaderSourceAttributeSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute") is not { } d2DShaderProfileAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only partial definition methods are possible targets + if (context.Symbol is not IMethodSymbol { IsPartialDefinition: true } methodSymbol) + { + return; + } + + // Emit a diagnostic if there is no shader profile available at any level + if (!methodSymbol.HasAttributeWithType(d2DShaderProfileAttributeSymbol) && + !methodSymbol.ContainingAssembly.HasAttributeWithType(d2DShaderProfileAttributeSymbol)) + { + context.ReportDiagnostic(Diagnostic.Create( + MissingShaderProfileForD2DPixelShaderSource, + methodSymbol.Locations.First(), + methodSymbol.Name, + methodSymbol.ContainingType)); + } + }, SymbolKind.Method); + }); + } +} \ No newline at end of file From 5cdb71684c92cb8fb50c03031f73c1d8820948ed Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Jan 2024 23:26:20 +0100 Subject: [PATCH 04/10] Add MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer tests --- .../D2DPixelShaderSourceAttribute.cs | 2 +- ...D2DPixelShaderSourceGenerator_Analyzers.cs | 87 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.D2D1/Attributes/D2DPixelShaderSourceAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DPixelShaderSourceAttribute.cs index da780e56..f1887143 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DPixelShaderSourceAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DPixelShaderSourceAttribute.cs @@ -21,7 +21,7 @@ namespace ComputeSharp.D2D1; /// float3 rgb = saturate(1.0 - color.rgb); /// return float4(rgb, 1); /// } -/// """")] +/// """)] /// static partial ReadOnlySpan<byte> GetBytecode(); /// /// diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs index 12450fcc..5b4d26bf 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs @@ -71,4 +71,91 @@ public class Test_D2DPixelShaderSourceGenerator_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task MissingShaderProfileForD2DPixelShaderSource_ShaderProfileOnMethod_DoesNotWarn() + { + const string source = """" + using System; + using ComputeSharp.D2D1; + + public partial class MyClass + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + return 0; + } + """)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + public static partial ReadOnlySpan InvertEffect(); + + public static partial ReadOnlySpan InvertEffect() => default; + } + """"; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task MissingShaderProfileForD2DPixelShaderSource_ShaderProfileOnAssembly_DoesNotWarn() + { + const string source = """" + using System; + using ComputeSharp.D2D1; + + [assembly: D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + + public partial class MyClass + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + return 0; + } + """)] + public static partial ReadOnlySpan InvertEffect(); + + public static partial ReadOnlySpan InvertEffect() => default; + } + """"; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task MissingShaderProfileForD2DPixelShaderSource_MissingShaderProfile_Warns() + { + const string source = """" + using System; + using ComputeSharp.D2D1; + + public partial class MyClass + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + return 0; + } + """)] + public static partial ReadOnlySpan {|CMPSD2D0055:InvertEffect|}(); + + public static partial ReadOnlySpan InvertEffect() => default; + } + """"; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file From 8c061c5b9e399df916d40190f61649d3242eeac8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Jan 2024 23:28:27 +0100 Subject: [PATCH 05/10] Add MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer --- .../D2DPixelShaderSourceGenerator.Execute.cs | 9 +-- .../D2DPixelShaderSourceGenerator.cs | 2 +- ...onsOnD2DPixelShaderSourceMethodAnalyzer.cs | 55 +++++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index 7c6f5154..84f4186f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -83,10 +83,9 @@ partial class D2DPixelShaderSourceGenerator /// /// Extracts the compile options for the current shader. /// - /// The collection of produced instances. /// The input instance to process. /// The compile options to use to compile the shader, if present. - public static D2D1CompileOptions GetCompileOptions(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) + public static D2D1CompileOptions GetCompileOptions(IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { @@ -98,12 +97,6 @@ partial class D2DPixelShaderSourceGenerator return (D2D1CompileOptions)attributeData.ConstructorArguments[0].Value!; } - diagnostics.Add( - MissingCompileOptionsForD2DPixelShaderSource, - methodSymbol, - methodSymbol.Name, - methodSymbol.ContainingType); - return D2D1CompileOptions.Default; } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index 78e3eef2..d0205095 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -42,7 +42,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato string methodName = methodSymbol.Name; string? invalidReturnType = Execute.GetInvalidReturnType(methodSymbol); string hlslSource = Execute.GetHlslSource(diagnostics, methodSymbol); - D2D1CompileOptions compileOptions = Execute.GetCompileOptions(diagnostics, methodSymbol); + D2D1CompileOptions compileOptions = Execute.GetCompileOptions(methodSymbol); // For the shader profile, reuse the same logic as in D2D1PixelShaderDescriptorGenerator D2D1ShaderProfile? requestedShaderProfile = Execute.GetShaderProfile(methodSymbol); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs new file mode 100644 index 00000000..a6f588d9 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs @@ -0,0 +1,55 @@ +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 with [D2DPixelShaderSource] isn't using [D2DCompileOptions]. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MissingCompileOptionsForD2DPixelShaderSource); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // Get the [D2DPixelShaderSource] and [D2DShaderProfile] symbols + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute") is not { } d2DPixelShaderSourceAttributeSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute") is not { } d2DCompileOptionsAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only partial definition methods are possible targets + if (context.Symbol is not IMethodSymbol { IsPartialDefinition: true } methodSymbol) + { + return; + } + + // Emit a diagnostic if there is no compile options available at any level + if (!methodSymbol.HasAttributeWithType(d2DCompileOptionsAttributeSymbol) && + !methodSymbol.ContainingAssembly.HasAttributeWithType(d2DCompileOptionsAttributeSymbol)) + { + context.ReportDiagnostic(Diagnostic.Create( + MissingCompileOptionsForD2DPixelShaderSource, + methodSymbol.Locations.First(), + methodSymbol.Name, + methodSymbol.ContainingType)); + } + }, SymbolKind.Method); + }); + } +} \ No newline at end of file From 71b2835d6c388610cb8a2a7050194ff1fa4ad4c8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Jan 2024 23:30:16 +0100 Subject: [PATCH 06/10] Add MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer tests --- ...D2DPixelShaderSourceGenerator_Analyzers.cs | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs index 5b4d26bf..a4fad771 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs @@ -158,4 +158,91 @@ public class Test_D2DPixelShaderSourceGenerator_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task MissingCompileOptionsForD2DPixelShaderSource_CompileOptionsOnMethod_DoesNotWarn() + { + const string source = """" + using System; + using ComputeSharp.D2D1; + + public partial class MyClass + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + return 0; + } + """)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)] + public static partial ReadOnlySpan InvertEffect(); + + public static partial ReadOnlySpan InvertEffect() => default; + } + """"; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task MissingCompileOptionsForD2DPixelShaderSource_CompileOptionsOnAssembly_DoesNotWarn() + { + const string source = """" + using System; + using ComputeSharp.D2D1; + + [assembly: D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)] + + public partial class MyClass + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + return 0; + } + """)] + public static partial ReadOnlySpan InvertEffect(); + + public static partial ReadOnlySpan InvertEffect() => default; + } + """"; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task MissingCompileOptionsForD2DPixelShaderSource_MissingCompileOptions_Warns() + { + const string source = """" + using System; + using ComputeSharp.D2D1; + + public partial class MyClass + { + [D2DPixelShaderSource(""" + #define D2D_INPUT_COUNT 0 + + #include "d2d1effecthelpers.hlsli" + + D2D_PS_ENTRY(Execute) + { + return 0; + } + """)] + public static partial ReadOnlySpan {|CMPSD2D0056:InvertEffect|}(); + + public static partial ReadOnlySpan InvertEffect() => default; + } + """"; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file From 50c73839bd1a54cd70989ecb79fefab50d6b1a65 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Jan 2024 23:38:48 +0100 Subject: [PATCH 07/10] Add InvalidD2DPixelShaderSourceAttributeUseAnalyzer ff fix --- .../D2DPixelShaderSourceGenerator.Execute.cs | 19 ++---- .../D2DPixelShaderSourceGenerator.cs | 10 ++-- ...2DPixelShaderSourceAttributeUseAnalyzer.cs | 59 +++++++++++++++++++ .../Extensions/AttributeDataExtensions.cs | 5 +- 4 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceAttributeUseAnalyzer.cs 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; From 37b820b6700f3d94bbef8b5525bc51bb350e04f1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Jan 2024 23:41:13 +0100 Subject: [PATCH 08/10] Add InvalidD2DPixelShaderSourceAttributeUseAnalyzer test --- ...D2DPixelShaderSourceGenerator_Analyzers.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs index a4fad771..840b9713 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs @@ -245,4 +245,23 @@ public class Test_D2DPixelShaderSourceGenerator_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task InvalidD2DPixelShaderSource_NullArgument_Warns() + { + const string source = """ + using System; + using ComputeSharp.D2D1; + + public partial class MyClass + { + [D2DPixelShaderSource(null)] + public static partial ReadOnlySpan {|CMPSD2D0052:InvertEffect|}(); + + public static partial ReadOnlySpan InvertEffect() => default; + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file From 40874d5d16fea49214892ddba7e0e9829c7ccd46 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Jan 2024 23:45:03 +0100 Subject: [PATCH 09/10] Further optimize diagnostics in D2DPixelShaderSourceGenerator --- .../D2DPixelShaderSourceGenerator.Execute.cs | 23 ++++++++----------- .../D2DPixelShaderSourceGenerator.cs | 8 +++---- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index b26dbb1c..daf3fcea 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -1,3 +1,4 @@ +using System.Collections.Immutable; using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; @@ -98,38 +99,34 @@ partial class D2DPixelShaderSourceGenerator /// /// The input instance to process. /// The source instance. - /// The collection of produced instances. - public static void GetInfoDiagnostics( - IMethodSymbol methodSymbol, - HlslBytecodeInfo info, - ImmutableArrayBuilder diagnostics) + /// The collection of produced instances. + public static ImmutableArray GetInfoDiagnostics(IMethodSymbol methodSymbol, HlslBytecodeInfo info) { - DiagnosticInfo? diagnostic = null; - if (info is HlslBytecodeInfo.Win32Error win32Error) { - diagnostic = DiagnosticInfo.Create( + DiagnosticInfo diagnostic = DiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithWin32Exception, methodSymbol, methodSymbol, methodSymbol.ContainingType, win32Error.HResult, win32Error.Message); + + return ImmutableArray.Create(diagnostic); } else if (info is HlslBytecodeInfo.CompilerError fxcError) { - diagnostic = DiagnosticInfo.Create( + DiagnosticInfo diagnostic = DiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithFxcCompilationException, methodSymbol, methodSymbol, methodSymbol.ContainingType, fxcError.Message); + + return ImmutableArray.Create(diagnostic); } - if (diagnostic is not null) - { - diagnostics.Add(diagnostic); - } + return ImmutableArray.Empty; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index 5906a970..0573a8fd 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -35,8 +35,6 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)context.TargetNode; IMethodSymbol methodSymbol = (IMethodSymbol)context.TargetSymbol; - using ImmutableArrayBuilder diagnostics = new(); - // Get the remaining info for the current shader ImmutableArray modifiers = methodDeclaration.Modifiers.Select(token => (ushort)token.Kind()).ToImmutableArray(); string methodName = methodSymbol.Name; @@ -65,8 +63,8 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato token.ThrowIfCancellationRequested(); - // Append any diagnostic for the shader compilation - Execute.GetInfoDiagnostics(methodSymbol, hlslInfo, diagnostics); + // Get any diagnostics for the shader compilation + ImmutableArray diagnostics = Execute.GetInfoDiagnostics(methodSymbol, hlslInfo); token.ThrowIfCancellationRequested(); @@ -82,7 +80,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato InvalidReturnType: invalidReturnType, HlslInfoKey: hlslInfoKey, HlslInfo: hlslInfo, - Diagnostcs: diagnostics.ToImmutable()); + Diagnostcs: diagnostics); }) .WithTrackingName(WellKnownTrackingNames.Execute); From 09b22885a933de228596c4857b0deef9df24ff80 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 7 Jan 2024 00:44:08 +0100 Subject: [PATCH 10/10] Fix analyzers triggering on unrelated partial methods --- .../InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs | 7 +++++++ ...2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs | 6 ++++++ ...D2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs index f14c2949..e30cd119 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs @@ -1,5 +1,6 @@ using System.Collections.Immutable; using System.Linq; +using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; @@ -42,6 +43,12 @@ public sealed class InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer : Diagno return; } + // Ignore methods without [D2DPixelShaderSource] + if (!methodSymbol.HasAttributeWithType(d2DPixelShaderSourceAttributeSymbol)) + { + return; + } + // Emit a diagnostic if the return type is not ReadOnlySpan if (!SymbolEqualityComparer.Default.Equals(methodSymbol.ReturnType, readOnlySpanOfByteSymbol)) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs index a6f588d9..654ed07d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs @@ -39,6 +39,12 @@ public sealed class MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer return; } + // Ignore methods without [D2DPixelShaderSource] + if (!methodSymbol.HasAttributeWithType(d2DPixelShaderSourceAttributeSymbol)) + { + return; + } + // Emit a diagnostic if there is no compile options available at any level if (!methodSymbol.HasAttributeWithType(d2DCompileOptionsAttributeSymbol) && !methodSymbol.ContainingAssembly.HasAttributeWithType(d2DCompileOptionsAttributeSymbol)) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs index 411c7afc..6eb10b60 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs @@ -39,6 +39,12 @@ public sealed class MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer return; } + // Ignore methods without [D2DPixelShaderSource] + if (!methodSymbol.HasAttributeWithType(d2DPixelShaderSourceAttributeSymbol)) + { + return; + } + // Emit a diagnostic if there is no shader profile available at any level if (!methodSymbol.HasAttributeWithType(d2DShaderProfileAttributeSymbol) && !methodSymbol.ContainingAssembly.HasAttributeWithType(d2DShaderProfileAttributeSymbol))