236
|
1 //===----------------------------------------------------------------------===//
|
150
|
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
|
221
|
9 // UNSUPPORTED: c++03
|
150
|
10
|
|
11 #include <cxxabi.h>
|
|
12 #include <cassert>
|
|
13 #include <stdlib.h>
|
|
14 #include <exception>
|
|
15 #include <typeinfo>
|
|
16
|
221
|
17 #include "test_macros.h"
|
|
18
|
150
|
19 class Base {
|
|
20 virtual void foo() {};
|
|
21 };
|
|
22
|
|
23 class Derived : public Base {};
|
|
24
|
|
25 Derived &test_bad_cast(Base& b) {
|
|
26 return dynamic_cast<Derived&>(b);
|
|
27 }
|
|
28
|
|
29 Base gB;
|
|
30
|
|
31 void my_terminate() { exit(0); }
|
|
32
|
|
33 int main ()
|
|
34 {
|
|
35 // swap-out the terminate handler
|
221
|
36 void (*default_handler)() = std::get_terminate();
|
150
|
37 std::set_terminate(my_terminate);
|
|
38
|
221
|
39 #ifndef TEST_HAS_NO_EXCEPTIONS
|
150
|
40 try {
|
|
41 #endif
|
|
42 Derived &d = test_bad_cast(gB);
|
|
43 assert(false);
|
|
44 ((void)d);
|
221
|
45 #ifndef TEST_HAS_NO_EXCEPTIONS
|
|
46 } catch (std::bad_cast const&) {
|
150
|
47 // success
|
|
48 return 0;
|
|
49 } catch (...) {
|
|
50 assert(false);
|
|
51 }
|
|
52 #endif
|
|
53
|
|
54 // failure, restore the default terminate handler and fire
|
|
55 std::set_terminate(default_handler);
|
|
56 std::terminate();
|
|
57 }
|