xref: /llvm-project/libcxxabi/test/cxa_bad_typeid.pass.cpp (revision eb8650a75793b2bd079d0c8901ff066f129061da)
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 {
foo()21   virtual void foo() {};
22 };
23 
24 class Derived : public Base {};
25 
test_bad_typeid(Derived * p)26 std::string test_bad_typeid(Derived *p) {
27     return typeid(*p).name();
28 }
29 
my_terminate()30 void my_terminate() { exit(0); }
31 
main()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(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