Skip to content

Commit 4dac4d3

Browse files
[lldb][swift] Fix demangling of nested async closures
These produce nested Node objects with the Kind::ExplicitClosure annotation, all of which must be peeled in order to find the Function node.
1 parent db0b3bf commit 4dac4d3

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeNames.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,26 @@ static bool IsSwiftAsyncFunctionSymbol(swift::Demangle::NodePointer node) {
8383
return false;
8484
if (hasChild(node, Node::Kind::AsyncSuspendResumePartialFunction))
8585
return false;
86-
87-
// Peel off layers over top of Function nodes.
88-
switch (node->getFirstChild()->getKind()) {
89-
case Node::Kind::Static:
90-
case Node::Kind::ExplicitClosure:
86+
// Peel off a Static node. If it exists, there will be a single instance and a
87+
// top level node.
88+
if (node->getFirstChild()->getKind() == Node::Kind::Static)
9189
node = node->getFirstChild();
92-
break;
93-
default:
94-
break;
95-
}
9690

97-
return childAtPath(node,
98-
{Node::Kind::Function, Node::Kind::Type,
99-
Node::Kind::FunctionType, Node::Kind::AsyncAnnotation}) ||
100-
childAtPath(node,
101-
{Node::Kind::Function, Node::Kind::Type,
102-
Node::Kind::DependentGenericType, Node::Kind::Type,
103-
Node::Kind::FunctionType, Node::Kind::AsyncAnnotation});
91+
// Get the ExplicitClosure or Function node.
92+
// For nested closures in Swift, the demangle tree is inverted: the
93+
// inner-most closure is the top-most ExplicitClosure node.
94+
NodePointer func_node = [&] {
95+
if (NodePointer func = childAtPath(node, Node::Kind::Function))
96+
return func;
97+
return childAtPath(node, Node::Kind::ExplicitClosure);
98+
}();
99+
100+
return childAtPath(func_node, {Node::Kind::Type, Node::Kind::FunctionType,
101+
Node::Kind::AsyncAnnotation}) ||
102+
childAtPath(func_node,
103+
{Node::Kind::Type, Node::Kind::DependentGenericType,
104+
Node::Kind::Type, Node::Kind::FunctionType,
105+
Node::Kind::AsyncAnnotation});
104106
}
105107

106108
bool SwiftLanguageRuntime::IsSwiftAsyncFunctionSymbol(StringRef name) {

lldb/unittests/Symbol/TestSwiftDemangler.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,22 @@ TEST(TestSwiftDemangleAsyncNames, ClosureAsync) {
4343
"$s1a8sayHelloyyYaFyypYacfU_TQ5_", "$s1a8sayHelloyyYaFyypYacfU_TY6_"};
4444
SmallVector<StringRef> nested2_funclets1 = {
4545
// Nesting level 2: a closure inside a closure.
46-
// FIXME: this one doesn't work: "$s1a8sayHelloyyYaFyypYacfU_yypYacfU_",
46+
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU_",
4747
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU_TY0_",
4848
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU_TQ1_",
4949
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU_TY2_",
5050
};
5151
SmallVector<StringRef> nested2_funclets2 = {
5252
// Nesting level 2: another closure, same level as the previous one.
53-
// FIXME: this one doesn't work: "$s1a8sayHelloyyYaFyypYacfU_yypYacfU0_",
53+
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU0_",
5454
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU0_TY0_",
5555
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU0_TQ1_",
5656
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU0_TY2_",
5757
};
5858
SmallVector<StringRef> nested2_funclets_top_not_async = {
5959
// Also nesting level 2: but this time, the top level function is _not_
6060
// async!
61-
// FIXME: this one doesn't work:
62-
// "$s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_",
61+
"$s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_",
6362
"$s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_TY0_",
6463
"$s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_TQ1_",
6564
"$s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_TY2_"};

0 commit comments

Comments
 (0)