catch_pointer_reference.pass.cpp
23.3 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//===---------------------- catch_pointer_referece.cpp --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This test case checks specifically the cases under bullet 3.1 & 3.2:
//
// C++ ABI 15.3:
// A handler is a match for an exception object of type E if
// * The handler is of type cv T or cv T& and E and T are the same type
// (ignoring the top-level cv-qualifiers), or
// * the handler is of type cv T or cv T& and T is an unambiguous base
// class of E, or
// / * the handler is of type cv1 T* cv2 and E is a pointer type that can \
// | be converted to the type of the handler by either or both of |
// | o a standard pointer conversion (4.10 [conv.ptr]) not involving |
// | conversions to private or protected or ambiguous classes |
// \ o a qualification conversion /
// * the handler is a pointer or pointer to member type and E is
// std::nullptr_t
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: no-exceptions
#include <exception>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
struct Base {};
struct Derived : Base {};
struct Derived2 : Base {};
struct Ambiguous : Derived, Derived2 {};
struct Private : private Base {};
struct Protected : protected Base {};
template <typename T // Handler type
,typename E // Thrown exception type
,typename O // Object type
>
void assert_catches()
{
try
{
O o;
throw static_cast<E>(&o);
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "Statements after throw must be unreachable");
}
catch (T t)
{
assert(true);
return;
}
catch (...)
{
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "Should not have entered catch-all");
}
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "The catch should have returned");
}
template <typename T // Handler type
,typename E // Thrown exception type
,typename O // Object type
>
void assert_cannot_catch()
{
try
{
O o;
throw static_cast<E>(&o);
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "Statements after throw must be unreachable");
}
catch (T t)
{
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "Should not have entered the catch");
}
catch (...)
{
assert(true);
return;
}
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "The catch-all should have returned");
}
void f1()
{
// Test that every combination of handler of type:
// cv1 Base * cv2
// catches an exception of type:
// Derived *
assert_catches< Base * , Derived *, Derived>();
assert_catches<const Base * , Derived *, Derived>();
assert_catches< volatile Base * , Derived *, Derived>();
assert_catches<const volatile Base * , Derived *, Derived>();
assert_catches< Base * const , Derived *, Derived>();
assert_catches<const Base * const , Derived *, Derived>();
assert_catches< volatile Base * const , Derived *, Derived>();
assert_catches<const volatile Base * const , Derived *, Derived>();
assert_catches< Base * volatile, Derived *, Derived>();
assert_catches<const Base * volatile, Derived *, Derived>();
assert_catches< volatile Base * volatile, Derived *, Derived>();
assert_catches<const volatile Base * volatile, Derived *, Derived>();
assert_catches< Base * const volatile, Derived *, Derived>();
assert_catches<const Base * const volatile, Derived *, Derived>();
assert_catches< volatile Base * const volatile, Derived *, Derived>();
assert_catches<const volatile Base * const volatile, Derived *, Derived>();
}
void f2()
{
// Test that every combination of handler of type:
// cv1 Base * cv2
// catches an exception of type:
// Base *
assert_catches< Base * , Base *, Derived>();
assert_catches<const Base * , Base *, Derived>();
assert_catches< volatile Base * , Base *, Derived>();
assert_catches<const volatile Base * , Base *, Derived>();
assert_catches< Base * const , Base *, Derived>();
assert_catches<const Base * const , Base *, Derived>();
assert_catches< volatile Base * const , Base *, Derived>();
assert_catches<const volatile Base * const , Base *, Derived>();
assert_catches< Base * volatile, Base *, Derived>();
assert_catches<const Base * volatile, Base *, Derived>();
assert_catches< volatile Base * volatile, Base *, Derived>();
assert_catches<const volatile Base * volatile, Base *, Derived>();
assert_catches< Base * const volatile, Base *, Derived>();
assert_catches<const Base * const volatile, Base *, Derived>();
assert_catches< volatile Base * const volatile, Base *, Derived>();
assert_catches<const volatile Base * const volatile, Base *, Derived>();
}
void f3()
{
// Test that every combination of handler of type:
// cv1 Derived * cv2
// catches an exception of type:
// Derived *
assert_catches< Derived * , Derived *, Derived>();
assert_catches<const Derived * , Derived *, Derived>();
assert_catches< volatile Derived * , Derived *, Derived>();
assert_catches<const volatile Derived * , Derived *, Derived>();
assert_catches< Derived * const , Derived *, Derived>();
assert_catches<const Derived * const , Derived *, Derived>();
assert_catches< volatile Derived * const , Derived *, Derived>();
assert_catches<const volatile Derived * const , Derived *, Derived>();
assert_catches< Derived * volatile, Derived *, Derived>();
assert_catches<const Derived * volatile, Derived *, Derived>();
assert_catches< volatile Derived * volatile, Derived *, Derived>();
assert_catches<const volatile Derived * volatile, Derived *, Derived>();
assert_catches< Derived * const volatile, Derived *, Derived>();
assert_catches<const Derived * const volatile, Derived *, Derived>();
assert_catches< volatile Derived * const volatile, Derived *, Derived>();
assert_catches<const volatile Derived * const volatile, Derived *, Derived>();
}
void f4()
{
// Test that every combination of handler of type:
// cv1 Derived * cv2
// cannot catch an exception of type:
// Base *
assert_cannot_catch< Derived * , Base *, Derived>();
assert_cannot_catch<const Derived * , Base *, Derived>();
assert_cannot_catch< volatile Derived * , Base *, Derived>();
assert_cannot_catch<const volatile Derived * , Base *, Derived>();
assert_cannot_catch< Derived * const , Base *, Derived>();
assert_cannot_catch<const Derived * const , Base *, Derived>();
assert_cannot_catch< volatile Derived * const , Base *, Derived>();
assert_cannot_catch<const volatile Derived * const , Base *, Derived>();
assert_cannot_catch< Derived * volatile, Base *, Derived>();
assert_cannot_catch<const Derived * volatile, Base *, Derived>();
assert_cannot_catch< volatile Derived * volatile, Base *, Derived>();
assert_cannot_catch<const volatile Derived * volatile, Base *, Derived>();
assert_cannot_catch< Derived * const volatile, Base *, Derived>();
assert_cannot_catch<const Derived * const volatile, Base *, Derived>();
assert_cannot_catch< volatile Derived * const volatile, Base *, Derived>();
assert_cannot_catch<const volatile Derived * const volatile, Base *, Derived>();
}
void f5()
{
// Test that every combination of handler of type:
// cv1 Derived * cv2 &
// catches an exception of type:
// Derived *
assert_catches< Derived * &, Derived *, Derived>();
assert_catches<const Derived * &, Derived *, Derived>();
assert_catches< volatile Derived * &, Derived *, Derived>();
assert_catches<const volatile Derived * &, Derived *, Derived>();
assert_catches< Derived * const &, Derived *, Derived>();
assert_catches<const Derived * const &, Derived *, Derived>();
assert_catches< volatile Derived * const &, Derived *, Derived>();
assert_catches<const volatile Derived * const &, Derived *, Derived>();
assert_catches< Derived * volatile &, Derived *, Derived>();
assert_catches<const Derived * volatile &, Derived *, Derived>();
assert_catches< volatile Derived * volatile &, Derived *, Derived>();
assert_catches<const volatile Derived * volatile &, Derived *, Derived>();
assert_catches< Derived * const volatile &, Derived *, Derived>();
assert_catches<const Derived * const volatile &, Derived *, Derived>();
assert_catches< volatile Derived * const volatile &, Derived *, Derived>();
assert_catches<const volatile Derived * const volatile &, Derived *, Derived>();
}
void f6()
{
// Test that every combination of handler of type:
// cv1 Base * cv2 &
// catches an exception of type:
// Base *
assert_catches< Base * &, Base *, Derived>();
assert_catches<const Base * &, Base *, Derived>();
assert_catches< volatile Base * &, Base *, Derived>();
assert_catches<const volatile Base * &, Base *, Derived>();
assert_catches< Base * const &, Base *, Derived>();
assert_catches<const Base * const &, Base *, Derived>();
assert_catches< volatile Base * const &, Base *, Derived>();
assert_catches<const volatile Base * const &, Base *, Derived>();
assert_catches< Base * volatile &, Base *, Derived>();
assert_catches<const Base * volatile &, Base *, Derived>();
assert_catches< volatile Base * volatile &, Base *, Derived>();
assert_catches<const volatile Base * volatile &, Base *, Derived>();
assert_catches< Base * const volatile &, Base *, Derived>();
assert_catches<const Base * const volatile &, Base *, Derived>();
assert_catches< volatile Base * const volatile &, Base *, Derived>();
assert_catches<const volatile Base * const volatile &, Base *, Derived>();
}
void f7()
{
// Test that every combination of handler of type:
// cv1 Derived * cv2 &
// cannot catch an exception of type:
// Base *
assert_cannot_catch< Derived * &, Base *, Derived>();
assert_cannot_catch<const Derived * &, Base *, Derived>();
assert_cannot_catch< volatile Derived * &, Base *, Derived>();
assert_cannot_catch<const volatile Derived * &, Base *, Derived>();
assert_cannot_catch< Derived * const &, Base *, Derived>();
assert_cannot_catch<const Derived * const &, Base *, Derived>();
assert_cannot_catch< volatile Derived * const &, Base *, Derived>();
assert_cannot_catch<const volatile Derived * const &, Base *, Derived>();
assert_cannot_catch< Derived * volatile &, Base *, Derived>();
assert_cannot_catch<const Derived * volatile &, Base *, Derived>();
assert_cannot_catch< volatile Derived * volatile &, Base *, Derived>();
assert_cannot_catch<const volatile Derived * volatile &, Base *, Derived>();
assert_cannot_catch< Derived * const volatile &, Base *, Derived>();
assert_cannot_catch<const Derived * const volatile &, Base *, Derived>();
assert_cannot_catch< volatile Derived * const volatile &, Base *, Derived>();
assert_cannot_catch<const volatile Derived * const volatile &, Base *, Derived>();
}
void f8()
{
// This test case has a caveat noted in the discussion here:
// https://gcc.gnu.org/ml/gcc-patches/2009-08/msg00264.html
// Specifically:
// This [test exposes a] corner case of the ARM C++ ABI. The generic C++
// ABI also gets this wrong, because I failed to notice the subtlety here.
// The issue is that 15.3/3 3rd bullet says:
// The handler is of type cv1 T* cv2 and E is a pointer type that
// can be converted to the type of the handler by either or both of:
// * a standard pointer conversion (4.10) not involving conversions
// to pointers to private or protected or ambiguous classes
// Notice that the handlers of type "cv1 T*cv2&" are not allowed such
// freedom to find a base class. The ABI error is that we treat handlers
// of reference type exactly the same as the corresponding hander of
// non-reference type. Elsewhere in the exception handling this makes no
// difference (for instance bullet 1 explicitly says 'cv T or cv T&').
//
// See also: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#388
//
// TL;DR: it is an unresolved C++ ABI defect that these do catch
// Test that every combination of handler of type:
// cv1 Base * cv2 &
// catches an exception of type:
// Derived *
assert_catches< Base * &, Derived *, Derived>();
assert_catches<const Base * &, Derived *, Derived>();
assert_catches< volatile Base * &, Derived *, Derived>();
assert_catches<const volatile Base * &, Derived *, Derived>();
assert_catches< Base * const &, Derived *, Derived>();
assert_catches<const Base * const &, Derived *, Derived>();
assert_catches< volatile Base * const &, Derived *, Derived>();
assert_catches<const volatile Base * const &, Derived *, Derived>();
assert_catches< Base * volatile &, Derived *, Derived>();
assert_catches<const Base * volatile &, Derived *, Derived>();
assert_catches< volatile Base * volatile &, Derived *, Derived>();
assert_catches<const volatile Base * volatile &, Derived *, Derived>();
assert_catches< Base * const volatile &, Derived *, Derived>();
assert_catches<const Base * const volatile &, Derived *, Derived>();
assert_catches< volatile Base * const volatile &, Derived *, Derived>();
assert_catches<const volatile Base * const volatile &, Derived *, Derived>();
}
void f9()
{
// Test that every combination of handler of type:
// cv1 Base * cv2
// cannot catch an exception of type:
// Ambiguous *
assert_cannot_catch< Base * , Ambiguous *, Ambiguous>();
assert_cannot_catch<const Base * , Ambiguous *, Ambiguous>();
assert_cannot_catch< volatile Base * , Ambiguous *, Ambiguous>();
assert_cannot_catch<const volatile Base * , Ambiguous *, Ambiguous>();
assert_cannot_catch< Base * const , Ambiguous *, Ambiguous>();
assert_cannot_catch<const Base * const , Ambiguous *, Ambiguous>();
assert_cannot_catch< volatile Base * const , Ambiguous *, Ambiguous>();
assert_cannot_catch<const volatile Base * const , Ambiguous *, Ambiguous>();
assert_cannot_catch< Base * volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch<const Base * volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch< volatile Base * volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch<const volatile Base * volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch< Base * const volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch<const Base * const volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch< volatile Base * const volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch<const volatile Base * const volatile, Ambiguous *, Ambiguous>();
}
void f10()
{
// Test that every combination of handler of type:
// cv1 Base * cv2
// cannot catch an exception of type:
// Private *
assert_cannot_catch< Base * , Private *, Private>();
assert_cannot_catch<const Base * , Private *, Private>();
assert_cannot_catch< volatile Base * , Private *, Private>();
assert_cannot_catch<const volatile Base * , Private *, Private>();
assert_cannot_catch< Base * const , Private *, Private>();
assert_cannot_catch<const Base * const , Private *, Private>();
assert_cannot_catch< volatile Base * const , Private *, Private>();
assert_cannot_catch<const volatile Base * const , Private *, Private>();
assert_cannot_catch< Base * volatile, Private *, Private>();
assert_cannot_catch<const Base * volatile, Private *, Private>();
assert_cannot_catch< volatile Base * volatile, Private *, Private>();
assert_cannot_catch<const volatile Base * volatile, Private *, Private>();
assert_cannot_catch< Base * const volatile, Private *, Private>();
assert_cannot_catch<const Base * const volatile, Private *, Private>();
assert_cannot_catch< volatile Base * const volatile, Private *, Private>();
assert_cannot_catch<const volatile Base * const volatile, Private *, Private>();
}
void f11()
{
// Test that every combination of handler of type:
// cv1 Base * cv2
// cannot catch an exception of type:
// Protected *
assert_cannot_catch< Base * , Protected *, Protected>();
assert_cannot_catch<const Base * , Protected *, Protected>();
assert_cannot_catch< volatile Base * , Protected *, Protected>();
assert_cannot_catch<const volatile Base * , Protected *, Protected>();
assert_cannot_catch< Base * const , Protected *, Protected>();
assert_cannot_catch<const Base * const , Protected *, Protected>();
assert_cannot_catch< volatile Base * const , Protected *, Protected>();
assert_cannot_catch<const volatile Base * const , Protected *, Protected>();
assert_cannot_catch< Base * volatile, Protected *, Protected>();
assert_cannot_catch<const Base * volatile, Protected *, Protected>();
assert_cannot_catch< volatile Base * volatile, Protected *, Protected>();
assert_cannot_catch<const volatile Base * volatile, Protected *, Protected>();
assert_cannot_catch< Base * const volatile, Protected *, Protected>();
assert_cannot_catch<const Base * const volatile, Protected *, Protected>();
assert_cannot_catch< volatile Base * const volatile, Protected *, Protected>();
assert_cannot_catch<const volatile Base * const volatile, Protected *, Protected>();
}
void f12()
{
// Test that every combination of handler of type:
// cv1 Base * cv2 &
// cannot catch an exception of type:
// Private *
assert_cannot_catch< Base * &, Private *, Private>();
assert_cannot_catch<const Base * &, Private *, Private>();
assert_cannot_catch< volatile Base * &, Private *, Private>();
assert_cannot_catch<const volatile Base * &, Private *, Private>();
assert_cannot_catch< Base * const &, Private *, Private>();
assert_cannot_catch<const Base * const &, Private *, Private>();
assert_cannot_catch< volatile Base * const &, Private *, Private>();
assert_cannot_catch<const volatile Base * const &, Private *, Private>();
assert_cannot_catch< Base * volatile &, Private *, Private>();
assert_cannot_catch<const Base * volatile &, Private *, Private>();
assert_cannot_catch< volatile Base * volatile &, Private *, Private>();
assert_cannot_catch<const volatile Base * volatile &, Private *, Private>();
assert_cannot_catch< Base * const volatile &, Private *, Private>();
assert_cannot_catch<const Base * const volatile &, Private *, Private>();
assert_cannot_catch< volatile Base * const volatile &, Private *, Private>();
assert_cannot_catch<const volatile Base * const volatile &, Private *, Private>();
}
void f13()
{
// Test that every combination of handler of type:
// cv1 Base * cv2 &
// cannot catch an exception of type:
// Protected *
assert_cannot_catch< Base * &, Protected *, Protected>();
assert_cannot_catch<const Base * &, Protected *, Protected>();
assert_cannot_catch< volatile Base * &, Protected *, Protected>();
assert_cannot_catch<const volatile Base * &, Protected *, Protected>();
assert_cannot_catch< Base * const &, Protected *, Protected>();
assert_cannot_catch<const Base * const &, Protected *, Protected>();
assert_cannot_catch< volatile Base * const &, Protected *, Protected>();
assert_cannot_catch<const volatile Base * const &, Protected *, Protected>();
assert_cannot_catch< Base * volatile &, Protected *, Protected>();
assert_cannot_catch<const Base * volatile &, Protected *, Protected>();
assert_cannot_catch< volatile Base * volatile &, Protected *, Protected>();
assert_cannot_catch<const volatile Base * volatile &, Protected *, Protected>();
assert_cannot_catch< Base * const volatile &, Protected *, Protected>();
assert_cannot_catch<const Base * const volatile &, Protected *, Protected>();
assert_cannot_catch< volatile Base * const volatile &, Protected *, Protected>();
assert_cannot_catch<const volatile Base * const volatile &, Protected *, Protected>();
}
int main()
{
f1();
f2();
f3();
f4();
f5();
f6();
f7();
f8();
f9();
f10();
f11();
f12();
f13();
}