aboutsummaryrefslogtreecommitdiff
path: root/clang/test/CodeGenCXX/wasm-eh.cpp
blob: af023f52191b979c9e5bf8535e5c7f992e1853df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// REQUIRES: webassembly-registered-target
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 | FileCheck %s
// RUN: %clang_cc1 %s -triple wasm64-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 | FileCheck %s

void may_throw();
void dont_throw() noexcept;

struct Cleanup {
  ~Cleanup() { dont_throw(); }
};

// Multiple catch clauses w/o catch-all
void test0() {
  try {
    may_throw();
  } catch (int) {
    dont_throw();
  } catch (double) {
    dont_throw();
  }
}

// CHECK-LABEL: define void @_Z5test0v() {{.*}} personality ptr @__gxx_wasm_personality_v0

// CHECK:   %[[INT_ALLOCA:.*]] = alloca i32
// CHECK:   invoke void @_Z9may_throwv()
// CHECK-NEXT:           to label %[[NORMAL_BB:.*]] unwind label %[[CATCHDISPATCH_BB:.*]]

// CHECK: [[CATCHDISPATCH_BB]]:
// CHECK-NEXT:   %[[CATCHSWITCH:.*]] = catchswitch within none [label %[[CATCHSTART_BB:.*]]] unwind to caller

// CHECK: [[CATCHSTART_BB]]:
// CHECK-NEXT:   %[[CATCHPAD:.*]] = catchpad within %[[CATCHSWITCH]] [ptr @_ZTIi, ptr @_ZTId]
// CHECK-NEXT:   %[[EXN:.*]] = call ptr @llvm.wasm.get.exception(token %[[CATCHPAD]])
// CHECK-NEXT:   store ptr %[[EXN]], ptr %exn.slot
// CHECK-NEXT:   %[[SELECTOR:.*]] = call i32 @llvm.wasm.get.ehselector(token %[[CATCHPAD]])
// CHECK-NEXT:   %[[TYPEID:.*]] = call i32 @llvm.eh.typeid.for(ptr @_ZTIi) #7
// CHECK-NEXT:   %[[MATCHES:.*]] = icmp eq i32 %[[SELECTOR]], %[[TYPEID]]
// CHECK-NEXT:   br i1 %[[MATCHES]], label %[[CATCH_INT_BB:.*]], label %[[CATCH_FALLTHROUGH_BB:.*]]

// CHECK: [[CATCH_INT_BB]]:
// CHECK-NEXT:   %[[EXN:.*]] = load ptr, ptr %exn.slot
// CHECK-NEXT:   %[[ADDR:.*]] = call ptr @__cxa_begin_catch(ptr %[[EXN]]) {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:   %[[INT_VAL:.*]] = load i32, ptr %[[ADDR]]
// CHECK-NEXT:   store i32 %[[INT_VAL]], ptr %[[INT_ALLOCA]]
// CHECK-NEXT:   call void @_Z10dont_throwv() {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:   call void @__cxa_end_catch() {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:   catchret from %[[CATCHPAD]] to label %[[CATCHRET_DEST_BB0:.*]]

// CHECK: [[CATCHRET_DEST_BB0]]:
// CHECK-NEXT:   br label %[[TRY_CONT_BB:.*]]

// CHECK: [[CATCH_FALLTHROUGH_BB]]
// CHECK-NEXT:   %[[TYPEID:.*]] = call i32 @llvm.eh.typeid.for(ptr @_ZTId) #7
// CHECK-NEXT:   %[[MATCHES:.*]] = icmp eq i32 %[[SELECTOR]], %[[TYPEID]]
// CHECK-NEXT:   br i1 %[[MATCHES]], label %[[CATCH_FLOAT_BB:.*]], label %[[RETHROW_BB:.*]]

// CHECK: [[CATCH_FLOAT_BB]]:
// CHECK:   catchret from %[[CATCHPAD]] to label %[[CATCHRET_DEST_BB1:.*]]

// CHECK: [[CATCHRET_DEST_BB1]]:
// CHECK-NEXT:   br label %[[TRY_CONT_BB]]

