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