Add InvalidGloballyCoherentFieldDeclarationAnalyzer

このコミットが含まれているのは:
Sergio Pedri committed 2023-11-24 10:30:08 +01:00
1 親 2f0d7911d6
コミット e1ca46aa4c
5ファイル変更
+164

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

+1
ファイルの表示
@@ -63,3 +63,4 @@ CMPS0054 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
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)
CMPS0058 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
@@ -0,0 +1,62 @@
using System.Collections.Immutable;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators;
/// <summary>
/// A diagnostic analyzer that generates an error whenever the [GloballyCoherent] attribute is used on an invalid target field.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InvalidGloballyCoherentFieldDeclarationAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidGloballyCoherentFieldDeclaration);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the IComputeShader, IComputeShader<TPixel>, ReadWriteBuffer<T> and [GloballyCoherent] symbols
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader") is not { } computeShaderSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader`1") is not { } pixelShaderSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.ReadWriteBuffer`1") is not { } readWriteBufferSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.GloballyCoherentAttribute") is not { } globallyCoherentAttributeSymbol)
{
return;
}
context.RegisterSymbolAction(context =>
{
if (context.Symbol is not IFieldSymbol { ContainingType: { } typeSymbol } fieldSymbol)
{
return;
}
// If the current type does not have [GloballyCoherent], there is nothing to do
if (!fieldSymbol.TryGetAttributeWithType(globallyCoherentAttributeSymbol, out AttributeData? attribute))
{
return;
}
// Emit a diagnostic if the field is not valid (either static, not of ReadWriteBuffer<T> type, or not within a compute shader type)
if (fieldSymbol.IsStatic ||
fieldSymbol.Type is not INamedTypeSymbol { IsGenericType: true } fieldTypeSymbol ||
!SymbolEqualityComparer.Default.Equals(fieldTypeSymbol.ConstructedFrom, readWriteBufferSymbol) ||
!MissingComputeShaderDescriptorOnComputeShaderAnalyzer.IsComputeShaderType(typeSymbol, computeShaderSymbol, pixelShaderSymbol))
{
context.ReportDiagnostic(Diagnostic.Create(
InvalidGloballyCoherentFieldDeclaration,
attribute.GetLocation(),
fieldSymbol));
}
}, SymbolKind.Field);
});
}
}
+16
ファイルの表示
@@ -814,4 +814,20 @@ partial class DiagnosticDescriptors
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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when a field annotated with <c>[GloballyCoherent]</c> is not valid.
/// <para>
/// Format: <c>"The field "{0}" is annotated with [GloballyCoherent], but is not a valid target for it (only ReadWriteBuffer&lt;T&gt; instance fields in compute shader types can be annotated with [GloballyCoherent])"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidGloballyCoherentFieldDeclaration = new(
id: "CMPS0058",
title: "Invalid [GloballyCoherent] field declaration",
messageFormat: """The field "{0}" is annotated with [GloballyCoherent], but is not a valid target for it (only ReadWriteBuffer<T> instance fields in compute shader types can be annotated with [GloballyCoherent])""",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The [GloballyCoherent] attribute is only valid on ReadWriteBuffer<T> instance fields in compute shader types.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+84
ファイルの表示
@@ -85,4 +85,88 @@ public class Test_Analyzers
await CSharpAnalyzerWithLanguageVersionTest<NotReadOnlyComputeShaderTypeWithFieldsAnalyzer>.VerifyAnalyzerAsync(source);
}
[TestMethod]
public async Task GloballyCoherentAttribute_NotWithinShaderType()
{
const string source = """
using ComputeSharp;
namespace MyFancyApp.Sample;
internal partial struct MyType
{
[{|CMPS0058:GloballyCoherent|}]
public ReadWriteBuffer<float> buffer;
}
""";
await CSharpAnalyzerWithLanguageVersionTest<InvalidGloballyCoherentFieldDeclarationAnalyzer>.VerifyAnalyzerAsync(source);
}
[TestMethod]
public async Task GloballyCoherentAttribute_IncorrectType()
{
const string source = """
using ComputeSharp;
namespace MyFancyApp.Sample;
internal partial struct MyType : IComputeShader
{
[{|CMPS0058:GloballyCoherent|}]
public ReadOnlyBuffer<float> buffer;
public void Execute()
{
}
}
""";
await CSharpAnalyzerWithLanguageVersionTest<InvalidGloballyCoherentFieldDeclarationAnalyzer>.VerifyAnalyzerAsync(source);
}
[TestMethod]
public async Task GloballyCoherentAttribute_StaticField()
{
const string source = """
using ComputeSharp;
namespace MyFancyApp.Sample;
internal partial struct MyType : IComputeShader
{
[{|CMPS0058:GloballyCoherent|}]
public static ReadWriteBuffer<float> buffer;
public void Execute()
{
}
}
""";
await CSharpAnalyzerWithLanguageVersionTest<InvalidGloballyCoherentFieldDeclarationAnalyzer>.VerifyAnalyzerAsync(source);
}
[TestMethod]
public async Task GloballyCoherentAttribute_ValidField_DoesNotWarn()
{
const string source = """
using ComputeSharp;
namespace MyFancyApp.Sample;
internal partial struct MyType : IComputeShader
{
[GloballyCoherent]
public ReadWriteBuffer<float> buffer;
public void Execute()
{
}
}
""";
await CSharpAnalyzerWithLanguageVersionTest<InvalidGloballyCoherentFieldDeclarationAnalyzer>.VerifyAnalyzerAsync(source);
}
}
+1
ファイルの表示
@@ -895,6 +895,7 @@ public class Test_D2DPixelShaderSourceGenerator
// Also validate all analyzers
await CSharpAnalyzerWithLanguageVersionTest<InvalidComputeContextCopyAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<InvalidGloballyCoherentFieldDeclarationAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<MissingAllowUnsafeBlocksCompilationOptionAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<MissingComputeShaderDescriptorOnComputeShaderAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);