// CHECK: [[RETHROW_BB]]:
// CHECK-NEXT:   call void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:   unreachable

// Single catch-all
void test1() {
  try {
    may_throw();
  } catch (...) {
    dont_throw();
  }
}

// CATCH-LABEL: @_Z5test1v()

// CHECK:   %[[CATCHSWITCH:.*]] = catchswitch within none [label %[[CATCHSTART_BB:.*]]] unwind to caller

// CHECK: [[CATCHSTART_BB]]:
// CHECK-NEXT:   %[[CATCHPAD:.*]] = catchpad within %[[CATCHSWITCH]] [ptr null]
// CHECK:   br label %[[CATCH_ALL_BB:.*]]

// CHECK: [[CATCH_ALL_BB]]:
// CHECK:   catchret from %[[CATCHPAD]] to label

// Multiple catch clauses w/ catch-all
void test2() {
  try {
    may_throw();
  } catch (int) {
    dont_throw();
  } catch (...) {
    dont_throw();
  }
}

// CHECK-LABEL: @_Z5test2v()

// CHECK:   %[[CATCHSWITCH:.*]] = catchswitch within none [label %[[CATCHSTART_BB:.*]]] unwind to caller

// CHECK: [[CATCHSTART_BB]]:
// CHECK-NEXT:   %[[CATCHPAD:.*]] = catchpad within %[[CATCHSWITCH]] [ptr @_ZTIi, ptr null]
// CHECK:   br i1 %{{.*}}, label %[[CATCH_INT_BB:.*]], label %[[CATCH_ALL_BB:.*]]

// CHECK: [[CATCH_INT_BB]]:
// CHECK:   catchret from %[[CATCHPAD]] to label

// CHECK: [[CATCH_ALL_BB]]:
// CHECK:   catchret from %[[CATCHPAD]] to label

// Cleanup
void test3() {
  Cleanup c;
  may_throw();
}

// CHECK-LABEL: @_Z5test3v()

// CHECK:   invoke void @_Z9may_throwv()
// CHECK-NEXT:           to label {{.*}} unwind label %[[EHCLEANUP_BB:.*]]

// CHECK: [[EHCLEANUP_BB]]:
// CHECK-NEXT:   %[[CLEANUPPAD:.*]] = cleanuppad within none []
// CHECK-NEXT:   call noundef ptr @_ZN7CleanupD1Ev(ptr {{[^,]*}} %{{.*}}) {{.*}} [ "funclet"(token %[[CLEANUPPAD]]) ]
// CHECK-NEXT:   cleanupret from %[[CLEANUPPAD]] unwind to caller

// Possibly throwing function call within a catch
void test4() {
  try {
    may_throw();
  } catch (int) {
    may_throw();
  }
}

// CHECK-LABEL: @_Z5test4v()

// CHECK:   %[[CATCHSWITCH]] = catchswitch within none [label %[[CATCHSTART_BB]]] unwind to caller

// CHECK: [[CATCHSTART_BB]]:
// CHECK:   %[[CATCHPAD:.*]] = catchpad within %[[CATCHSWITCH]] [ptr @_ZTIi]

// CHECK:   invoke void @_Z9may_throwv() [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:           to label %[[INVOKE_CONT_BB:.*]] unwind label %[[EHCLEANUP_BB:.*]]

// CHECK: [[INVOKE_CONT_BB]]:
// CHECK-NEXT:   call void @__cxa_end_catch() {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:   catchret from %[[CATCHPAD]] to label

// CHECK: [[EHCLEANUP_BB]]:
// CHECK-NEXT:   %[[CLEANUPPAD:.*]] = cleanuppad within %[[CATCHPAD]] []
// CHECK-NEXT:   call void @__cxa_end_catch() {{.*}} [ "funclet"(token %[[CLEANUPPAD]]) ]
// CHECK-NEXT:   cleanupret from %[[CLEANUPPAD]] unwind to caller

// Possibly throwing function call within a catch-all
void test5() {
  try {
    may_throw();
  } catch (...) {
    may_throw();
  }
}

// CHECK-LABEL: @_Z5test5v()

// CHECK:   %[[CATCHSWITCH:.*]] = catchswitch within none [label %[[CATCHSTART_BB]]] unwind to caller

