xref: /aosp_15_r20/external/libcxxabi/test/catch_function_01.pass.cpp (revision c05d8e5dc3e10f6ce4317e8bc22cc4a25f55fa94)
1*c05d8e5dSAndroid Build Coastguard Worker //===----------------------- catch_function_01.cpp ------------------------===//
2*c05d8e5dSAndroid Build Coastguard Worker //
3*c05d8e5dSAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*c05d8e5dSAndroid Build Coastguard Worker //
5*c05d8e5dSAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*c05d8e5dSAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*c05d8e5dSAndroid Build Coastguard Worker //
8*c05d8e5dSAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*c05d8e5dSAndroid Build Coastguard Worker 
10*c05d8e5dSAndroid Build Coastguard Worker // Can you have a catch clause of array type that catches anything?
11*c05d8e5dSAndroid Build Coastguard Worker 
12*c05d8e5dSAndroid Build Coastguard Worker // GCC incorrectly allows function pointer to be caught by reference.
13*c05d8e5dSAndroid Build Coastguard Worker // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69372
14*c05d8e5dSAndroid Build Coastguard Worker // XFAIL: gcc
15*c05d8e5dSAndroid Build Coastguard Worker // UNSUPPORTED: libcxxabi-no-exceptions
16*c05d8e5dSAndroid Build Coastguard Worker 
17*c05d8e5dSAndroid Build Coastguard Worker #include <cassert>
18*c05d8e5dSAndroid Build Coastguard Worker 
19*c05d8e5dSAndroid Build Coastguard Worker template <class Tp>
can_convert(Tp)20*c05d8e5dSAndroid Build Coastguard Worker bool can_convert(Tp) { return true; }
21*c05d8e5dSAndroid Build Coastguard Worker 
22*c05d8e5dSAndroid Build Coastguard Worker template <class>
can_convert(...)23*c05d8e5dSAndroid Build Coastguard Worker bool can_convert(...) { return false; }
24*c05d8e5dSAndroid Build Coastguard Worker 
f()25*c05d8e5dSAndroid Build Coastguard Worker void f() {}
26*c05d8e5dSAndroid Build Coastguard Worker 
main()27*c05d8e5dSAndroid Build Coastguard Worker int main()
28*c05d8e5dSAndroid Build Coastguard Worker {
29*c05d8e5dSAndroid Build Coastguard Worker     typedef void Function();
30*c05d8e5dSAndroid Build Coastguard Worker     assert(!can_convert<Function&>(&f));
31*c05d8e5dSAndroid Build Coastguard Worker     assert(!can_convert<void*>(&f));
32*c05d8e5dSAndroid Build Coastguard Worker     try
33*c05d8e5dSAndroid Build Coastguard Worker     {
34*c05d8e5dSAndroid Build Coastguard Worker         throw f;     // converts to void (*)()
35*c05d8e5dSAndroid Build Coastguard Worker         assert(false);
36*c05d8e5dSAndroid Build Coastguard Worker     }
37*c05d8e5dSAndroid Build Coastguard Worker     catch (Function& b)  // can't catch void (*)()
38*c05d8e5dSAndroid Build Coastguard Worker     {
39*c05d8e5dSAndroid Build Coastguard Worker         assert(false);
40*c05d8e5dSAndroid Build Coastguard Worker     }
41*c05d8e5dSAndroid Build Coastguard Worker     catch (void*) // can't catch as void*
42*c05d8e5dSAndroid Build Coastguard Worker     {
43*c05d8e5dSAndroid Build Coastguard Worker         assert(false);
44*c05d8e5dSAndroid Build Coastguard Worker     }
45*c05d8e5dSAndroid Build Coastguard Worker     catch(Function*)
46*c05d8e5dSAndroid Build Coastguard Worker     {
47*c05d8e5dSAndroid Build Coastguard Worker     }
48*c05d8e5dSAndroid Build Coastguard Worker     catch (...)
49*c05d8e5dSAndroid Build Coastguard Worker     {
50*c05d8e5dSAndroid Build Coastguard Worker         assert(false);
51*c05d8e5dSAndroid Build Coastguard Worker     }
52*c05d8e5dSAndroid Build Coastguard Worker }
53