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