[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / libcxx / src / support / runtime / exception_fallback.ipp
blob3b2716d36f0e6823f1f451807ffb405e842807b7
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #include <cstdio>
12 namespace std {
14 static constinit std::terminate_handler  __terminate_handler = nullptr;
15 static constinit std::unexpected_handler __unexpected_handler = nullptr;
17 // libcxxrt provides implementations of these functions itself.
18 unexpected_handler
19 set_unexpected(unexpected_handler func) noexcept
21   return __libcpp_atomic_exchange(&__unexpected_handler, func);
24 unexpected_handler
25 get_unexpected() noexcept
27   return __libcpp_atomic_load(&__unexpected_handler);
30 _LIBCPP_NORETURN
31 void unexpected()
33     (*get_unexpected())();
34     // unexpected handler should not return
35     terminate();
38 terminate_handler
39 set_terminate(terminate_handler func) noexcept
41   return __libcpp_atomic_exchange(&__terminate_handler, func);
44 terminate_handler
45 get_terminate() noexcept
47   return __libcpp_atomic_load(&__terminate_handler);
50 _LIBCPP_NORETURN
51 void
52 terminate() noexcept
54 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
55     try
56     {
57 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
58         (*get_terminate())();
59         // handler should not return
60         fprintf(stderr, "terminate_handler unexpectedly returned\n");
61         ::abort();
62 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
63     }
64     catch (...)
65     {
66         // handler should not throw exception
67         fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
68         ::abort();
69     }
70 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
73 bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
75 int uncaught_exceptions() noexcept
77 #warning uncaught_exception not yet implemented
78   fprintf(stderr, "uncaught_exceptions not yet implemented\n");
79   ::abort();
83 exception::~exception() noexcept
87 const char* exception::what() const noexcept
89   return "std::exception";
92 bad_exception::~bad_exception() noexcept
96 const char* bad_exception::what() const noexcept
98   return "std::bad_exception";
102 bad_alloc::bad_alloc() noexcept
106 bad_alloc::~bad_alloc() noexcept
110 const char*
111 bad_alloc::what() const noexcept
113     return "std::bad_alloc";
116 bad_array_new_length::bad_array_new_length() noexcept
120 bad_array_new_length::~bad_array_new_length() noexcept
124 const char*
125 bad_array_new_length::what() const noexcept
127     return "bad_array_new_length";
130 bad_cast::bad_cast() noexcept
134 bad_typeid::bad_typeid() noexcept
138 bad_cast::~bad_cast() noexcept
142 const char*
143 bad_cast::what() const noexcept
145   return "std::bad_cast";
148 bad_typeid::~bad_typeid() noexcept
152 const char*
153 bad_typeid::what() const noexcept
155   return "std::bad_typeid";
158 } // namespace std