libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

rethrow_if_nested.pass.cpp (2680B)


      1 //===----------------------------------------------------------------------===//
      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: libcpp-no-exceptions
     11 
     12 // This test fails due to a stack overflow
     13 // XFAIL: LIBCXX-WINDOWS-FIXME
     14 
     15 // <exception>
     16 
     17 // class nested_exception;
     18 
     19 // template <class E> void rethrow_if_nested(const E& e);
     20 
     21 #include <exception>
     22 #include <cstdlib>
     23 #include <cassert>
     24 
     25 #include "test_macros.h"
     26 
     27 class A
     28 {
     29     int data_;
     30 public:
     31     explicit A(int data) : data_(data) {}
     32     virtual ~A() TEST_NOEXCEPT {}
     33 
     34     friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
     35 };
     36 
     37 class B
     38     : public std::nested_exception,
     39       public A
     40 {
     41 public:
     42     explicit B(int data) : A(data) {}
     43     B(const B& b) : A(b) {}
     44 };
     45 
     46 class C
     47 {
     48 public:
     49     virtual ~C() {}
     50     C * operator&() const { assert(false); return nullptr; } // should not be called
     51 };
     52 
     53 class D : private std::nested_exception {};
     54 
     55 
     56 class E1 : public std::nested_exception {};
     57 class E2 : public std::nested_exception {};
     58 class E : public E1, public E2 {};
     59 
     60 int main()
     61 {
     62     {
     63         try
     64         {
     65             A a(3);  // not a polymorphic type --> no effect
     66             std::rethrow_if_nested(a);
     67             assert(true);
     68         }
     69         catch (...)
     70         {
     71             assert(false);
     72         }
     73     }
     74     {
     75         try
     76         {
     77             D s;  // inaccessible base class --> no effect
     78             std::rethrow_if_nested(s);
     79             assert(true);
     80         }
     81         catch (...)
     82         {
     83             assert(false);
     84         }
     85     }
     86     {
     87         try
     88         {
     89             E s;  // ambiguous base class --> no effect
     90             std::rethrow_if_nested(s);
     91             assert(true);
     92         }
     93         catch (...)
     94         {
     95             assert(false);
     96         }
     97     }
     98     {
     99         try
    100         {
    101             throw B(5);
    102         }
    103         catch (const B& b)
    104         {
    105             try
    106             {
    107                 throw b;
    108             }
    109             catch (const A& a)
    110             {
    111                 try
    112                 {
    113                     std::rethrow_if_nested(a);
    114                     assert(false);
    115                 }
    116                 catch (const B& b2)
    117                 {
    118                     assert(b2 == B(5));
    119                 }
    120             }
    121         }
    122     }
    123     {
    124         try
    125         {
    126             std::rethrow_if_nested(C());
    127             assert(true);
    128         }
    129         catch (...)
    130         {
    131             assert(false);
    132         }
    133     }
    134 
    135 }