From 43b79bdf4bbe64536aa5b6c716cfd8b45220aa7e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Nov 2023 00:14:37 +0100 Subject: [PATCH] Skip warnings for shader types with no fields --- ...dOnlyPixelShaderTypeWithFieldsAnalyzer.cs} | 6 ++--- ...mputeSharp.SourceGeneration.Hlsl.projitems | 2 +- ...adOnlyShaderTypeWithFieldsAnalyzerBase.cs} | 9 ++++++- ...nlyComputeShaderTypeWithFieldsAnalyzer.cs} | 8 +++--- .../Diagnostics/DiagnosticDescriptors.cs | 2 +- .../Test_Analyzers.cs | 27 +++++++++++++++++-- .../Test_D2DPixelShaderSourceGenerator.cs | 2 +- .../Test_Analyzers.cs | 4 +-- .../Test_D2DPixelShaderSourceGenerator.cs | 2 +- 9 files changed, 46 insertions(+), 16 deletions(-) rename src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/{NotReadonlyPixelShaderTypeAnalyzer.cs => NotReadOnlyPixelShaderTypeWithFieldsAnalyzer.cs} (65%) rename src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/{NotReadonlyShaderTypeAnalyzerBase.cs => NotReadOnlyShaderTypeWithFieldsAnalyzerBase.cs} (87%) rename src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/{NotReadonlyComputeShaderTypeAnalyzer.cs => NotReadOnlyComputeShaderTypeWithFieldsAnalyzer.cs} (57%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadonlyPixelShaderTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyPixelShaderTypeWithFieldsAnalyzer.cs similarity index 65% rename from src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadonlyPixelShaderTypeAnalyzer.cs rename to src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyPixelShaderTypeWithFieldsAnalyzer.cs index cc5f2a0b..b860616a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadonlyPixelShaderTypeAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyPixelShaderTypeWithFieldsAnalyzer.cs @@ -8,12 +8,12 @@ namespace ComputeSharp.D2D1.SourceGenerators; /// [DiagnosticAnalyzer(LanguageNames.CSharp)] -public sealed class NotReadonlyPixelShaderTypeAnalyzer : NotReadonlyShaderTypeAnalyzerBase +public sealed class NotReadOnlyPixelShaderTypeWithFieldsAnalyzer : NotReadOnlyShaderTypeWithFieldsAnalyzerBase { /// - /// Creates a new instance. + /// Creates a new instance. /// - public NotReadonlyPixelShaderTypeAnalyzer() + public NotReadOnlyPixelShaderTypeWithFieldsAnalyzer() : base(NotReadOnlyShaderType, ImmutableArray.Create("ComputeSharp.D2D1.ID2D1PixelShader")) { } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index 975059a8..ed307b28 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -10,7 +10,7 @@ - + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadonlyShaderTypeAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadOnlyShaderTypeWithFieldsAnalyzerBase.cs similarity index 87% rename from src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadonlyShaderTypeAnalyzerBase.cs rename to src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadOnlyShaderTypeWithFieldsAnalyzerBase.cs index fcdafa96..1b70e96b 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadonlyShaderTypeAnalyzerBase.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadOnlyShaderTypeWithFieldsAnalyzerBase.cs @@ -11,7 +11,7 @@ namespace ComputeSharp.SourceGeneration.Diagnostics; /// /// The instance to use. /// The fully qualified metadata names of all candidate shader interface types. -public abstract class NotReadonlyShaderTypeAnalyzerBase( +public abstract class NotReadOnlyShaderTypeWithFieldsAnalyzerBase( DiagnosticDescriptor diagnosticDescriptor, ImmutableArray shaderInterfaceTypeFullyQualifiedMetadataNames) : DiagnosticAnalyzer { @@ -40,6 +40,13 @@ public abstract class NotReadonlyShaderTypeAnalyzerBase( return; } + // In order to trigger the analyzer, we must have at least one field. + // If not (eg. shaders that just return a constant color), no need. + if (!typeSymbol.GetMembers().Any(static m => m is IFieldSymbol { IsStatic: false, IsConst: false, IsFixedSizeBuffer: false })) + { + return; + } + // Emit a diagnostic if the type is a shader type if (IsShaderType(typeSymbol, typeSymbols)) { diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadonlyComputeShaderTypeAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyComputeShaderTypeWithFieldsAnalyzer.cs similarity index 57% rename from src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadonlyComputeShaderTypeAnalyzer.cs rename to src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyComputeShaderTypeWithFieldsAnalyzer.cs index 2e573391..39b7889b 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadonlyComputeShaderTypeAnalyzer.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyComputeShaderTypeWithFieldsAnalyzer.cs @@ -8,13 +8,13 @@ namespace ComputeSharp.SourceGenerators; /// [DiagnosticAnalyzer(LanguageNames.CSharp)] -public sealed class NotReadonlyComputeShaderTypeAnalyzer : NotReadonlyShaderTypeAnalyzerBase +public sealed class NotReadOnlyComputeShaderTypeWithFieldsAnalyzer : NotReadOnlyShaderTypeWithFieldsAnalyzerBase { /// - /// Creates a new instance. + /// Creates a new instance. /// - public NotReadonlyComputeShaderTypeAnalyzer() - : base(NotReadonlyShaderType, ImmutableArray.Create("ComputeSharp.IComputeShader", "ComputeSharp.IComputeShader`1")) + public NotReadOnlyComputeShaderTypeWithFieldsAnalyzer() + : base(NotReadOnlyShaderType, ImmutableArray.Create("ComputeSharp.IComputeShader", "ComputeSharp.IComputeShader`1")) { } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 6831320d..1219c4d1 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -805,7 +805,7 @@ partial class DiagnosticDescriptors /// Format: "The shader of type {0} is not readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone)". /// /// - public static readonly DiagnosticDescriptor NotReadonlyShaderType = new( + public static readonly DiagnosticDescriptor NotReadOnlyShaderType = new( id: "CMPS0057", title: "Not readonly shader type (using IComputeShader or IComputeShader)", messageFormat: "The shader of type {0} is not readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone)", diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs index a8cbf3e3..a3f01820 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs @@ -9,7 +9,7 @@ namespace ComputeSharp.D2D1.Tests.SourceGenerators; public class Test_Analyzers { [TestMethod] - public async Task NotReadonlyShaderType() + public async Task NotReadOnlyShaderType_WithFields_Warns() { const string source = """ using ComputeSharp; @@ -18,6 +18,29 @@ public class Test_Analyzers namespace MyFancyApp.Sample; internal partial struct {|CMPSD2D0070:MyShader|} : ID2D1PixelShader + { + public float number; + + public Float4 Execute() + { + return this.number; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task NotReadOnlyShaderType_WithNoFields_DoesNotWarn() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + namespace MyFancyApp.Sample; + + internal partial struct MyShader : ID2D1PixelShader { public Float4 Execute() { @@ -26,6 +49,6 @@ public class Test_Analyzers } """; - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs index 58214c41..26f08a89 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs @@ -1812,7 +1812,7 @@ public class Test_D2DPixelShaderSourceGenerator await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); // Get all assembly references for the .NET TFM and ComputeSharp IEnumerable metadataReferences = diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs index 38d357f0..113cd657 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs @@ -66,7 +66,7 @@ public class Test_Analyzers } [TestMethod] - public async Task NotReadonlyShaderType() + public async Task NotReadOnlyShaderType() { const string source = """ using ComputeSharp; @@ -83,6 +83,6 @@ public class Test_Analyzers } """; - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs index d6988e4c..86ed4d84 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs @@ -899,7 +899,7 @@ public class Test_D2DPixelShaderSourceGenerator await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); // Get all assembly references for the .NET TFM and ComputeSharp IEnumerable metadataReferences =