xref: /freebsd/contrib/llvm-project/libcxx/src/support/runtime/exception_msvc.ipp (revision a90b9d0159070121c221b966469c3e36d912bf82)
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 <stdio.h>
15#include <stdlib.h>
16
17extern "C" {
18typedef void(__cdecl* terminate_handler)();
19_LIBCPP_CRT_FUNC terminate_handler __cdecl set_terminate(terminate_handler _NewTerminateHandler) throw();
20_LIBCPP_CRT_FUNC terminate_handler __cdecl _get_terminate();
21
22typedef void(__cdecl* unexpected_handler)();
23unexpected_handler __cdecl set_unexpected(unexpected_handler _NewUnexpectedHandler) throw();
24unexpected_handler __cdecl _get_unexpected();
25
26int __cdecl __uncaught_exceptions();
27}
28
29namespace std {
30
31unexpected_handler set_unexpected(unexpected_handler func) noexcept { return ::set_unexpected(func); }
32
33unexpected_handler get_unexpected() noexcept { return ::_get_unexpected(); }
34
35_LIBCPP_NORETURN void unexpected() {
36  (*get_unexpected())();
37  // unexpected handler should not return
38  terminate();
39}
40
41terminate_handler set_terminate(terminate_handler func) noexcept { return ::set_terminate(func); }
42
43terminate_handler get_terminate() noexcept { return ::_get_terminate(); }
44
45_LIBCPP_NORETURN void terminate() noexcept {
46#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
47  try {
48#endif // _LIBCPP_HAS_NO_EXCEPTIONS
49    (*get_terminate())();
50    // handler should not return
51    fprintf(stderr, "terminate_handler unexpectedly returned\n");
52    ::abort();
53#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
54  } catch (...) {
55    // handler should not throw exception
56    fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
57    ::abort();
58  }
59#endif // _LIBCPP_HAS_NO_EXCEPTIONS
60}
61
62bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
63
64int uncaught_exceptions() noexcept { return __uncaught_exceptions(); }
65
66#if !defined(_LIBCPP_ABI_VCRUNTIME)
67bad_cast::bad_cast() noexcept {}
68
69bad_cast::~bad_cast() noexcept {}
70
71const char* bad_cast::what() const noexcept { return "std::bad_cast"; }
72
73bad_typeid::bad_typeid() noexcept {}
74
75bad_typeid::~bad_typeid() noexcept {}
76
77const char* bad_typeid::what() const noexcept { return "std::bad_typeid"; }
78
79exception::~exception() noexcept {}
80
81const char* exception::what() const noexcept { return "std::exception"; }
82
83bad_exception::~bad_exception() noexcept {}
84
85const char* bad_exception::what() const noexcept { return "std::bad_exception"; }
86
87bad_alloc::bad_alloc() noexcept {}
88
89bad_alloc::~bad_alloc() noexcept {}
90
91const char* bad_alloc::what() const noexcept { return "std::bad_alloc"; }
92
93bad_array_new_length::bad_array_new_length() noexcept {}
94
95bad_array_new_length::~bad_array_new_length() noexcept {}
96
97const char* bad_array_new_length::what() const noexcept { return "bad_array_new_length"; }
98#endif // !_LIBCPP_ABI_VCRUNTIME
99
100} // namespace std
101