| 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 | //===------------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03 |
| 10 | |
| 11 | #include <cxxabi.h> |
| 12 | #include <cassert> |
| 13 | #include <stdlib.h> |
| 14 | #include <exception> |
| 15 | #include <typeinfo> |
| 16 | #include <string> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | |
| 20 | class Base { |
| 21 | virtual void foo() {}; |
| 22 | }; |
| 23 | |
| 24 | class Derived : public Base {}; |
| 25 | |
| 26 | std::string test_bad_typeid(Derived *p) { |
| 27 | return typeid(*p).name(); |
| 28 | } |
| 29 | |
| 30 | void my_terminate() { exit(status: 0); } |
| 31 | |
| 32 | int main () |
| 33 | { |
| 34 | // swap-out the terminate handler |
| 35 | void (*default_handler)() = std::get_terminate(); |
| 36 | std::set_terminate(my_terminate); |
| 37 | |
| 38 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 39 | try { |
| 40 | #endif |
| 41 | test_bad_typeid(p: nullptr); |
| 42 | assert(false); |
| 43 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 44 | } catch (std::bad_typeid const&) { |
| 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 | } |
| 56 | |