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