diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
index 33e24b45..32262bc6 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -83,3 +83,5 @@ CMPSD2D0073 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0074 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0075 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0076 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPSD2D0077 | ComputeSharp.D2D1.Shaders | Info | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPSD2D0078 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp)
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DRuntimeCompilationDisabledAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DRuntimeCompilationDisabledAnalyzer.cs
index 3041935b..f1d29829 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DRuntimeCompilationDisabledAnalyzer.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DRuntimeCompilationDisabledAnalyzer.cs
@@ -14,7 +14,10 @@ namespace ComputeSharp.D2D1.SourceGenerators;
public sealed class D2DRuntimeCompilationDisabledAnalyzer : DiagnosticAnalyzer
{
///
- public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(D2DRuntimeCompilationDisabled);
+ public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(
+ D2DRuntimeCompilationDisabled,
+ D2DRuntimeCompilationAlreadyEnabled,
+ D2DRuntimeCompilationNotNecessary);
///
public override void Initialize(AnalysisContext context)
@@ -46,17 +49,43 @@ public sealed class D2DRuntimeCompilationDisabledAnalyzer : DiagnosticAnalyzer
return;
}
- // If the shader is being precompiled, we don't have anything else to do
- if (typeSymbol.HasAttributeWithType(d2DShaderProfileAttributeSymbol) ||
- typeSymbol.ContainingAssembly.HasAttributeWithType(d2DShaderProfileAttributeSymbol))
+ // This analyzer will detect the following cases:
+ // 1) Shader not precompiled, and missing [D2DEnableRuntimeCompilation] (D2DRuntimeCompilationDisabled)
+ // 2) Shader with [D2DEnableRuntimeCompilation] on the assembly and also on the shader type (D2DRuntimeCompilationAlreadyEnabled)
+ // 3) Shader being precompiled, but also with [D2DEnableRuntimeCompilation] (D2DRuntimeCompilationNotNecessary)
+ bool hasD2DShaderProfileAttributeOnAssembly = typeSymbol.ContainingAssembly.HasAttributeWithType(d2DShaderProfileAttributeSymbol);
+ bool hasD2DShaderProfileAttributeOnType = typeSymbol.HasAttributeWithType(d2DShaderProfileAttributeSymbol);
+ bool hasD2DEnableRuntimeCompilationAttributeOnAssembly = typeSymbol.ContainingAssembly.HasAttributeWithType(d2DEnableRuntimeCompilationAttributeSymbol);
+ bool hasD2DEnableRuntimeCompilationAttributeOnType = typeSymbol.TryGetAttributeWithType(
+ d2DEnableRuntimeCompilationAttributeSymbol,
+ out AttributeData? d2DEnableRuntimeCompilationAttributeData);
+
+ // If [D2DEnableRuntimeCompilation] is present on the type and also on the assembly, emit a diagnostic (2)
+ if (hasD2DEnableRuntimeCompilationAttributeOnAssembly && hasD2DEnableRuntimeCompilationAttributeOnType)
{
- return;
+ context.ReportDiagnostic(Diagnostic.Create(
+ D2DRuntimeCompilationAlreadyEnabled,
+ d2DEnableRuntimeCompilationAttributeData!.GetLocation(),
+ typeSymbol));
}
- // Emit a diagnostic if there is no [D2DEnableRuntimeCompilation] set anywhere
- if (!typeSymbol.HasAttributeWithType(d2DEnableRuntimeCompilationAttributeSymbol) &&
- !typeSymbol.ContainingAssembly.HasAttributeWithType(d2DEnableRuntimeCompilationAttributeSymbol))
+ // Check if the shader is precompiled:
+ // - If it is, we should check that it's not annotated with [D2DEnableRuntimeCompilation]
+ // - If it is not, we should check that it is annotated with [D2DEnableRuntimeCompilation]
+ if (hasD2DShaderProfileAttributeOnType || hasD2DShaderProfileAttributeOnAssembly)
{
+ // Emit a diagnostic if [D2DEnableRuntimeCompilation] is present on the type (3)
+ if (hasD2DEnableRuntimeCompilationAttributeOnType)
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ D2DRuntimeCompilationNotNecessary,
+ d2DEnableRuntimeCompilationAttributeData!.GetLocation(),
+ typeSymbol));
+ }
+ }
+ else if (!hasD2DEnableRuntimeCompilationAttributeOnType && !hasD2DEnableRuntimeCompilationAttributeOnAssembly)
+ {
+ // Emit a diagnostic if there is no [D2DEnableRuntimeCompilation] set anywhere (1)
context.ReportDiagnostic(Diagnostic.Create(
D2DRuntimeCompilationDisabled,
typeSymbol.Locations.FirstOrDefault(),
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index ec267d8d..6002b367 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -1127,7 +1127,7 @@ partial class DiagnosticDescriptors
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
///
- /// Gets a for an invocation of a Math or MathF API.
+ /// Gets a for a shader not precompiled but with runtime compilation disabled.
///
/// Format: "The shader {0} is not precompiled, but runtime compilation is not enabled (either precompile the shader by using [D2DShaderProfile], which is recommended, or enable runtime compilation using [D2DEnableRuntimeCompilation])".
///
@@ -1141,4 +1141,36 @@ partial class DiagnosticDescriptors
isEnabledByDefault: true,
description: "Shaders must either be precompiled using [D2DShaderProfile], which is recommended, or runtime compilation for shaders must be enabled using [D2DEnableRuntimeCompilation].",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for an unnecessary [D2DEnableRuntimeCompilation] use.
+ ///
+ /// Format: "The shader {0} is annotated with [D2DEnableRuntimeCompilation], but its containing assembly is already annotated with this attribute (so using it again on the shader type is unnecessary)".
+ ///
+ ///
+ public static readonly DiagnosticDescriptor D2DRuntimeCompilationAlreadyEnabled = new(
+ id: "CMPSD2D0077",
+ title: "D2D runtime compilation already enabled",
+ messageFormat: "The shader {0} is annotated with [D2DEnableRuntimeCompilation], but its containing assembly is already annotated with this attribute (so using it again on the shader type is unnecessary)",
+ category: "ComputeSharp.D2D1.Shaders",
+ defaultSeverity: DiagnosticSeverity.Info,
+ isEnabledByDefault: true,
+ description: "If an assembly is annotated with [D2DEnableRuntimeCompilation], that applies to all shader types within it. That means that using [D2DEnableRuntimeCompilation] on shader types too is unnecessary.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for an unnecessary [D2DEnableRuntimeCompilation] use.
+ ///
+ /// Format: "The shader {0} is annotated with [D2DEnableRuntimeCompilation], but it is also being precompiled, as it has [D2DShaderProfile] on the type or containing assembly (so using [D2DEnableRuntimeCompilation] is unnecessary)".
+ ///
+ ///
+ public static readonly DiagnosticDescriptor D2DRuntimeCompilationNotNecessary = new(
+ id: "CMPSD2D0078",
+ title: "D2D runtime compilation is not necessary",
+ messageFormat: "The shader {0} is annotated with [D2DEnableRuntimeCompilation], but it is also being precompiled, as it has [D2DShaderProfile] on the type or containing assembly (so using [D2DEnableRuntimeCompilation] is unnecessary)",
+ category: "ComputeSharp.D2D1.Shaders",
+ defaultSeverity: DiagnosticSeverity.Warning,
+ isEnabledByDefault: true,
+ description: "If a shader is precompiled (ie. it has [D2DShaderProfile] on the type declaration or on its containing assembly), also using [D2DEnableRuntimeCompilation] is unnecessary.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
\ No newline at end of file
diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs
index 3ba72361..d322b56c 100644
--- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs
+++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs
@@ -262,4 +262,75 @@ public class Test_Analyzers
await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
}
+
+ [TestMethod]
+ public async Task ShaderWithNoPrecompiledBytecode_WithUnnecessaryD2DEnableRuntimeCompilation_Warns()
+ {
+ const string source = """
+ using ComputeSharp;
+ using ComputeSharp.D2D1;
+
+ [assembly: D2DEnableRuntimeCompilation]
+
+ [D2DInputCount(0)]
+ [{|CMPSD2D0077:D2DEnableRuntimeCompilation|}]
+ [D2DGeneratedPixelShaderDescriptor]
+ internal partial struct MyType : ID2D1PixelShader
+ {
+ public Float4 Execute()
+ {
+ return 0;
+ }
+ }
+ """;
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task ShaderWithPrecompiledBytecode_FromAssembly_WithUnnecessaryD2DEnableRuntimeCompilation_Warns()
+ {
+ const string source = """
+ using ComputeSharp;
+ using ComputeSharp.D2D1;
+
+ [assembly: D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
+
+ [D2DInputCount(0)]
+ [{|CMPSD2D0078:D2DEnableRuntimeCompilation|}]
+ [D2DGeneratedPixelShaderDescriptor]
+ internal partial struct MyType : ID2D1PixelShader
+ {
+ public Float4 Execute()
+ {
+ return 0;
+ }
+ }
+ """;
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task ShaderWithPrecompiledBytecode_FromType_WithUnnecessaryD2DEnableRuntimeCompilation_Warns()
+ {
+ const string source = """
+ using ComputeSharp;
+ using ComputeSharp.D2D1;
+
+ [D2DInputCount(0)]
+ [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
+ [{|CMPSD2D0078:D2DEnableRuntimeCompilation|}]
+ [D2DGeneratedPixelShaderDescriptor]
+ internal partial struct MyType : ID2D1PixelShader
+ {
+ public Float4 Execute()
+ {
+ return 0;
+ }
+ }
+ """;
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
}
\ No newline at end of file