/ test / catch_const_pointer_nullptr.pass.cpp
catch_const_pointer_nullptr.pass.cpp
  1  //===--------------------- catch_const_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: no-exceptions
 10  
 11  #include <cassert>
 12  
 13  // Clang emits  warnings about exceptions of type 'Child' being caught by
 14  // an earlier handler of type 'Base'. Congrats clang, you've just
 15  // diagnosed the behavior under test.
 16  #if defined(__clang__)
 17  #pragma clang diagnostic ignored "-Wexceptions"
 18  #endif
 19  
 20  #if __has_feature(cxx_nullptr)
 21  
 22  struct A {};
 23  
 24  void test1()
 25  {
 26      try
 27      {
 28          throw nullptr;
 29          assert(false);
 30      }
 31      catch (A* p)
 32      {
 33          assert(!p);
 34      }
 35      catch (const A*)
 36      {
 37          assert(false);
 38      }
 39  }
 40  
 41  
 42  void test2()
 43  {
 44      try
 45      {
 46          throw nullptr;
 47          assert(false);
 48      }
 49      catch (const A* p)
 50      {
 51          assert(!p);
 52      }
 53      catch (A*)
 54      {
 55          assert(false);
 56      }
 57  }
 58  
 59  void test3()
 60  {
 61      try
 62      {
 63          throw nullptr;
 64          assert(false);
 65      }
 66      catch (const A* const p)
 67      {
 68          assert(!p);
 69      }
 70      catch (A*)
 71      {
 72          assert(false);
 73      }
 74  }
 75  
 76  void test4()
 77  {
 78      try
 79      {
 80          throw nullptr;
 81          assert(false);
 82      }
 83      catch (A* p)
 84      {
 85          assert(!p);
 86      }
 87      catch (const A* const)
 88      {
 89          assert(false);
 90      }
 91  }
 92  
 93  void test5()
 94  {
 95      try
 96      {
 97          throw nullptr;
 98          assert(false);
 99      }
100      catch (A const* p)
101      {
102          assert(!p);
103      }
104      catch (A*)
105      {
106          assert(false);
107      }
108  }
109  
110  void test6()
111  {
112      try
113      {
114          throw nullptr;
115          assert(false);
116      }
117      catch (A* p)
118      {
119          assert(!p);
120      }
121      catch (A const*)
122      {
123          assert(false);
124      }
125  }
126  
127  
128  #else
129  
130  void test1() {}
131  void test2() {}
132  void test3() {}
133  void test4() {}
134  void test5() {}
135  void test6() {}
136  
137  #endif
138  
139  int main(int, char**) {
140      test1();
141      test2();
142      test3();
143      test4();
144      test5();
145      test6();
146  
147      return 0;
148  }