Home | History | Annotate | Download | only in test
      1 //===------------------------- catch_ptr_02.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: libcxxabi-no-exceptions
     11 
     12 #include <cassert>
     13 
     14 // Clang emits  warnings about exceptions of type 'Child' being caught by
     15 // an earlier handler of type 'Base'. Congrats clang, you've just
     16 // diagnosed the behavior under test.
     17 #if defined(__clang__)
     18 #pragma clang diagnostic ignored "-Wexceptions"
     19 #endif
     20 
     21 #if __cplusplus < 201103L
     22 #define DISABLE_NULLPTR_TESTS
     23 #endif
     24 
     25 struct  A {};
     26 A a;
     27 const A ca = A();
     28 
     29 void test1 ()
     30 {
     31     try
     32     {
     33         throw &a;
     34         assert(false);
     35     }
     36     catch ( const A* )
     37     {
     38     }
     39     catch ( A *)
     40     {
     41         assert (false);
     42     }
     43 }
     44 
     45 void test2 ()
     46 {
     47     try
     48      {
     49         throw &a;
     50         assert(false);
     51     }
     52     catch ( A* )
     53     {
     54     }
     55     catch ( const A *)
     56     {
     57          assert (false);
     58     }
     59 }
     60 
     61 void test3 ()
     62 {
     63     try
     64     {
     65         throw &ca;
     66         assert(false);
     67     }
     68     catch ( const A* )
     69     {
     70     }
     71     catch ( A *)
     72     {
     73         assert (false);
     74     }
     75 }
     76 
     77 void test4 ()
     78 {
     79     try
     80     {
     81         throw &ca;
     82         assert(false);
     83     }
     84     catch ( A *)
     85     {
     86         assert (false);
     87     }
     88     catch ( const A* )
     89     {
     90     }
     91 }
     92 
     93 struct base1 {int x;};
     94 struct base2 {int x;};
     95 struct derived : base1, base2 {};
     96 
     97 void test5 ()
     98 {
     99     try
    100     {
    101         throw (derived*)0;
    102         assert(false);
    103     }
    104     catch (base2 *p) {
    105         assert (p == 0);
    106     }
    107     catch (...)
    108     {
    109         assert (false);
    110     }
    111 }
    112 
    113 void test6 ()
    114 {
    115 #if !defined(DISABLE_NULLPTR_TESTS)
    116     try
    117     {
    118         throw nullptr;
    119         assert(false);
    120     }
    121     catch (base2 *p) {
    122         assert (p == nullptr);
    123     }
    124     catch (...)
    125     {
    126         assert (false);
    127     }
    128 #endif
    129 }
    130 
    131 void test7 ()
    132 {
    133     try
    134     {
    135         throw (derived*)12;
    136         assert(false);
    137     }
    138     catch (base2 *p) {
    139         assert ((unsigned long)p == 12+sizeof(base1));
    140     }
    141     catch (...)
    142     {
    143         assert (false);
    144     }
    145 }
    146 
    147 
    148 struct vBase {};
    149 struct vDerived : virtual public vBase {};
    150 
    151 void test8 ()
    152 {
    153     vDerived derived;
    154     try
    155     {
    156         throw &derived;
    157         assert(false);
    158     }
    159     catch (vBase *p) {
    160         assert(p != 0);
    161     }
    162     catch (...)
    163     {
    164         assert (false);
    165     }
    166 }
    167 
    168 void test9 ()
    169 {
    170 #if !defined(DISABLE_NULLPTR_TESTS)
    171     try
    172     {
    173         throw nullptr;
    174         assert(false);
    175     }
    176     catch (vBase *p) {
    177         assert(p == 0);
    178     }
    179     catch (...)
    180     {
    181         assert (false);
    182     }
    183 #endif
    184 }
    185 
    186 void test10 ()
    187 {
    188     try
    189     {
    190         throw (vDerived*)0;
    191         assert(false);
    192     }
    193     catch (vBase *p) {
    194         assert(p == 0);
    195     }
    196     catch (...)
    197     {
    198         assert (false);
    199     }
    200 }
    201 
    202 int main()
    203 {
    204     test1();
    205     test2();
    206     test3();
    207     test4();
    208     test5();
    209     test6();
    210     test7();
    211     test8();
    212     test9();
    213     test10();
    214 }
    215