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 | // UNSUPPORTED: c++03 |
10 | // UNSUPPORTED: no-exceptions |
11 | |
12 | #include <cassert> |
13 | #include <cstddef> |
14 | #include <cstdlib> |
15 | #include <type_traits> |
16 | |
17 | struct A {}; |
18 | |
19 | template<typename T, bool CanCatchNullptr> |
20 | static void catch_nullptr_test() { |
21 | try { |
22 | throw nullptr; |
23 | } catch (T &p) { |
24 | assert(CanCatchNullptr && !static_cast<bool>(p)); |
25 | } catch (...) { |
26 | assert(!CanCatchNullptr); |
27 | } |
28 | } |
29 | |
30 | int main(int, char**) |
31 | { |
32 | static_assert(std::is_same<std::nullptr_t, decltype(nullptr)>::value, "" ); |
33 | |
34 | // A reference to nullptr_t can catch nullptr. |
35 | catch_nullptr_test<std::nullptr_t, true>(); |
36 | catch_nullptr_test<const std::nullptr_t, true>(); |
37 | catch_nullptr_test<volatile std::nullptr_t, true>(); |
38 | catch_nullptr_test<const volatile std::nullptr_t, true>(); |
39 | |
40 | // No other reference type can. |
41 | #if 0 |
42 | // FIXME: These tests fail, because the ABI provides no way for us to |
43 | // distinguish this from catching by value. |
44 | catch_nullptr_test<void *, false>(); |
45 | catch_nullptr_test<void * const, false>(); |
46 | catch_nullptr_test<int *, false>(); |
47 | catch_nullptr_test<A *, false>(); |
48 | catch_nullptr_test<int A::*, false>(); |
49 | catch_nullptr_test<int (A::*)(), false>(); |
50 | #endif |
51 | |
52 | return 0; |
53 | } |
54 | |