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