diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor.cs new file mode 100644 index 00000000..43e4f83c --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor.cs @@ -0,0 +1,72 @@ +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Mappings; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.D2D1.SourceGenerators.Diagnostics.SuppressionDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +/// +/// A diagnostic suppressor to suppress CS0649 warnings for uninitialized D2D1 resource texture fields. +/// +/// +/// That is, this diagnostic suppressor will suppress the following diagnostic: +/// +/// public struct MyShader : ID2D1PixelShader +/// { +/// [D2DResourceTextureIndex(0)] +/// private D2D1ResourceTexture1D<float4> texture; +/// +/// public float4 Execute() +/// { +/// return texture[0]; +/// } +/// } +/// +/// +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor : DiagnosticSuppressor +{ + /// + public override ImmutableArray SupportedSuppressions => ImmutableArray.Create(UninitializedD2D1ResourceTextureField); + + /// + public override void ReportSuppressions(SuppressionAnalysisContext context) + { + foreach (Diagnostic diagnostic in context.ReportedDiagnostics) + { + SyntaxNode? syntaxNode = diagnostic.Location.SourceTree?.GetRoot(context.CancellationToken).FindNode(diagnostic.Location.SourceSpan); + + if (syntaxNode is not null) + { + SemanticModel semanticModel = context.GetSemanticModel(syntaxNode.SyntaxTree); + + // Get the field symbol + ISymbol? declaredSymbol = semanticModel.GetDeclaredSymbol(syntaxNode, context.CancellationToken); + + // Check if the field is in a struct and it's of a D2D1 resource texture type + if (declaredSymbol is IFieldSymbol fieldSymbol && + fieldSymbol.ContainingType is { TypeKind: TypeKind.Struct } structSymbol && + HlslKnownTypes.IsResourceTextureType(fieldSymbol.Type.GetFullMetadataName())) + { + // Get the ID2D1PixelShader interface symbol to the check the containing type of the field + INamedTypeSymbol? pixelShaderInterfaceSymbol = semanticModel.Compilation.GetTypeByMetadataName(typeof(ID2D1PixelShader).FullName)!; + + if (pixelShaderInterfaceSymbol is null) + { + continue; + } + + // Also check if the containing type is in fact a D2D1 pixel shader type + if (ID2D1ShaderGenerator.IsD2D1PixelShaderType(structSymbol, pixelShaderInterfaceSymbol)) + { + context.ReportSuppression(Suppression.Create(UninitializedD2D1ResourceTextureField, diagnostic)); + } + } + } + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/SuppressionDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/SuppressionDescriptors.cs new file mode 100644 index 00000000..b9b3864b --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/SuppressionDescriptors.cs @@ -0,0 +1,17 @@ +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.D2D1.SourceGenerators.Diagnostics; + +/// +/// A container for all instances for suppressed diagnostics by analyzers in this project. +/// +internal static class SuppressionDescriptors +{ + /// + /// Gets a for an uninitialized D2D1 resource texture field. + /// + public static readonly SuppressionDescriptor UninitializedD2D1ResourceTextureField = new( + id: "CMPSD2DSPR0001", + suppressedDiagnosticId: "CS0649", + justification: "Fields of type D2D1ResourceTexture1D, D2D1ResourceTexture2D and D2D1ResourceTexture3D are implicitly mapped to resource texture objects into the resulting D2D1 pixel shader for their containing type, and don't need to be initialized"); +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs index 79a971ae..b213fcc4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs @@ -16,7 +16,7 @@ partial class ID2D1ShaderGenerator /// The input instance to check. /// The instance for . /// Whether or not implements . - private static bool IsD2D1PixelShaderType(INamedTypeSymbol typeSymbol, INamedTypeSymbol d2D1PixelShaderSymbol) + public static bool IsD2D1PixelShaderType(INamedTypeSymbol typeSymbol, INamedTypeSymbol d2D1PixelShaderSymbol) { foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) { diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests.cs new file mode 100644 index 00000000..7a873e9a --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests.cs @@ -0,0 +1,18 @@ +namespace ComputeSharp.D2D1.Tests; + +/// +/// A test class for the diagnostic suppressor for D2D1 resource texture fields being left uninitialized. +/// +internal sealed partial class D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests +{ + public readonly partial struct MyShader : ID2D1PixelShader + { + // This test just needs to validate the project builds fine with this shader. + // If the diagnostic suppressor wasn't working, it'd fail to build because + // Roslyn would emit a CS0649 warning, which would be treated as an error. + [D2DResourceTextureIndex(0)] + private readonly D2D1ResourceTexture1D texture; + + public float4 Execute() => texture[0]; + } +}