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 // Catching an exception thrown as nullptr was not properly handled before
10 // 2f984cab4fa7, which landed in macOS 10.13
11 // XFAIL: with_system_cxx_lib=macosx10.12
12 // XFAIL: with_system_cxx_lib=macosx10.11
13 // XFAIL: with_system_cxx_lib=macosx10.10
14 // XFAIL: with_system_cxx_lib=macosx10.9
15 
16 // UNSUPPORTED: c++03
17 // UNSUPPORTED: no-exceptions
18 
19 #include <cassert>
20 #include <cstdlib>
21 
22 struct A {};
23 
24 void test1()
25 {
26     try
27     {
28         throw nullptr;
29         assert(false);
30     }
31     catch (int* p)
32     {
33         assert(!p);
34     }
35     catch (long*)
36     {
37         assert(false);
38     }
39 }
40 
41 void test2()
42 {
43     try
44     {
45         throw nullptr;
46         assert(false);
47     }
48     catch (A* p)
49     {
50         assert(!p);
51     }
52     catch (int*)
53     {
54         assert(false);
55     }
56 }
57 
58 template <class Catch>
59 void catch_nullptr_test() {
60   try {
61     throw nullptr;
62     assert(false);
63   } catch (Catch c) {
64     assert(!c);
65   } catch (...) {
66     assert(false);
67   }
68 }
69 
70 
71 int main(int, char**)
72 {
73   // catch naked nullptrs
74   test1();
75   test2();
76 
77   catch_nullptr_test<int*>();
78   catch_nullptr_test<int**>();
79   catch_nullptr_test<int A::*>();
80   catch_nullptr_test<const int A::*>();
81   catch_nullptr_test<int A::**>();
82 
83   return 0;
84 }
85