Skip to content

Commit db0b3bf

Browse files
[lldb] Add unittests for SwiftLanguageRuntime demangling functions
This should make it easier to make changes and ensure they are correct. For example, as commented in the tests, there are known issues with the async function detection.
1 parent 9835aa0 commit db0b3bf

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

lldb/unittests/Symbol/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(SWIFT_SOURCES
22
TestSwiftASTContext.cpp
33
TestTypeSystemSwiftTypeRef.cpp
4+
TestSwiftDemangler.cpp
45
)
56
set(SWIFT_LIBS
67
lldbPluginTypeSystemSwift
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "gtest/gtest.h"
2+
3+
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
4+
5+
using namespace lldb;
6+
using namespace lldb_private;
7+
using namespace llvm;
8+
static constexpr auto IsSwiftMangledName =
9+
SwiftLanguageRuntime::IsSwiftMangledName;
10+
static constexpr auto IsAnySwiftAsyncFunctionSymbol = [](StringRef name) {
11+
return SwiftLanguageRuntime::IsAnySwiftAsyncFunctionSymbol(name);
12+
};
13+
14+
TEST(TestSwiftDemangleAsyncNames, BasicAsync) {
15+
// "sayBasic" == a basic async function
16+
// "sayGeneric" == a generic async function
17+
SmallVector<StringRef> basic_funclets = {
18+
"$s1a8sayBasicyySSYaF",
19+
"$s1a8sayBasicyySSYaFTY0_",
20+
"$s1a8sayBasicyySSYaFTQ1_",
21+
"$s1a8sayBasicyySSYaFTY2_",
22+
};
23+
SmallVector<StringRef> generic_funclets = {
24+
"$s1a10sayGenericyyxYalF",
25+
"$s1a10sayGenericyyxYalFTY0_",
26+
"$s1a10sayGenericyyxYalFTQ1_",
27+
"$s1a10sayGenericyyxYalFTY2_",
28+
};
29+
for (StringRef async_name :
30+
llvm::concat<StringRef>(basic_funclets, generic_funclets)) {
31+
EXPECT_TRUE(IsSwiftMangledName(async_name)) << async_name;
32+
EXPECT_TRUE(IsAnySwiftAsyncFunctionSymbol(async_name)) << async_name;
33+
}
34+
}
35+
36+
TEST(TestSwiftDemangleAsyncNames, ClosureAsync) {
37+
// These are all async closures
38+
SmallVector<StringRef> nested1_funclets = {
39+
// Nesting level 1: a closure inside a function.
40+
"$s1a8sayHelloyyYaFyypYacfU_", "$s1a8sayHelloyyYaFyypYacfU_TY0_",
41+
"$s1a8sayHelloyyYaFyypYacfU_TQ1_", "$s1a8sayHelloyyYaFyypYacfU_TY2_",
42+
"$s1a8sayHelloyyYaFyypYacfU_TQ3_", "$s1a8sayHelloyyYaFyypYacfU_TY4_",
43+
"$s1a8sayHelloyyYaFyypYacfU_TQ5_", "$s1a8sayHelloyyYaFyypYacfU_TY6_"};
44+
SmallVector<StringRef> nested2_funclets1 = {
45+
// Nesting level 2: a closure inside a closure.
46+
// FIXME: this one doesn't work: "$s1a8sayHelloyyYaFyypYacfU_yypYacfU_",
47+
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU_TY0_",
48+
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU_TQ1_",
49+
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU_TY2_",
50+
};
51+
SmallVector<StringRef> nested2_funclets2 = {
52+
// Nesting level 2: another closure, same level as the previous one.
53+
// FIXME: this one doesn't work: "$s1a8sayHelloyyYaFyypYacfU_yypYacfU0_",
54+
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU0_TY0_",
55+
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU0_TQ1_",
56+
"$s1a8sayHelloyyYaFyypYacfU_yypYacfU0_TY2_",
57+
};
58+
SmallVector<StringRef> nested2_funclets_top_not_async = {
59+
// Also nesting level 2: but this time, the top level function is _not_
60+
// async!
61+
// FIXME: this one doesn't work:
62+
// "$s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_",
63+
"$s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_TY0_",
64+
"$s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_TQ1_",
65+
"$s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_TY2_"};
66+
67+
for (StringRef async_name : llvm::concat<StringRef>(
68+
nested1_funclets, nested2_funclets1, nested2_funclets2,
69+
nested2_funclets_top_not_async)) {
70+
EXPECT_TRUE(IsSwiftMangledName(async_name)) << async_name;
71+
EXPECT_TRUE(IsAnySwiftAsyncFunctionSymbol(async_name)) << async_name;
72+
}
73+
}
74+
75+
TEST(TestSwiftDemangleAsyncNames, StaticAsync) {
76+
// static async functions
77+
SmallVector<StringRef> async_names = {
78+
"$s1a6StructV9sayStaticyySSYaFZ"
79+
"$s1a6StructV9sayStaticyySSYaFZTY0_",
80+
"$s1a6StructV9sayStaticyySSYaFZTQ1_",
81+
"$s1a6StructV9sayStaticyySSYaFZTY2_",
82+
};
83+
84+
for (StringRef async_name : async_names) {
85+
EXPECT_TRUE(IsSwiftMangledName(async_name)) << async_name;
86+
EXPECT_TRUE(IsAnySwiftAsyncFunctionSymbol(async_name)) << async_name;
87+
}
88+
}

0 commit comments

Comments
 (0)