blob: 664e7f48c09a636026b614acf925f338a09b6ac3 [file] [log] [blame]
Eric Fiselierd22c9dc2017-02-10 08:57:35 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#include <cstdio>
12
13namespace std {
14
15_LIBCPP_SAFE_STATIC static std::terminate_handler __terminate_handler;
16_LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler;
17
18
19// libcxxrt provides implementations of these functions itself.
20unexpected_handler
21set_unexpected(unexpected_handler func) _NOEXCEPT
22{
Weiming Zhaofbfaec72017-09-19 23:18:03 +000023 return __libcpp_atomic_exchange(&__unexpected_handler, func);
Eric Fiselierd22c9dc2017-02-10 08:57:35 +000024}
25
26unexpected_handler
27get_unexpected() _NOEXCEPT
28{
Weiming Zhaofbfaec72017-09-19 23:18:03 +000029 return __libcpp_atomic_load(&__unexpected_handler);
Eric Fiselierd22c9dc2017-02-10 08:57:35 +000030
31}
32
33_LIBCPP_NORETURN
34void unexpected()
35{
36 (*get_unexpected())();
37 // unexpected handler should not return
38 terminate();
39}
40
41terminate_handler
42set_terminate(terminate_handler func) _NOEXCEPT
43{
Weiming Zhaofbfaec72017-09-19 23:18:03 +000044 return __libcpp_atomic_exchange(&__terminate_handler, func);
Eric Fiselierd22c9dc2017-02-10 08:57:35 +000045}
46
47terminate_handler
48get_terminate() _NOEXCEPT
49{
Weiming Zhaofbfaec72017-09-19 23:18:03 +000050 return __libcpp_atomic_load(&__terminate_handler);
Eric Fiselierd22c9dc2017-02-10 08:57:35 +000051}
52
53#ifndef __EMSCRIPTEN__ // We provide this in JS
54_LIBCPP_NORETURN
55void
56terminate() _NOEXCEPT
57{
58#ifndef _LIBCPP_NO_EXCEPTIONS
59 try
60 {
61#endif // _LIBCPP_NO_EXCEPTIONS
62 (*get_terminate())();
63 // handler should not return
64 fprintf(stderr, "terminate_handler unexpectedly returned\n");
65 ::abort();
66#ifndef _LIBCPP_NO_EXCEPTIONS
67 }
68 catch (...)
69 {
70 // handler should not throw exception
71 fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
72 ::abort();
73 }
74#endif // _LIBCPP_NO_EXCEPTIONS
75}
76#endif // !__EMSCRIPTEN__
77
78#if !defined(__EMSCRIPTEN__)
79bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; }
80
81int uncaught_exceptions() _NOEXCEPT
82{
83#warning uncaught_exception not yet implemented
84 fprintf(stderr, "uncaught_exceptions not yet implemented\n");
85 ::abort();
86}
87#endif // !__EMSCRIPTEN__
88
89
90exception::~exception() _NOEXCEPT
91{
92}
93
94const char* exception::what() const _NOEXCEPT
95{
96 return "std::exception";
97}
98
99bad_exception::~bad_exception() _NOEXCEPT
100{
101}
102
103const char* bad_exception::what() const _NOEXCEPT
104{
105 return "std::bad_exception";
106}
107
108
109bad_alloc::bad_alloc() _NOEXCEPT
110{
111}
112
113bad_alloc::~bad_alloc() _NOEXCEPT
114{
115}
116
117const char*
118bad_alloc::what() const _NOEXCEPT
119{
120 return "std::bad_alloc";
121}
122
123bad_array_new_length::bad_array_new_length() _NOEXCEPT
124{
125}
126
127bad_array_new_length::~bad_array_new_length() _NOEXCEPT
128{
129}
130
131const char*
132bad_array_new_length::what() const _NOEXCEPT
133{
134 return "bad_array_new_length";
135}
136
137
138bad_array_length::bad_array_length() _NOEXCEPT
139{
140}
141
142bad_array_length::~bad_array_length() _NOEXCEPT
143{
144}
145
146const char*
147bad_array_length::what() const _NOEXCEPT
148{
149 return "bad_array_length";
150}
151
152
153bad_cast::bad_cast() _NOEXCEPT
154{
155}
156
157bad_typeid::bad_typeid() _NOEXCEPT
158{
159}
160
161bad_cast::~bad_cast() _NOEXCEPT
162{
163}
164
165const char*
166bad_cast::what() const _NOEXCEPT
167{
168 return "std::bad_cast";
169}
170
171bad_typeid::~bad_typeid() _NOEXCEPT
172{
173}
174
175const char*
176bad_typeid::what() const _NOEXCEPT
177{
178 return "std::bad_typeid";
179}
180
181} // namespace std