Richard Smith | 80b64f0 | 2016-11-02 23:41:51 +0000 | [diff] [blame] | 1 | //===--------------- catch_member_function_pointer_02.cpp -----------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Richard Smith | 80b64f0 | 2016-11-02 23:41:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // Can a noexcept member function pointer be caught by a non-noexcept catch |
| 10 | // clause? |
Louis Dionne | 8c61114 | 2020-04-17 10:29:15 -0400 | [diff] [blame] | 11 | // UNSUPPORTED: no-exceptions, libcxxabi-no-noexcept-function-type |
Richard Smith | 80b64f0 | 2016-11-02 23:41:51 +0000 | [diff] [blame] | 12 | |
Eric Fiselier | 0af5356 | 2017-05-09 00:11:02 +0000 | [diff] [blame] | 13 | // GCC 7 and 8 support noexcept function types but this test still fails. |
| 14 | // This is likely a bug in their implementation. Investigation needed. |
Eric Fiselier | 75c9eb5 | 2019-09-13 18:43:29 +0000 | [diff] [blame] | 15 | // XFAIL: gcc-7, gcc-8, gcc-9, gcc-10 |
Eric Fiselier | 0af5356 | 2017-05-09 00:11:02 +0000 | [diff] [blame] | 16 | |
Richard Smith | 80b64f0 | 2016-11-02 23:41:51 +0000 | [diff] [blame] | 17 | #include <cassert> |
| 18 | |
| 19 | struct X { |
| 20 | template<bool Noexcept> void f() noexcept(Noexcept) {} |
| 21 | }; |
| 22 | template<bool Noexcept> using FnType = void (X::*)() noexcept(Noexcept); |
| 23 | |
| 24 | template<bool ThrowNoexcept, bool CatchNoexcept> |
| 25 | void check() |
| 26 | { |
| 27 | try |
| 28 | { |
| 29 | auto p = &X::f<ThrowNoexcept>; |
| 30 | throw p; |
| 31 | assert(false); |
| 32 | } |
| 33 | catch (FnType<CatchNoexcept> p) |
| 34 | { |
| 35 | assert(ThrowNoexcept || !CatchNoexcept); |
| 36 | assert(p == &X::f<ThrowNoexcept>); |
| 37 | } |
| 38 | catch (...) |
| 39 | { |
| 40 | assert(!ThrowNoexcept && CatchNoexcept); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void check_deep() { |
| 45 | FnType<true> p = &X::f<true>; |
| 46 | try |
| 47 | { |
| 48 | throw &p; |
| 49 | } |
| 50 | catch (FnType<false> *q) |
| 51 | { |
| 52 | assert(false); |
| 53 | } |
| 54 | catch (FnType<true> *q) |
| 55 | { |
| 56 | } |
| 57 | catch (...) |
| 58 | { |
| 59 | assert(false); |
| 60 | } |
| 61 | } |
| 62 | |
Louis Dionne | 504bc07 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 63 | int main(int, char**) |
Richard Smith | 80b64f0 | 2016-11-02 23:41:51 +0000 | [diff] [blame] | 64 | { |
| 65 | check<false, false>(); |
| 66 | check<false, true>(); |
| 67 | check<true, false>(); |
| 68 | check<true, true>(); |
| 69 | check_deep(); |
Louis Dionne | 504bc07 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 70 | |
| 71 | return 0; |
Richard Smith | 80b64f0 | 2016-11-02 23:41:51 +0000 | [diff] [blame] | 72 | } |