diff --git a/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs b/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs index 9688b5b3..bddd91ad 100644 --- a/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs +++ b/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs @@ -2,6 +2,7 @@ using System.Collections.Immutable; using System.Composition; using System.Threading; using System.Threading.Tasks; +using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; @@ -20,6 +21,25 @@ namespace ComputeSharp.D2D1.CodeFixers; [Shared] public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFixProvider { + /// + /// The set of type names for all D2D attributes that can be over shader types. + /// + private static readonly ImmutableArray D2DAttributeTypeNames = ImmutableArray.Create( + "ComputeSharp.D2D1.D2DCompileOptionsAttribute", + "ComputeSharp.D2D1.D2DEffectAuthorAttribute", + "ComputeSharp.D2D1.D2DEffectCategoryAttribute", + "ComputeSharp.D2D1.D2DEffectDescriptionAttribute", + "ComputeSharp.D2D1.D2DEffectDisplayNameAttribute", + "ComputeSharp.D2D1.D2DEffectIdAttribute", + "ComputeSharp.D2D1.D2DInputComplexAttribute", + "ComputeSharp.D2D1.D2DInputCountAttribute", + "ComputeSharp.D2D1.D2DInputDescriptionAttribute", + "ComputeSharp.D2D1.D2DInputSimpleAttribute", + "ComputeSharp.D2D1.D2DOutputBufferAttribute", + "ComputeSharp.D2D1.D2DPixelOptionsAttribute", + "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute", + "ComputeSharp.D2D1.D2DShaderProfileAttribute"); + /// public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(MissingPixelShaderDescriptorOnPixelShaderTypeId); @@ -92,6 +112,12 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFix return structDeclaration; } + // Build the map of D2D attributes to look for + if (!semanticModel.Compilation.TryBuildNamedTypeSymbolSet(D2DAttributeTypeNames, out ImmutableHashSet? d2DAttributeTypeSymbols)) + { + return structDeclaration; + } + // Also bail if we can't resolve the [D2DGeneratedPixelShaderDescriptor] attribute symbol (this should really never happen) if (semanticModel.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute") is not INamedTypeSymbol attributeSymbol) { @@ -111,14 +137,14 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFix continue; } - // Make sure we find a readable identifier for the attribute - if (attribute.Name is not IdentifierNameSyntax { Identifier.Value: string identifier }) + // Resolve the symbol for the attribute (stop here if this failed for whatever reason) + if (!semanticModel.GetSymbolInfo(attribute, cancellationToken).TryGetAttributeTypeSymbol(out INamedTypeSymbol? attributeTypeSymbol)) { break; } // If the attribute is D2D one, increment the index and continue - if (identifier.Contains("D2D")) + if (d2DAttributeTypeSymbols.Contains(attributeTypeSymbol)) { index++; } diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems index 3a2bf8d3..413e50cc 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems @@ -18,6 +18,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration/Extensions/SymbolInfoExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/SymbolInfoExtensions.cs new file mode 100644 index 00000000..2317a5d1 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Extensions/SymbolInfoExtensions.cs @@ -0,0 +1,44 @@ +using System.Diagnostics.CodeAnalysis; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.SourceGeneration.Extensions; + +/// +/// Extension methods for the type. +/// +internal static class SymbolInfoExtensions +{ + /// + /// Tries to get the resolved attribute type symbol from a given value. + /// + /// The value to check. + /// The resulting attribute type symbol, if correctly resolved. + /// Whether is resolved to a symbol. + /// + /// This can be used to ensure users haven't eg. spelled names incorrecty or missed a using directive. Normally, code would just + /// not compile if that was the case, but that doesn't apply for attributes using invalid targets. In that case, Roslyn will ignore + /// any errors, meaning the generator has to validate the type symbols are correctly resolved on its own. + /// + public static bool TryGetAttributeTypeSymbol(this SymbolInfo symbolInfo, [NotNullWhen(true)] out INamedTypeSymbol? typeSymbol) + { + ISymbol? attributeSymbol = symbolInfo.Symbol; + + // If no symbol is selected and there is a single candidate symbol, use that + if (attributeSymbol is null && symbolInfo.CandidateSymbols is [ISymbol candidateSymbol]) + { + attributeSymbol = candidateSymbol; + } + + // Extract the symbol from either the current one or the containing type + if ((attributeSymbol as INamedTypeSymbol ?? attributeSymbol?.ContainingType) is not INamedTypeSymbol resultingSymbol) + { + typeSymbol = null; + + return false; + } + + typeSymbol = resultingSymbol; + + return true; + } +} diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs index dc404974..756229cf 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs @@ -294,4 +294,68 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer await test.RunAsync(); } + + [TestMethod] + public async Task InsertsAttributeAfterAllOtherD2DAttributes_WithFakeAttributes() + { + string original = """ + using System; + using ComputeSharp.D2D1; + + [D2DInputCount(2)] + [D2DInputSimple(0)] + [ComputeSharp.D2D1.D2DInputSimple(1)] + [D2DFake] + [Test] + partial struct {|CMPSD2D0065:MyShader|} : ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + public class TestAttribute : Attribute + { + } + + public class D2DFakeAttribute : Attribute + { + } + """; + + string @fixed = """ + using System; + using ComputeSharp.D2D1; + + [D2DInputCount(2)] + [D2DInputSimple(0)] + [ComputeSharp.D2D1.D2DInputSimple(1)] + [D2DGeneratedPixelShaderDescriptor] + [D2DFake] + [Test] + partial struct {|CMPSD2D0065:MyShader|} : ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + public class TestAttribute : Attribute + { + } + + public class D2DFakeAttribute : Attribute + { + } + """; + + CSharpCodeFixTest test = new() + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 + }; + + test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly); + test.TestState.AdditionalReferences.Add(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly); + test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", "[*]\nend_of_line = lf")); + + await test.RunAsync(); + } }