blob: 50185736ebd6467fcbe108262c9a81ccc49e4737 [file] [log] [blame]
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +00001//===----------------------- 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
20class Base {
21 virtual void foo() {};
22};
23
24class Derived : public Base {};
25
26std::string test_bad_typeid(Derived *p) {
Asiri Rathnayakea0d7e1a2016-06-01 19:13:53 +000027 return typeid(*p).name();
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000028}
29
30void my_terminate() { std::cout << "A" << std::endl; exit(0); }
31
32int 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}