1 // { dg-do run } 2 // Test pointer chain catching 3 // Copyright (C) 2000, 2002 Free Software Foundation, Inc. 4 // Contributed by Nathan Sidwell 9 Apr 2000 <nathan@nathan (at) codesourcery.com> 5 6 #include <stdio.h> 7 8 void fn () {} 9 struct A {void fn () {}}; 10 static int var = 1; 11 static const int const_var = 2; 12 13 struct B; 14 struct C; 15 16 int test0 () 17 { 18 try 19 { 20 throw &fn; 21 } 22 catch (void *) 23 { 24 // should not decay to void * 25 fprintf(stderr, "25\n"); 26 return 1; 27 } 28 catch (...) 29 { 30 return 0; 31 } 32 return -1; 33 } 34 35 int test1 () 36 { 37 try 38 { 39 throw &A::fn; 40 } 41 catch (void *) 42 { 43 // should not decay to void * 44 fprintf(stderr, "44\n"); 45 return 1; 46 } 47 catch (...) 48 { 49 return 0; 50 } 51 return -1; 52 } 53 54 int test2 () 55 { 56 try 57 { 58 throw &var; 59 } 60 catch (void *) 61 { 62 // should decay to void * 63 return 0; 64 } 65 catch (...) 66 { 67 fprintf(stderr, "67\n"); 68 return 1; 69 } 70 return -1; 71 } 72 73 int test3 () 74 { 75 try 76 { 77 throw &var; 78 } 79 catch (void const *) 80 { 81 // should decay to const void * 82 return 0; 83 } 84 catch (...) 85 { 86 fprintf(stderr, "86\n"); 87 return 1; 88 } 89 return -1; 90 } 91 92 int test4 () 93 { 94 try 95 { 96 throw &const_var; 97 } 98 catch (void *) 99 { 100 fprintf(stderr, "100\n"); 101 // should not decay to void * 102 return 1; 103 } 104 catch (void const *) 105 { 106 // should decay to const void * 107 return 0; 108 } 109 catch (...) 110 { 111 return 2; 112 } 113 return -1; 114 } 115 116 int test5 () 117 { 118 try 119 { 120 throw (void ***)0; 121 } 122 catch (void ***) 123 { 124 return 0; 125 } 126 catch (...) 127 { 128 fprintf(stderr, "128\n"); 129 return 1; 130 } 131 return -1; 132 } 133 134 int test6 () 135 { 136 try 137 { 138 throw (void const* const* const*)0; 139 } 140 catch (void ***) 141 { 142 fprintf(stderr, "142\n"); 143 return 1; 144 } 145 catch (void * const* const*) 146 { 147 return 2; 148 } 149 catch (void const* * const*) 150 { 151 return 3; 152 } 153 catch (void const* const* *) 154 { 155 return 4; 156 } 157 catch (void const* const* const *) 158 { 159 return 0; 160 } 161 catch (...) 162 { 163 return 1; 164 } 165 return -1; 166 } 167 168 int test7 () 169 { 170 try 171 { 172 throw (void ***)0; 173 } 174 catch (void const* const**) 175 { 176 return 1; 177 } 178 catch (void const** const *) 179 { 180 return 2; 181 } 182 catch (void * const* const *) 183 { 184 return 0; 185 } 186 catch (...) 187 { 188 return 3; 189 } 190 return -1; 191 } 192 193 int test8 () 194 { 195 try 196 { 197 throw (B **)0; 198 } 199 catch (C **) 200 { 201 return 1; 202 } 203 catch (B **) 204 { 205 return 0; 206 } 207 catch (...) 208 { 209 return 2; 210 } 211 return -1; 212 } 213 214 int test9 () 215 { 216 try 217 { 218 throw (B **)0; 219 } 220 catch (C const *const *) 221 { 222 return 1; 223 } 224 catch (B const *const *) 225 { 226 return 0; 227 } 228 catch (...) 229 { 230 return 2; 231 } 232 return -1; 233 } 234 235 static int (*tests[])() = 236 { 237 test0, 238 test1, 239 test2, 240 test3, 241 test4, 242 243 test5, 244 test6, 245 test7, 246 247 test8, 248 test9, 249 250 NULL 251 }; 252 253 int main () 254 { 255 int ix; 256 int errors = 0; 257 258 for (ix = 0; tests[ix]; ix++) 259 { 260 int n = tests[ix] (); 261 262 if (n) 263 { 264 printf ("test %d failed %d\n", ix, n); 265 errors++; 266 } 267 } 268 return errors; 269 } 270