Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 1 | //===----------------------- cxa_bad_typeid.pass.cpp ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===------------------------------------------------------------------------===// |
| 9 | |
| 10 | // UNSUPPORTED: c++98, c++03 |
| 11 | |
| 12 | #include <cxxabi.h> |
| 13 | #include <cassert> |
| 14 | #include <stdlib.h> |
| 15 | #include <exception> |
| 16 | #include <typeinfo> |
| 17 | #include <string> |
| 18 | #include <iostream> |
| 19 | |
| 20 | class Base { |
| 21 | virtual void foo() {}; |
| 22 | }; |
| 23 | |
| 24 | class Derived : public Base {}; |
| 25 | |
| 26 | std::string test_bad_typeid(Derived *p) { |
Asiri Rathnayake | a0d7e1a | 2016-06-01 19:13:53 +0000 | [diff] [blame] | 27 | return typeid(*p).name(); |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void my_terminate() { std::cout << "A" << std::endl; exit(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 LIBCXXABI_HAS_NO_EXCEPTIONS |
| 39 | try { |
| 40 | #endif |
| 41 | test_bad_typeid(nullptr); |
| 42 | assert(false); |
| 43 | #ifndef LIBCXXABI_HAS_NO_EXCEPTIONS |
| 44 | } catch (std::bad_typeid) { |
| 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 | } |