1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // Can a noexcept function pointer be caught by a non-noexcept catch clause?
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-exceptions
12
13 // Support for catching a function pointer including noexcept was shipped in macOS 10.13
14 // XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx10.{{9|10|11|12}}
15
16 #include <cassert>
17
f()18 template<bool Noexcept> void f() noexcept(Noexcept) {}
19 template<bool Noexcept> using FnType = void() noexcept(Noexcept);
20
21 template<bool ThrowNoexcept, bool CatchNoexcept>
check()22 void check()
23 {
24 try
25 {
26 auto *p = f<ThrowNoexcept>;
27 throw p;
28 assert(false);
29 }
30 catch (FnType<CatchNoexcept> *p)
31 {
32 assert(ThrowNoexcept || !CatchNoexcept);
33 assert(p == &f<ThrowNoexcept>);
34 }
35 catch (...)
36 {
37 assert(!ThrowNoexcept && CatchNoexcept);
38 }
39 }
40
check_deep()41 void check_deep() {
42 auto *p = f<true>;
43 try
44 {
45 throw &p;
46 }
47 catch (FnType<false> **q)
48 {
49 assert(false);
50 }
51 catch (FnType<true> **q)
52 {
53 }
54 catch (...)
55 {
56 assert(false);
57 }
58 }
59
main(int,char **)60 int main(int, char**)
61 {
62 check<false, false>();
63 check<false, true>();
64 check<true, false>();
65 check<true, true>();
66 check_deep();
67
68 return 0;
69 }
70