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
18extern "C" {
19typedef 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
23typedef void(__cdecl* unexpected_handler)();
24unexpected_handler __cdecl set_unexpected(unexpected_handler _NewUnexpectedHandler) throw();
25unexpected_handler __cdecl _get_unexpected();
26
27int __cdecl __uncaught_exceptions();
28}
29
30namespace std {
31
32unexpected_handler set_unexpected(unexpected_handler func) noexcept { return ::set_unexpected(func); }
33
34unexpected_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
42terminate_handler set_terminate(terminate_handler func) noexcept { return ::set_terminate(func); }
43
44terminate_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
61bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
62
63int uncaught_exceptions() noexcept { return __uncaught_exceptions(); }
64
65#if !defined(_LIBCPP_ABI_VCRUNTIME)
66bad_cast::bad_cast() noexcept {}
67
68bad_cast::~bad_cast() noexcept {}
69
70const char* bad_cast::what() const noexcept { return "std::bad_cast"; }
71
72bad_typeid::bad_typeid() noexcept {}
73
74bad_typeid::~bad_typeid() noexcept {}
75
76const char* bad_typeid::what() const noexcept { return "std::bad_typeid"; }
77
78exception::~exception() noexcept {}
79
80const char* exception::what() const noexcept { return "std::exception"; }
81
82bad_exception::~bad_exception() noexcept {}
83
84const char* bad_exception::what() const noexcept { return "std::bad_exception"; }
85
86bad_alloc::bad_alloc() noexcept {}
87
88bad_alloc::~bad_alloc() noexcept {}
89
90const char* bad_alloc::what() const noexcept { return "std::bad_alloc"; }
91
92bad_array_new_length::bad_array_new_length() noexcept {}
93
94bad_array_new_length::~bad_array_new_length() noexcept {}
95
96const char* bad_array_new_length::what() const noexcept { return "bad_array_new_length"; }
97#endif // !_LIBCPP_ABI_VCRUNTIME
98
99} // namespace std