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