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#include <cstdio> 11 12namespace std { 13 14_LIBCPP_SAFE_STATIC static std::terminate_handler __terminate_handler; 15_LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler; 16 17 18// libcxxrt provides implementations of these functions itself. 19unexpected_handler 20set_unexpected(unexpected_handler func) _NOEXCEPT 21{ 22 return __libcpp_atomic_exchange(&__unexpected_handler, func); 23} 24 25unexpected_handler 26get_unexpected() _NOEXCEPT 27{ 28 return __libcpp_atomic_load(&__unexpected_handler); 29 30} 31 32_LIBCPP_NORETURN 33void unexpected() 34{ 35 (*get_unexpected())(); 36 // unexpected handler should not return 37 terminate(); 38} 39 40terminate_handler 41set_terminate(terminate_handler func) _NOEXCEPT 42{ 43 return __libcpp_atomic_exchange(&__terminate_handler, func); 44} 45 46terminate_handler 47get_terminate() _NOEXCEPT 48{ 49 return __libcpp_atomic_load(&__terminate_handler); 50} 51 52#ifndef __EMSCRIPTEN__ // We provide this in JS 53_LIBCPP_NORETURN 54void 55terminate() _NOEXCEPT 56{ 57#ifndef _LIBCPP_NO_EXCEPTIONS 58 try 59 { 60#endif // _LIBCPP_NO_EXCEPTIONS 61 (*get_terminate())(); 62 // handler should not return 63 fprintf(stderr, "terminate_handler unexpectedly returned\n"); 64 ::abort(); 65#ifndef _LIBCPP_NO_EXCEPTIONS 66 } 67 catch (...) 68 { 69 // handler should not throw exception 70 fprintf(stderr, "terminate_handler unexpectedly threw an exception\n"); 71 ::abort(); 72 } 73#endif // _LIBCPP_NO_EXCEPTIONS 74} 75#endif // !__EMSCRIPTEN__ 76 77#if !defined(__EMSCRIPTEN__) 78bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } 79 80int uncaught_exceptions() _NOEXCEPT 81{ 82#warning uncaught_exception not yet implemented 83 fprintf(stderr, "uncaught_exceptions not yet implemented\n"); 84 ::abort(); 85} 86#endif // !__EMSCRIPTEN__ 87 88 89exception::~exception() _NOEXCEPT 90{ 91} 92 93const char* exception::what() const _NOEXCEPT 94{ 95 return "std::exception"; 96} 97 98bad_exception::~bad_exception() _NOEXCEPT 99{ 100} 101 102const char* bad_exception::what() const _NOEXCEPT 103{ 104 return "std::bad_exception"; 105} 106 107 108bad_alloc::bad_alloc() _NOEXCEPT 109{ 110} 111 112bad_alloc::~bad_alloc() _NOEXCEPT 113{ 114} 115 116const char* 117bad_alloc::what() const _NOEXCEPT 118{ 119 return "std::bad_alloc"; 120} 121 122bad_array_new_length::bad_array_new_length() _NOEXCEPT 123{ 124} 125 126bad_array_new_length::~bad_array_new_length() _NOEXCEPT 127{ 128} 129 130const char* 131bad_array_new_length::what() const _NOEXCEPT 132{ 133 return "bad_array_new_length"; 134} 135 136bad_cast::bad_cast() _NOEXCEPT 137{ 138} 139 140bad_typeid::bad_typeid() _NOEXCEPT 141{ 142} 143 144bad_cast::~bad_cast() _NOEXCEPT 145{ 146} 147 148const char* 149bad_cast::what() const _NOEXCEPT 150{ 151 return "std::bad_cast"; 152} 153 154bad_typeid::~bad_typeid() _NOEXCEPT 155{ 156} 157 158const char* 159bad_typeid::what() const _NOEXCEPT 160{ 161 return "std::bad_typeid"; 162} 163 164} // namespace std 165