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