Correctly insert attribute after built-in D2D attributes

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-03 21:14:24 +02:00
1 親 1013cfb6f3
コミット 112a4af860
4ファイル変更
+138 -3

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

+29 -3
ファイルの表示
@@ -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
{
/// <summary>
/// The set of type names for all D2D attributes that can be over shader types.
/// </summary>
private static readonly ImmutableArray<string> 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");
/// <inheritdoc/>
public override ImmutableArray<string> 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<INamedTypeSymbol>? 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++;
}
+1
ファイルの表示
@@ -18,6 +18,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IFieldSymbolExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ITypeSymbolExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ISymbolExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SymbolInfoExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)helpers\DynamicCache{TKey,TValue}.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\EquatableArray{T}.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\HashCode.cs" />
+44
ファイルの表示
@@ -0,0 +1,44 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGeneration.Extensions;
/// <summary>
/// Extension methods for the <see cref="SymbolInfo"/> type.
/// </summary>
internal static class SymbolInfoExtensions
{
/// <summary>
/// Tries to get the resolved attribute type symbol from a given <see cref="SymbolInfo"/> value.
/// </summary>
/// <param name="symbolInfo">The <see cref="SymbolInfo"/> value to check.</param>
/// <param name="typeSymbol">The resulting attribute type symbol, if correctly resolved.</param>
/// <returns>Whether <paramref name="symbolInfo"/> is resolved to a symbol.</returns>
/// <remarks>
/// 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.
/// </remarks>
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;
}
}
@@ -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();
}
}