215 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: no-exceptions10 11// Compilers emit warnings about exceptions of type 'Child' being caught by12// an earlier handler of type 'Base'. Congrats, you've just diagnosed the13// behavior under test.14// ADDITIONAL_COMPILE_FLAGS: -Wno-exceptions15 16#include <cassert>17#include <stdint.h>18 19#if __cplusplus < 201103L20#define DISABLE_NULLPTR_TESTS21#endif22 23struct A {};24A a;25const A ca = A();26 27void test1 ()28{29 try30 {31 throw &a;32 assert(false);33 }34 catch ( const A* )35 {36 }37 catch ( A *)38 {39 assert (false);40 }41}42 43void test2 ()44{45 try46 {47 throw &a;48 assert(false);49 }50 catch ( A* )51 {52 }53 catch ( const A *)54 {55 assert (false);56 }57}58 59void test3 ()60{61 try62 {63 throw &ca;64 assert(false);65 }66 catch ( const A* )67 {68 }69 catch ( A *)70 {71 assert (false);72 }73}74 75void test4 ()76{77 try78 {79 throw &ca;80 assert(false);81 }82 catch ( A *)83 {84 assert (false);85 }86 catch ( const A* )87 {88 }89}90 91struct base1 {int x;};92struct base2 {int x;};93struct derived : base1, base2 {};94 95void test5 ()96{97 try98 {99 throw (derived*)0;100 assert(false);101 }102 catch (base2 *p) {103 assert (p == 0);104 }105 catch (...)106 {107 assert (false);108 }109}110 111void test6 ()112{113#if !defined(DISABLE_NULLPTR_TESTS)114 try115 {116 throw nullptr;117 assert(false);118 }119 catch (base2 *p) {120 assert (p == nullptr);121 }122 catch (...)123 {124 assert (false);125 }126#endif127}128 129void test7 ()130{131 try132 {133 throw (derived*)12;134 assert(false);135 }136 catch (base2 *p) {137 assert ((uintptr_t)p == 12+sizeof(base1));138 }139 catch (...)140 {141 assert (false);142 }143}144 145 146struct vBase {};147struct vDerived : virtual public vBase {};148 149void test8 ()150{151 vDerived derived;152 try153 {154 throw &derived;155 assert(false);156 }157 catch (vBase *p) {158 assert(p != 0);159 }160 catch (...)161 {162 assert (false);163 }164}165 166void test9 ()167{168#if !defined(DISABLE_NULLPTR_TESTS)169 try170 {171 throw nullptr;172 assert(false);173 }174 catch (vBase *p) {175 assert(p == 0);176 }177 catch (...)178 {179 assert (false);180 }181#endif182}183 184void test10 ()185{186 try187 {188 throw (vDerived*)0;189 assert(false);190 }191 catch (vBase *p) {192 assert(p == 0);193 }194 catch (...)195 {196 assert (false);197 }198}199 200int main(int, char**)201{202 test1();203 test2();204 test3();205 test4();206 test5();207 test6();208 test7();209 test8();210 test9();211 test10();212 213 return 0;214}215