Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===---------------------- system_error.cpp ------------------------------===// |
| 2 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Jonathan Roelofs | a409d59 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 10 | #include "__config" |
Dan Albert | a76dfbd | 2015-01-06 17:34:51 +0000 | [diff] [blame] | 11 | |
| 12 | #define _LIBCPP_BUILDING_SYSTEM_ERROR |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 13 | #include "system_error" |
Dan Albert | a76dfbd | 2015-01-06 17:34:51 +0000 | [diff] [blame] | 14 | |
Eric Fiselier | e8fd164 | 2015-08-18 21:08:54 +0000 | [diff] [blame] | 15 | #include "include/config_elast.h" |
Eric Fiselier | 9778a6d | 2016-06-14 03:45:31 +0000 | [diff] [blame] | 16 | #include "cerrno" |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 17 | #include "cstring" |
Eric Fiselier | 9778a6d | 2016-06-14 03:45:31 +0000 | [diff] [blame] | 18 | #include "cstdio" |
| 19 | #include "cstdlib" |
| 20 | #include "cassert" |
Dan Albert | a76dfbd | 2015-01-06 17:34:51 +0000 | [diff] [blame] | 21 | #include "string" |
Eric Fiselier | 9778a6d | 2016-06-14 03:45:31 +0000 | [diff] [blame] | 22 | #include "string.h" |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | |
Dan Albert | 953d7d4 | 2016-06-15 20:20:32 +0000 | [diff] [blame] | 24 | #if defined(__ANDROID__) |
| 25 | #include <android/api-level.h> |
| 26 | #endif |
| 27 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 29 | |
| 30 | // class error_category |
| 31 | |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 32 | error_category::error_category() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 36 | error_category::~error_category() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
| 40 | error_condition |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 41 | error_category::default_error_condition(int ev) const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | { |
| 43 | return error_condition(ev, *this); |
| 44 | } |
| 45 | |
| 46 | bool |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 47 | error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | { |
| 49 | return default_error_condition(code) == condition; |
| 50 | } |
| 51 | |
| 52 | bool |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 53 | error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | { |
| 55 | return *this == code.category() && code.value() == condition; |
| 56 | } |
| 57 | |
Saleem Abdulrasool | c8bf961 | 2016-12-31 21:24:04 +0000 | [diff] [blame] | 58 | #if !defined(_LIBCPP_HAS_NO_THREADS) |
Eric Fiselier | 9778a6d | 2016-06-14 03:45:31 +0000 | [diff] [blame] | 59 | namespace { |
| 60 | |
| 61 | // GLIBC also uses 1024 as the maximum buffer size internally. |
| 62 | constexpr size_t strerror_buff_size = 1024; |
| 63 | |
| 64 | string do_strerror_r(int ev); |
| 65 | |
Dan Albert | 953d7d4 | 2016-06-15 20:20:32 +0000 | [diff] [blame] | 66 | #if defined(__linux__) && !defined(_LIBCPP_HAS_MUSL_LIBC) \ |
| 67 | && (!defined(__ANDROID__) || __ANDROID_API__ >= 23) |
Eric Fiselier | 9778a6d | 2016-06-14 03:45:31 +0000 | [diff] [blame] | 68 | // GNU Extended version |
| 69 | string do_strerror_r(int ev) { |
| 70 | char buffer[strerror_buff_size]; |
| 71 | char* ret = ::strerror_r(ev, buffer, strerror_buff_size); |
| 72 | return string(ret); |
| 73 | } |
| 74 | #else |
| 75 | // POSIX version |
| 76 | string do_strerror_r(int ev) { |
| 77 | char buffer[strerror_buff_size]; |
| 78 | const int old_errno = errno; |
Eric Fiselier | 61df790 | 2016-06-14 06:08:10 +0000 | [diff] [blame] | 79 | int ret; |
| 80 | if ((ret = ::strerror_r(ev, buffer, strerror_buff_size)) != 0) { |
Eric Fiselier | 79e0574 | 2016-06-14 06:03:20 +0000 | [diff] [blame] | 81 | // If `ret == -1` then the error is specified using `errno`, otherwise |
| 82 | // `ret` represents the error. |
| 83 | const int new_errno = ret == -1 ? errno : ret; |
Eric Fiselier | 9778a6d | 2016-06-14 03:45:31 +0000 | [diff] [blame] | 84 | errno = old_errno; |
| 85 | if (new_errno == EINVAL) { |
| 86 | std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev); |
| 87 | return string(buffer); |
| 88 | } else { |
| 89 | assert(new_errno == ERANGE); |
| 90 | // FIXME maybe? 'strerror_buff_size' is likely to exceed the |
| 91 | // maximum error size so ERANGE shouldn't be returned. |
| 92 | std::abort(); |
| 93 | } |
| 94 | } |
| 95 | return string(buffer); |
| 96 | } |
| 97 | #endif |
| 98 | |
| 99 | } // end namespace |
Saleem Abdulrasool | c8bf961 | 2016-12-31 21:24:04 +0000 | [diff] [blame] | 100 | #endif |
Eric Fiselier | 9778a6d | 2016-06-14 03:45:31 +0000 | [diff] [blame] | 101 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 102 | string |
| 103 | __do_message::message(int ev) const |
| 104 | { |
Eric Fiselier | 9778a6d | 2016-06-14 03:45:31 +0000 | [diff] [blame] | 105 | #if defined(_LIBCPP_HAS_NO_THREADS) |
| 106 | return string(::strerror(ev)); |
| 107 | #else |
| 108 | return do_strerror_r(ev); |
| 109 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | class _LIBCPP_HIDDEN __generic_error_category |
| 113 | : public __do_message |
| 114 | { |
| 115 | public: |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 116 | virtual const char* name() const _NOEXCEPT; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 117 | virtual string message(int ev) const; |
| 118 | }; |
| 119 | |
| 120 | const char* |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 121 | __generic_error_category::name() const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 122 | { |
| 123 | return "generic"; |
| 124 | } |
| 125 | |
| 126 | string |
| 127 | __generic_error_category::message(int ev) const |
| 128 | { |
Jonathan Roelofs | a409d59 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 129 | #ifdef _LIBCPP_ELAST |
| 130 | if (ev > _LIBCPP_ELAST) |
Howard Hinnant | 128ba71 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 131 | return string("unspecified generic_category error"); |
Jonathan Roelofs | a409d59 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 132 | #endif // _LIBCPP_ELAST |
Howard Hinnant | 128ba71 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 133 | return __do_message::message(ev); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | const error_category& |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 137 | generic_category() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 138 | { |
| 139 | static __generic_error_category s; |
| 140 | return s; |
| 141 | } |
| 142 | |
| 143 | class _LIBCPP_HIDDEN __system_error_category |
| 144 | : public __do_message |
| 145 | { |
| 146 | public: |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 147 | virtual const char* name() const _NOEXCEPT; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 148 | virtual string message(int ev) const; |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 149 | virtual error_condition default_error_condition(int ev) const _NOEXCEPT; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | const char* |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 153 | __system_error_category::name() const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 154 | { |
| 155 | return "system"; |
| 156 | } |
| 157 | |
| 158 | string |
| 159 | __system_error_category::message(int ev) const |
| 160 | { |
Jonathan Roelofs | a409d59 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 161 | #ifdef _LIBCPP_ELAST |
| 162 | if (ev > _LIBCPP_ELAST) |
Howard Hinnant | 128ba71 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 163 | return string("unspecified system_category error"); |
Jonathan Roelofs | a409d59 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 164 | #endif // _LIBCPP_ELAST |
Howard Hinnant | 128ba71 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 165 | return __do_message::message(ev); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | error_condition |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 169 | __system_error_category::default_error_condition(int ev) const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 170 | { |
Jonathan Roelofs | a409d59 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 171 | #ifdef _LIBCPP_ELAST |
| 172 | if (ev > _LIBCPP_ELAST) |
Howard Hinnant | 128ba71 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 173 | return error_condition(ev, system_category()); |
Jonathan Roelofs | a409d59 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 174 | #endif // _LIBCPP_ELAST |
Howard Hinnant | 128ba71 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 175 | return error_condition(ev, generic_category()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | const error_category& |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 179 | system_category() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 180 | { |
| 181 | static __system_error_category s; |
| 182 | return s; |
| 183 | } |
| 184 | |
| 185 | // error_condition |
| 186 | |
| 187 | string |
| 188 | error_condition::message() const |
| 189 | { |
| 190 | return __cat_->message(__val_); |
| 191 | } |
| 192 | |
| 193 | // error_code |
| 194 | |
| 195 | string |
| 196 | error_code::message() const |
| 197 | { |
| 198 | return __cat_->message(__val_); |
| 199 | } |
| 200 | |
| 201 | // system_error |
| 202 | |
| 203 | string |
| 204 | system_error::__init(const error_code& ec, string what_arg) |
| 205 | { |
| 206 | if (ec) |
| 207 | { |
| 208 | if (!what_arg.empty()) |
| 209 | what_arg += ": "; |
| 210 | what_arg += ec.message(); |
| 211 | } |
Richard Trieu | 1c545ba | 2015-04-30 21:47:28 +0000 | [diff] [blame] | 212 | return what_arg; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | system_error::system_error(error_code ec, const string& what_arg) |
| 216 | : runtime_error(__init(ec, what_arg)), |
| 217 | __ec_(ec) |
| 218 | { |
| 219 | } |
| 220 | |
| 221 | system_error::system_error(error_code ec, const char* what_arg) |
| 222 | : runtime_error(__init(ec, what_arg)), |
| 223 | __ec_(ec) |
| 224 | { |
| 225 | } |
| 226 | |
| 227 | system_error::system_error(error_code ec) |
| 228 | : runtime_error(__init(ec, "")), |
| 229 | __ec_(ec) |
| 230 | { |
| 231 | } |
| 232 | |
| 233 | system_error::system_error(int ev, const error_category& ecat, const string& what_arg) |
| 234 | : runtime_error(__init(error_code(ev, ecat), what_arg)), |
| 235 | __ec_(error_code(ev, ecat)) |
| 236 | { |
| 237 | } |
| 238 | |
| 239 | system_error::system_error(int ev, const error_category& ecat, const char* what_arg) |
| 240 | : runtime_error(__init(error_code(ev, ecat), what_arg)), |
| 241 | __ec_(error_code(ev, ecat)) |
| 242 | { |
| 243 | } |
| 244 | |
| 245 | system_error::system_error(int ev, const error_category& ecat) |
| 246 | : runtime_error(__init(error_code(ev, ecat), "")), |
| 247 | __ec_(error_code(ev, ecat)) |
| 248 | { |
| 249 | } |
| 250 | |
Howard Hinnant | a62f289 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 251 | system_error::~system_error() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 252 | { |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | __throw_system_error(int ev, const char* what_arg) |
| 257 | { |
Howard Hinnant | 54b409f | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 258 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 259 | throw system_error(error_code(ev, system_category()), what_arg); |
Howard Hinnant | e00e6f2 | 2013-03-28 18:56:26 +0000 | [diff] [blame] | 260 | #else |
| 261 | (void)ev; |
| 262 | (void)what_arg; |
Marshall Clow | d437fa5 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 263 | _VSTD::abort(); |
Howard Hinnant | 54b409f | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 264 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | _LIBCPP_END_NAMESPACE_STD |