xref: /aosp_15_r20/external/libcxxabi/test/catch_function_03.pass.cpp (revision c05d8e5dc3e10f6ce4317e8bc22cc4a25f55fa94)
1*c05d8e5dSAndroid Build Coastguard Worker //===---------------------- catch_function_03.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 a noexcept function pointer be caught by a non-noexcept catch clause?
11*c05d8e5dSAndroid Build Coastguard Worker // UNSUPPORTED: libcxxabi-no-exceptions, libcxxabi-no-noexcept-function-type
12*c05d8e5dSAndroid Build Coastguard Worker 
13*c05d8e5dSAndroid Build Coastguard Worker #include <cassert>
14*c05d8e5dSAndroid Build Coastguard Worker 
f()15*c05d8e5dSAndroid Build Coastguard Worker template<bool Noexcept> void f() noexcept(Noexcept) {}
16*c05d8e5dSAndroid Build Coastguard Worker template<bool Noexcept> using FnType = void() noexcept(Noexcept);
17*c05d8e5dSAndroid Build Coastguard Worker 
18*c05d8e5dSAndroid Build Coastguard Worker template<bool ThrowNoexcept, bool CatchNoexcept>
check()19*c05d8e5dSAndroid Build Coastguard Worker void check()
20*c05d8e5dSAndroid Build Coastguard Worker {
21*c05d8e5dSAndroid Build Coastguard Worker     try
22*c05d8e5dSAndroid Build Coastguard Worker     {
23*c05d8e5dSAndroid Build Coastguard Worker         auto *p = f<ThrowNoexcept>;
24*c05d8e5dSAndroid Build Coastguard Worker         throw p;
25*c05d8e5dSAndroid Build Coastguard Worker         assert(false);
26*c05d8e5dSAndroid Build Coastguard Worker     }
27*c05d8e5dSAndroid Build Coastguard Worker     catch (FnType<CatchNoexcept> *p)
28*c05d8e5dSAndroid Build Coastguard Worker     {
29*c05d8e5dSAndroid Build Coastguard Worker         assert(ThrowNoexcept || !CatchNoexcept);
30*c05d8e5dSAndroid Build Coastguard Worker         assert(p == &f<ThrowNoexcept>);
31*c05d8e5dSAndroid Build Coastguard Worker     }
32*c05d8e5dSAndroid Build Coastguard Worker     catch (...)
33*c05d8e5dSAndroid Build Coastguard Worker     {
34*c05d8e5dSAndroid Build Coastguard Worker         assert(!ThrowNoexcept && CatchNoexcept);
35*c05d8e5dSAndroid Build Coastguard Worker     }
36*c05d8e5dSAndroid Build Coastguard Worker }
37*c05d8e5dSAndroid Build Coastguard Worker 
check_deep()38*c05d8e5dSAndroid Build Coastguard Worker void check_deep() {
39*c05d8e5dSAndroid Build Coastguard Worker     auto *p = f<true>;
40*c05d8e5dSAndroid Build Coastguard Worker     try
41*c05d8e5dSAndroid Build Coastguard Worker     {
42*c05d8e5dSAndroid Build Coastguard Worker         throw &p;
43*c05d8e5dSAndroid Build Coastguard Worker     }
44*c05d8e5dSAndroid Build Coastguard Worker     catch (FnType<false> **q)
45*c05d8e5dSAndroid Build Coastguard Worker     {
46*c05d8e5dSAndroid Build Coastguard Worker         assert(false);
47*c05d8e5dSAndroid Build Coastguard Worker     }
48*c05d8e5dSAndroid Build Coastguard Worker     catch (FnType<true> **q)
49*c05d8e5dSAndroid Build Coastguard Worker     {
50*c05d8e5dSAndroid Build Coastguard Worker     }
51*c05d8e5dSAndroid Build Coastguard Worker     catch (...)
52*c05d8e5dSAndroid Build Coastguard Worker     {
53*c05d8e5dSAndroid Build Coastguard Worker         assert(false);
54*c05d8e5dSAndroid Build Coastguard Worker     }
55*c05d8e5dSAndroid Build Coastguard Worker }
56*c05d8e5dSAndroid Build Coastguard Worker 
main()57*c05d8e5dSAndroid Build Coastguard Worker int main()
58*c05d8e5dSAndroid Build Coastguard Worker {
59*c05d8e5dSAndroid Build Coastguard Worker     check<false, false>();
60*c05d8e5dSAndroid Build Coastguard Worker     check<false, true>();
61*c05d8e5dSAndroid Build Coastguard Worker     check<true, false>();
62*c05d8e5dSAndroid Build Coastguard Worker     check<true, true>();
63*c05d8e5dSAndroid Build Coastguard Worker     check_deep();
64*c05d8e5dSAndroid Build Coastguard Worker }
65