comparison libcxxabi/test/cxa_bad_cast.pass.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 2e18cbf3894f
comparison
equal deleted inserted replaced
147:c2174574ed3a 150:1d019706d866
1 //===----------------------- cxa_bad_cast.pass.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: c++98, c++03
10
11 #include <cxxabi.h>
12 #include <cassert>
13 #include <stdlib.h>
14 #include <exception>
15 #include <typeinfo>
16
17 class Base {
18 virtual void foo() {};
19 };
20
21 class Derived : public Base {};
22
23 Derived &test_bad_cast(Base& b) {
24 return dynamic_cast<Derived&>(b);
25 }
26
27 Base gB;
28
29 void my_terminate() { exit(0); }
30
31 int main ()
32 {
33 // swap-out the terminate handler
34 void (*default_handler)() = std::get_terminate();
35 std::set_terminate(my_terminate);
36
37 #ifndef LIBCXXABI_HAS_NO_EXCEPTIONS
38 try {
39 #endif
40 Derived &d = test_bad_cast(gB);
41 assert(false);
42 ((void)d);
43 #ifndef LIBCXXABI_HAS_NO_EXCEPTIONS
44 } catch (std::bad_cast) {
45 // success
46 return 0;
47 } catch (...) {
48 assert(false);
49 }
50 #endif
51
52 // failure, restore the default terminate handler and fire
53 std::set_terminate(default_handler);
54 std::terminate();
55 }