// CHECK: [[CATCHSTART_BB]]:
// CHECK:   %[[CATCHPAD:.*]] = catchpad within %[[CATCHSWITCH]] [ptr null]

// CHECK:   invoke void @_Z9may_throwv() [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:           to label %[[INVOKE_CONT_BB0:.*]] unwind label %[[EHCLEANUP_BB:.*]]

// CHECK: [[INVOKE_CONT_BB0]]:
// CHECK-NEXT:   call void @__cxa_end_catch() [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:   catchret from %[[CATCHPAD]] to label

// CHECK: [[EHCLEANUP_BB]]:
// CHECK-NEXT:   %[[CLEANUPPAD0:.*]] = cleanuppad within %[[CATCHPAD]] []
// CHECK-NEXT:   invoke void @__cxa_end_catch() [ "funclet"(token %[[CLEANUPPAD0]]) ]
// CHECK-NEXT:           to label %[[INVOKE_CONT_BB1:.*]] unwind label %[[TERMINATE_BB:.*]]

// CHECK: [[INVOKE_CONT_BB1]]:
// CHECK-NEXT:   cleanupret from %[[CLEANUPPAD0]] unwind to caller

// CHECK: [[TERMINATE_BB]]:
// CHECK-NEXT:   %[[CLEANUPPAD1:.*]] = cleanuppad within %[[CLEANUPPAD0]] []
// CHECK-NEXT:   call void @_ZSt9terminatev() {{.*}} [ "funclet"(token %[[CLEANUPPAD1]]) ]
// CHECK-NEXT:   unreachable

// Try-catch with cleanups
void test6() {
  Cleanup c1;
  try {
    Cleanup c2;
    may_throw();
  } catch (int) {
    Cleanup c3;
    may_throw();
  }
}

// CHECK-LABEL: @_Z5test6v()
// CHECK:   invoke void @_Z9may_throwv()
// CHECK-NEXT:           to label %{{.*}} unwind label %[[EHCLEANUP_BB0:.*]]

// CHECK: [[EHCLEANUP_BB0]]:
// CHECK-NEXT:   %[[CLEANUPPAD0:.*]] = cleanuppad within none []
// CHECK-NEXT:   call noundef ptr @_ZN7CleanupD1Ev(ptr {{.*}}) {{.*}} [ "funclet"(token %[[CLEANUPPAD0]]) ]
// CHECK-NEXT:   cleanupret from %[[CLEANUPPAD0]] unwind label %[[CATCH_DISPATCH_BB:.*]]

// CHECK: [[CATCH_DISPATCH_BB]]:
// CHECK-NEXT:  %[[CATCHSWITCH:.*]] = catchswitch within none [label %[[CATCHSTART_BB:.*]]] unwind label %[[EHCLEANUP_BB1:.*]]

// CHECK: [[CATCHSTART_BB]]:
// CHECK-NEXT:   %[[CATCHPAD:.*]] = catchpad within %[[CATCHSWITCH]] [ptr @_ZTIi]
// CHECK:   br i1 %{{.*}}, label %[[CATCH_INT_BB:.*]], label %[[RETHROW_BB:.*]]

// CHECK: [[CATCH_INT_BB]]:
// CHECK:   invoke void @_Z9may_throwv() [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:           to label %[[INVOKE_CONT_BB:.*]] unwind label %[[EHCLEANUP_BB2:.*]]

// CHECK: [[INVOKE_CONT_BB]]:
// CHECK:   catchret from %[[CATCHPAD]] to label %{{.*}}

// CHECK: [[RETHROW_BB]]:
// CHECK-NEXT:   invoke void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
// CHECK-NEXT:          to label %[[UNREACHABLE_BB:.*]] unwind label %[[EHCLEANUP_BB1:.*]]

// CHECK: [[EHCLEANUP_BB2]]:
// CHECK-NEXT:   %[[CLEANUPPAD2:.*]] = cleanuppad within %[[CATCHPAD]] []
// CHECK-NEXT:   call noundef ptr @_ZN7CleanupD1Ev(ptr {{[^,]*}} %{{.*}}) {{.*}} [ "funclet"(token %[[CLEANUPPAD2]]) ]
// CHECK-NEXT:   cleanupret from %[[CLEANUPPAD2]] unwind label %[[EHCLEANUP_BB3:.*]]

