• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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++03
10 
11 #include <cxxabi.h>
12 #include <cassert>
13 #include <stdlib.h>
14 #include <exception>
15 #include <typeinfo>
16 
17 #include "test_macros.h"
18 
19 class Base {
foo()20   virtual void foo() {};
21 };
22 
23 class Derived : public Base {};
24 
test_bad_cast(Base & b)25 Derived &test_bad_cast(Base& b) {
26   return dynamic_cast<Derived&>(b);
27 }
28 
29 Base gB;
30 
my_terminate()31 void my_terminate() { exit(0); }
32 
main()33 int main ()
34 {
35     // swap-out the terminate handler
36     void (*default_handler)() = std::get_terminate();
37     std::set_terminate(my_terminate);
38 
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);
51     }
52 #endif
53 
54     // failure, restore the default terminate handler and fire
55     std::set_terminate(default_handler);
56     std::terminate();
57 }
58