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