diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 0dfa9366..df77bc64 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -76,3 +76,4 @@ CMPSD2D0066 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0067 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0068 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0069 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0070 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadonlyPixelShaderTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadonlyPixelShaderTypeAnalyzer.cs new file mode 100644 index 00000000..cc5f2a0b --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadonlyPixelShaderTypeAnalyzer.cs @@ -0,0 +1,20 @@ +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotReadonlyPixelShaderTypeAnalyzer : NotReadonlyShaderTypeAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotReadonlyPixelShaderTypeAnalyzer() + : base(NotReadOnlyShaderType, ImmutableArray.Create("ComputeSharp.D2D1.ID2D1PixelShader")) + { + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index b18c63ae..1cf84379 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1038,4 +1038,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "A D2D1 shader cannot use D2D1CompileOptions.EnableLinking in its [D2DCompileOptions] attribute if it doesn't support linking (only D2D1 shaders with no complex inputs can use this option).", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when a shader type with any fields is not readonly. + /// + /// 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( + id: "CMPSD2D0070", + title: "Not readonly shader type (using ID2D1PixelShader)", + 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)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true, + description: "Shader types should be readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index d89faabf..2be10a4e 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -62,3 +62,4 @@ CMPS0053 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/S CMPS0054 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0055 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0056 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0057 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadonlyComputeShaderTypeAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadonlyComputeShaderTypeAnalyzer.cs new file mode 100644 index 00000000..2e573391 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadonlyComputeShaderTypeAnalyzer.cs @@ -0,0 +1,20 @@ +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotReadonlyComputeShaderTypeAnalyzer : NotReadonlyShaderTypeAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotReadonlyComputeShaderTypeAnalyzer() + : 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 158b37e3..6831320d 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -798,4 +798,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [GeneratedComputeShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when a shader type with any fields is not readonly. + /// + /// 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( + 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)", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true, + description: "Shader types should be readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file