• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===---------------------- catch_function_03.cpp -------------------------===//
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: no-exceptions, libcxxabi-no-noexcept-function-type
11 
12 #include <cassert>
13 
f()14 template<bool Noexcept> void f() noexcept(Noexcept) {}
15 template<bool Noexcept> using FnType = void() noexcept(Noexcept);
16 
17 template<bool ThrowNoexcept, bool CatchNoexcept>
check()18 void check()
19 {
20     try
21     {
22         auto *p = f<ThrowNoexcept>;
23         throw p;
24         assert(false);
25     }
26     catch (FnType<CatchNoexcept> *p)
27     {
28         assert(ThrowNoexcept || !CatchNoexcept);
29         assert(p == &f<ThrowNoexcept>);
30     }
31     catch (...)
32     {
33         assert(!ThrowNoexcept && CatchNoexcept);
34     }
35 }
36 
check_deep()37 void check_deep() {
38     auto *p = f<true>;
39     try
40     {
41         throw &p;
42     }
43     catch (FnType<false> **q)
44     {
45         assert(false);
46     }
47     catch (FnType<true> **q)
48     {
49     }
50     catch (...)
51     {
52         assert(false);
53     }
54 }
55 
main(int,char **)56 int main(int, char**)
57 {
58     check<false, false>();
59     check<false, true>();
60     check<true, false>();
61     check<true, true>();
62     check_deep();
63 
64     return 0;
65 }
66