Minor bug fixes to source generator body rewrite

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-25 01:16:55 +01:00
1 親 9a220c1ada
コミット 6548d1acb3
3ファイル変更
+38 -15

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

+1 -1
ファイルの表示
@@ -21,7 +21,7 @@ namespace ComputeSharp.SourceGenerators.Extensions
{
return symbol switch
{
INamespaceSymbol ns when ns.IsGlobalNamespace => builder.Insert(0, "global::"),
INamespaceSymbol ns when ns.IsGlobalNamespace => builder,
INamespaceSymbol ns when ns.ContainingNamespace is { IsGlobalNamespace: false }
=> BuildFrom(ns.ContainingNamespace, builder.Insert(0, $".{ns.MetadataName}")),
ITypeSymbol ts when ts.ContainingType is ISymbol pt => BuildFrom(pt, builder.Insert(0, $"+{ts.MetadataName}")),
+19
ファイルの表示
@@ -11,6 +11,9 @@ namespace ComputeSharp.SourceGenerators.Extensions
/// </summary>
internal static class SyntaxNodeExtensions
{
/// <summary>
/// A custom <see cref="SymbolDisplayFormat"/> instance with fully qualified style, without global::.
/// </summary>
private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new(
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
@@ -60,5 +63,21 @@ namespace ComputeSharp.SourceGenerators.Extensions
return ParseTypeName(typeName);
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> with a block body.
/// </summary>
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
/// <returns>A node like the one in input, but always with a block body.</returns>
[Pure]
public static MethodDeclarationSyntax WithBlockBody(this MethodDeclarationSyntax node)
{
return node.WithBody((node.Body, node.ExpressionBody) switch
{
(BlockSyntax block, _) => block,
(_, ArrowExpressionClauseSyntax arrow) => Block(ExpressionStatement(arrow.Expression)),
_ => Block()
});
}
}
}
+18 -14
ファイルの表示
@@ -47,20 +47,11 @@ namespace ComputeSharp.SourceGenerators
{
IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!;
// Extract the source from either a block or an expression body
var methodWithBlockBody = methodDeclaration.WithBody(
(methodDeclaration.Body, methodDeclaration.ExpressionBody) switch
{
(BlockSyntax block, _) => block,
(_, ArrowExpressionClauseSyntax arrow) => Block(ExpressionStatement(arrow.Expression)),
_ => Block()
});
// Rewrite the method syntax tree
var processedMethod = new ShaderSourceRewriter(semanticModel)
.Visit(methodWithBlockBody)
.NormalizeWhitespace()
.ToFullString();
var processedMethod = new ShaderSourceRewriter(semanticModel).Visit(methodDeclaration)!.WithoutTrivia();
// Produce the final method source
var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString();
// Create the compilation unit with the source attribute
var source =
@@ -69,7 +60,7 @@ namespace ComputeSharp.SourceGenerators
Attribute(IdentifierName("ComputeSharp.IComputeShaderSource")).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structFullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.Name))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(processedMethod))))))
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(processedMethodSource))))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
.NormalizeWhitespace()
@@ -102,6 +93,13 @@ namespace ComputeSharp.SourceGenerators
this.semanticModel = semanticModel;
}
/// <inheritdoc cref="CSharpSyntaxRewriter.Visit(SyntaxNode?)"/>
public TNode? Visit<TNode>(TNode? node)
where TNode : SyntaxNode
{
return (TNode?)base.Visit(node);
}
/// <inheritdoc/>
public override SyntaxNode VisitParameter(ParameterSyntax node)
{
@@ -168,6 +166,12 @@ namespace ComputeSharp.SourceGenerators
return node;
}
/// <inheritdoc/>
public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node)
{
return ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithBlockBody();
}
}
}
}