[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / libcxxabi / test / cxa_bad_cast.pass.cpp
blobf34492bc29dcdcaaba2893f680ea1f85ca0765d6
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-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
11 #include <cxxabi.h>
12 #include <cassert>
13 #include <stdlib.h>
14 #include <exception>
15 #include <typeinfo>
17 #include "test_macros.h"
19 class Base {
20 virtual void foo() {};
23 class Derived : public Base {};
25 Derived &test_bad_cast(Base& b) {
26 return dynamic_cast<Derived&>(b);
29 Base gB;
31 void my_terminate() { exit(0); }
33 int main ()
35 // swap-out the terminate handler
36 void (*default_handler)() = std::get_terminate();
37 std::set_terminate(my_terminate);
39 #ifndef TEST_HAS_NO_EXCEPTIONS
40 try {
41 #endif
42 Derived &d = test_bad_cast(gB);
43 assert(false);
44 ((void)d);
45 #ifndef TEST_HAS_NO_EXCEPTIONS
46 } catch (std::bad_cast const&) {
47 // success
48 return 0;
49 } catch (...) {
50 assert(false);
52 #endif
54 // failure, restore the default terminate handler and fire
55 std::set_terminate(default_handler);
56 std::terminate();