llvm-project
223 строки · 7.0 Кб
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <__assert>10#include <__config>11#include <__verbose_abort>12#include <cerrno>13#include <cstdio>14#include <cstdlib>15#include <cstring>16#include <string.h>17#include <string>18#include <system_error>19
20#include "include/config_elast.h"21
22#if defined(__ANDROID__)23# include <android/api-level.h>24#endif25
26_LIBCPP_BEGIN_NAMESPACE_STD
27
28namespace {29#if !defined(_LIBCPP_HAS_NO_THREADS)30
31// GLIBC also uses 1024 as the maximum buffer size internally.
32constexpr size_t strerror_buff_size = 1024;33
34string do_strerror_r(int ev);35
36# if defined(_LIBCPP_MSVCRT_LIKE)37string do_strerror_r(int ev) {38char buffer[strerror_buff_size];39if (::strerror_s(buffer, strerror_buff_size, ev) == 0)40return string(buffer);41std::snprintf(buffer, strerror_buff_size, "unknown error %d", ev);42return string(buffer);43}
44# else45
46// Only one of the two following functions will be used, depending on
47// the return type of strerror_r:
48
49// For the GNU variant, a char* return value:
50__attribute__((unused)) const char* handle_strerror_r_return(char* strerror_return, char* buffer) {51// GNU always returns a string pointer in its return value. The52// string might point to either the input buffer, or a static53// buffer, but we don't care which.54return strerror_return;55}
56
57// For the POSIX variant: an int return value.
58__attribute__((unused)) const char* handle_strerror_r_return(int strerror_return, char* buffer) {59// The POSIX variant either:60// - fills in the provided buffer and returns 061// - returns a positive error value, or62// - returns -1 and fills in errno with an error value.63if (strerror_return == 0)64return buffer;65
66// Only handle EINVAL. Other errors abort.67int new_errno = strerror_return == -1 ? errno : strerror_return;68if (new_errno == EINVAL)69return "";70
71_LIBCPP_ASSERT_INTERNAL(new_errno == ERANGE, "unexpected error from ::strerror_r");72// FIXME maybe? 'strerror_buff_size' is likely to exceed the73// maximum error size so ERANGE shouldn't be returned.74std::abort();75}
76
77// This function handles both GNU and POSIX variants, dispatching to
78// one of the two above functions.
79string do_strerror_r(int ev) {80char buffer[strerror_buff_size];81// Preserve errno around the call. (The C++ standard requires that82// system_error functions not modify errno).83const int old_errno = errno;84const char* error_message = handle_strerror_r_return(::strerror_r(ev, buffer, strerror_buff_size), buffer);85// If we didn't get any message, print one now.86if (!error_message[0]) {87std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);88error_message = buffer;89}90errno = old_errno;91return string(error_message);92}
93# endif94
95#endif // !defined(_LIBCPP_HAS_NO_THREADS)96
97string make_error_str(const error_code& ec, string what_arg) {98if (ec) {99if (!what_arg.empty()) {100what_arg += ": ";101}102what_arg += ec.message();103}104return what_arg;105}
106
107string make_error_str(const error_code& ec) {108if (ec) {109return ec.message();110}111return string();112}
113} // end namespace114
115string __do_message::message(int ev) const {116#if defined(_LIBCPP_HAS_NO_THREADS)117return string(::strerror(ev));118#else119return do_strerror_r(ev);120#endif121}
122
123class _LIBCPP_HIDDEN __generic_error_category : public __do_message {124public:125virtual const char* name() const noexcept;126virtual string message(int ev) const;127};128
129const char* __generic_error_category::name() const noexcept { return "generic"; }130
131string __generic_error_category::message(int ev) const {132#ifdef _LIBCPP_ELAST133if (ev > _LIBCPP_ELAST)134return string("unspecified generic_category error");135#endif // _LIBCPP_ELAST136return __do_message::message(ev);137}
138
139const error_category& generic_category() noexcept {140union AvoidDestroyingGenericCategory {141__generic_error_category generic_error_category;142constexpr explicit AvoidDestroyingGenericCategory() : generic_error_category() {}143~AvoidDestroyingGenericCategory() {}144};145constinit static AvoidDestroyingGenericCategory helper;146return helper.generic_error_category;147}
148
149class _LIBCPP_HIDDEN __system_error_category : public __do_message {150public:151virtual const char* name() const noexcept;152virtual string message(int ev) const;153virtual error_condition default_error_condition(int ev) const noexcept;154};155
156const char* __system_error_category::name() const noexcept { return "system"; }157
158string __system_error_category::message(int ev) const {159#ifdef _LIBCPP_ELAST160if (ev > _LIBCPP_ELAST)161return string("unspecified system_category error");162#endif // _LIBCPP_ELAST163return __do_message::message(ev);164}
165
166error_condition __system_error_category::default_error_condition(int ev) const noexcept {167#ifdef _LIBCPP_ELAST168if (ev > _LIBCPP_ELAST)169return error_condition(ev, system_category());170#endif // _LIBCPP_ELAST171return error_condition(ev, generic_category());172}
173
174const error_category& system_category() noexcept {175union AvoidDestroyingSystemCategory {176__system_error_category system_error_category;177constexpr explicit AvoidDestroyingSystemCategory() : system_error_category() {}178~AvoidDestroyingSystemCategory() {}179};180constinit static AvoidDestroyingSystemCategory helper;181return helper.system_error_category;182}
183
184// error_condition
185
186string error_condition::message() const { return __cat_->message(__val_); }187
188// error_code
189
190string error_code::message() const { return __cat_->message(__val_); }191
192// system_error
193
194system_error::system_error(error_code ec, const string& what_arg)195: runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}196
197system_error::system_error(error_code ec, const char* what_arg)198: runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}199
200system_error::system_error(error_code ec) : runtime_error(make_error_str(ec)), __ec_(ec) {}201
202system_error::system_error(int ev, const error_category& ecat, const string& what_arg)203: runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}204
205system_error::system_error(int ev, const error_category& ecat, const char* what_arg)206: runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}207
208system_error::system_error(int ev, const error_category& ecat)209: runtime_error(make_error_str(error_code(ev, ecat))), __ec_(error_code(ev, ecat)) {}210
211system_error::~system_error() noexcept {}212
213void __throw_system_error(int ev, const char* what_arg) {214#ifndef _LIBCPP_HAS_NO_EXCEPTIONS215std::__throw_system_error(error_code(ev, system_category()), what_arg);216#else217// The above could also handle the no-exception case, but for size, avoid referencing system_category() unnecessarily.218_LIBCPP_VERBOSE_ABORT(219"system_error was thrown in -fno-exceptions mode with error %i and message \"%s\"", ev, what_arg);220#endif221}
222
223_LIBCPP_END_NAMESPACE_STD
224