// CHECK: [[EHCLEANUP_BB3]]:
// CHECK-NEXT:   %[[CLEANUPPAD3:.*]] = cleanuppad within %[[CATCHPAD]] []
// CHECK:   cleanupret from %[[CLEANUPPAD3]] unwind label %[[EHCLEANUP_BB1:.*]]

// CHECK: [[EHCLEANUP_BB1]]:
// CHECK-NEXT:   %[[CLEANUPPAD1:.*]] = cleanuppad within none []
// CHECK-NEXT:   call noundef ptr @_ZN7CleanupD1Ev(ptr {{[^,]*}} %{{.*}}) {{.*}} [ "funclet"(token %[[CLEANUPPAD1]]) ]
// CHECK-NEXT:   cleanupret from %[[CLEANUPPAD1]] unwind to caller

// CHECK: [[UNREACHABLE_BB]]:
// CHECK-NEXT:   unreachable

// Nested try-catches within a try with cleanups
void test7() {
  Cleanup c1;
  may_throw();
  try {
    Cleanup c2;
    may_throw();
    try {
      Cleanup c3;
      may_throw();
    } catch (int) {
      may_throw();
    } catch (double) {
      may_throw();
    }
  } catch (int) {
    may_throw();
  } catch (...) {
    may_throw();
  }
}

// CHECK-LABEL: @_Z5test7v()
// CHECK:   invoke void @_Z9may_throwv()

// CHECK:   invoke void @_Z9may_throwv()

// CHECK:   invoke void @_Z9may_throwv()

// CHECK:   %[[CLEANUPPAD0:.*]] = cleanuppad within none []
// CHECK:   cleanupret from %[[CLEANUPPAD0]] unwind label

// CHECK:   %[[CATCHSWITCH0:.*]] = catchswitch within none

// CHECK:   %[[CATCHPAD0:.*]] = catchpad within %[[CATCHSWITCH0]] [ptr @_ZTIi, ptr @_ZTId]

// CHECK:   invoke void @_Z9may_throwv() [ "funclet"(token %[[CATCHPAD0]]) ]

// CHECK:   catchret from %[[CATCHPAD0]] to label

// CHECK:   invoke void @_Z9may_throwv() [ "funclet"(token %[[CATCHPAD0]]) ]

// CHECK:   catchret from %[[CATCHPAD0]] to label

// CHECK:   invoke void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD0]]) ]

// CHECK:   %[[CLEANUPPAD1:.*]] = cleanuppad within %[[CATCHPAD0]] []
// CHECK:   cleanupret from %[[CLEANUPPAD1]] unwind label

// CHECK:   %[[CLEANUPPAD2:.*]] = cleanuppad within %[[CATCHPAD0]] []
// CHECK:   cleanupret from %[[CLEANUPPAD2]] unwind label

// CHECK:   %[[CLEANUPPAD3:.*]] = cleanuppad within none []
// CHECK:   cleanupret from %[[CLEANUPPAD3]] unwind label

// CHECK:   %[[CATCHSWITCH1:.*]] = catchswitch within none

// CHECK:   %[[CATCHPAD1:.*]] = catchpad within %[[CATCHSWITCH1]] [ptr @_ZTIi, ptr null]

// CHECK:   invoke void @_Z9may_throwv() [ "funclet"(token %[[CATCHPAD1]]) ]

// CHECK:   catchret from %[[CATCHPAD1]] to label

// CHECK:   invoke void @_Z9may_throwv() [ "funclet"(token %[[CATCHPAD1]]) ]

// CHECK:   invoke void @__cxa_end_catch() [ "funclet"(token %[[CATCHPAD1]]) ]

// CHECK:   catchret from %[[CATCHPAD1]] to label

// CHECK:   %[[CLEANUPPAD4:.*]] = cleanuppad within %[[CATCHPAD1]] []
// CHECK:   invoke void @__cxa_end_catch() [ "funclet"(token %[[CLEANUPPAD4]]) ]

