From 44bf98cfa0a4fd5b71036270a130073236c4d73b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 3 Sep 2022 23:34:52 +0200 Subject: [PATCH] Add D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor --- ...eUninitializedFieldDiagnosticSuppressor.cs | 72 +++++++++++++++++++ .../Diagnostics/SuppressionDescriptors.cs | 17 +++++ .../ID2D1ShaderGenerator.Helpers.cs | 2 +- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressor.cs create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/SuppressionDescriptors.cs 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) {