aboutsummaryrefslogtreecommitdiff
path: root/lib/libcxx/src/system_error.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2024-05-09 01:52:26 -0700
committerAndrew Kelley <andrew@ziglang.org>2024-05-09 01:52:26 -0700
commitbcb534c295d5cc6fd63caa570cc08e6b148a507c (patch)
tree0b17cb1e632d894f50f25e550d5113f232b0e877 /lib/libcxx/src/system_error.cpp
parentd9b00ee4ba48717ff6b306a6f9419e7b604ac04b (diff)
parent74f52954b9cb40d59d80b839b45bb859146731a7 (diff)
downloadzig-bcb534c295d5cc6fd63caa570cc08e6b148a507c.tar.gz
zig-bcb534c295d5cc6fd63caa570cc08e6b148a507c.zip
Merge branch 'llvm18'
Upgrades the LLVM, Clang, and LLD dependencies to LLVM 18.x Related to #16270
Diffstat (limited to 'lib/libcxx/src/system_error.cpp')
-rw-r--r--lib/libcxx/src/system_error.cpp262
1 files changed, 94 insertions, 168 deletions
diff --git a/lib/libcxx/src/system_error.cpp b/lib/libcxx/src/system_error.cpp
index 7875f59d88..f518b480a2 100644
--- a/lib/libcxx/src/system_error.cpp
+++ b/lib/libcxx/src/system_error.cpp
@@ -6,68 +6,34 @@
//
//===----------------------------------------------------------------------===//
-#include <__config>
-#ifdef _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
-# define _LIBCPP_ERROR_CATEGORY_DEFINE_LEGACY_INLINE_FUNCTIONS
-#endif
-
#include <__assert>
+#include <__config>
#include <__verbose_abort>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
-#include <string>
#include <string.h>
+#include <string>
#include <system_error>
#include "include/config_elast.h"
#if defined(__ANDROID__)
-#include <android/api-level.h>
+# include <android/api-level.h>
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
-// class error_category
-
-#if defined(_LIBCPP_ERROR_CATEGORY_DEFINE_LEGACY_INLINE_FUNCTIONS)
-error_category::error_category() noexcept
-{
-}
-#endif
-
-error_category::~error_category() noexcept
-{
-}
-
-error_condition
-error_category::default_error_condition(int ev) const noexcept
-{
- return error_condition(ev, *this);
-}
-
-bool
-error_category::equivalent(int code, const error_condition& condition) const noexcept
-{
- return default_error_condition(code) == condition;
-}
-
-bool
-error_category::equivalent(const error_code& code, int condition) const noexcept
-{
- return *this == code.category() && code.value() == condition;
-}
-
-#if !defined(_LIBCPP_HAS_NO_THREADS)
namespace {
+#if !defined(_LIBCPP_HAS_NO_THREADS)
// GLIBC also uses 1024 as the maximum buffer size internally.
constexpr size_t strerror_buff_size = 1024;
string do_strerror_r(int ev);
-#if defined(_LIBCPP_MSVCRT_LIKE)
+# if defined(_LIBCPP_MSVCRT_LIKE)
string do_strerror_r(int ev) {
char buffer[strerror_buff_size];
if (::strerror_s(buffer, strerror_buff_size, ev) == 0)
@@ -75,14 +41,13 @@ string do_strerror_r(int ev) {
std::snprintf(buffer, strerror_buff_size, "unknown error %d", ev);
return string(buffer);
}
-#else
+# else
// Only one of the two following functions will be used, depending on
// the return type of strerror_r:
// For the GNU variant, a char* return value:
-__attribute__((unused)) const char *
-handle_strerror_r_return(char *strerror_return, char *buffer) {
+__attribute__((unused)) const char* handle_strerror_r_return(char* strerror_return, char* buffer) {
// GNU always returns a string pointer in its return value. The
// string might point to either the input buffer, or a static
// buffer, but we don't care which.
@@ -90,8 +55,7 @@ handle_strerror_r_return(char *strerror_return, char *buffer) {
}
// For the POSIX variant: an int return value.
-__attribute__((unused)) const char *
-handle_strerror_r_return(int strerror_return, char *buffer) {
+__attribute__((unused)) const char* handle_strerror_r_return(int strerror_return, char* buffer) {
// The POSIX variant either:
// - fills in the provided buffer and returns 0
// - returns a positive error value, or
@@ -104,7 +68,7 @@ handle_strerror_r_return(int strerror_return, char *buffer) {
if (new_errno == EINVAL)
return "";
- _LIBCPP_ASSERT_UNCATEGORIZED(new_errno == ERANGE, "unexpected error from ::strerror_r");
+ _LIBCPP_ASSERT_INTERNAL(new_errno == ERANGE, "unexpected error from ::strerror_r");
// FIXME maybe? 'strerror_buff_size' is likely to exceed the
// maximum error size so ERANGE shouldn't be returned.
std::abort();
@@ -113,184 +77,146 @@ handle_strerror_r_return(int strerror_return, char *buffer) {
// This function handles both GNU and POSIX variants, dispatching to
// one of the two above functions.
string do_strerror_r(int ev) {
- char buffer[strerror_buff_size];
- // Preserve errno around the call. (The C++ standard requires that
- // system_error functions not modify errno).
- const int old_errno = errno;
- const char *error_message = handle_strerror_r_return(
- ::strerror_r(ev, buffer, strerror_buff_size), buffer);
- // If we didn't get any message, print one now.
- if (!error_message[0]) {
- std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);
- error_message = buffer;
+ char buffer[strerror_buff_size];
+ // Preserve errno around the call. (The C++ standard requires that
+ // system_error functions not modify errno).
+ const int old_errno = errno;
+ const char* error_message = handle_strerror_r_return(::strerror_r(ev, buffer, strerror_buff_size), buffer);
+ // If we didn't get any message, print one now.
+ if (!error_message[0]) {
+ std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);
+ error_message = buffer;
+ }
+ errno = old_errno;
+ return string(error_message);
+}
+# endif
+
+#endif // !defined(_LIBCPP_HAS_NO_THREADS)
+
+string make_error_str(const error_code& ec, string what_arg) {
+ if (ec) {
+ if (!what_arg.empty()) {
+ what_arg += ": ";
}
- errno = old_errno;
- return string(error_message);
+ what_arg += ec.message();
+ }
+ return what_arg;
+}
+
+string make_error_str(const error_code& ec) {
+ if (ec) {
+ return ec.message();
+ }
+ return string();
}
-#endif
} // end namespace
-#endif
-string
-__do_message::message(int ev) const
-{
+string __do_message::message(int ev) const {
#if defined(_LIBCPP_HAS_NO_THREADS)
- return string(::strerror(ev));
+ return string(::strerror(ev));
#else
- return do_strerror_r(ev);
+ return do_strerror_r(ev);
#endif
}
-class _LIBCPP_HIDDEN __generic_error_category
- : public __do_message
-{
+class _LIBCPP_HIDDEN __generic_error_category : public __do_message {
public:
- virtual const char* name() const noexcept;
- virtual string message(int ev) const;
+ virtual const char* name() const noexcept;
+ virtual string message(int ev) const;
};
-const char*
-__generic_error_category::name() const noexcept
-{
- return "generic";
-}
+const char* __generic_error_category::name() const noexcept { return "generic"; }
-string
-__generic_error_category::message(int ev) const
-{
+string __generic_error_category::message(int ev) const {
#ifdef _LIBCPP_ELAST
- if (ev > _LIBCPP_ELAST)
- return string("unspecified generic_category error");
+ if (ev > _LIBCPP_ELAST)
+ return string("unspecified generic_category error");
#endif // _LIBCPP_ELAST
- return __do_message::message(ev);
+ return __do_message::message(ev);
}
-const error_category&
-generic_category() noexcept
-{
- static __generic_error_category s;
- return s;
+const error_category& generic_category() noexcept {
+ union AvoidDestroyingGenericCategory {
+ __generic_error_category generic_error_category;
+ constexpr explicit AvoidDestroyingGenericCategory() : generic_error_category() {}
+ ~AvoidDestroyingGenericCategory() {}
+ };
+ constinit static AvoidDestroyingGenericCategory helper;
+ return helper.generic_error_category;
}
-class _LIBCPP_HIDDEN __system_error_category
- : public __do_message
-{
+class _LIBCPP_HIDDEN __system_error_category : public __do_message {
public:
- virtual const char* name() const noexcept;
- virtual string message(int ev) const;
- virtual error_condition default_error_condition(int ev) const noexcept;
+ virtual const char* name() const noexcept;
+ virtual string message(int ev) const;
+ virtual error_condition default_error_condition(int ev) const noexcept;
};
-const char*
-__system_error_category::name() const noexcept
-{
- return "system";
-}
+const char* __system_error_category::name() const noexcept { return "system"; }
-string
-__system_error_category::message(int ev) const
-{
+string __system_error_category::message(int ev) const {
#ifdef _LIBCPP_ELAST
- if (ev > _LIBCPP_ELAST)
- return string("unspecified system_category error");
+ if (ev > _LIBCPP_ELAST)
+ return string("unspecified system_category error");
#endif // _LIBCPP_ELAST
- return __do_message::message(ev);
+ return __do_message::message(ev);
}
-error_condition
-__system_error_category::default_error_condition(int ev) const noexcept
-{
+error_condition __system_error_category::default_error_condition(int ev) const noexcept {
#ifdef _LIBCPP_ELAST
- if (ev > _LIBCPP_ELAST)
- return error_condition(ev, system_category());
+ if (ev > _LIBCPP_ELAST)
+ return error_condition(ev, system_category());
#endif // _LIBCPP_ELAST
- return error_condition(ev, generic_category());
+ return error_condition(ev, generic_category());
}
-const error_category&
-system_category() noexcept
-{
- static __system_error_category s;
- return s;
+const error_category& system_category() noexcept {
+ union AvoidDestroyingSystemCategory {
+ __system_error_category system_error_category;
+ constexpr explicit AvoidDestroyingSystemCategory() : system_error_category() {}
+ ~AvoidDestroyingSystemCategory() {}
+ };
+ constinit static AvoidDestroyingSystemCategory helper;
+ return helper.system_error_category;
}
// error_condition
-string
-error_condition::message() const
-{
- return __cat_->message(__val_);
-}
+string error_condition::message() const { return __cat_->message(__val_); }
// error_code
-string
-error_code::message() const
-{
- return __cat_->message(__val_);
-}
+string error_code::message() const { return __cat_->message(__val_); }
// system_error
-string
-system_error::__init(const error_code& ec, string what_arg)
-{
- if (ec)
- {
- if (!what_arg.empty())
- what_arg += ": ";
- what_arg += ec.message();
- }
- return what_arg;
-}
-
system_error::system_error(error_code ec, const string& what_arg)
- : runtime_error(__init(ec, what_arg)),
- __ec_(ec)
-{
-}
+ : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}
system_error::system_error(error_code ec, const char* what_arg)
- : runtime_error(__init(ec, what_arg)),
- __ec_(ec)
-{
-}
+ : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}
-system_error::system_error(error_code ec)
- : runtime_error(__init(ec, "")),
- __ec_(ec)
-{
-}
+system_error::system_error(error_code ec) : runtime_error(make_error_str(ec)), __ec_(ec) {}
system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
- : runtime_error(__init(error_code(ev, ecat), what_arg)),
- __ec_(error_code(ev, ecat))
-{
-}
+ : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}
system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
- : runtime_error(__init(error_code(ev, ecat), what_arg)),
- __ec_(error_code(ev, ecat))
-{
-}
+ : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}
system_error::system_error(int ev, const error_category& ecat)
- : runtime_error(__init(error_code(ev, ecat), "")),
- __ec_(error_code(ev, ecat))
-{
-}
+ : runtime_error(make_error_str(error_code(ev, ecat))), __ec_(error_code(ev, ecat)) {}
-system_error::~system_error() noexcept
-{
-}
+system_error::~system_error() noexcept {}
-void
-__throw_system_error(int ev, const char* what_arg)
-{
+void __throw_system_error(int ev, const char* what_arg) {
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- throw system_error(error_code(ev, system_category()), what_arg);
+ std::__throw_system_error(error_code(ev, system_category()), what_arg);
#else
- _LIBCPP_VERBOSE_ABORT("system_error was thrown in -fno-exceptions mode with error %i and message \"%s\"", ev, what_arg);
+ // The above could also handle the no-exception case, but for size, avoid referencing system_category() unnecessarily.
+ _LIBCPP_VERBOSE_ABORT(
+ "system_error was thrown in -fno-exceptions mode with error %i and message \"%s\"", ev, what_arg);
#endif
}