Add more diagnostics for [D2DEnableRuntimeCompilation]

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-16 13:19:55 +01:00
1 親 2d03612a1d
コミット 3722eb3026
4ファイル変更
+143 -9

検索条件に一致するファイルはありません

+2
ファイルの表示
@@ -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)
@@ -14,7 +14,10 @@ namespace ComputeSharp.D2D1.SourceGenerators;
public sealed class D2DRuntimeCompilationDisabledAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(D2DRuntimeCompilationDisabled);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
D2DRuntimeCompilationDisabled,
D2DRuntimeCompilationAlreadyEnabled,
D2DRuntimeCompilationNotNecessary);
/// <inheritdoc/>
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(),
+33 -1
ファイルの表示
@@ -1127,7 +1127,7 @@ partial class DiagnosticDescriptors
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invocation of a <c>Math</c> or <c>MathF</c> API.
/// Gets a <see cref="DiagnosticDescriptor"/> for a shader not precompiled but with runtime compilation disabled.
/// <para>
/// Format: <c>"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])"</c>.
/// </para>
@@ -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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an unnecessary [D2DEnableRuntimeCompilation] use.
/// <para>
/// Format: <c>"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)"</c>.
/// </para>
/// </summary>
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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an unnecessary [D2DEnableRuntimeCompilation] use.
/// <para>
/// Format: <c>"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)"</c>.
/// </para>
/// </summary>
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");
}
+71
ファイルの表示
@@ -262,4 +262,75 @@ public class Test_Analyzers
await CSharpAnalyzerWithLanguageVersionTest<D2DRuntimeCompilationDisabledAnalyzer>.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<D2DRuntimeCompilationDisabledAnalyzer>.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<D2DRuntimeCompilationDisabledAnalyzer>.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<D2DRuntimeCompilationDisabledAnalyzer>.VerifyAnalyzerAsync(source);
}
}