| 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 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_ABI_MICROSOFT |
| 11 | # error this header can only be used when targeting the MSVC ABI |
| 12 | #endif |
| 13 | |
| 14 | #include <__verbose_abort> |
| 15 | #include <exception> |
| 16 | #include <new> |
| 17 | |
| 18 | extern "C" { |
| 19 | typedef void(__cdecl* terminate_handler)(); |
| 20 | _LIBCPP_CRT_FUNC terminate_handler __cdecl set_terminate(terminate_handler _NewTerminateHandler) throw(); |
| 21 | _LIBCPP_CRT_FUNC terminate_handler __cdecl _get_terminate(); |
| 22 | |
| 23 | typedef void(__cdecl* unexpected_handler)(); |
| 24 | unexpected_handler __cdecl set_unexpected(unexpected_handler _NewUnexpectedHandler) throw(); |
| 25 | unexpected_handler __cdecl _get_unexpected(); |
| 26 | |
| 27 | int __cdecl __uncaught_exceptions(); |
| 28 | } |
| 29 | |
| 30 | namespace std { |
| 31 | |
| 32 | unexpected_handler set_unexpected(unexpected_handler func) noexcept { return ::set_unexpected(func); } |
| 33 | |
| 34 | unexpected_handler get_unexpected() noexcept { return ::_get_unexpected(); } |
| 35 | |
| 36 | [[noreturn]] void unexpected() { |
| 37 | (*get_unexpected())(); |
| 38 | // unexpected handler should not return |
| 39 | terminate(); |
| 40 | } |
| 41 | |
| 42 | terminate_handler set_terminate(terminate_handler func) noexcept { return ::set_terminate(func); } |
| 43 | |
| 44 | terminate_handler get_terminate() noexcept { return ::_get_terminate(); } |
| 45 | |
| 46 | [[noreturn]] void terminate() noexcept { |
| 47 | #if _LIBCPP_HAS_EXCEPTIONS |
| 48 | try { |
| 49 | #endif // _LIBCPP_HAS_EXCEPTIONS |
| 50 | (*get_terminate())(); |
| 51 | // handler should not return |
| 52 | __libcpp_verbose_abort("terminate_handler unexpectedly returned\n"); |
| 53 | #if _LIBCPP_HAS_EXCEPTIONS |
| 54 | } catch (...) { |
| 55 | // handler should not throw exception |
| 56 | __libcpp_verbose_abort("terminate_handler unexpectedly threw an exception\n"); |
| 57 | } |
| 58 | #endif // _LIBCPP_HAS_EXCEPTIONS |
| 59 | } |
| 60 | |
| 61 | bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; } |
| 62 | |
| 63 | int uncaught_exceptions() noexcept { return __uncaught_exceptions(); } |
| 64 | |
| 65 | #if !defined(_LIBCPP_ABI_VCRUNTIME) |
| 66 | bad_cast::bad_cast() noexcept {} |
| 67 | |
| 68 | bad_cast::~bad_cast() noexcept {} |
| 69 | |
| 70 | const char* bad_cast::what() const noexcept { return "std::bad_cast"; } |
| 71 | |
| 72 | bad_typeid::bad_typeid() noexcept {} |
| 73 | |
| 74 | bad_typeid::~bad_typeid() noexcept {} |
| 75 | |
| 76 | const char* bad_typeid::what() const noexcept { return "std::bad_typeid"; } |
| 77 | |
| 78 | exception::~exception() noexcept {} |
| 79 | |
| 80 | const char* exception::what() const noexcept { return "std::exception"; } |
| 81 | |
| 82 | bad_exception::~bad_exception() noexcept {} |
| 83 | |
| 84 | const char* bad_exception::what() const noexcept { return "std::bad_exception"; } |
| 85 | |
| 86 | bad_alloc::bad_alloc() noexcept {} |
| 87 | |
| 88 | bad_alloc::~bad_alloc() noexcept {} |
| 89 | |
| 90 | const char* bad_alloc::what() const noexcept { return "std::bad_alloc"; } |
| 91 | |
| 92 | bad_array_new_length::bad_array_new_length() noexcept {} |
| 93 | |
| 94 | bad_array_new_length::~bad_array_new_length() noexcept {} |
| 95 | |
| 96 | const char* bad_array_new_length::what() const noexcept { return "bad_array_new_length"; } |
| 97 | #endif // !_LIBCPP_ABI_VCRUNTIME |
| 98 | |
| 99 | } // namespace std |