@@ -22,6 +22,22 @@ static constexpr auto AreFuncletsOfSameAsyncFunction = [](StringRef name1,
22
22
23
23
using FuncletComparisonResult = SwiftLanguageRuntime::FuncletComparisonResult;
24
24
25
+ // / Helpful printer for the tests.
26
+ namespace lldb_private {
27
+ std::ostream &operator <<(std::ostream &os,
28
+ const FuncletComparisonResult &result) {
29
+ switch (result) {
30
+ case FuncletComparisonResult::NotBothFunclets:
31
+ return os << " NotBothFunclets" ;
32
+ case FuncletComparisonResult::SameAsyncFunction:
33
+ return os << " SameAsyncFunction" ;
34
+ case FuncletComparisonResult::DifferentAsyncFunctions:
35
+ return os << " DifferentAsyncFunctions" ;
36
+ }
37
+ return os << " FuncletComparisonResult invalid enumeration" ;
38
+ }
39
+ } // namespace lldb_private
40
+
25
41
// / Checks that all names in \c funclets belong to the same function.
26
42
static void CheckGroupOfFuncletsFromSameFunction (ArrayRef<StringRef> funclets) {
27
43
for (StringRef funclet1 : funclets)
@@ -86,6 +102,85 @@ TEST(TestSwiftDemangleAsyncNames, BasicAsync) {
86
102
CheckFuncletNumbersAreARange (generic_funclets);
87
103
}
88
104
105
+ // The funclets below are created from this program:
106
+ // swiftc -g -Onone test.swift -o - -emit-ir -module-name a \
107
+ // | grep "define.*sayHello"
108
+ // func work() async {}
109
+ // func async_int() async -> Int { return 42; }
110
+ // func sayHello() async {
111
+ // let closure: (Any) async -> () = { _ in
112
+ // print("hello")
113
+ // await work()
114
+ // print("hello")
115
+ //
116
+ // let inner_closure: (Any) async -> () = { _ in
117
+ // print("hello")
118
+ // await work()
119
+ // print("hello")
120
+ // }
121
+ // await inner_closure(10)
122
+ // print("hello")
123
+ //
124
+ // let inner_closure2: (Any) async -> () = { _ in
125
+ // print("hello")
126
+ // await work()
127
+ // print("hello")
128
+ // }
129
+ //
130
+ // await inner_closure2(10)
131
+ // print("hello")
132
+ // async let x = await async_int();
133
+ // print(await x);
134
+ // }
135
+ // async let x = await async_int();
136
+ // print(await x);
137
+ // await closure(10)
138
+ // async let explicit_inside_implicit_closure =
139
+ // { _ in
140
+ // print("hello")
141
+ // await work()
142
+ // print("hello")
143
+ // return 42
144
+ // }(10)
145
+ // print(await explicit_inside_implicit_closure)
146
+ // }
147
+ // func sayHello2() async {
148
+ // {_ in
149
+ // }(10)
150
+ // async let another_explicit_inside_implicit_closure =
151
+ // { _ in
152
+ // print("hello")
153
+ // await work()
154
+ // print("hello")
155
+ // return 42
156
+ // }(10)
157
+ // print(await another_explicit_inside_implicit_closure)
158
+ // }
159
+ // protocol RandomNumberGenerator {
160
+ // func random(in range: ClosedRange<Int>) async -> Int
161
+ // static func static_random() async -> Int
162
+ // }
163
+ // class Generator: RandomNumberGenerator {
164
+ // func random(in range: ClosedRange<Int>) async -> Int {
165
+ // try? await Task.sleep(for: .milliseconds(500))
166
+ // return Int.random(in: range)
167
+ // }
168
+ // static func static_random() async -> Int {
169
+ // print("hello")
170
+ // try? await Task.sleep(for: .milliseconds(500))
171
+ // print("hello")
172
+ // return 42
173
+ // }
174
+ // }
175
+ // func doMath<RNG: RandomNumberGenerator>(with rng: RNG) async {
176
+ // let x = await rng.random(in: 0...100)
177
+ // print("X is \(x)")
178
+ // let y = await rng.random(in: 101...200)
179
+ // print("Y is \(y)")
180
+ // print("The magic number is \(x + y)")
181
+ // }
182
+
183
+
89
184
TEST (TestSwiftDemangleAsyncNames, ClosureAsync) {
90
185
// These are all async closures
91
186
SmallVector<StringRef> nested1_funclets = {
@@ -116,34 +211,83 @@ TEST(TestSwiftDemangleAsyncNames, ClosureAsync) {
116
211
" $s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_TQ1_" ,
117
212
" $s1a18myNonAsyncFunctionyyFyyYacfU_SiypYacfU_SSypYacfU0_TY2_" };
118
213
119
- for (StringRef async_name : llvm::concat<StringRef>(
120
- nested1_funclets, nested2_funclets1, nested2_funclets2,
121
- nested2_funclets_top_not_async)) {
122
- EXPECT_TRUE (IsSwiftMangledName (async_name)) << async_name;
123
- EXPECT_TRUE (IsAnySwiftAsyncFunctionSymbol (async_name)) << async_name;
124
- }
214
+ SmallVector<StringRef> implicit_closure_inside_function = {
215
+ " $s1a8sayHelloyyYaFSiyYaYbcfu_" ,
216
+ " $s1a8sayHelloyyYaFSiyYaYbcfu_TQ0_" ,
217
+ " $s1a8sayHelloyyYaFSiyYaYbcfu_TY1_" ,
218
+ };
219
+ SmallVector<StringRef> implicit_closure_inside_explicit_closure = {
220
+ " $s1a8sayHelloyyYaFyypYacfU_SiyYaYbcfu_" ,
221
+ " $s1a8sayHelloyyYaFyypYacfU_SiyYaYbcfu_TQ0_" ,
222
+ " $s1a8sayHelloyyYaFyypYacfU_SiyYaYbcfu_TY1_" ,
223
+ };
224
+ SmallVector<StringRef> explicit_closure_inside_implicit_closure = {
225
+ " $s1a8sayHelloyyYaFSiyYaYbcfu0_S2iYaXEfU0_" ,
226
+ " $s1a8sayHelloyyYaFSiyYaYbcfu0_S2iYaXEfU0_TY0_" ,
227
+ " $s1a8sayHelloyyYaFSiyYaYbcfu0_S2iYaXEfU0_TQ1_" ,
228
+ " $s1a8sayHelloyyYaFSiyYaYbcfu0_S2iYaXEfU0_TY2_" ,
229
+ };
230
+ SmallVector<StringRef> another_explicit_closure_inside_implicit_closure = {
231
+ " $s1a9sayHello2yyYaFSiyYaYbcfu_S2iYaXEfU0_" ,
232
+ " $s1a9sayHello2yyYaFSiyYaYbcfu_S2iYaXEfU0_TY0_" ,
233
+ " $s1a9sayHello2yyYaFSiyYaYbcfu_S2iYaXEfU0_TQ1_" ,
234
+ " $s1a9sayHello2yyYaFSiyYaYbcfu_S2iYaXEfU0_TY2_" ,
235
+ };
236
+ SmallVector<StringRef> witness_funclets = {
237
+ " $s1a9GeneratorCAA012RandomNumberA0A2aDP6random2inSiSNySiG_tYaFTW" ,
238
+ " $s1a9GeneratorCAA012RandomNumberA0A2aDP6random2inSiSNySiG_tYaFTWTQ0_" ,
239
+ };
240
+ SmallVector<StringRef> actual_funclets = {
241
+ " $s1a9GeneratorC6random2inSiSNySiG_tYaF" ,
242
+ " $s1a9GeneratorC6random2inSiSNySiG_tYaFTY0_" ,
243
+ " $s1a9GeneratorC6random2inSiSNySiG_tYaFTQ1_" ,
244
+ " $s1a9GeneratorC6random2inSiSNySiG_tYaFTY2_" ,
245
+ " $s1a9GeneratorC6random2inSiSNySiG_tYaFTY3_" ,
246
+ };
247
+ SmallVector<StringRef> static_witness_funclets = {
248
+ " $s1a9GeneratorCAA012RandomNumberA0A2aDP13static_randomSiyYaFZTW" ,
249
+ " $s1a9GeneratorCAA012RandomNumberA0A2aDP13static_randomSiyYaFZTWTQ0_" ,
250
+ };
251
+ SmallVector<StringRef> static_actual_funclets = {
252
+ " $s1a9GeneratorC13static_randomSiyYaFZ" ,
253
+ " $s1a9GeneratorC13static_randomSiyYaFZTY0_" ,
254
+ " $s1a9GeneratorC13static_randomSiyYaFZTQ1_" ,
255
+ " $s1a9GeneratorC13static_randomSiyYaFZTY2_" ,
256
+ " $s1a9GeneratorC13static_randomSiyYaFZTY3_" ,
257
+ };
258
+
259
+ SmallVector<ArrayRef<StringRef>, 0 > funclet_groups = {
260
+ nested1_funclets,
261
+ nested2_funclets1,
262
+ nested2_funclets2,
263
+ nested2_funclets_top_not_async,
264
+ implicit_closure_inside_function,
265
+ implicit_closure_inside_explicit_closure,
266
+ explicit_closure_inside_implicit_closure,
267
+ another_explicit_closure_inside_implicit_closure,
268
+ witness_funclets,
269
+ actual_funclets,
270
+ static_witness_funclets,
271
+ static_actual_funclets,
272
+ };
273
+
274
+ for (ArrayRef<StringRef> funclet_group : funclet_groups)
275
+ for (StringRef async_name : funclet_group) {
276
+ EXPECT_TRUE (IsSwiftMangledName (async_name)) << async_name;
277
+ EXPECT_TRUE (IsAnySwiftAsyncFunctionSymbol (async_name)) << async_name;
278
+ }
279
+
280
+ for (ArrayRef<StringRef> funclet_group : funclet_groups)
281
+ CheckGroupOfFuncletsFromSameFunction (funclet_group);
282
+
283
+ for (ArrayRef<StringRef> funclet_group1 : funclet_groups)
284
+ for (ArrayRef<StringRef> funclet_group2 : funclet_groups)
285
+ if (funclet_group1.data () != funclet_group2.data ())
286
+ CheckGroupOfFuncletsFromDifferentFunctions (funclet_group1,
287
+ funclet_group2);
125
288
126
- CheckGroupOfFuncletsFromSameFunction (nested1_funclets);
127
- CheckGroupOfFuncletsFromSameFunction (nested2_funclets1);
128
- CheckGroupOfFuncletsFromSameFunction (nested2_funclets2);
129
- CheckGroupOfFuncletsFromSameFunction (nested2_funclets_top_not_async);
130
-
131
- CheckGroupOfFuncletsFromDifferentFunctions (nested1_funclets,
132
- nested2_funclets1);
133
- CheckGroupOfFuncletsFromDifferentFunctions (nested1_funclets,
134
- nested2_funclets2);
135
- CheckGroupOfFuncletsFromDifferentFunctions (nested1_funclets,
136
- nested2_funclets_top_not_async);
137
- CheckGroupOfFuncletsFromDifferentFunctions (nested2_funclets1,
138
- nested2_funclets2);
139
- CheckGroupOfFuncletsFromDifferentFunctions (nested2_funclets1,
140
- nested2_funclets_top_not_async);
141
- CheckGroupOfFuncletsFromDifferentFunctions (nested2_funclets2,
142
- nested2_funclets_top_not_async);
143
- CheckFuncletNumbersAreARange (nested1_funclets);
144
- CheckFuncletNumbersAreARange (nested2_funclets1);
145
- CheckFuncletNumbersAreARange (nested2_funclets2);
146
- CheckFuncletNumbersAreARange (nested2_funclets_top_not_async);
289
+ for (ArrayRef<StringRef> funclet_group : funclet_groups)
290
+ CheckFuncletNumbersAreARange (funclet_group);
147
291
}
148
292
149
293
TEST (TestSwiftDemangleAsyncNames, StaticAsync) {
0 commit comments