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 test1()29void 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 test2()45void 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 test3()61void 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 test4()77void 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 test5()97void 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 test6()113void 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 test7()131void 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 test8()151void 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 test9()168void 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 test10()186void 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 main()202int main() 203 { 204 test1(); 205 test2(); 206 test3(); 207 test4(); 208 test5(); 209 test6(); 210 test7(); 211 test8(); 212 test9(); 213 test10(); 214 } 215