// CHECK:   cleanupret from %[[CLEANUPPAD4]] unwind label

// CHECK:   %[[CLEANUPPAD5:.*]] = cleanuppad within %[[CATCHPAD1]] []
// CHECK:   cleanupret from %[[CLEANUPPAD5]] unwind label

// CHECK:   %[[CLEANUPPAD6:.*]] = cleanuppad within none []
// CHECK:   cleanupret from %[[CLEANUPPAD6]] unwind to caller

// CHECK:   unreachable

// CHECK:   %[[CLEANUPPAD7:.*]] = cleanuppad within %[[CLEANUPPAD4]] []
// CHECK:   call void @_ZSt9terminatev() {{.*}} [ "funclet"(token %[[CLEANUPPAD7]]) ]
// CHECK:   unreachable

// Nested try-catches within a catch
void test8() {
  try {
    may_throw();
  } catch (int) {
    try {
      may_throw();
    } catch (int) {
      may_throw();
    }
  }
}

// CHECK-LABEL: @_Z5test8v()
// CHECK:   invoke void @_Z9may_throwv()

// CHECK:   %[[CATCHSWITCH0:.*]] = catchswitch within none

// CHECK:   %[[CATCHPAD0:.*]] = catchpad within %[[CATCHSWITCH0]] [ptr @_ZTIi]

// CHECK:   invoke void @_Z9may_throwv() [ "funclet"(token %[[CATCHPAD0]]) ]

// CHECK:   %[[CATCHSWITCH1:.*]] = catchswitch within %[[CATCHPAD0]]

// CHECK:   %[[CATCHPAD1:.*]] = catchpad within %[[CATCHSWITCH1]] [ptr @_ZTIi]

// CHECK:   invoke void @_Z9may_throwv() [ "funclet"(token %[[CATCHPAD1]]) ]

// CHECK:   catchret from %[[CATCHPAD1]] to label

// CHECK:   invoke void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD1]]) ]

// CHECK:   catchret from %[[CATCHPAD0]] to label

// CHECK:   call void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD0]]) ]
// CHECK:   unreachable

// CHECK:   %[[CLEANUPPAD0:.*]] = cleanuppad within %[[CATCHPAD1]] []
// CHECK:   cleanupret from %[[CLEANUPPAD0]] unwind label

// CHECK:   %[[CLEANUPPAD1:.*]] = cleanuppad within %[[CATCHPAD0]] []
// CHECK:   cleanupret from %[[CLEANUPPAD1]] unwind to caller

// CHECK:   unreachable

// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-DEFAULT
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -Wwasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-ON
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -Wno-wasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-OFF
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fexceptions -fcxx-exceptions -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=EM-EH-WARNING

// Wasm EH ignores dynamic exception specifications with types at the moment.
// This is controlled by -Wwasm-exception-spec, which is on by default. This
// warning can be suppressed with -Wno-wasm-exception-spec. Checks if a warning
// message is correctly printed or not printed depending on the options.
void test9() throw(int) {
}
// WARNING-DEFAULT: warning: dynamic exception specifications with types are currently ignored in wasm
// WARNING-ON: warning: dynamic exception specifications with types are currently ignored in wasm
// WARNING-OFF-NOT: warning: dynamic exception specifications with types are currently ignored in wasm
// EM-EH-WARNING: warning: dynamic exception specifications with types are currently ignored in wasm

// Wasm curremtly treats 'throw()' in the same way as 'noexept'. Check if the
// same warning message is printed as if when a 'noexcept' function throws.
void test10() throw() {
  throw 3;
}
// WARNING-DEFAULT: warning: 'test10' has a non-throwing exception specification but can still throw
// WARNING-DEFAULT: function declared non-throwing here

// Here we only check if the command enables wasm exception handling in the
// backend so that exception handling instructions can be generated in .s file.

// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -S -o - -std=c++11 | FileCheck %s --check-prefix=ASSEMBLY

// ASSEMBLY: try
// ASSEMBLY: catch
// ASSEMBLY: rethrow
// ASSEMBLY: end_try