Home | History | Annotate | Download | only in test
      1 //===--------------------- catch_pointer_nullptr.cpp ----------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03, libcxxabi-no-exceptions
     11 
     12 #include <cassert>
     13 #include <cstdlib>
     14 
     15 struct A {};
     16 
     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 
     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>
     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 
     64 int main()
     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