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