diff options
author | Bruce Mitchener <bruce.mitchener@gmail.com> | 2013-02-17 14:29:14 +0700 |
---|---|---|
committer | Bruce Mitchener <bruce.mitchener@gmail.com> | 2013-03-25 00:34:11 +0700 |
commit | 59ff5a6a3c3e1f5255c5cf29f98df633a77b89b3 (patch) | |
tree | c7660fa62600366e3479dbf6b2fd1d25709af1b5 /system/lib/libcxx | |
parent | 80fd6f0bce2b95db6ec539c9275ce24585550e7c (diff) |
Update to current libcxx.
This doesn't work yet as it needs to be customized for use with
emscripten still.
Diffstat (limited to 'system/lib/libcxx')
29 files changed, 1758 insertions, 641 deletions
diff --git a/system/lib/libcxx/CREDITS.TXT b/system/lib/libcxx/CREDITS.TXT new file mode 100644 index 00000000..52948510 --- /dev/null +++ b/system/lib/libcxx/CREDITS.TXT @@ -0,0 +1,91 @@ +This file is a partial list of people who have contributed to the LLVM/libc++ +project. If you have contributed a patch or made some other contribution to +LLVM/libc++, please submit a patch to this file to add yourself, and it will be +done! + +The list is sorted by surname and formatted to allow easy grepping and +beautification by scripts. The fields are: name (N), email (E), web-address +(W), PGP key ID and fingerprint (P), description (D), and snail-mail address +(S). + +N: Saleem Abdulrasool +E: compnerd@compnerd.org +D: Minor patches and Linux fixes. + +N: Dimitry Andric +E: dimitry@andric.com +D: Visibility fixes, minor FreeBSD portability patches. + +N: Holger Arnold +E: holgerar@gmail.com +D: Minor fix. + +N: Ruben Van Boxem +E: vanboxem dot ruben at gmail dot com +D: Initial Windows patches. + +N: David Chisnall +E: theraven at theravensnest dot org +D: FreeBSD and Solaris ports, libcxxrt support, some atomics work. + +N: Marshall Clow +E: mclow.lists@gmail.com +E: marshall@idio.com +D: Minor patches and bug fixes. + +N: Google Inc. +D: Copyright owner and contributor of the CityHash algorithm + +N: Howard Hinnant +E: hhinnant@apple.com +D: Architect and primary author of libc++ + +N: Hyeon-bin Jeong +E: tuhertz@gmail.com +D: Minor patches and bug fixes. + +N: Argyrios Kyrtzidis +E: kyrtzidis@apple.com +D: Bug fixes. + +N: Michel Morin +E: mimomorin@gmail.com +D: Minor patches to is_convertible. + +N: Andrew Morrow +E: andrew.c.morrow@gmail.com +D: Minor patches and Linux fixes. + +N: Arvid Picciani +E: aep at exys dot org +D: Minor patches and musl port. + +N: Bjorn Reese +E: breese@users.sourceforge.net +D: Initial regex prototype + +N: Jonathan Sauer +D: Minor patches, mostly related to constexpr + +N: Craig Silverstein +E: csilvers@google.com +D: Implemented Cityhash as the string hash function on 64-bit machines + +N: Richard Smith +D: Minor patches. + +N: Michael van der Westhuizen +E: r1mikey at gmail dot com + +N: Klaas de Vries +E: klaas at klaasgaaf dot nl +D: Minor bug fix. + +N: Zhang Xiongpang +E: zhangxiongpang@gmail.com +D: Minor patches and bug fixes. + +N: Jeffrey Yasskin +E: jyasskin@gmail.com +E: jyasskin@google.com +D: Linux fixes. diff --git a/system/lib/libcxx/LICENSE.txt b/system/lib/libcxx/LICENSE.txt index 926f0676..5ed8ec22 100644 --- a/system/lib/libcxx/LICENSE.txt +++ b/system/lib/libcxx/LICENSE.txt @@ -14,7 +14,7 @@ Full text of the relevant licenses is included below. University of Illinois/NCSA Open Source License -Copyright (c) 2009-2010 by the contributors listed in CREDITS.TXT +Copyright (c) 2009-2013 by the contributors listed in CREDITS.TXT All rights reserved. @@ -55,7 +55,7 @@ SOFTWARE. ============================================================================== -Copyright (c) 2009-2010 by the contributors listed in CREDITS.TXT +Copyright (c) 2009-2013 by the contributors listed in CREDITS.TXT Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/system/lib/libcxx/chrono.cpp b/system/lib/libcxx/chrono.cpp index 416b9501..1ce2e280 100644 --- a/system/lib/libcxx/chrono.cpp +++ b/system/lib/libcxx/chrono.cpp @@ -24,6 +24,8 @@ namespace chrono // system_clock +const bool system_clock::is_steady; + system_clock::time_point system_clock::now() _NOEXCEPT { @@ -46,6 +48,8 @@ system_clock::from_time_t(time_t t) _NOEXCEPT // steady_clock +const bool steady_clock::is_steady; + #if __APPLE__ // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom @@ -61,7 +65,7 @@ static steady_clock::rep steady_simplified() { - return mach_absolute_time(); + return static_cast<steady_clock::rep>(mach_absolute_time()); } static diff --git a/system/lib/libcxx/condition_variable.cpp b/system/lib/libcxx/condition_variable.cpp index ca1dca32..de0f6f47 100644 --- a/system/lib/libcxx/condition_variable.cpp +++ b/system/lib/libcxx/condition_variable.cpp @@ -16,18 +16,17 @@ _LIBCPP_BEGIN_NAMESPACE_STD condition_variable::~condition_variable() { - int e = pthread_cond_destroy(&__cv_); -// assert(e == 0); + pthread_cond_destroy(&__cv_); } void -condition_variable::notify_one() +condition_variable::notify_one() _NOEXCEPT { pthread_cond_signal(&__cv_); } void -condition_variable::notify_all() +condition_variable::notify_all() _NOEXCEPT { pthread_cond_broadcast(&__cv_); } @@ -52,10 +51,22 @@ condition_variable::__do_timed_wait(unique_lock<mutex>& lk, __throw_system_error(EPERM, "condition_variable::timed wait: mutex not locked"); nanoseconds d = tp.time_since_epoch(); + if (d > nanoseconds(0x59682F000000E941)) + d = nanoseconds(0x59682F000000E941); timespec ts; seconds s = duration_cast<seconds>(d); - ts.tv_sec = static_cast<decltype(ts.tv_sec)>(s.count()); - ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count()); + typedef decltype(ts.tv_sec) ts_sec; + _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max(); + if (s.count() < ts_sec_max) + { + ts.tv_sec = static_cast<ts_sec>(s.count()); + ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count()); + } + else + { + ts.tv_sec = ts_sec_max; + ts.tv_nsec = giga::num - 1; + } int ec = pthread_cond_timedwait(&__cv_, lk.mutex()->native_handle(), &ts); if (ec != 0 && ec != ETIMEDOUT) __throw_system_error(ec, "condition_variable timed_wait failed"); diff --git a/system/lib/libcxx/debug.cpp b/system/lib/libcxx/debug.cpp index 2d2d6432..f3a0262d 100644 --- a/system/lib/libcxx/debug.cpp +++ b/system/lib/libcxx/debug.cpp @@ -23,7 +23,7 @@ __get_db() { static __libcpp_db db; return &db; -}; +} _LIBCPP_VISIBLE const __libcpp_db* @@ -120,20 +120,18 @@ __libcpp_db::__insert_ic(void* __i, const void* __c) { WLock _(mut()); __i_node* i = __insert_iterator(__i); - _LIBCPP_ASSERT(__cbeg_ != __cend_, "Container constructed in a translation unit with debug mode disabled." - " But it is being used in a translation unit with debug mode enabled." - " Enable it in the other translation unit with #define _LIBCPP_DEBUG2 1"); - size_t hc = hash<const void*>()(__c) % (__cend_ - __cbeg_); + const char* errmsg = + "Container constructed in a translation unit with debug mode disabled." + " But it is being used in a translation unit with debug mode enabled." + " Enable it in the other translation unit with #define _LIBCPP_DEBUG2 1"; + _LIBCPP_ASSERT(__cbeg_ != __cend_, errmsg); + size_t hc = hash<const void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_); __c_node* c = __cbeg_[hc]; - _LIBCPP_ASSERT(c != nullptr, "Container constructed in a translation unit with debug mode disabled." - " But it is being used in a translation unit with debug mode enabled." - " Enable it in the other translation unit with #define _LIBCPP_DEBUG2 1"); + _LIBCPP_ASSERT(c != nullptr, errmsg); while (c->__c_ != __c) { c = c->__next_; - _LIBCPP_ASSERT(c != nullptr, "Container constructed in a translation unit with debug mode disabled." - " But it is being used in a translation unit with debug mode enabled." - " Enable it in the other translation unit with #define _LIBCPP_DEBUG2 1"); + _LIBCPP_ASSERT(c != nullptr, errmsg); } c->__add(i); i->__c_ = c; @@ -143,12 +141,16 @@ __c_node* __libcpp_db::__insert_c(void* __c) { WLock _(mut()); - if (__csz_ + 1 > __cend_ - __cbeg_) + if (__csz_ + 1 > static_cast<size_t>(__cend_ - __cbeg_)) { - size_t nc = __next_prime(2*(__cend_ - __cbeg_) + 1); + size_t nc = __next_prime(2*static_cast<size_t>(__cend_ - __cbeg_) + 1); __c_node** cbeg = (__c_node**)calloc(nc, sizeof(void*)); if (cbeg == nullptr) +#ifndef _LIBCPP_NO_EXCEPTIONS throw bad_alloc(); +#else + abort(); +#endif for (__c_node** p = __cbeg_; p != __cend_; ++p) { __c_node* q = *p; @@ -165,11 +167,15 @@ __libcpp_db::__insert_c(void* __c) __cbeg_ = cbeg; __cend_ = __cbeg_ + nc; } - size_t hc = hash<void*>()(__c) % (__cend_ - __cbeg_); + size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_); __c_node* p = __cbeg_[hc]; __c_node* r = __cbeg_[hc] = (__c_node*)malloc(sizeof(__c_node)); if (__cbeg_[hc] == nullptr) +#ifndef _LIBCPP_NO_EXCEPTIONS throw bad_alloc(); +#else + abort(); +#endif r->__c_ = __c; r->__next_ = p; ++__csz_; @@ -182,7 +188,7 @@ __libcpp_db::__erase_i(void* __i) WLock _(mut()); if (__ibeg_ != __iend_) { - size_t hi = hash<void*>()(__i) % (__iend_ - __ibeg_); + size_t hi = hash<void*>()(__i) % static_cast<size_t>(__iend_ - __ibeg_); __i_node* p = __ibeg_[hi]; if (p != nullptr) { @@ -211,7 +217,7 @@ void __libcpp_db::__invalidate_all(void* __c) { WLock _(mut()); - size_t hc = hash<void*>()(__c) % (__cend_ - __cbeg_); + size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_); __c_node* p = __cbeg_[hc]; _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __invalidate_all A"); while (p->__c_ != __c) @@ -230,7 +236,7 @@ __c_node* __libcpp_db::__find_c_and_lock(void* __c) const { mut().lock(); - size_t hc = hash<void*>()(__c) % (__cend_ - __cbeg_); + size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_); __c_node* p = __cbeg_[hc]; _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __find_c_and_lock A"); while (p->__c_ != __c) @@ -241,6 +247,20 @@ __libcpp_db::__find_c_and_lock(void* __c) const return p; } +__c_node* +__libcpp_db::__find_c(void* __c) const +{ + size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_); + __c_node* p = __cbeg_[hc]; + _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __find_c A"); + while (p->__c_ != __c) + { + p = p->__next_; + _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __find_c B"); + } + return p; +} + void __libcpp_db::unlock() const { @@ -251,7 +271,7 @@ void __libcpp_db::__erase_c(void* __c) { WLock _(mut()); - size_t hc = hash<void*>()(__c) % (__cend_ - __cbeg_); + size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_); __c_node* p = __cbeg_[hc]; __c_node* q = nullptr; _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __erase_c A"); @@ -348,7 +368,7 @@ void __libcpp_db::swap(void* c1, void* c2) { WLock _(mut()); - size_t hc = hash<void*>()(c1) % (__cend_ - __cbeg_); + size_t hc = hash<void*>()(c1) % static_cast<size_t>(__cend_ - __cbeg_); __c_node* p1 = __cbeg_[hc]; _LIBCPP_ASSERT(p1 != nullptr, "debug mode internal logic error swap A"); while (p1->__c_ != c1) @@ -356,7 +376,7 @@ __libcpp_db::swap(void* c1, void* c2) p1 = p1->__next_; _LIBCPP_ASSERT(p1 != nullptr, "debug mode internal logic error swap B"); } - hc = hash<void*>()(c2) % (__cend_ - __cbeg_); + hc = hash<void*>()(c2) % static_cast<size_t>(__cend_ - __cbeg_); __c_node* p2 = __cbeg_[hc]; _LIBCPP_ASSERT(p2 != nullptr, "debug mode internal logic error swap C"); while (p2->__c_ != c2) @@ -380,18 +400,47 @@ __libcpp_db::__insert_i(void* __i) __insert_iterator(__i); } +void +__c_node::__add(__i_node* i) +{ + if (end_ == cap_) + { + size_t nc = 2*static_cast<size_t>(cap_ - beg_); + if (nc == 0) + nc = 1; + __i_node** beg = (__i_node**)malloc(nc * sizeof(__i_node*)); + if (beg == nullptr) +#ifndef _LIBCPP_NO_EXCEPTIONS + throw bad_alloc(); +#else + abort(); +#endif + if (nc > 1) + memcpy(beg, beg_, nc/2*sizeof(__i_node*)); + free(beg_); + beg_ = beg; + end_ = beg_ + nc/2; + cap_ = beg_ + nc; + } + *end_++ = i; +} + // private api _LIBCPP_HIDDEN __i_node* __libcpp_db::__insert_iterator(void* __i) { - if (__isz_ + 1 > __iend_ - __ibeg_) + if (__isz_ + 1 > static_cast<size_t>(__iend_ - __ibeg_)) { - size_t nc = __next_prime(2*(__iend_ - __ibeg_) + 1); + size_t nc = __next_prime(2*static_cast<size_t>(__iend_ - __ibeg_) + 1); __i_node** ibeg = (__i_node**)calloc(nc, sizeof(void*)); if (ibeg == nullptr) +#ifndef _LIBCPP_NO_EXCEPTIONS throw bad_alloc(); +#else + abort(); +#endif for (__i_node** p = __ibeg_; p != __iend_; ++p) { __i_node* q = *p; @@ -408,11 +457,15 @@ __libcpp_db::__insert_iterator(void* __i) __ibeg_ = ibeg; __iend_ = __ibeg_ + nc; } - size_t hi = hash<void*>()(__i) % (__iend_ - __ibeg_); + size_t hi = hash<void*>()(__i) % static_cast<size_t>(__iend_ - __ibeg_); __i_node* p = __ibeg_[hi]; __i_node* r = __ibeg_[hi] = (__i_node*)malloc(sizeof(__i_node)); if (r == nullptr) +#ifndef _LIBCPP_NO_EXCEPTIONS throw bad_alloc(); +#else + abort(); +#endif ::new(r) __i_node(__i, p, nullptr); ++__isz_; return r; @@ -425,7 +478,7 @@ __libcpp_db::__find_iterator(const void* __i) const __i_node* r = nullptr; if (__ibeg_ != __iend_) { - size_t h = hash<const void*>()(__i) % (__iend_ - __ibeg_); + size_t h = hash<const void*>()(__i) % static_cast<size_t>(__iend_ - __ibeg_); for (__i_node* nd = __ibeg_[h]; nd != nullptr; nd = nd->__next_) { if (nd->__i_ == __i) @@ -440,34 +493,12 @@ __libcpp_db::__find_iterator(const void* __i) const _LIBCPP_HIDDEN void -__c_node::__add(__i_node* i) -{ - if (end_ == cap_) - { - size_t nc = 2*(cap_ - beg_); - if (nc == 0) - nc = 1; - __i_node** beg = (__i_node**)malloc(nc * sizeof(__i_node*)); - if (beg == nullptr) - throw bad_alloc(); - if (nc > 1) - memcpy(beg, beg_, nc/2*sizeof(__i_node*)); - free(beg_); - beg_ = beg; - end_ = beg_ + nc/2; - cap_ = beg_ + nc; - } - *end_++ = i; -} - -_LIBCPP_HIDDEN -void __c_node::__remove(__i_node* p) { __i_node** r = find(beg_, end_, p); _LIBCPP_ASSERT(r != end_, "debug mode internal logic error __c_node::__remove"); if (--end_ != r) - memmove(r, r+1, (end_ - r)*sizeof(__i_node*)); + memmove(r, r+1, static_cast<size_t>(end_ - r)*sizeof(__i_node*)); } _LIBCPP_END_NAMESPACE_STD diff --git a/system/lib/libcxx/exception.cpp b/system/lib/libcxx/exception.cpp index cba355e9..0cd182b7 100644 --- a/system/lib/libcxx/exception.cpp +++ b/system/lib/libcxx/exception.cpp @@ -10,69 +10,82 @@ #include "exception" +#ifndef __has_include +#define __has_include(inc) 0 +#endif + #if __APPLE__ #include <cxxabi.h> + using namespace __cxxabiv1; - using namespace __cxxabiapple; - // On Darwin, there are two STL shared libraries and a lower level ABI - // shared libray. The globals holding the current terminate handler and - // current unexpected handler are in the ABI library. - #define __terminate_handler __cxxabiapple::__cxa_terminate_handler - #define __unexpected_handler __cxxabiapple::__cxa_unexpected_handler #define HAVE_DEPENDENT_EH_ABI 1 -#elif defined(LIBCXXRT) + #ifndef _LIBCPPABI_VERSION + using namespace __cxxabiapple; + // On Darwin, there are two STL shared libraries and a lower level ABI + // shared libray. The globals holding the current terminate handler and + // current unexpected handler are in the ABI library. + #define __terminate_handler __cxxabiapple::__cxa_terminate_handler + #define __unexpected_handler __cxxabiapple::__cxa_unexpected_handler + #endif // _LIBCPPABI_VERSION +#elif defined(LIBCXXRT) || __has_include(<cxxabi.h>) #include <cxxabi.h> using namespace __cxxabiv1; - #define HAVE_DEPENDENT_EH_ABI 1 -#else // __APPLE__ + #if defined(LIBCXXRT) || defined(_LIBCPPABI_VERSION) + #define HAVE_DEPENDENT_EH_ABI 1 + #endif +#elif !defined(__GLIBCXX__) // __has_include(<cxxabi.h>) static std::terminate_handler __terminate_handler; static std::unexpected_handler __unexpected_handler; -#endif // __APPLE__ +#endif // __has_include(<cxxabi.h>) + +namespace std +{ + +#if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) -#ifndef LIBCXXRT // libcxxrt provides implementations of these functions itself. -std::unexpected_handler -std::set_unexpected(std::unexpected_handler func) _NOEXCEPT +unexpected_handler +set_unexpected(unexpected_handler func) _NOEXCEPT { return __sync_lock_test_and_set(&__unexpected_handler, func); } -std::unexpected_handler -std::get_unexpected() _NOEXCEPT +unexpected_handler +get_unexpected() _NOEXCEPT { - return __sync_fetch_and_add(&__unexpected_handler, (std::unexpected_handler)0); + return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); } -_ATTRIBUTE(noreturn) +_LIBCPP_NORETURN void -std::unexpected() +unexpected() { - (*std::get_unexpected())(); + (*get_unexpected())(); // unexpected handler should not return - std::terminate(); + terminate(); } -std::terminate_handler -std::set_terminate(std::terminate_handler func) _NOEXCEPT +terminate_handler +set_terminate(terminate_handler func) _NOEXCEPT { return __sync_lock_test_and_set(&__terminate_handler, func); } -std::terminate_handler -std::get_terminate() _NOEXCEPT +terminate_handler +get_terminate() _NOEXCEPT { - return __sync_fetch_and_add(&__terminate_handler, (std::terminate_handler)0); + return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); } -_ATTRIBUTE(noreturn) +_LIBCPP_NORETURN void -std::terminate() _NOEXCEPT +terminate() _NOEXCEPT { #ifndef _LIBCPP_NO_EXCEPTIONS try { #endif // _LIBCPP_NO_EXCEPTIONS - (*std::get_terminate())(); + (*get_terminate())(); // handler should not return ::abort (); #ifndef _LIBCPP_NO_EXCEPTIONS @@ -84,38 +97,37 @@ std::terminate() _NOEXCEPT } #endif // _LIBCPP_NO_EXCEPTIONS } -#endif // LIBCXXRT +#endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) -#ifndef EMSCRIPTEN // This is implemented in Javascript for Emscripten -bool std::uncaught_exception() _NOEXCEPT +#if !defined(LIBCXXRT) && !defined(__GLIBCXX__) +bool uncaught_exception() _NOEXCEPT { -#if __APPLE__ +#if __APPLE__ || defined(_LIBCPPABI_VERSION) // on Darwin, there is a helper function so __cxa_get_globals is private - return __cxxabiapple::__cxa_uncaught_exception(); -#elif LIBCXXRT - __cxa_eh_globals * globals = __cxa_get_globals(); - return (globals->uncaughtExceptions != 0); + return __cxa_uncaught_exception(); #else // __APPLE__ #warning uncaught_exception not yet implemented ::abort(); #endif // __APPLE__ } -#endif // EMSCRIPTEN -namespace std -{ +#ifndef _LIBCPPABI_VERSION exception::~exception() _NOEXCEPT { } -bad_exception::~bad_exception() _NOEXCEPT +const char* exception::what() const _NOEXCEPT { + return "std::exception"; } -const char* exception::what() const _NOEXCEPT +#endif // _LIBCPPABI_VERSION +#endif //LIBCXXRT +#if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) + +bad_exception::~bad_exception() _NOEXCEPT { - return "std::exception"; } const char* bad_exception::what() const _NOEXCEPT @@ -123,6 +135,9 @@ const char* bad_exception::what() const _NOEXCEPT return "std::bad_exception"; } +#endif + + exception_ptr::~exception_ptr() _NOEXCEPT { #if HAVE_DEPENDENT_EH_ABI @@ -169,7 +184,7 @@ nested_exception::~nested_exception() _NOEXCEPT { } -_ATTRIBUTE(noreturn) +_LIBCPP_NORETURN void nested_exception::rethrow_nested() const { @@ -178,15 +193,14 @@ nested_exception::rethrow_nested() const rethrow_exception(__ptr_); } -} // std -std::exception_ptr std::current_exception() _NOEXCEPT +exception_ptr current_exception() _NOEXCEPT { #if HAVE_DEPENDENT_EH_ABI // be nicer if there was a constructor that took a ptr, then // this whole function would be just: // return exception_ptr(__cxa_current_primary_exception()); - std::exception_ptr ptr; + exception_ptr ptr; ptr.__ptr_ = __cxa_current_primary_exception(); return ptr; #else // __APPLE__ @@ -195,7 +209,8 @@ std::exception_ptr std::current_exception() _NOEXCEPT #endif // __APPLE__ } -void std::rethrow_exception(exception_ptr p) +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) { #if HAVE_DEPENDENT_EH_ABI __cxa_rethrow_primary_exception(p.__ptr_); @@ -206,3 +221,4 @@ void std::rethrow_exception(exception_ptr p) ::abort(); #endif // __APPLE__ } +} // std diff --git a/system/lib/libcxx/future.cpp b/system/lib/libcxx/future.cpp index ff591105..7d9a5b5d 100644 --- a/system/lib/libcxx/future.cpp +++ b/system/lib/libcxx/future.cpp @@ -29,7 +29,7 @@ __future_error_category::name() const _NOEXCEPT string __future_error_category::message(int ev) const { - switch (ev) + switch (static_cast<future_errc>(ev)) { case future_errc::broken_promise: return string("The associated promise has been destructed prior " @@ -47,7 +47,7 @@ __future_error_category::message(int ev) const } const error_category& -future_category() +future_category() _NOEXCEPT { static __future_error_category __f; return __f; @@ -78,8 +78,8 @@ __assoc_sub_state::set_value() throw future_error(make_error_code(future_errc::promise_already_satisfied)); #endif __state_ |= __constructed | ready; - __lk.unlock(); __cv_.notify_all(); + __lk.unlock(); } void @@ -152,9 +152,9 @@ __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk) { if (!__is_ready()) { - if (__state_ & deferred) + if (__state_ & static_cast<unsigned>(deferred)) { - __state_ &= ~deferred; + __state_ &= ~static_cast<unsigned>(deferred); __lk.unlock(); __execute(); } diff --git a/system/lib/libcxx/hash.cpp b/system/lib/libcxx/hash.cpp index 728b9bd3..a0135002 100644 --- a/system/lib/libcxx/hash.cpp +++ b/system/lib/libcxx/hash.cpp @@ -10,6 +10,9 @@ #include "__hash_table" #include "algorithm" #include "stdexcept" +#include "type_traits" + +#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" _LIBCPP_BEGIN_NAMESPACE_STD @@ -144,21 +147,23 @@ const unsigned indices[] = // are fewer potential primes to search, and fewer potential primes to divide // against. +template <size_t _Sz = sizeof(size_t)> inline _LIBCPP_INLINE_VISIBILITY -void -__check_for_overflow(size_t N, integral_constant<size_t, 32>) +typename enable_if<_Sz == 4, void>::type +__check_for_overflow(size_t N) { -#ifndef _LIBCPP_NO_EXCEPTIONS +#ifndef _LIBCPP_NO_EXCEPTIONS if (N > 0xFFFFFFFB) throw overflow_error("__next_prime overflow"); #endif } +template <size_t _Sz = sizeof(size_t)> inline _LIBCPP_INLINE_VISIBILITY -void -__check_for_overflow(size_t N, integral_constant<size_t, 64>) +typename enable_if<_Sz == 8, void>::type +__check_for_overflow(size_t N) { -#ifndef _LIBCPP_NO_EXCEPTIONS +#ifndef _LIBCPP_NO_EXCEPTIONS if (N > 0xFFFFFFFFFFFFFFC5ull) throw overflow_error("__next_prime overflow"); #endif @@ -174,14 +179,14 @@ __next_prime(size_t n) return *std::lower_bound(small_primes, small_primes + N, n); // Else n > largest small_primes // Check for overflow - __check_for_overflow(n, integral_constant<size_t, - sizeof(n) * __CHAR_BIT__>()); + __check_for_overflow(n); // Start searching list of potential primes: L * k0 + indices[in] const size_t M = sizeof(indices) / sizeof(indices[0]); // Select first potential prime >= n // Known a-priori n >= L size_t k0 = n / L; - size_t in = std::lower_bound(indices, indices + M, n - k0 * L) - indices; + size_t in = static_cast<size_t>(std::lower_bound(indices, indices + M, n - k0 * L) + - indices); n = L * k0 + indices[in]; while (true) { diff --git a/system/lib/libcxx/ios.cpp b/system/lib/libcxx/ios.cpp index 80917a04..732a61bb 100644 --- a/system/lib/libcxx/ios.cpp +++ b/system/lib/libcxx/ios.cpp @@ -401,7 +401,7 @@ ios_base::move(ios_base& rhs) } void -ios_base::swap(ios_base& rhs) +ios_base::swap(ios_base& rhs) _NOEXCEPT { _VSTD::swap(__fmtflags_, rhs.__fmtflags_); _VSTD::swap(__precision_, rhs.__precision_); diff --git a/system/lib/libcxx/iostream.cpp b/system/lib/libcxx/iostream.cpp index 157c3977..f5b959b4 100644 --- a/system/lib/libcxx/iostream.cpp +++ b/system/lib/libcxx/iostream.cpp @@ -9,45 +9,58 @@ #include "__std_stream" #include "string" +#include "new" _LIBCPP_BEGIN_NAMESPACE_STD -static __stdinbuf<char> __cin(stdin); -static __stdoutbuf<char> __cout(stdout); -static __stdoutbuf<char> __cerr(stderr); -static __stdinbuf<wchar_t> __wcin(stdin); -static __stdoutbuf<wchar_t> __wcout(stdout); -static __stdoutbuf<wchar_t> __wcerr(stderr); - -istream cin(&__cin); -ostream cout(&__cout); -ostream cerr(&__cerr); -ostream clog(&__cerr); -wistream wcin(&__wcin); -wostream wcout(&__wcout); -wostream wcerr(&__wcerr); -wostream wclog(&__wcerr); +_ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin [sizeof(__stdinbuf <char>)]; +_ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)]; +_ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)]; +_ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin [sizeof(__stdinbuf <wchar_t>)]; +_ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)]; +_ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)]; + +_ALIGNAS_TYPE (istream) char cin [sizeof(istream)]; +_ALIGNAS_TYPE (ostream) char cout[sizeof(ostream)]; +_ALIGNAS_TYPE (ostream) char cerr[sizeof(ostream)]; +_ALIGNAS_TYPE (ostream) char clog[sizeof(ostream)]; +_ALIGNAS_TYPE (wistream) char wcin [sizeof(wistream)]; +_ALIGNAS_TYPE (wostream) char wcout[sizeof(wostream)]; +_ALIGNAS_TYPE (wostream) char wcerr[sizeof(wostream)]; +_ALIGNAS_TYPE (wostream) char wclog[sizeof(wostream)]; ios_base::Init __start_std_streams; ios_base::Init::Init() { - cin.tie(&cout); - _VSTD::unitbuf(cerr); - cerr.tie(&cout); + istream* cin_ptr = ::new(cin) istream(::new(__cin) __stdinbuf <char>(stdin) ); + ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout)); + ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr)); + ::new(clog) ostream(cerr_ptr->rdbuf()); + cin_ptr->tie(cout_ptr); + _VSTD::unitbuf(*cerr_ptr); + cerr_ptr->tie(cout_ptr); - wcin.tie(&wcout); - _VSTD::unitbuf(wcerr); - wcerr.tie(&wcout); + wistream* wcin_ptr = ::new(wcin) wistream(::new(__wcin) __stdinbuf <wchar_t>(stdin) ); + wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout)); + wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr)); + ::new(wclog) wostream(wcerr_ptr->rdbuf()); + wcin_ptr->tie(wcout_ptr); + _VSTD::unitbuf(*wcerr_ptr); + wcerr_ptr->tie(wcout_ptr); } ios_base::Init::~Init() { - cout.flush(); - clog.flush(); + ostream* cout_ptr = (ostream*)cout; + ostream* clog_ptr = (ostream*)clog; + cout_ptr->flush(); + clog_ptr->flush(); - wcout.flush(); - wclog.flush(); + wostream* wcout_ptr = (wostream*)wcout; + wostream* wclog_ptr = (wostream*)wclog; + wcout_ptr->flush(); + wclog_ptr->flush(); } _LIBCPP_END_NAMESPACE_STD diff --git a/system/lib/libcxx/locale.cpp b/system/lib/libcxx/locale.cpp index 87c47636..21497903 100644 --- a/system/lib/libcxx/locale.cpp +++ b/system/lib/libcxx/locale.cpp @@ -7,6 +7,12 @@ // //===----------------------------------------------------------------------===// +// On Solaris, we need to define something to make the C99 parts of localeconv +// visible. +#ifdef __sun__ +#define _LCONV_C99 +#endif + #include "string" #include "locale" #include "codecvt" @@ -20,38 +26,27 @@ #include "cwctype" #include "__sso_allocator" #if _WIN32 -#include <locale.h> +#include <support/win32/locale_win32.h> #else // _WIN32 #include <langinfo.h> #endif // _!WIN32 #include <stdlib.h> -#ifdef _LIBCPP_STABLE_APPLE_ABI -namespace { - decltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>())) - inline _LIBCPP_INLINE_VISIBILITY - mb_cur_max_l(locale_t loc) - { - return MB_CUR_MAX_L(loc); - } -} -#endif +// On Linux, wint_t and wchar_t have different signed-ness, and this causes +// lots of noise in the build log, but no bugs that I know of. +#pragma clang diagnostic ignored "-Wsign-conversion" _LIBCPP_BEGIN_NAMESPACE_STD -#ifndef _LIBCPP_STABLE_APPLE_ABI +#ifdef __cloc_defined locale_t __cloc() { // In theory this could create a race condition. In practice // the race condition is non-fatal since it will just create // a little resource leak. Better approach would be appreciated. -#ifdef __APPLE__ - return 0; -#else static locale_t result = newlocale(LC_ALL_MASK, "C", 0); return result; -#endif } -#endif // _LIBCPP_STABLE_APPLE_ABI +#endif // __cloc_defined namespace { @@ -90,14 +85,44 @@ make(A0 a0, A1 a1, A2 a2) return *(T*)&buf; } +template <typename T, size_t N> +inline +_LIBCPP_CONSTEXPR +size_t +countof(const T (&)[N]) +{ + return N; +} + +template <typename T> +inline +_LIBCPP_CONSTEXPR +size_t +countof(const T * const begin, const T * const end) +{ + return static_cast<size_t>(end - begin); +} + } +const locale::category locale::none; +const locale::category locale::collate; +const locale::category locale::ctype; +const locale::category locale::monetary; +const locale::category locale::numeric; +const locale::category locale::time; +const locale::category locale::messages; +const locale::category locale::all; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" + class _LIBCPP_HIDDEN locale::__imp : public facet { enum {N = 28}; - string name_; vector<facet*, __sso_allocator<facet*, N> > facets_; + string name_; public: explicit __imp(size_t refs = 0); explicit __imp(const string& name, size_t refs = 0); @@ -108,7 +133,8 @@ public: ~__imp(); const string& name() const {return name_;} - bool has_facet(long id) const {return id < facets_.size() && facets_[id];} + bool has_facet(long id) const + {return static_cast<size_t>(id) < facets_.size() && facets_[static_cast<size_t>(id)];} const locale::facet* use_facet(long id) const; static const locale& make_classic(); @@ -119,46 +145,48 @@ private: template <class F> void install_from(const __imp& other); }; +#pragma clang diagnostic pop + locale::__imp::__imp(size_t refs) : facet(refs), - name_("C"), - facets_(N) + facets_(N), + name_("C") { facets_.clear(); - install(&make<_VSTD::collate<char> >(1)); - install(&make<_VSTD::collate<wchar_t> >(1)); - install(&make<_VSTD::ctype<char> >((ctype_base::mask*)0, false, 1)); - install(&make<_VSTD::ctype<wchar_t> >(1)); - install(&make<codecvt<char, char, mbstate_t> >(1)); - install(&make<codecvt<wchar_t, char, mbstate_t> >(1)); - install(&make<codecvt<char16_t, char, mbstate_t> >(1)); - install(&make<codecvt<char32_t, char, mbstate_t> >(1)); - install(&make<numpunct<char> >(1)); - install(&make<numpunct<wchar_t> >(1)); - install(&make<num_get<char> >(1)); - install(&make<num_get<wchar_t> >(1)); - install(&make<num_put<char> >(1)); - install(&make<num_put<wchar_t> >(1)); - install(&make<moneypunct<char, false> >(1)); - install(&make<moneypunct<char, true> >(1)); - install(&make<moneypunct<wchar_t, false> >(1)); - install(&make<moneypunct<wchar_t, true> >(1)); - install(&make<money_get<char> >(1)); - install(&make<money_get<wchar_t> >(1)); - install(&make<money_put<char> >(1)); - install(&make<money_put<wchar_t> >(1)); - install(&make<time_get<char> >(1)); - install(&make<time_get<wchar_t> >(1)); - install(&make<time_put<char> >(1)); - install(&make<time_put<wchar_t> >(1)); - install(&make<_VSTD::messages<char> >(1)); - install(&make<_VSTD::messages<wchar_t> >(1)); + install(&make<_VSTD::collate<char> >(1u)); + install(&make<_VSTD::collate<wchar_t> >(1u)); + install(&make<_VSTD::ctype<char> >((ctype_base::mask*)0, false, 1u)); + install(&make<_VSTD::ctype<wchar_t> >(1u)); + install(&make<codecvt<char, char, mbstate_t> >(1u)); + install(&make<codecvt<wchar_t, char, mbstate_t> >(1u)); + install(&make<codecvt<char16_t, char, mbstate_t> >(1u)); + install(&make<codecvt<char32_t, char, mbstate_t> >(1u)); + install(&make<numpunct<char> >(1u)); + install(&make<numpunct<wchar_t> >(1u)); + install(&make<num_get<char> >(1u)); + install(&make<num_get<wchar_t> >(1u)); + install(&make<num_put<char> >(1u)); + install(&make<num_put<wchar_t> >(1u)); + install(&make<moneypunct<char, false> >(1u)); + install(&make<moneypunct<char, true> >(1u)); + install(&make<moneypunct<wchar_t, false> >(1u)); + install(&make<moneypunct<wchar_t, true> >(1u)); + install(&make<money_get<char> >(1u)); + install(&make<money_get<wchar_t> >(1u)); + install(&make<money_put<char> >(1u)); + install(&make<money_put<wchar_t> >(1u)); + install(&make<time_get<char> >(1u)); + install(&make<time_get<wchar_t> >(1u)); + install(&make<time_put<char> >(1u)); + install(&make<time_put<wchar_t> >(1u)); + install(&make<_VSTD::messages<char> >(1u)); + install(&make<_VSTD::messages<wchar_t> >(1u)); } locale::__imp::__imp(const string& name, size_t refs) : facet(refs), - name_(name), - facets_(N) + facets_(N), + name_(name) { #ifndef _LIBCPP_NO_EXCEPTIONS try @@ -200,9 +228,14 @@ locale::__imp::__imp(const string& name, size_t refs) #endif // _LIBCPP_NO_EXCEPTIONS } +// NOTE avoid the `base class should be explicitly initialized in the +// copy constructor` warning emitted by GCC +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wextra" + locale::__imp::__imp(const __imp& other) - : name_(other.name_), - facets_(max<size_t>(N, other.facets_.size())) + : facets_(max<size_t>(N, other.facets_.size())), + name_(other.name_) { facets_ = other.facets_; for (unsigned i = 0; i < facets_.size(); ++i) @@ -210,9 +243,11 @@ locale::__imp::__imp(const __imp& other) facets_[i]->__add_shared(); } +#pragma GCC diagnostic pop + locale::__imp::__imp(const __imp& other, const string& name, locale::category c) - : name_("*"), - facets_(N) + : facets_(N), + name_("*") { facets_ = other.facets_; for (unsigned i = 0; i < facets_.size(); ++i) @@ -282,8 +317,8 @@ locale::__imp::install_from(const locale::__imp& one) } locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c) - : name_("*"), - facets_(N) + : facets_(N), + name_("*") { facets_ = other.facets_; for (unsigned i = 0; i < facets_.size(); ++i) @@ -352,8 +387,8 @@ locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c) } locale::__imp::__imp(const __imp& other, facet* f, long id) - : name_("*"), - facets_(max<size_t>(N, other.facets_.size()+1)) + : facets_(max<size_t>(N, other.facets_.size()+1)), + name_("*") { f->__add_shared(); unique_ptr<facet, release> hold(f); @@ -376,11 +411,11 @@ locale::__imp::install(facet* f, long id) { f->__add_shared(); unique_ptr<facet, release> hold(f); - if (id >= facets_.size()) - facets_.resize(id+1); - if (facets_[id]) - facets_[id]->__release_shared(); - facets_[id] = hold.release(); + if (static_cast<size_t>(id) >= facets_.size()) + facets_.resize(static_cast<size_t>(id+1)); + if (facets_[static_cast<size_t>(id)]) + facets_[static_cast<size_t>(id)]->__release_shared(); + facets_[static_cast<size_t>(id)] = hold.release(); } const locale::facet* @@ -390,7 +425,7 @@ locale::__imp::use_facet(long id) const if (!has_facet(id)) throw bad_cast(); #endif // _LIBCPP_NO_EXCEPTIONS - return facets_[id]; + return facets_[static_cast<size_t>(id)]; } // locale @@ -401,7 +436,7 @@ locale::__imp::make_classic() // only one thread can get in here and it only gets in once static aligned_storage<sizeof(locale)>::type buf; locale* c = (locale*)&buf; - c->__locale_ = &make<__imp>(1); + c->__locale_ = &make<__imp>(1u); return *c; } @@ -417,7 +452,6 @@ locale::__imp::make_global() { // only one thread can get in here and it only gets in once static aligned_storage<sizeof(locale)>::type buf; - locale* g = (locale*)&buf; ::new (&buf) locale(locale::classic()); return *(locale*)&buf; } @@ -695,6 +729,19 @@ collate_byname<wchar_t>::do_transform(const char_type* lo, const char_type* hi) // template <> class ctype<wchar_t>; +const ctype_base::mask ctype_base::space; +const ctype_base::mask ctype_base::print; +const ctype_base::mask ctype_base::cntrl; +const ctype_base::mask ctype_base::upper; +const ctype_base::mask ctype_base::lower; +const ctype_base::mask ctype_base::alpha; +const ctype_base::mask ctype_base::digit; +const ctype_base::mask ctype_base::punct; +const ctype_base::mask ctype_base::xdigit; +const ctype_base::mask ctype_base::blank; +const ctype_base::mask ctype_base::alnum; +const ctype_base::mask ctype_base::graph; + locale::id ctype<wchar_t>::id; ctype<wchar_t>::~ctype() @@ -737,10 +784,12 @@ ctype<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) wchar_t ctype<wchar_t>::do_toupper(char_type c) const { -#if !(defined(_LIBCPP_STABLE_APPLE_ABI) || defined(__FreeBSD__)) +#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE + return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c; +#elif defined(__GLIBC__) return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c; #else - return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c; + return (isascii(c) && iswlower_l(c, __cloc())) ? c-L'a'+L'A' : c; #endif } @@ -748,11 +797,13 @@ const wchar_t* ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const { for (; low != high; ++low) -#if !(defined(_LIBCPP_STABLE_APPLE_ABI) || defined(__FreeBSD__)) +#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE + *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low; +#elif defined(__GLIBC__) *low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low] : *low; #else - *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low; + *low = (isascii(*low) && islower_l(*low, __cloc())) ? (*low-L'a'+L'A') : *low; #endif return low; } @@ -760,10 +811,12 @@ ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const wchar_t ctype<wchar_t>::do_tolower(char_type c) const { -#ifndef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE + return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c; +#elif defined(__GLIBC__) return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c; #else - return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c; + return (isascii(c) && isupper_l(c, __cloc())) ? c-L'A'+'a' : c; #endif } @@ -771,11 +824,13 @@ const wchar_t* ctype<wchar_t>::do_tolower(char_type* low, const char_type* high) const { for (; low != high; ++low) -#ifndef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE + *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low; +#elif defined(__GLIBC__) *low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low] : *low; #else - *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low; + *low = (isascii(*low) && isupper_l(*low, __cloc())) ? *low-L'A'+L'a' : *low; #endif return low; } @@ -807,7 +862,7 @@ ctype<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfau { for (; low != high; ++low, ++dest) if (isascii(*low)) - *dest = *low; + *dest = static_cast<char>(*low); else *dest = dfault; return low; @@ -835,10 +890,14 @@ ctype<char>::~ctype() char ctype<char>::do_toupper(char_type c) const { -#ifndef _LIBCPP_STABLE_APPLE_ABI - return isascii(c) ? __classic_upper_table()[c] : c; +#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE + return isascii(c) ? + static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(c)]) : c; +#elif defined(__GLIBC__) + return isascii(c) ? + static_cast<char>(__classic_upper_table()[static_cast<size_t>(c)]) : c; #else - return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c; + return (isascii(c) && islower_l(c, __cloc())) ? c-'a'+'A' : c; #endif } @@ -846,10 +905,14 @@ const char* ctype<char>::do_toupper(char_type* low, const char_type* high) const { for (; low != high; ++low) -#ifndef _LIBCPP_STABLE_APPLE_ABI - *low = isascii(*low) ? __classic_upper_table()[*low] : *low; +#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE + *low = isascii(*low) ? + static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(*low)]) : *low; +#elif defined(__GLIBC__) + *low = isascii(*low) ? + static_cast<char>(__classic_upper_table()[static_cast<size_t>(*low)]) : *low; #else - *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low; + *low = (isascii(*low) && islower_l(*low, __cloc())) ? *low-'a'+'A' : *low; #endif return low; } @@ -857,10 +920,14 @@ ctype<char>::do_toupper(char_type* low, const char_type* high) const char ctype<char>::do_tolower(char_type c) const { -#ifndef _LIBCPP_STABLE_APPLE_ABI - return isascii(c) ? __classic_lower_table()[c] : c; +#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE + return isascii(c) ? + static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(c)]) : c; +#elif defined(__GLIBC__) + return isascii(c) ? + static_cast<char>(__classic_lower_table()[static_cast<size_t>(c)]) : c; #else - return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c; + return (isascii(c) && isupper_l(c, __cloc())) ? c-'A'+'a' : c; #endif } @@ -868,10 +935,12 @@ const char* ctype<char>::do_tolower(char_type* low, const char_type* high) const { for (; low != high; ++low) -#ifndef _LIBCPP_STABLE_APPLE_ABI - *low = isascii(*low) ? __classic_lower_table()[*low] : *low; +#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE + *low = isascii(*low) ? static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(*low)]) : *low; +#elif defined(__GLIBC__) + *low = isascii(*low) ? static_cast<char>(__classic_lower_table()[static_cast<size_t>(*low)]) : *low; #else - *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low; + *low = (isascii(*low) && isupper_l(*low, __cloc())) ? *low-'A'+'a' : *low; #endif return low; } @@ -909,11 +978,6 @@ ctype<char>::do_narrow(const char_type* low, const char_type* high, char dfault, return low; } -// XXX Emscripten define local table -extern "C" const unsigned short ** __ctype_b_loc(); -extern "C" const int ** __ctype_tolower_loc(); -extern "C" const int ** __ctype_toupper_loc(); - const ctype<char>::mask* ctype<char>::classic_table() _NOEXCEPT { @@ -921,44 +985,34 @@ ctype<char>::classic_table() _NOEXCEPT return _DefaultRuneLocale.__runetype; #elif defined(__GLIBC__) return __cloc()->__ctype_b; +#elif __sun__ + return __ctype_mask; +#elif _WIN32 + return _ctype+1; // internal ctype mask table defined in msvcrt.dll // This is assumed to be safe, which is a nonsense assumption because we're // going to end up dereferencing it later... -#elif defined(EMSCRIPTEN) - return *__ctype_b_loc(); #else + // Platform not supported: abort so the person doing the port knows what to + // fix +# warning ctype<char>::classic_table() is not implemented + abort(); return NULL; #endif } -#ifndef _LIBCPP_STABLE_APPLE_ABI +#if defined(__GLIBC__) const int* ctype<char>::__classic_lower_table() _NOEXCEPT { -#if defined(__APPLE__) || defined(__FreeBSD__) - return _DefaultRuneLocale.__maplower; -#elif defined(__GLIBC__) return __cloc()->__ctype_tolower; -#elif defined(EMSCRIPTEN) - return *__ctype_tolower_loc(); -#else - return NULL; -#endif } const int* ctype<char>::__classic_upper_table() _NOEXCEPT { -#if defined(__APPLE__) || defined(__FreeBSD__) - return _DefaultRuneLocale.__mapupper; -#elif defined(__GLIBC__) return __cloc()->__ctype_toupper; -#elif defined(EMSCRIPTEN) - return *__ctype_toupper_loc(); -#else - return NULL; -#endif } -#endif // _LIBCPP_STABLE_APPLE_ABI +#endif // __GLIBC__ // template <> class ctype_byname<char> @@ -992,28 +1046,28 @@ ctype_byname<char>::~ctype_byname() char ctype_byname<char>::do_toupper(char_type c) const { - return toupper_l(c, __l); + return static_cast<char>(toupper_l(c, __l)); } const char* ctype_byname<char>::do_toupper(char_type* low, const char_type* high) const { for (; low != high; ++low) - *low = toupper_l(*low, __l); + *low = static_cast<char>(toupper_l(*low, __l)); return low; } char ctype_byname<char>::do_tolower(char_type c) const { - return tolower_l(c, __l); + return static_cast<char>(tolower_l(c, __l)); } const char* ctype_byname<char>::do_tolower(char_type* low, const char_type* high) const { for (; low != high; ++low) - *low = tolower_l(*low, __l); + *low = static_cast<char>(tolower_l(*low, __l)); return low; } @@ -1052,18 +1106,19 @@ ctype_byname<wchar_t>::do_is(mask m, char_type c) const #ifdef _LIBCPP_WCTYPE_IS_MASK return static_cast<bool>(iswctype_l(c, m, __l)); #else - // FIXME: This is broken for things that test more than one flag. - if (m & space && !iswspace_l(c, __l)) return false; - if (m & print && !iswprint_l(c, __l)) return false; - if (m & cntrl && !iswcntrl_l(c, __l)) return false; - if (m & upper && !iswupper_l(c, __l)) return false; - if (m & lower && !iswlower_l(c, __l)) return false; - if (m & alpha && !iswalpha_l(c, __l)) return false; - if (m & digit && !iswdigit_l(c, __l)) return false; - if (m & punct && !iswpunct_l(c, __l)) return false; - if (m & xdigit && !iswxdigit_l(c, __l)) return false; - if (m & blank && !iswblank_l(c, __l)) return false; - return true; + bool result = false; + wint_t ch = static_cast<wint_t>(c); + if (m & space) result |= (iswspace_l(ch, __l) != 0); + if (m & print) result |= (iswprint_l(ch, __l) != 0); + if (m & cntrl) result |= (iswcntrl_l(ch, __l) != 0); + if (m & upper) result |= (iswupper_l(ch, __l) != 0); + if (m & lower) result |= (iswlower_l(ch, __l) != 0); + if (m & alpha) result |= (iswalpha_l(ch, __l) != 0); + if (m & digit) result |= (iswdigit_l(ch, __l) != 0); + if (m & punct) result |= (iswpunct_l(ch, __l) != 0); + if (m & xdigit) result |= (iswxdigit_l(ch, __l) != 0); + if (m & blank) result |= (iswblank_l(ch, __l) != 0); + return result; #endif } @@ -1077,23 +1132,24 @@ ctype_byname<wchar_t>::do_is(const char_type* low, const char_type* high, mask* else { *vec = 0; - if (iswspace_l(*low, __l)) + wint_t ch = static_cast<wint_t>(*low); + if (iswspace_l(ch, __l)) *vec |= space; - if (iswprint_l(*low, __l)) + if (iswprint_l(ch, __l)) *vec |= print; - if (iswcntrl_l(*low, __l)) + if (iswcntrl_l(ch, __l)) *vec |= cntrl; - if (iswupper_l(*low, __l)) + if (iswupper_l(ch, __l)) *vec |= upper; - if (iswlower_l(*low, __l)) + if (iswlower_l(ch, __l)) *vec |= lower; - if (iswalpha_l(*low, __l)) + if (iswalpha_l(ch, __l)) *vec |= alpha; - if (iswdigit_l(*low, __l)) + if (iswdigit_l(ch, __l)) *vec |= digit; - if (iswpunct_l(*low, __l)) + if (iswpunct_l(ch, __l)) *vec |= punct; - if (iswxdigit_l(*low, __l)) + if (iswxdigit_l(ch, __l)) *vec |= xdigit; } } @@ -1109,17 +1165,17 @@ ctype_byname<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* if (iswctype_l(*low, m, __l)) break; #else - if (m & space && !iswspace_l(*low, __l)) continue; - if (m & print && !iswprint_l(*low, __l)) continue; - if (m & cntrl && !iswcntrl_l(*low, __l)) continue; - if (m & upper && !iswupper_l(*low, __l)) continue; - if (m & lower && !iswlower_l(*low, __l)) continue; - if (m & alpha && !iswalpha_l(*low, __l)) continue; - if (m & digit && !iswdigit_l(*low, __l)) continue; - if (m & punct && !iswpunct_l(*low, __l)) continue; - if (m & xdigit && !iswxdigit_l(*low, __l)) continue; - if (m & blank && !iswblank_l(*low, __l)) continue; - break; + wint_t ch = static_cast<wint_t>(*low); + if (m & space && iswspace_l(ch, __l)) break; + if (m & print && iswprint_l(ch, __l)) break; + if (m & cntrl && iswcntrl_l(ch, __l)) break; + if (m & upper && iswupper_l(ch, __l)) break; + if (m & lower && iswlower_l(ch, __l)) break; + if (m & alpha && iswalpha_l(ch, __l)) break; + if (m & digit && iswdigit_l(ch, __l)) break; + if (m & punct && iswpunct_l(ch, __l)) break; + if (m & xdigit && iswxdigit_l(ch, __l)) break; + if (m & blank && iswblank_l(ch, __l)) break; #endif } return low; @@ -1134,16 +1190,17 @@ ctype_byname<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type if (!iswctype_l(*low, m, __l)) break; #else - if (m & space && iswspace_l(*low, __l)) continue; - if (m & print && iswprint_l(*low, __l)) continue; - if (m & cntrl && iswcntrl_l(*low, __l)) continue; - if (m & upper && iswupper_l(*low, __l)) continue; - if (m & lower && iswlower_l(*low, __l)) continue; - if (m & alpha && iswalpha_l(*low, __l)) continue; - if (m & digit && iswdigit_l(*low, __l)) continue; - if (m & punct && iswpunct_l(*low, __l)) continue; - if (m & xdigit && iswxdigit_l(*low, __l)) continue; - if (m & blank && iswblank_l(*low, __l)) continue; + wint_t ch = static_cast<wint_t>(*low); + if (m & space && iswspace_l(ch, __l)) continue; + if (m & print && iswprint_l(ch, __l)) continue; + if (m & cntrl && iswcntrl_l(ch, __l)) continue; + if (m & upper && iswupper_l(ch, __l)) continue; + if (m & lower && iswlower_l(ch, __l)) continue; + if (m & alpha && iswalpha_l(ch, __l)) continue; + if (m & digit && iswdigit_l(ch, __l)) continue; + if (m & punct && iswpunct_l(ch, __l)) continue; + if (m & xdigit && iswxdigit_l(ch, __l)) continue; + if (m & blank && iswblank_l(ch, __l)) continue; break; #endif } @@ -1181,7 +1238,7 @@ ctype_byname<wchar_t>::do_tolower(char_type* low, const char_type* high) const wchar_t ctype_byname<wchar_t>::do_widen(char c) const { -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS return btowc_l(c, __l); #else return __btowc_l(c, __l); @@ -1192,7 +1249,7 @@ const char* ctype_byname<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const { for (; low != high; ++low, ++dest) -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS *dest = btowc_l(*low, __l); #else *dest = __btowc_l(*low, __l); @@ -1203,12 +1260,12 @@ ctype_byname<wchar_t>::do_widen(const char* low, const char* high, char_type* de char ctype_byname<wchar_t>::do_narrow(char_type c, char dfault) const { -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS int r = wctob_l(c, __l); #else int r = __wctob_l(c, __l); #endif - return r != WEOF ? static_cast<char>(r) : dfault; + return r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault; } const wchar_t* @@ -1216,12 +1273,12 @@ ctype_byname<wchar_t>::do_narrow(const char_type* low, const char_type* high, ch { for (; low != high; ++low, ++dest) { -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS int r = wctob_l(*low, __l); #else int r = __wctob_l(*low, __l); #endif - *dest = r != WEOF ? static_cast<char>(r) : dfault; + *dest = r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault; } return low; } @@ -1278,7 +1335,7 @@ int codecvt<char, char, mbstate_t>::do_length(state_type&, const extern_type* frm, const extern_type* end, size_t mx) const { - return static_cast<int>(min<size_t>(mx, end-frm)); + return static_cast<int>(min<size_t>(mx, static_cast<size_t>(end-frm))); } int @@ -1330,8 +1387,9 @@ codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st, { // save state in case needed to reover to_nxt on error mbstate_t save_state = st; -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t n = wcsnrtombs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t n = wcsnrtombs_l(to, &frm_nxt, static_cast<size_t>(fend-frm), + static_cast<size_t>(to_end-to), &st, __l); #else size_t n = __wcsnrtombs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l); #endif @@ -1340,7 +1398,7 @@ codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st, // need to recover to_nxt for (to_nxt = to; frm != frm_nxt; ++frm) { -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS n = wcrtomb_l(to_nxt, *frm, &save_state, __l); #else n = __wcrtomb_l(to_nxt, *frm, &save_state, __l); @@ -1361,14 +1419,14 @@ codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st, { // Try to write the terminating null extern_type tmp[MB_LEN_MAX]; -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS n = wcrtomb_l(tmp, intern_type(), &st, __l); #else n = __wcrtomb_l(tmp, intern_type(), &st, __l); #endif if (n == size_t(-1)) // on error return error; - if (n > to_end-to_nxt) // is there room? + if (n > static_cast<size_t>(to_end-to_nxt)) // is there room? return partial; for (extern_type* p = tmp; n; --n) // write it *to_nxt++ = *p++; @@ -1398,8 +1456,9 @@ codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st, { // save state in case needed to reover to_nxt on error mbstate_t save_state = st; -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t n = mbsnrtowcs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t n = mbsnrtowcs_l(to, &frm_nxt, static_cast<size_t>(fend-frm), + static_cast<size_t>(to_end-to), &st, __l); #else size_t n = __mbsnrtowcs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l); #endif @@ -1408,8 +1467,9 @@ codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st, // need to recover to_nxt for (to_nxt = to; frm != frm_nxt; ++to_nxt) { -#ifdef _LIBCPP_STABLE_APPLE_ABI - n = mbrtowc_l(to_nxt, frm, fend-frm, &save_state, __l); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + n = mbrtowc_l(to_nxt, frm, static_cast<size_t>(fend-frm), + &save_state, __l); #else n = __mbrtowc_l(to_nxt, frm, fend-frm, &save_state, __l); #endif @@ -1418,10 +1478,10 @@ codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st, case 0: ++frm; break; - case static_cast<size_t>(-1): // XXX EMSCRIPTEN + case size_t(-1): frm_nxt = frm; return error; - case static_cast<size_t>(-2): // XXX EMSCRIPTEN + case size_t(-2): frm_nxt = frm; return partial; default: @@ -1440,7 +1500,7 @@ codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st, if (fend != frm_end) // set up next null terminated sequence { // Try to write the terminating null -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS n = mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l); #else n = __mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l); @@ -1464,7 +1524,7 @@ codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st, { to_nxt = to; extern_type tmp[MB_LEN_MAX]; -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS size_t n = wcrtomb_l(tmp, intern_type(), &st, __l); #else size_t n = __wcrtomb_l(tmp, intern_type(), &st, __l); @@ -1472,7 +1532,7 @@ codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st, if (n == size_t(-1) || n == 0) // on error return error; --n; - if (n > to_end-to_nxt) // is there room? + if (n > static_cast<size_t>(to_end-to_nxt)) // is there room? return partial; for (extern_type* p = tmp; n; --n) // write it *to_nxt++ = *p++; @@ -1482,14 +1542,14 @@ codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st, int codecvt<wchar_t, char, mbstate_t>::do_encoding() const _NOEXCEPT { -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS if (mbtowc_l((wchar_t*) 0, (const char*) 0, MB_LEN_MAX, __l) == 0) #else if (__mbtowc_l((wchar_t*) 0, (const char*) 0, MB_LEN_MAX, __l) == 0) #endif { // stateless encoding -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS if (__l == 0 || MB_CUR_MAX_L(__l) == 1) // there are no known constant length encodings #else if (__l == 0 || __mb_cur_max_l(__l) == 1) // there are no known constant length encodings @@ -1513,8 +1573,8 @@ codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st, int nbytes = 0; for (size_t nwchar_t = 0; nwchar_t < mx && frm != frm_end; ++nwchar_t) { -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t n = mbrlen_l(frm, frm_end-frm, &st, __l); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t n = mbrlen_l(frm, static_cast<size_t>(frm_end-frm), &st, __l); #else size_t n = __mbrlen_l(frm, frm_end-frm, &st, __l); #endif @@ -1524,8 +1584,8 @@ codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st, ++nbytes; ++frm; break; - case static_cast<size_t>(-1): // XXX EMSCRIPTEN - case static_cast<size_t>(-2): // XXX EMSCRIPTEN + case size_t(-1): + case size_t(-2): return nbytes; default: nbytes += n; @@ -1539,10 +1599,10 @@ codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st, int codecvt<wchar_t, char, mbstate_t>::do_max_length() const _NOEXCEPT { -#ifdef _LIBCPP_STABLE_APPLE_ABI - return __l == 0 ? 1 : MB_CUR_MAX_L(__l); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + return __l == 0 ? 1 : static_cast<int>( MB_CUR_MAX_L(__l)); #else - return __l == 0 ? 1 : __mb_cur_max_l(__l); + return __l == 0 ? 1 : static_cast<int>(__mb_cur_max_l(__l)); #endif } @@ -1997,9 +2057,6 @@ utf8_to_utf16_length(const uint8_t* frm, const uint8_t* frm_end, break; uint8_t c2 = frm_nxt[1]; uint8_t c3 = frm_nxt[2]; - uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12) - | ((c2 & 0x3F) << 6) - | (c3 & 0x3F)); switch (c1) { case 0xE0: @@ -2017,7 +2074,7 @@ utf8_to_utf16_length(const uint8_t* frm, const uint8_t* frm_end, } if ((c3 & 0xC0) != 0x80) break; - if ((((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F)) > Maxcode) + if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode) break; frm_nxt += 3; } @@ -2259,7 +2316,7 @@ utf8_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, { if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80)) break; - if ((((c1 & 0x1F) << 6) | (frm_nxt[1] & 0x3F)) > Maxcode) + if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode) break; frm_nxt += 2; } @@ -2286,7 +2343,7 @@ utf8_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, } if ((c3 & 0xC0) != 0x80) break; - if ((((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F)) > Maxcode) + if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode) break; frm_nxt += 3; } @@ -2314,12 +2371,8 @@ utf8_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, } if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80) break; - uint32_t t = static_cast<uint32_t>(((c1 & 0x07) << 18) - | ((c2 & 0x3F) << 12) - | ((c3 & 0x3F) << 6) - | (c4 & 0x3F)); - if ((((c1 & 0x07) << 18) | ((c2 & 0x3F) << 12) | - ((c3 & 0x3F) << 6) | (c4 & 0x3F)) > Maxcode) + if ((((c1 & 0x07u) << 18) | ((c2 & 0x3Fu) << 12) | + ((c3 & 0x3Fu) << 6) | (c4 & 0x3Fu)) > Maxcode) break; frm_nxt += 4; } @@ -2488,7 +2541,7 @@ utf8_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end, { if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80)) break; - if ((((c1 & 0x1F) << 6) | (frm_nxt[1] & 0x3F)) > Maxcode) + if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode) break; frm_nxt += 2; } @@ -2515,7 +2568,7 @@ utf8_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end, } if ((c3 & 0xC0) != 0x80) break; - if ((((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F)) > Maxcode) + if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode) break; frm_nxt += 3; } @@ -2587,7 +2640,7 @@ utf16be_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_ } for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt) { - uint16_t c1 = frm_nxt[0] << 8 | frm_nxt[1]; + uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]); if ((c1 & 0xFC00) == 0xDC00) return codecvt_base::error; if ((c1 & 0xFC00) != 0xD800) @@ -2601,7 +2654,7 @@ utf16be_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_ { if (frm_end-frm_nxt < 4) return codecvt_base::partial; - uint16_t c2 = frm_nxt[2] << 8 | frm_nxt[3]; + uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]); if ((c2 & 0xFC00) != 0xDC00) return codecvt_base::error; uint32_t t = static_cast<uint32_t>( @@ -2624,7 +2677,6 @@ utf16be_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, codecvt_mode mode = codecvt_mode(0)) { const uint8_t* frm_nxt = frm; - frm_nxt = frm; if (mode & consume_header) { if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF) @@ -2632,7 +2684,7 @@ utf16be_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, } for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t) { - uint16_t c1 = frm_nxt[0] << 8 | frm_nxt[1]; + uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]); if ((c1 & 0xFC00) == 0xDC00) break; if ((c1 & 0xFC00) != 0xD800) @@ -2645,7 +2697,7 @@ utf16be_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, { if (frm_end-frm_nxt < 4) break; - uint16_t c2 = frm_nxt[2] << 8 | frm_nxt[3]; + uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]); if ((c2 & 0xFC00) != 0xDC00) break; uint32_t t = static_cast<uint32_t>( @@ -2720,7 +2772,7 @@ utf16le_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_ } for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt) { - uint16_t c1 = frm_nxt[1] << 8 | frm_nxt[0]; + uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]); if ((c1 & 0xFC00) == 0xDC00) return codecvt_base::error; if ((c1 & 0xFC00) != 0xD800) @@ -2734,7 +2786,7 @@ utf16le_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_ { if (frm_end-frm_nxt < 4) return codecvt_base::partial; - uint16_t c2 = frm_nxt[3] << 8 | frm_nxt[2]; + uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]); if ((c2 & 0xFC00) != 0xDC00) return codecvt_base::error; uint32_t t = static_cast<uint32_t>( @@ -2757,7 +2809,6 @@ utf16le_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, codecvt_mode mode = codecvt_mode(0)) { const uint8_t* frm_nxt = frm; - frm_nxt = frm; if (mode & consume_header) { if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE) @@ -2765,7 +2816,7 @@ utf16le_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, } for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t) { - uint16_t c1 = frm_nxt[1] << 8 | frm_nxt[0]; + uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]); if ((c1 & 0xFC00) == 0xDC00) break; if ((c1 & 0xFC00) != 0xD800) @@ -2778,7 +2829,7 @@ utf16le_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, { if (frm_end-frm_nxt < 4) break; - uint16_t c2 = frm_nxt[3] << 8 | frm_nxt[2]; + uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]); if ((c2 & 0xFC00) != 0xDC00) break; uint32_t t = static_cast<uint32_t>( @@ -2836,7 +2887,7 @@ utf16be_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_ } for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt) { - uint16_t c1 = frm_nxt[0] << 8 | frm_nxt[1]; + uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]); if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode) return codecvt_base::error; *to_nxt = c1; @@ -2852,7 +2903,6 @@ utf16be_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end, codecvt_mode mode = codecvt_mode(0)) { const uint8_t* frm_nxt = frm; - frm_nxt = frm; if (mode & consume_header) { if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF) @@ -2860,7 +2910,7 @@ utf16be_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end, } for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t) { - uint16_t c1 = frm_nxt[0] << 8 | frm_nxt[1]; + uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]); if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode) break; frm_nxt += 2; @@ -2911,7 +2961,7 @@ utf16le_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_ } for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt) { - uint16_t c1 = frm_nxt[1] << 8 | frm_nxt[0]; + uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]); if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode) return codecvt_base::error; *to_nxt = c1; @@ -2935,7 +2985,7 @@ utf16le_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end, } for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t) { - uint16_t c1 = frm_nxt[1] << 8 | frm_nxt[0]; + uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]); if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode) break; frm_nxt += 2; @@ -4088,11 +4138,11 @@ numpunct_byname<char>::__init(const char* nm) { __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); #ifndef _LIBCPP_NO_EXCEPTIONS - if (loc == 0) + if (loc == nullptr) throw runtime_error("numpunct_byname<char>::numpunct_byname" " failed to construct for " + string(nm)); #endif // _LIBCPP_NO_EXCEPTIONS -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS lconv* lc = localeconv_l(loc.get()); #else lconv* lc = __localeconv_l(loc.get()); @@ -4131,11 +4181,11 @@ numpunct_byname<wchar_t>::__init(const char* nm) { __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); #ifndef _LIBCPP_NO_EXCEPTIONS - if (loc == 0) + if (loc == nullptr) throw runtime_error("numpunct_byname<char>::numpunct_byname" " failed to construct for " + string(nm)); #endif // _LIBCPP_NO_EXCEPTIONS -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS lconv* lc = localeconv_l(loc.get()); #else lconv* lc = __localeconv_l(loc.get()); @@ -4179,7 +4229,7 @@ __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end, { if (0 < *__ig && *__ig < numeric_limits<char>::max()) { - if (*__ig != *__r) + if (static_cast<unsigned>(*__ig) != *__r) { __err = ios_base::failbit; return; @@ -4190,7 +4240,7 @@ __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end, } if (0 < *__ig && *__ig < numeric_limits<char>::max()) { - if (*__ig < __g_end[-1] || __g_end[-1] == 0) + if (static_cast<unsigned>(*__ig) < __g_end[-1] || __g_end[-1] == 0) __err = ios_base::failbit; } } @@ -4563,11 +4613,14 @@ __time_get::~__time_get() freelocale(__loc_); } +#pragma clang diagnostic ignored "-Wmissing-field-initializers" +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" + template <> string __time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct) { - tm t; + tm t = {0}; t.tm_sec = 59; t.tm_min = 55; t.tm_hour = 23; @@ -4581,7 +4634,7 @@ __time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct) char f[3] = {0}; f[0] = '%'; f[1] = fmt; - size_t n = strftime_l(buf, 100, f, &t, __loc_); + size_t n = strftime_l(buf, countof(buf), f, &t, __loc_); char* bb = buf; char* be = buf + n; string result; @@ -4596,7 +4649,7 @@ __time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct) } char* w = bb; ios_base::iostate err = ios_base::goodbit; - int i = __scan_keyword(w, be, this->__weeks_, this->__weeks_+14, + ptrdiff_t i = __scan_keyword(w, be, this->__weeks_, this->__weeks_+14, ct, err, false) - this->__weeks_; if (i < 14) @@ -4707,11 +4760,13 @@ __time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct) return result; } +#pragma clang diagnostic ignored "-Wmissing-braces" + template <> wstring __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct) { - tm t; + tm t = {0}; t.tm_sec = 59; t.tm_min = 55; t.tm_hour = 23; @@ -4725,19 +4780,19 @@ __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct) char f[3] = {0}; f[0] = '%'; f[1] = fmt; - size_t be = strftime_l(buf, 100, f, &t, __loc_); + strftime_l(buf, countof(buf), f, &t, __loc_); wchar_t wbuf[100]; wchar_t* wbb = wbuf; mbstate_t mb = {0}; const char* bb = buf; -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t i = mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t j = mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_); #else - size_t i = __mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); + size_t j = __mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_); #endif - if (i == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); - wchar_t* wbe = wbb + i; + wchar_t* wbe = wbb + j; wstring result; while (wbb != wbe) { @@ -4750,7 +4805,7 @@ __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct) } wchar_t* w = wbb; ios_base::iostate err = ios_base::goodbit; - int i = __scan_keyword(w, wbe, this->__weeks_, this->__weeks_+14, + ptrdiff_t i = __scan_keyword(w, wbe, this->__weeks_, this->__weeks_+14, ct, err, false) - this->__weeks_; if (i < 14) @@ -4865,32 +4920,32 @@ template <> void __time_get_storage<char>::init(const ctype<char>& ct) { - tm t; + tm t = {0}; char buf[100]; // __weeks_ for (int i = 0; i < 7; ++i) { t.tm_wday = i; - strftime_l(buf, 100, "%A", &t, __loc_); + strftime_l(buf, countof(buf), "%A", &t, __loc_); __weeks_[i] = buf; - strftime_l(buf, 100, "%a", &t, __loc_); + strftime_l(buf, countof(buf), "%a", &t, __loc_); __weeks_[i+7] = buf; } // __months_ for (int i = 0; i < 12; ++i) { t.tm_mon = i; - strftime_l(buf, 100, "%B", &t, __loc_); + strftime_l(buf, countof(buf), "%B", &t, __loc_); __months_[i] = buf; - strftime_l(buf, 100, "%b", &t, __loc_); + strftime_l(buf, countof(buf), "%b", &t, __loc_); __months_[i+12] = buf; } // __am_pm_ t.tm_hour = 1; - strftime_l(buf, 100, "%p", &t, __loc_); + strftime_l(buf, countof(buf), "%p", &t, __loc_); __am_pm_[0] = buf; t.tm_hour = 13; - strftime_l(buf, 100, "%p", &t, __loc_); + strftime_l(buf, countof(buf), "%p", &t, __loc_); __am_pm_[1] = buf; __c_ = __analyze('c', ct); __r_ = __analyze('r', ct); @@ -4904,7 +4959,6 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct) { tm t = {0}; char buf[100]; - size_t be; wchar_t wbuf[100]; wchar_t* wbe; mbstate_t mb = {0}; @@ -4912,27 +4966,27 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct) for (int i = 0; i < 7; ++i) { t.tm_wday = i; - be = strftime_l(buf, 100, "%A", &t, __loc_); + strftime_l(buf, countof(buf), "%A", &t, __loc_); mb = mbstate_t(); const char* bb = buf; -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #else - size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); + size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __weeks_[i].assign(wbuf, wbe); - be = strftime_l(buf, 100, "%a", &t, __loc_); + strftime_l(buf, countof(buf), "%a", &t, __loc_); mb = mbstate_t(); bb = buf; -#ifdef _LIBCPP_STABLE_APPLE_ABI - j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #else - j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); + j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __weeks_[i+7].assign(wbuf, wbe); @@ -4941,55 +4995,55 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct) for (int i = 0; i < 12; ++i) { t.tm_mon = i; - be = strftime_l(buf, 100, "%B", &t, __loc_); + strftime_l(buf, countof(buf), "%B", &t, __loc_); mb = mbstate_t(); const char* bb = buf; -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #else - size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); + size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __months_[i].assign(wbuf, wbe); - be = strftime_l(buf, 100, "%b", &t, __loc_); + strftime_l(buf, countof(buf), "%b", &t, __loc_); mb = mbstate_t(); bb = buf; -#ifdef _LIBCPP_STABLE_APPLE_ABI - j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #else - j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); + j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __months_[i+12].assign(wbuf, wbe); } // __am_pm_ t.tm_hour = 1; - be = strftime_l(buf, 100, "%p", &t, __loc_); + strftime_l(buf, countof(buf), "%p", &t, __loc_); mb = mbstate_t(); const char* bb = buf; -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #else - size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); + size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __am_pm_[0].assign(wbuf, wbe); t.tm_hour = 13; - be = strftime_l(buf, 100, "%p", &t, __loc_); + strftime_l(buf, countof(buf), "%p", &t, __loc_); mb = mbstate_t(); bb = buf; -#ifdef _LIBCPP_STABLE_APPLE_ABI - j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #else - j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); + j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __am_pm_[1].assign(wbuf, wbe); @@ -5250,7 +5304,7 @@ __time_put::__do_put(char* __nb, char*& __ne, const tm* __tm, char fmt[] = {'%', __fmt, __mod, 0}; if (__mod != 0) swap(fmt[1], fmt[2]); - size_t n = strftime_l(__nb, __ne-__nb, fmt, __tm, __loc_); + size_t n = strftime_l(__nb, countof(__nb, __ne), fmt, __tm, __loc_); __ne = __nb + n; } @@ -5263,128 +5317,212 @@ __time_put::__do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm, __do_put(__nar, __ne, __tm, __fmt, __mod); mbstate_t mb = {0}; const char* __nb = __nar; -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t j = mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t j = mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_); #else - size_t j = __mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_); + size_t j = __mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); __we = __wb + j; } // moneypunct_byname +template <class charT> static void -__init_pat(money_base::pattern& pat, char cs_precedes, char sep_by_space, char sign_posn) +__init_pat(money_base::pattern& pat, basic_string<charT>& __curr_symbol_, + bool intl, char cs_precedes, char sep_by_space, char sign_posn, + charT space_char) { const char sign = static_cast<char>(money_base::sign); const char space = static_cast<char>(money_base::space); const char none = static_cast<char>(money_base::none); const char symbol = static_cast<char>(money_base::symbol); const char value = static_cast<char>(money_base::value); + const bool symbol_contains_sep = intl && __curr_symbol_.size() == 4; + + // Comments on case branches reflect 'C11 7.11.2.1 The localeconv + // function'. "Space between sign and symbol or value" means that + // if the sign is adjacent to the symbol, there's a space between + // them, and otherwise there's a space between the sign and value. + // + // C11's localeconv specifies that the fourth character of an + // international curr_symbol is used to separate the sign and + // value when sep_by_space says to do so. C++ can't represent + // that, so we just use a space. When sep_by_space says to + // separate the symbol and value-or-sign with a space, we rearrange the + // curr_symbol to put its spacing character on the correct side of + // the symbol. + // + // We also need to avoid adding an extra space between the sign + // and value when the currency symbol is suppressed (by not + // setting showbase). We match glibc's strfmon by interpreting + // sep_by_space==1 as "omit the space when the currency symbol is + // absent". + // + // Users who want to get this right should use ICU instead. + switch (cs_precedes) { - case 0: + case 0: // value before curr_symbol + if (symbol_contains_sep) { + // Move the separator to before the symbol, to place it + // between the value and symbol. + rotate(__curr_symbol_.begin(), __curr_symbol_.begin() + 3, + __curr_symbol_.end()); + } switch (sign_posn) { - case 0: + case 0: // Parentheses surround the quantity and currency symbol. pat.field[0] = sign; pat.field[1] = value; + pat.field[2] = none; // Any space appears in the symbol. pat.field[3] = symbol; switch (sep_by_space) { - case 0: - pat.field[2] = none; + case 0: // No space separates the currency symbol and value. + // This case may have changed between C99 and C11; + // assume the currency symbol matches the intention. + case 2: // Space between sign and currency or value. + // The "sign" is two parentheses, so no space here either. return; - case 1: - case 2: - pat.field[2] = space; + case 1: // Space between currency-and-sign or currency and value. + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[2]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.insert(0, 1, space_char); + } return; default: break; } break; - case 1: + case 1: // The sign string precedes the quantity and currency symbol. pat.field[0] = sign; pat.field[3] = symbol; switch (sep_by_space) { - case 0: + case 0: // No space separates the currency symbol and value. pat.field[1] = value; pat.field[2] = none; return; - case 1: + case 1: // Space between currency-and-sign or currency and value. pat.field[1] = value; - pat.field[2] = space; + pat.field[2] = none; + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[2]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.insert(0, 1, space_char); + } return; - case 2: + case 2: // Space between sign and currency or value. pat.field[1] = space; pat.field[2] = value; + if (symbol_contains_sep) { + // Remove the separator from the symbol, since it + // has already appeared after the sign. + __curr_symbol_.erase(__curr_symbol_.begin()); + } return; default: break; } break; - case 2: + case 2: // The sign string succeeds the quantity and currency symbol. pat.field[0] = value; pat.field[3] = sign; switch (sep_by_space) { - case 0: + case 0: // No space separates the currency symbol and value. pat.field[1] = none; pat.field[2] = symbol; return; - case 1: - pat.field[1] = space; + case 1: // Space between currency-and-sign or currency and value. + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[1]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.insert(0, 1, space_char); + } + pat.field[1] = none; pat.field[2] = symbol; return; - case 2: + case 2: // Space between sign and currency or value. pat.field[1] = symbol; pat.field[2] = space; + if (symbol_contains_sep) { + // Remove the separator from the symbol, since it + // should not be removed if showbase is absent. + __curr_symbol_.erase(__curr_symbol_.begin()); + } return; default: break; } break; - case 3: + case 3: // The sign string immediately precedes the currency symbol. pat.field[0] = value; pat.field[3] = symbol; switch (sep_by_space) { - case 0: + case 0: // No space separates the currency symbol and value. pat.field[1] = none; pat.field[2] = sign; return; - case 1: + case 1: // Space between currency-and-sign or currency and value. pat.field[1] = space; pat.field[2] = sign; + if (symbol_contains_sep) { + // Remove the separator from the symbol, since it + // has already appeared before the sign. + __curr_symbol_.erase(__curr_symbol_.begin()); + } return; - case 2: + case 2: // Space between sign and currency or value. pat.field[1] = sign; - pat.field[2] = space; + pat.field[2] = none; + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[2]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.insert(0, 1, space_char); + } return; default: break; } break; - case 4: + case 4: // The sign string immediately succeeds the currency symbol. pat.field[0] = value; pat.field[3] = sign; switch (sep_by_space) { - case 0: + case 0: // No space separates the currency symbol and value. pat.field[1] = none; pat.field[2] = symbol; return; - case 1: - pat.field[1] = space; + case 1: // Space between currency-and-sign or currency and value. + pat.field[1] = none; pat.field[2] = symbol; + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[1]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.insert(0, 1, space_char); + } return; - case 2: + case 2: // Space between sign and currency or value. pat.field[1] = symbol; pat.field[2] = space; + if (symbol_contains_sep) { + // Remove the separator from the symbol, since it + // should not disappear when showbase is absent. + __curr_symbol_.erase(__curr_symbol_.begin()); + } return; default: break; @@ -5394,105 +5532,157 @@ __init_pat(money_base::pattern& pat, char cs_precedes, char sep_by_space, char s break; } break; - case 1: + case 1: // curr_symbol before value switch (sign_posn) { - case 0: + case 0: // Parentheses surround the quantity and currency symbol. pat.field[0] = sign; pat.field[1] = symbol; + pat.field[2] = none; // Any space appears in the symbol. pat.field[3] = value; switch (sep_by_space) { - case 0: - pat.field[2] = none; + case 0: // No space separates the currency symbol and value. + // This case may have changed between C99 and C11; + // assume the currency symbol matches the intention. + case 2: // Space between sign and currency or value. + // The "sign" is two parentheses, so no space here either. return; - case 1: - case 2: - pat.field[2] = space; + case 1: // Space between currency-and-sign or currency and value. + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[2]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.insert(0, 1, space_char); + } return; default: break; } break; - case 1: + case 1: // The sign string precedes the quantity and currency symbol. pat.field[0] = sign; pat.field[3] = value; switch (sep_by_space) { - case 0: + case 0: // No space separates the currency symbol and value. pat.field[1] = symbol; pat.field[2] = none; return; - case 1: + case 1: // Space between currency-and-sign or currency and value. pat.field[1] = symbol; - pat.field[2] = space; + pat.field[2] = none; + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[2]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.push_back(space_char); + } return; - case 2: + case 2: // Space between sign and currency or value. pat.field[1] = space; pat.field[2] = symbol; + if (symbol_contains_sep) { + // Remove the separator from the symbol, since it + // has already appeared after the sign. + __curr_symbol_.pop_back(); + } return; default: break; } break; - case 2: + case 2: // The sign string succeeds the quantity and currency symbol. pat.field[0] = symbol; pat.field[3] = sign; switch (sep_by_space) { - case 0: + case 0: // No space separates the currency symbol and value. pat.field[1] = none; pat.field[2] = value; return; - case 1: - pat.field[1] = space; + case 1: // Space between currency-and-sign or currency and value. + pat.field[1] = none; pat.field[2] = value; + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[1]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.push_back(space_char); + } return; - case 2: + case 2: // Space between sign and currency or value. pat.field[1] = value; pat.field[2] = space; + if (symbol_contains_sep) { + // Remove the separator from the symbol, since it + // will appear before the sign. + __curr_symbol_.pop_back(); + } return; default: break; } break; - case 3: + case 3: // The sign string immediately precedes the currency symbol. pat.field[0] = sign; pat.field[3] = value; switch (sep_by_space) { - case 0: + case 0: // No space separates the currency symbol and value. pat.field[1] = symbol; pat.field[2] = none; return; - case 1: + case 1: // Space between currency-and-sign or currency and value. pat.field[1] = symbol; - pat.field[2] = space; + pat.field[2] = none; + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[2]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.push_back(space_char); + } return; - case 2: + case 2: // Space between sign and currency or value. pat.field[1] = space; pat.field[2] = symbol; + if (symbol_contains_sep) { + // Remove the separator from the symbol, since it + // has already appeared after the sign. + __curr_symbol_.pop_back(); + } return; default: break; } break; - case 4: + case 4: // The sign string immediately succeeds the currency symbol. pat.field[0] = symbol; pat.field[3] = value; switch (sep_by_space) { - case 0: + case 0: // No space separates the currency symbol and value. pat.field[1] = sign; pat.field[2] = none; return; - case 1: + case 1: // Space between currency-and-sign or currency and value. pat.field[1] = sign; pat.field[2] = space; + if (symbol_contains_sep) { + // Remove the separator from the symbol, since it + // should not disappear when showbase is absent. + __curr_symbol_.pop_back(); + } return; - case 2: - pat.field[1] = space; + case 2: // Space between sign and currency or value. + pat.field[1] = none; pat.field[2] = sign; + if (!symbol_contains_sep) { + // We insert the space into the symbol instead of + // setting pat.field[1]=space so that when + // showbase is not set, the space goes away too. + __curr_symbol_.push_back(space_char); + } return; default: break; @@ -5518,11 +5708,11 @@ moneypunct_byname<char, false>::init(const char* nm) typedef moneypunct<char, false> base; __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); #ifndef _LIBCPP_NO_EXCEPTIONS - if (loc == 0) + if (loc == nullptr) throw runtime_error("moneypunct_byname" " failed to construct for " + string(nm)); #endif // _LIBCPP_NO_EXCEPTIONS -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS lconv* lc = localeconv_l(loc.get()); #else lconv* lc = __localeconv_l(loc.get()); @@ -5549,8 +5739,14 @@ moneypunct_byname<char, false>::init(const char* nm) __negative_sign_ = "()"; else __negative_sign_ = lc->negative_sign; - __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn); - __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn); + // Assume the positive and negative formats will want spaces in + // the same places in curr_symbol since there's no way to + // represent anything else. + string_type __dummy_curr_symbol = __curr_symbol_; + __init_pat(__pos_format_, __dummy_curr_symbol, false, + lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' '); + __init_pat(__neg_format_, __curr_symbol_, false, + lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' '); } template<> @@ -5560,11 +5756,11 @@ moneypunct_byname<char, true>::init(const char* nm) typedef moneypunct<char, true> base; __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); #ifndef _LIBCPP_NO_EXCEPTIONS - if (loc == 0) + if (loc == nullptr) throw runtime_error("moneypunct_byname" " failed to construct for " + string(nm)); #endif // _LIBCPP_NO_EXCEPTIONS -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS lconv* lc = localeconv_l(loc.get()); #else lconv* lc = __localeconv_l(loc.get()); @@ -5599,12 +5795,22 @@ moneypunct_byname<char, true>::init(const char* nm) __negative_sign_ = "()"; else __negative_sign_ = lc->negative_sign; + // Assume the positive and negative formats will want spaces in + // the same places in curr_symbol since there's no way to + // represent anything else. + string_type __dummy_curr_symbol = __curr_symbol_; #if _WIN32 - __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn); - __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn); + __init_pat(__pos_format_, __dummy_curr_symbol, true, + lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' '); + __init_pat(__neg_format_, __curr_symbol_, true, + lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' '); #else - __init_pat(__pos_format_, lc->int_p_cs_precedes, lc->int_p_sep_by_space, lc->int_p_sign_posn); - __init_pat(__neg_format_, lc->int_n_cs_precedes, lc->int_n_sep_by_space, lc->int_n_sign_posn); + __init_pat(__pos_format_, __dummy_curr_symbol, true, + lc->int_p_cs_precedes, lc->int_p_sep_by_space, + lc->int_p_sign_posn, ' '); + __init_pat(__neg_format_, __curr_symbol_, true, + lc->int_n_cs_precedes, lc->int_n_sep_by_space, + lc->int_n_sign_posn, ' '); #endif // _WIN32 } @@ -5615,11 +5821,11 @@ moneypunct_byname<wchar_t, false>::init(const char* nm) typedef moneypunct<wchar_t, false> base; __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); #ifndef _LIBCPP_NO_EXCEPTIONS - if (loc == 0) + if (loc == nullptr) throw runtime_error("moneypunct_byname" " failed to construct for " + string(nm)); #endif // _LIBCPP_NO_EXCEPTIONS -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS lconv* lc = localeconv_l(loc.get()); #else lconv* lc = __localeconv_l(loc.get()); @@ -5636,12 +5842,12 @@ moneypunct_byname<wchar_t, false>::init(const char* nm) wchar_t wbuf[100]; mbstate_t mb = {0}; const char* bb = lc->currency_symbol; -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #else - size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); + size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wchar_t* wbe = wbuf + j; __curr_symbol_.assign(wbuf, wbe); @@ -5655,12 +5861,12 @@ moneypunct_byname<wchar_t, false>::init(const char* nm) { mb = mbstate_t(); bb = lc->positive_sign; -#ifdef _LIBCPP_STABLE_APPLE_ABI - j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #else - j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); + j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __positive_sign_.assign(wbuf, wbe); @@ -5671,18 +5877,24 @@ moneypunct_byname<wchar_t, false>::init(const char* nm) { mb = mbstate_t(); bb = lc->negative_sign; -#ifdef _LIBCPP_STABLE_APPLE_ABI - j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #else - j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); + j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __negative_sign_.assign(wbuf, wbe); } - __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn); - __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn); + // Assume the positive and negative formats will want spaces in + // the same places in curr_symbol since there's no way to + // represent anything else. + string_type __dummy_curr_symbol = __curr_symbol_; + __init_pat(__pos_format_, __dummy_curr_symbol, false, + lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' '); + __init_pat(__neg_format_, __curr_symbol_, false, + lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' '); } template<> @@ -5692,11 +5904,11 @@ moneypunct_byname<wchar_t, true>::init(const char* nm) typedef moneypunct<wchar_t, true> base; __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); #ifndef _LIBCPP_NO_EXCEPTIONS - if (loc == 0) + if (loc == nullptr) throw runtime_error("moneypunct_byname" " failed to construct for " + string(nm)); #endif // _LIBCPP_NO_EXCEPTIONS -#ifdef _LIBCPP_STABLE_APPLE_ABI +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS lconv* lc = localeconv_l(loc.get()); #else lconv* lc = __localeconv_l(loc.get()); @@ -5713,12 +5925,12 @@ moneypunct_byname<wchar_t, true>::init(const char* nm) wchar_t wbuf[100]; mbstate_t mb = {0}; const char* bb = lc->int_curr_symbol; -#ifdef _LIBCPP_STABLE_APPLE_ABI - size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #else - size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); + size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wchar_t* wbe = wbuf + j; __curr_symbol_.assign(wbuf, wbe); @@ -5736,12 +5948,12 @@ moneypunct_byname<wchar_t, true>::init(const char* nm) { mb = mbstate_t(); bb = lc->positive_sign; -#ifdef _LIBCPP_STABLE_APPLE_ABI - j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #else - j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); + j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __positive_sign_.assign(wbuf, wbe); @@ -5756,22 +5968,32 @@ moneypunct_byname<wchar_t, true>::init(const char* nm) { mb = mbstate_t(); bb = lc->negative_sign; -#ifdef _LIBCPP_STABLE_APPLE_ABI - j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); +#ifdef _LIBCPP_LOCALE__L_EXTENSIONS + j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #else - j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); + j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); #endif - if (j == -1) + if (j == size_t(-1)) __throw_runtime_error("locale not supported"); wbe = wbuf + j; __negative_sign_.assign(wbuf, wbe); } + // Assume the positive and negative formats will want spaces in + // the same places in curr_symbol since there's no way to + // represent anything else. + string_type __dummy_curr_symbol = __curr_symbol_; #if _WIN32 - __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn); - __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn); + __init_pat(__pos_format_, __dummy_curr_symbol, true, + lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' '); + __init_pat(__neg_format_, __curr_symbol_, true, + lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' '); #else // _WIN32 - __init_pat(__pos_format_, lc->int_p_cs_precedes, lc->int_p_sep_by_space, lc->int_p_sign_posn); - __init_pat(__neg_format_, lc->int_n_cs_precedes, lc->int_n_sep_by_space, lc->int_n_sign_posn); + __init_pat(__pos_format_, __dummy_curr_symbol, true, + lc->int_p_cs_precedes, lc->int_p_sep_by_space, + lc->int_p_sign_posn, L' '); + __init_pat(__neg_format_, __curr_symbol_, true, + lc->int_n_cs_precedes, lc->int_n_sep_by_space, + lc->int_n_sign_posn, L' '); #endif // _WIN32 } @@ -5790,14 +6012,14 @@ template class collate<wchar_t>; template class num_get<char>; template class num_get<wchar_t>; -template class __num_get<char>; -template class __num_get<wchar_t>; +template struct __num_get<char>; +template struct __num_get<wchar_t>; template class num_put<char>; template class num_put<wchar_t>; -template class __num_put<char>; -template class __num_put<wchar_t>; +template struct __num_put<char>; +template struct __num_put<wchar_t>; template class time_get<char>; template class time_get<wchar_t>; diff --git a/system/lib/libcxx/memory.cpp b/system/lib/libcxx/memory.cpp index cb5e5e7b..14084a52 100644 --- a/system/lib/libcxx/memory.cpp +++ b/system/lib/libcxx/memory.cpp @@ -7,7 +7,10 @@ // //===----------------------------------------------------------------------===// +#define _LIBCPP_BUILDING_MEMORY #include "memory" +#include "mutex" +#include "thread" _LIBCPP_BEGIN_NAMESPACE_STD @@ -100,10 +103,7 @@ __shared_weak_count::lock() _NOEXCEPT if (__sync_bool_compare_and_swap(&__shared_owners_, object_owners, object_owners+1)) - { - __add_weak(); return this; - } object_owners = __shared_owners_; } return 0; @@ -119,6 +119,53 @@ __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT #endif // _LIBCPP_NO_RTTI +#if __has_feature(cxx_atomic) + +static const std::size_t __sp_mut_count = 16; +static mutex mut_back[__sp_mut_count]; + +_LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT + : __lx(p) +{ +} + +void +__sp_mut::lock() _NOEXCEPT +{ + mutex& m = *static_cast<mutex*>(__lx); + unsigned count = 0; + while (!m.try_lock()) + { + if (++count > 16) + { + m.lock(); + break; + } + this_thread::yield(); + } +} + +void +__sp_mut::unlock() _NOEXCEPT +{ + static_cast<mutex*>(__lx)->unlock(); +} + +__sp_mut& +__get_sp_mut(const void* p) +{ + static __sp_mut muts[__sp_mut_count] + { + &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3], + &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7], + &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11], + &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15] + }; + return muts[hash<const void*>()(p) & (__sp_mut_count-1)]; +} + +#endif // __has_feature(cxx_atomic) + void declare_reachable(void*) { @@ -154,7 +201,7 @@ align(size_t alignment, size_t size, void*& ptr, size_t& space) { char* p1 = static_cast<char*>(ptr); char* p2 = (char*)((size_t)(p1 + (alignment - 1)) & -alignment); - ptrdiff_t d = p2 - p1; + size_t d = static_cast<size_t>(p2 - p1); if (d <= space - size) { r = p2; diff --git a/system/lib/libcxx/mutex.cpp b/system/lib/libcxx/mutex.cpp index 16817198..42195aa8 100644 --- a/system/lib/libcxx/mutex.cpp +++ b/system/lib/libcxx/mutex.cpp @@ -7,6 +7,7 @@ // //===----------------------------------------------------------------------===// +#define _LIBCPP_BUILDING_MUTEX #include "mutex" #include "limits" #include "system_error" @@ -20,8 +21,7 @@ const adopt_lock_t adopt_lock = {}; mutex::~mutex() { - int e = pthread_mutex_destroy(&__m_); -// assert(e == 0); + pthread_mutex_destroy(&__m_); } void @@ -33,13 +33,13 @@ mutex::lock() } bool -mutex::try_lock() +mutex::try_lock() _NOEXCEPT { return pthread_mutex_trylock(&__m_) == 0; } void -mutex::unlock() +mutex::unlock() _NOEXCEPT { int ec = pthread_mutex_unlock(&__m_); assert(ec == 0); @@ -91,14 +91,14 @@ recursive_mutex::lock() } void -recursive_mutex::unlock() +recursive_mutex::unlock() _NOEXCEPT { int e = pthread_mutex_unlock(&__m_); assert(e == 0); } bool -recursive_mutex::try_lock() +recursive_mutex::try_lock() _NOEXCEPT { return pthread_mutex_trylock(&__m_) == 0; } @@ -125,7 +125,7 @@ timed_mutex::lock() } bool -timed_mutex::try_lock() +timed_mutex::try_lock() _NOEXCEPT { unique_lock<mutex> lk(__m_, try_to_lock); if (lk.owns_lock() && !__locked_) @@ -137,7 +137,7 @@ timed_mutex::try_lock() } void -timed_mutex::unlock() +timed_mutex::unlock() _NOEXCEPT { lock_guard<mutex> _(__m_); __locked_ = false; @@ -176,7 +176,7 @@ recursive_timed_mutex::lock() } bool -recursive_timed_mutex::try_lock() +recursive_timed_mutex::try_lock() _NOEXCEPT { pthread_t id = pthread_self(); unique_lock<mutex> lk(__m_, try_to_lock); @@ -192,7 +192,7 @@ recursive_timed_mutex::try_lock() } void -recursive_timed_mutex::unlock() +recursive_timed_mutex::unlock() _NOEXCEPT { unique_lock<mutex> lk(__m_); if (--__count_ == 0) diff --git a/system/lib/libcxx/new.cpp b/system/lib/libcxx/new.cpp index 1e8ed88d..3ad593a3 100644 --- a/system/lib/libcxx/new.cpp +++ b/system/lib/libcxx/new.cpp @@ -11,14 +11,26 @@ #include "new" +#ifndef __has_include +#define __has_include(inc) 0 +#endif + #if __APPLE__ #include <cxxabi.h> - // On Darwin, there are two STL shared libraries and a lower level ABI - // shared libray. The global holding the current new handler is - // in the ABI library and named __cxa_new_handler. - #define __new_handler __cxxabiapple::__cxa_new_handler + + #ifndef _LIBCPPABI_VERSION + // On Darwin, there are two STL shared libraries and a lower level ABI + // shared libray. The global holding the current new handler is + // in the ABI library and named __cxa_new_handler. + #define __new_handler __cxxabiapple::__cxa_new_handler + #endif #else // __APPLE__ - static std::new_handler __new_handler; + #if defined(LIBCXXRT) || __has_include(<cxxabi.h>) + #include <cxxabi.h> + #endif // __has_include(<cxxabi.h>) + #ifndef _LIBCPPABI_VERSION + static std::new_handler __new_handler; + #endif // _LIBCPPABI_VERSION #endif // Implement all new and delete operators as weak definitions @@ -83,7 +95,7 @@ operator new[](size_t size) __attribute__((__weak__, __visibility__("default"))) void* -operator new[](size_t size, const std::nothrow_t& nothrow) _NOEXCEPT +operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT { void* p = 0; #ifndef _LIBCPP_NO_EXCEPTIONS @@ -134,6 +146,8 @@ namespace std const nothrow_t nothrow = {}; +#ifndef _LIBCPPABI_VERSION + new_handler set_new_handler(new_handler handler) _NOEXCEPT { @@ -146,6 +160,8 @@ get_new_handler() _NOEXCEPT return __sync_fetch_and_add(&__new_handler, (new_handler)0); } +#ifndef LIBCXXRT + bad_alloc::bad_alloc() _NOEXCEPT { } @@ -160,6 +176,8 @@ bad_alloc::what() const _NOEXCEPT return "std::bad_alloc"; } +#endif //LIBCXXRT + bad_array_new_length::bad_array_new_length() _NOEXCEPT { } @@ -174,6 +192,8 @@ bad_array_new_length::what() const _NOEXCEPT return "bad_array_new_length"; } +#endif + void __throw_bad_alloc() { diff --git a/system/lib/libcxx/random.cpp b/system/lib/libcxx/random.cpp index eca97bc8..97a40c50 100644 --- a/system/lib/libcxx/random.cpp +++ b/system/lib/libcxx/random.cpp @@ -10,6 +10,9 @@ #include "random" #include "system_error" +#ifdef __sun__ +#define rename solaris_headers_are_broken +#endif #include <fcntl.h> #include <unistd.h> #include <errno.h> @@ -37,7 +40,7 @@ random_device::operator()() } double -random_device::entropy() const +random_device::entropy() const _NOEXCEPT { return 0; } diff --git a/system/lib/libcxx/readme.txt b/system/lib/libcxx/readme.txt index c7e41df9..c0c90c3a 100644 --- a/system/lib/libcxx/readme.txt +++ b/system/lib/libcxx/readme.txt @@ -1 +1 @@ -These files are from libc++, svn revision 140465, Sep 24 2011 +These files are from libc++, svn revision 176559, Mar 7 2013 diff --git a/system/lib/libcxx/regex.cpp b/system/lib/libcxx/regex.cpp index 65e9f886..e3ec2810 100644 --- a/system/lib/libcxx/regex.cpp +++ b/system/lib/libcxx/regex.cpp @@ -69,12 +69,17 @@ regex_error::~regex_error() throw() {} namespace { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" + struct collationnames { const char* elem_; char char_; }; +#pragma clang diagnostic pop + const collationnames collatenames[] = { {"A", 0x41}, @@ -190,12 +195,17 @@ const collationnames collatenames[] = {"zero", 0x30} }; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" + struct classnames { const char* elem_; ctype_base::mask mask_; }; +#pragma clang diagnostic pop + const classnames ClassNames[] = { {"alnum", ctype_base::alnum}, diff --git a/system/lib/libcxx/stdexcept.cpp b/system/lib/libcxx/stdexcept.cpp index 28917887..660ebfe2 100644 --- a/system/lib/libcxx/stdexcept.cpp +++ b/system/lib/libcxx/stdexcept.cpp @@ -16,6 +16,16 @@ #include <cstddef> #include "system_error" +#ifndef __has_include +#define __has_include(inc) 0 +#endif + +#if __APPLE__ +#include <cxxabi.h> +#elif defined(LIBCXXRT) || __has_include(<cxxabi.h>) +#include <cxxabi.h> +#endif + // Note: optimize for size #pragma GCC visibility push(hidden) @@ -29,7 +39,7 @@ private: const char* str_; typedef std::size_t unused_t; - typedef std::int32_t count_t; + typedef std::ptrdiff_t count_t; static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) + sizeof(count_t)); @@ -67,7 +77,7 @@ __libcpp_nmstr::operator=(const __libcpp_nmstr& s) const char* p = str_; str_ = s.str_; __sync_add_and_fetch(&count(), 1); - if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), -1) < 0) + if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0) delete [] (p-offset); return *this; } @@ -75,7 +85,7 @@ __libcpp_nmstr::operator=(const __libcpp_nmstr& s) inline __libcpp_nmstr::~__libcpp_nmstr() { - if (__sync_add_and_fetch(&count(), -1) < 0) + if (__sync_add_and_fetch(&count(), count_t(-1)) < 0) delete [] (str_ - offset); } @@ -113,6 +123,8 @@ logic_error::operator=(const logic_error& le) _NOEXCEPT return *this; } +#ifndef _LIBCPPABI_VERSION + logic_error::~logic_error() _NOEXCEPT { __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; @@ -126,6 +138,8 @@ logic_error::what() const _NOEXCEPT return s.c_str(); } +#endif + runtime_error::runtime_error(const string& msg) { __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; @@ -153,6 +167,8 @@ runtime_error::operator=(const runtime_error& le) _NOEXCEPT return *this; } +#ifndef _LIBCPPABI_VERSION + runtime_error::~runtime_error() _NOEXCEPT { __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; @@ -175,4 +191,6 @@ range_error::~range_error() _NOEXCEPT {} overflow_error::~overflow_error() _NOEXCEPT {} underflow_error::~underflow_error() _NOEXCEPT {} +#endif + } // std diff --git a/system/lib/libcxx/string.cpp b/system/lib/libcxx/string.cpp index 1f58e365..40723e74 100644 --- a/system/lib/libcxx/string.cpp +++ b/system/lib/libcxx/string.cpp @@ -31,17 +31,17 @@ stoi(const string& str, size_t* idx, int base) { char* ptr; const char* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; long r = strtol(p, &ptr, base); - if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) - ptr = const_cast<char*>(p); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stoi: no conversion"); + if (errno_save == ERANGE || r < numeric_limits<int>::min() || + numeric_limits<int>::max() < r) throw out_of_range("stoi: out of range"); + if (ptr == p) + throw invalid_argument("stoi: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return static_cast<int>(r); @@ -52,17 +52,17 @@ stoi(const wstring& str, size_t* idx, int base) { wchar_t* ptr; const wchar_t* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; long r = wcstol(p, &ptr, base); - if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) - ptr = const_cast<wchar_t*>(p); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stoi: no conversion"); + if (errno_save == ERANGE || r < numeric_limits<int>::min() || + numeric_limits<int>::max() < r) throw out_of_range("stoi: out of range"); + if (ptr == p) + throw invalid_argument("stoi: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return static_cast<int>(r); @@ -73,15 +73,16 @@ stol(const string& str, size_t* idx, int base) { char* ptr; const char* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; long r = strtol(p, &ptr, base); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stol: no conversion"); + if (errno_save == ERANGE) throw out_of_range("stol: out of range"); + if (ptr == p) + throw invalid_argument("stol: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return r; @@ -92,15 +93,16 @@ stol(const wstring& str, size_t* idx, int base) { wchar_t* ptr; const wchar_t* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; long r = wcstol(p, &ptr, base); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stol: no conversion"); + if (errno_save == ERANGE) throw out_of_range("stol: out of range"); + if (ptr == p) + throw invalid_argument("stol: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return r; @@ -111,15 +113,16 @@ stoul(const string& str, size_t* idx, int base) { char* ptr; const char* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; unsigned long r = strtoul(p, &ptr, base); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stoul: no conversion"); + if (errno_save == ERANGE) throw out_of_range("stoul: out of range"); + if (ptr == p) + throw invalid_argument("stoul: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return r; @@ -130,15 +133,16 @@ stoul(const wstring& str, size_t* idx, int base) { wchar_t* ptr; const wchar_t* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; unsigned long r = wcstoul(p, &ptr, base); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stoul: no conversion"); + if (errno_save == ERANGE) throw out_of_range("stoul: out of range"); + if (ptr == p) + throw invalid_argument("stoul: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return r; @@ -149,15 +153,16 @@ stoll(const string& str, size_t* idx, int base) { char* ptr; const char* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; long long r = strtoll(p, &ptr, base); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stoll: no conversion"); + if (errno_save == ERANGE) throw out_of_range("stoll: out of range"); + if (ptr == p) + throw invalid_argument("stoll: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return r; @@ -168,15 +173,16 @@ stoll(const wstring& str, size_t* idx, int base) { wchar_t* ptr; const wchar_t* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; long long r = wcstoll(p, &ptr, base); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stoll: no conversion"); + if (errno_save == ERANGE) throw out_of_range("stoll: out of range"); + if (ptr == p) + throw invalid_argument("stoll: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return r; @@ -187,15 +193,16 @@ stoull(const string& str, size_t* idx, int base) { char* ptr; const char* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; unsigned long long r = strtoull(p, &ptr, base); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stoull: no conversion"); + if (errno_save == ERANGE) throw out_of_range("stoull: out of range"); + if (ptr == p) + throw invalid_argument("stoull: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return r; @@ -206,15 +213,16 @@ stoull(const wstring& str, size_t* idx, int base) { wchar_t* ptr; const wchar_t* const p = str.c_str(); + typename remove_reference<decltype(errno)>::type errno_save = errno; + errno = 0; unsigned long long r = wcstoull(p, &ptr, base); - if (ptr == p) - { + swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS - if (r == 0) - throw invalid_argument("stoull: no conversion"); + if (errno_save == ERANGE) throw out_of_range("stoull: out of range"); + if (ptr == p) + throw invalid_argument("stoull: no conversion"); #endif // _LIBCPP_NO_EXCEPTIONS - } if (idx) *idx = static_cast<size_t>(ptr - p); return r; @@ -225,9 +233,9 @@ stof(const string& str, size_t* idx) { char* ptr; const char* const p = str.c_str(); - int errno_save = errno; + typename remove_reference<decltype(errno)>::type errno_save = errno; errno = 0; - double r = strtod(p, &ptr); + float r = strtof(p, &ptr); swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS if (errno_save == ERANGE) @@ -237,7 +245,7 @@ stof(const string& str, size_t* idx) #endif // _LIBCPP_NO_EXCEPTIONS if (idx) *idx = static_cast<size_t>(ptr - p); - return static_cast<float>(r); + return r; } float @@ -245,9 +253,9 @@ stof(const wstring& str, size_t* idx) { wchar_t* ptr; const wchar_t* const p = str.c_str(); - int errno_save = errno; + typename remove_reference<decltype(errno)>::type errno_save = errno; errno = 0; - double r = wcstod(p, &ptr); + float r = wcstof(p, &ptr); swap(errno, errno_save); #ifndef _LIBCPP_NO_EXCEPTIONS if (errno_save == ERANGE) @@ -257,7 +265,7 @@ stof(const wstring& str, size_t* idx) #endif // _LIBCPP_NO_EXCEPTIONS if (idx) *idx = static_cast<size_t>(ptr - p); - return static_cast<float>(r); + return r; } double @@ -265,7 +273,7 @@ stod(const string& str, size_t* idx) { char* ptr; const char* const p = str.c_str(); - int errno_save = errno; + typename remove_reference<decltype(errno)>::type errno_save = errno; errno = 0; double r = strtod(p, &ptr); swap(errno, errno_save); @@ -285,7 +293,7 @@ stod(const wstring& str, size_t* idx) { wchar_t* ptr; const wchar_t* const p = str.c_str(); - int errno_save = errno; + typename remove_reference<decltype(errno)>::type errno_save = errno; errno = 0; double r = wcstod(p, &ptr); swap(errno, errno_save); @@ -305,7 +313,7 @@ stold(const string& str, size_t* idx) { char* ptr; const char* const p = str.c_str(); - int errno_save = errno; + typename remove_reference<decltype(errno)>::type errno_save = errno; errno = 0; long double r = strtold(p, &ptr); swap(errno, errno_save); @@ -325,7 +333,7 @@ stold(const wstring& str, size_t* idx) { wchar_t* ptr; const wchar_t* const p = str.c_str(); - int errno_save = errno; + typename remove_reference<decltype(errno)>::type errno_save = errno; errno = 0; long double r = wcstold(p, &ptr); swap(errno, errno_save); @@ -346,7 +354,7 @@ string to_string(int val) s.resize(s.capacity()); while (true) { - int n2 = snprintf(&s[0], s.size()+1, "%d", val); + size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%d", val)); if (n2 <= s.size()) { s.resize(n2); @@ -363,7 +371,7 @@ string to_string(unsigned val) s.resize(s.capacity()); while (true) { - int n2 = snprintf(&s[0], s.size()+1, "%u", val); + size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%u", val)); if (n2 <= s.size()) { s.resize(n2); @@ -380,7 +388,7 @@ string to_string(long val) s.resize(s.capacity()); while (true) { - int n2 = snprintf(&s[0], s.size()+1, "%ld", val); + size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%ld", val)); if (n2 <= s.size()) { s.resize(n2); @@ -397,7 +405,7 @@ string to_string(unsigned long val) s.resize(s.capacity()); while (true) { - int n2 = snprintf(&s[0], s.size()+1, "%lu", val); + size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%lu", val)); if (n2 <= s.size()) { s.resize(n2); @@ -414,7 +422,7 @@ string to_string(long long val) s.resize(s.capacity()); while (true) { - int n2 = snprintf(&s[0], s.size()+1, "%lld", val); + size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%lld", val)); if (n2 <= s.size()) { s.resize(n2); @@ -431,7 +439,7 @@ string to_string(unsigned long long val) s.resize(s.capacity()); while (true) { - int n2 = snprintf(&s[0], s.size()+1, "%llu", val); + size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%llu", val)); if (n2 <= s.size()) { s.resize(n2); @@ -448,7 +456,7 @@ string to_string(float val) s.resize(s.capacity()); while (true) { - int n2 = snprintf(&s[0], s.size()+1, "%f", val); + size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%f", val)); if (n2 <= s.size()) { s.resize(n2); @@ -465,7 +473,7 @@ string to_string(double val) s.resize(s.capacity()); while (true) { - int n2 = snprintf(&s[0], s.size()+1, "%f", val); + size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%f", val)); if (n2 <= s.size()) { s.resize(n2); @@ -482,7 +490,7 @@ string to_string(long double val) s.resize(s.capacity()); while (true) { - int n2 = snprintf(&s[0], s.size()+1, "%Lf", val); + size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%Lf", val)); if (n2 <= s.size()) { s.resize(n2); @@ -505,7 +513,7 @@ wstring to_wstring(int val) int n2 = swprintf(&s[0], s.size()+1, L"%d", val); if (n2 > 0) { - s.resize(n2); + s.resize(static_cast<size_t>(n2)); break; } s.resize(2*s.size()); @@ -526,7 +534,7 @@ wstring to_wstring(unsigned val) int n2 = swprintf(&s[0], s.size()+1, L"%u", val); if (n2 > 0) { - s.resize(n2); + s.resize(static_cast<size_t>(n2)); break; } s.resize(2*s.size()); @@ -547,7 +555,7 @@ wstring to_wstring(long val) int n2 = swprintf(&s[0], s.size()+1, L"%ld", val); if (n2 > 0) { - s.resize(n2); + s.resize(static_cast<size_t>(n2)); break; } s.resize(2*s.size()); @@ -568,7 +576,7 @@ wstring to_wstring(unsigned long val) int n2 = swprintf(&s[0], s.size()+1, L"%lu", val); if (n2 > 0) { - s.resize(n2); + s.resize(static_cast<size_t>(n2)); break; } s.resize(2*s.size()); @@ -589,7 +597,7 @@ wstring to_wstring(long long val) int n2 = swprintf(&s[0], s.size()+1, L"%lld", val); if (n2 > 0) { - s.resize(n2); + s.resize(static_cast<size_t>(n2)); break; } s.resize(2*s.size()); @@ -610,7 +618,7 @@ wstring to_wstring(unsigned long long val) int n2 = swprintf(&s[0], s.size()+1, L"%llu", val); if (n2 > 0) { - s.resize(n2); + s.resize(static_cast<size_t>(n2)); break; } s.resize(2*s.size()); @@ -629,7 +637,7 @@ wstring to_wstring(float val) int n2 = swprintf(&s[0], s.size()+1, L"%f", val); if (n2 > 0) { - s.resize(n2); + s.resize(static_cast<size_t>(n2)); break; } s.resize(2*s.size()); @@ -648,7 +656,7 @@ wstring to_wstring(double val) int n2 = swprintf(&s[0], s.size()+1, L"%f", val); if (n2 > 0) { - s.resize(n2); + s.resize(static_cast<size_t>(n2)); break; } s.resize(2*s.size()); @@ -667,7 +675,7 @@ wstring to_wstring(long double val) int n2 = swprintf(&s[0], s.size()+1, L"%Lf", val); if (n2 > 0) { - s.resize(n2); + s.resize(static_cast<size_t>(n2)); break; } s.resize(2*s.size()); diff --git a/system/lib/libcxx/strstream.cpp b/system/lib/libcxx/strstream.cpp index 53139509..8cd19e6a 100644 --- a/system/lib/libcxx/strstream.cpp +++ b/system/lib/libcxx/strstream.cpp @@ -34,7 +34,7 @@ void strstreambuf::__init(char* __gnext, streamsize __n, char* __pbeg) { if (__n == 0) - __n = strlen(__gnext); + __n = static_cast<streamsize>(strlen(__gnext)); else if (__n < 0) __n = INT_MAX; if (__pbeg == nullptr) @@ -160,12 +160,12 @@ strstreambuf::overflow(int_type __c) streamsize new_size = max<streamsize>(__alsize_, 2*old_size); char* buf = nullptr; if (__palloc_) - buf = static_cast<char*>(__palloc_(new_size)); + buf = static_cast<char*>(__palloc_(static_cast<size_t>(new_size))); else buf = new char[new_size]; if (buf == nullptr) return int_type(EOF); - memcpy(buf, eback(), old_size); + memcpy(buf, eback(), static_cast<size_t>(old_size)); ptrdiff_t ninp = gptr() - eback(); ptrdiff_t einp = egptr() - eback(); ptrdiff_t nout = pptr() - pbase(); @@ -179,7 +179,7 @@ strstreambuf::overflow(int_type __c) } setg(buf, buf + ninp, buf + einp); setp(buf + einp, buf + einp + eout); - pbump(nout); + pbump(static_cast<int>(nout)); __strmode_ |= __allocated; } *pptr() = static_cast<char>(__c); diff --git a/system/lib/libcxx/support/solaris/README b/system/lib/libcxx/support/solaris/README new file mode 100644 index 00000000..89c887a3 --- /dev/null +++ b/system/lib/libcxx/support/solaris/README @@ -0,0 +1,4 @@ +This directory contains a partial implementation of the xlocale APIs for +Solaris. Some portions are lifted from FreeBSD libc, and so are covered by a +2-clause BSD license instead of the MIT/UUIC license that the rest of libc++ is +distributed under. diff --git a/system/lib/libcxx/support/solaris/mbsnrtowcs.inc b/system/lib/libcxx/support/solaris/mbsnrtowcs.inc new file mode 100644 index 00000000..07404527 --- /dev/null +++ b/system/lib/libcxx/support/solaris/mbsnrtowcs.inc @@ -0,0 +1,76 @@ + + +/*- + * As noted in the source, some portions of this implementation are copied from + * FreeBSD libc. These are covered by the following copyright: + * + * Copyright (c) 2002-2004 Tim J. Robbins. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +size_t +mbsnrtowcs_l(wchar_t * __restrict dst, const char ** __restrict src, + size_t nms, size_t len, mbstate_t * __restrict ps, locale_t loc) +{ + const char *s; + size_t nchr; + wchar_t wc; + size_t nb; + FIX_LOCALE(loc); + + s = *src; + nchr = 0; + + if (dst == NULL) { + for (;;) { + if ((nb = mbrtowc_l(&wc, s, nms, ps, loc)) == (size_t)-1) + /* Invalid sequence - mbrtowc() sets errno. */ + return ((size_t)-1); + else if (nb == 0 || nb == (size_t)-2) + return (nchr); + s += nb; + nms -= nb; + nchr++; + } + /*NOTREACHED*/ + } + + while (len-- > 0) { + if ((nb = mbrtowc_l(dst, s, nms, ps, loc)) == (size_t)-1) { + *src = s; + return ((size_t)-1); + } else if (nb == (size_t)-2) { + *src = s + nms; + return (nchr); + } else if (nb == 0) { + *src = NULL; + return (nchr); + } + s += nb; + nms -= nb; + nchr++; + dst++; + } + *src = s; + return (nchr); +} diff --git a/system/lib/libcxx/support/solaris/wcsnrtombs.inc b/system/lib/libcxx/support/solaris/wcsnrtombs.inc new file mode 100644 index 00000000..67e7078f --- /dev/null +++ b/system/lib/libcxx/support/solaris/wcsnrtombs.inc @@ -0,0 +1,93 @@ +/*- + * Copyright (c) 2002-2004 Tim J. Robbins. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + +size_t +wcsnrtombs_l(char * __restrict dst, const wchar_t ** __restrict src, + size_t nwc, size_t len, mbstate_t * __restrict ps, locale_t loc) +{ + FIX_LOCALE(loc); + mbstate_t mbsbak; + char buf[MB_CUR_MAX_L(loc)]; + const wchar_t *s; + size_t nbytes; + size_t nb; + + s = *src; + nbytes = 0; + + if (dst == NULL) { + while (nwc-- > 0) { + if ((nb = wcrtomb_l(buf, *s, ps, loc)) == (size_t)-1) + /* Invalid character - wcrtomb() sets errno. */ + return ((size_t)-1); + else if (*s == L'\0') + return (nbytes + nb - 1); + s++; + nbytes += nb; + } + return (nbytes); + } + + while (len > 0 && nwc-- > 0) { + if (len > (size_t)MB_CUR_MAX_L(loc)) { + /* Enough space to translate in-place. */ + if ((nb = wcrtomb_l(dst, *s, ps, loc)) == (size_t)-1) { + *src = s; + return ((size_t)-1); + } + } else { + /* + * May not be enough space; use temp. buffer. + * + * We need to save a copy of the conversion state + * here so we can restore it if the multibyte + * character is too long for the buffer. + */ + mbsbak = *ps; + if ((nb = wcrtomb_l(buf, *s, ps, loc)) == (size_t)-1) { + *src = s; + return ((size_t)-1); + } + if (nb > (int)len) { + /* MB sequence for character won't fit. */ + *ps = mbsbak; + break; + } + memcpy(dst, buf, nb); + } + if (*s == L'\0') { + *src = NULL; + return (nbytes + nb - 1); + } + s++; + dst += nb; + len -= nb; + nbytes += nb; + } + *src = s; + return (nbytes); +} + diff --git a/system/lib/libcxx/support/solaris/xlocale.c b/system/lib/libcxx/support/solaris/xlocale.c new file mode 100644 index 00000000..a2c1fa90 --- /dev/null +++ b/system/lib/libcxx/support/solaris/xlocale.c @@ -0,0 +1,245 @@ + +#ifdef __sun__ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <dlfcn.h> +#include <locale.h> +#include <limits.h> +#include <assert.h> +#include <sys/localedef.h> +#include "xlocale.h" + +static _LC_locale_t *__C_locale; + +#define FIX_LOCALE(l) l = (l == 0) ? __C_locale : l + +#include "mbsnrtowcs.inc" +#include "wcsnrtombs.inc" + +size_t __mb_cur_max(locale_t __l) { + FIX_LOCALE(__l); + return (__l->lc_ctype->cmapp->cm_mb_cur_max); +} + +wint_t btowc_l(int __c, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_ctype->cmapp->core.user_api->btowc(__l->lc_ctype->cmapp, __c); +} + +int wctob_l(wint_t __c, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_ctype->cmapp->core.user_api->wctob(__l->lc_ctype->cmapp, __c); +} + +size_t wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_ctype->cmapp->core.user_api->wcrtomb(__l->lc_ctype->cmapp, + __s, __wc, __ps); +} + +size_t mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n, + mbstate_t *__ps, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_ctype->cmapp->core.user_api->mbrtowc(__l->lc_ctype->cmapp, + __pwc, __s, __n, __ps); +} + +int mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_ctype->cmapp->core.user_api->mbtowc(__l->lc_ctype->cmapp, + __pwc, __pmb, __max); +} + +size_t mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_ctype->cmapp->core.user_api->mbrlen(__l->lc_ctype->cmapp, __s, + __n, __ps); +} + +struct lconv *localeconv_l(locale_t __l) { + FIX_LOCALE(__l); + return __l->core.user_api->localeconv(__l); +} + +size_t mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len, + mbstate_t *__ps, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_ctype->cmapp->core.user_api->mbsrtowcs(__l->lc_ctype->cmapp, + __dest, __src, __len, __ps); +} + +int wcscoll_l(const wchar_t *__s1, const wchar_t *__s2, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_collate->core.user_api->wcscoll(__l->lc_collate, + __s1, __s2); +} + +int strcoll_l(const char *__s1, const char *__s2, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_collate->core.user_api->strcoll(__l->lc_collate, + __s1, __s2); +} + +size_t strxfrm_l(char *__s1, const char *__s2, size_t __n, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_collate->core.user_api->strxfrm(__l->lc_collate, + __s1, __s2, __n); +} +size_t strftime_l(char *__s, size_t __size, const char *__fmt, const struct tm + *__tm, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_time->core.user_api->strftime(__l->lc_time, + __s, __size, __fmt, __tm); +} + +size_t wcsxfrm_l(wchar_t *__ws1, const wchar_t *__ws2, size_t __n, + locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_collate->core.user_api->wcsxfrm(__l->lc_collate, + __ws1, __ws2, __n); +} + +#define LOCALE_ISCTYPE(ctype, m) \ + int is##ctype##_l(int __c, locale_t __l) { \ + if ((__c < 0) || (__c > 255)) return 0;\ + FIX_LOCALE(__l);\ + return __l->lc_ctype->mask[__c] & m;\ + }\ + int isw##ctype##_l(wchar_t __c, locale_t __l) { \ + FIX_LOCALE(__l);\ + return __l->lc_ctype->core.user_api->iswctype(__l->lc_ctype, __c, m);\ + } + +LOCALE_ISCTYPE(alnum, _ISALNUM) +LOCALE_ISCTYPE(alpha, _ISALPHA) +LOCALE_ISCTYPE(blank, _ISALPHA) +LOCALE_ISCTYPE(cntrl, _ISCNTRL) +LOCALE_ISCTYPE(digit, _ISDIGIT) +LOCALE_ISCTYPE(graph, _ISGRAPH) +LOCALE_ISCTYPE(lower, _ISLOWER) +LOCALE_ISCTYPE(print, _ISPRINT) +LOCALE_ISCTYPE(punct, _ISPUNCT) +LOCALE_ISCTYPE(space, _ISSPACE) +LOCALE_ISCTYPE(upper, _ISUPPER) +LOCALE_ISCTYPE(xdigit, _ISXDIGIT) + +int iswctype_l(wint_t __c, wctype_t __m, locale_t __l) { + FIX_LOCALE(__l);\ + return __l->lc_ctype->core.user_api->iswctype(__l->lc_ctype, __c, __m);\ +} + +int toupper_l(int __c, locale_t __l) { + FIX_LOCALE(__l); + if ((__c < 0) || (__c > __l->lc_ctype->max_upper)) return __c; + return __l->lc_ctype->upper[__c]; +} +int tolower_l(int __c, locale_t __l) { + FIX_LOCALE(__l); + if ((__c < 0) || (__c > __l->lc_ctype->max_lower)) return __c; + return __l->lc_ctype->lower[__c]; +} +wint_t towupper_l(wint_t __c, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_ctype->core.user_api->towupper(__l->lc_ctype, __c); +} +wint_t towlower_l(wint_t __c, locale_t __l) { + FIX_LOCALE(__l); + return __l->lc_ctype->core.user_api->towlower(__l->lc_ctype, __c); +} + +// FIXME: This disregards the locale, which is Very Wrong +#define vsnprintf_l(__s, __n, __l, __format, __va) \ + vsnprintf(__s, __n, __format, __va) + +int sprintf_l(char *__s, locale_t __l, const char *__format, ...) { + va_list __va; + va_start(__va, __format); + int __res = vsnprintf_l(__s, SIZE_MAX, __l, __format, __va); + va_end(__va); + return __res; +} + +int snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) +{ + va_list __va; + va_start(__va, __format); + int __res = vsnprintf_l(__s, __n , __l, __format, __va); + va_end(__va); + return __res; +} + +int asprintf_l(char **__s, locale_t __l, const char *__format, ...) { + va_list __va; + va_start(__va, __format); + // FIXME: + int __res = vasprintf(__s, __format, __va); + va_end(__va); + return __res; +} + +int sscanf_l(const char *__s, locale_t __l, const char *__format, ...) { + va_list __va; + va_start(__va, __format); + // FIXME: + int __res = vsscanf(__s, __format, __va); + va_end(__va); + return __res; +} + +locale_t newlocale(int mask, const char *locale, locale_t base) { + + if ((locale == NULL) || (locale[0] == '\0') || + ((locale[0] == 'C') && (locale[1] == '\0'))) + { + return __C_locale; + } + + // Solaris locales are shared libraries that contain + char *path; +#ifdef __LP64 + asprintf(&path, "/usr/lib/locale/%1$s/amd64/%1$s.so.3", locale); +#else + asprintf(&path, "/usr/lib/locale/%1$s/%1$s.so.3", locale); +#endif + void *handle = dlopen(path, RTLD_LOCAL | RTLD_NOW); + free(path); + if (!handle) + return 0; + _LC_locale_t *(*init)() = dlsym(handle, "instantiate"); + if (!init) + return 0; + _LC_locale_t *p = init(); + if (!p) + return 0; + + if (!base) + base = __C_locale; + + locale_t ret = calloc(1, sizeof(struct _LC_locale_t)); + memcpy(ret, p, sizeof (_LC_locale_t)); + ret->lc_collate = (mask & LC_COLLATE_MASK) ? p->lc_collate : base->lc_collate; + ret->lc_ctype = (mask & LC_CTYPE_MASK) ? p->lc_ctype : base->lc_ctype; + ret->lc_messages = (mask & LC_MESSAGES_MASK) ? p->lc_messages : base->lc_messages; + ret->lc_monetary = (mask & LC_MONETARY_MASK) ? p->lc_monetary : base->lc_monetary; + ret->lc_time = (mask & LC_TIME_MASK) ? p->lc_time : base->lc_time; + return ret; +} + +void freelocale(locale_t loc) +{ + if (loc != __C_locale) + free(loc); +} + +__attribute__((constructor)) +static void setupCLocale(void) { + // The default initial locale is the C locale. This is a statically + // allocated locale inside libc. At program start, __lc_locale will point to + // this. We need to grab a copy because it's not a public symbol. If we had + // access to the source code for libc, then we'd just use it directly... + assert('C' == setlocale(LC_ALL, 0)[0]); + __C_locale = __lc_locale; +} +#endif diff --git a/system/lib/libcxx/support/win32/locale_win32.cpp b/system/lib/libcxx/support/win32/locale_win32.cpp new file mode 100644 index 00000000..02b5874e --- /dev/null +++ b/system/lib/libcxx/support/win32/locale_win32.cpp @@ -0,0 +1,94 @@ +// -*- C++ -*- +//===-------------------- support/win32/locale_win32.cpp ------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "support/win32/locale_win32.h" + +#include <stdarg.h> // va_start, va_end + +// FIXME: base currently unused. Needs manual work to construct the new locale +locale_t newlocale( int mask, const char * locale, locale_t /*base*/ ) +{ + return _create_locale( mask, locale ); +} +locale_t uselocale( locale_t newloc ) +{ + locale_t old_locale = _get_current_locale(); + // uselocale sets the thread's locale by definition, so unconditionally use thread-local locale + _configthreadlocale( _ENABLE_PER_THREAD_LOCALE ); + // uselocale sets all categories + setlocale( LC_ALL, newloc->locinfo->lc_category[LC_ALL].locale ); + // uselocale returns the old locale_t + return old_locale; +} +lconv *localeconv_l( locale_t loc ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return localeconv(); +} +size_t mbrlen_l( const char *__restrict__ s, size_t n, + mbstate_t *__restrict__ ps, locale_t loc ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return mbrlen( s, n, ps ); +} +size_t mbsrtowcs_l( wchar_t *__restrict__ dst, const char **__restrict__ src, + size_t len, mbstate_t *__restrict__ ps, locale_t loc ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return mbsrtowcs( dst, src, len, ps ); +} +size_t wcrtomb_l( char *__restrict__ s, wchar_t wc, mbstate_t *__restrict__ ps, + locale_t loc ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return wcrtomb( s, wc, ps ); +} +size_t mbrtowc_l( wchar_t *__restrict__ pwc, const char *__restrict__ s, + size_t n, mbstate_t *__restrict__ ps, locale_t loc ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return mbrtowc( pwc, s, n, ps ); +} +size_t mbsnrtowcs_l( wchar_t *__restrict__ dst, const char **__restrict__ src, + size_t nms, size_t len, mbstate_t *__restrict__ ps, locale_t loc ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return mbsnrtowcs( dst, src, nms, len, ps ); +} +size_t wcsnrtombs_l( char *__restrict__ dst, const wchar_t **__restrict__ src, + size_t nwc, size_t len, mbstate_t *__restrict__ ps, locale_t loc ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return wcsnrtombs( dst, src, nwc, len, ps ); +} +wint_t btowc_l( int c, locale_t loc ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return btowc( c ); +} +int wctob_l( wint_t c, locale_t loc ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return wctob( c ); +} + +int asprintf_l( char **ret, locale_t loc, const char *format, ... ) +{ + va_list ap; + va_start( ap, format ); + int result = vasprintf_l( ret, loc, format, ap ); + va_end(ap); + return result; +} +int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap ) +{ + __locale_raii __current( uselocale(loc), uselocale ); + return vasprintf( ret, format, ap ); +} diff --git a/system/lib/libcxx/support/win32/support.cpp b/system/lib/libcxx/support/win32/support.cpp new file mode 100644 index 00000000..9e85077a --- /dev/null +++ b/system/lib/libcxx/support/win32/support.cpp @@ -0,0 +1,70 @@ +// -*- C++ -*- +//===----------------------- support/win32/support.h ----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include <support/win32/support.h> +#include <stdarg.h> // va_start, va_end +#include <stddef.h> // size_t +#include <stdlib.h> // malloc +#include <stdio.h> // vsprintf, vsnprintf +#include <string.h> // strcpy, wcsncpy + +int asprintf(char **sptr, const char *__restrict fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + int result = vasprintf(sptr, fmt, ap); + va_end(ap); + return result; +} +int vasprintf( char **sptr, const char *__restrict fmt, va_list ap ) +{ + *sptr = NULL; + int count = vsnprintf( *sptr, 0, fmt, ap ); + if( (count >= 0) && ((*sptr = (char*)malloc(count+1)) != NULL) ) + { + vsprintf( *sptr, fmt, ap ); + sptr[count] = '\0'; + } + + return count; +} + +// FIXME: use wcrtomb and avoid copy +// use mbsrtowcs which is available, first copy first nwc elements of src +size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src, + size_t nmc, size_t len, mbstate_t *__restrict ps ) +{ + char* local_src = new char[nmc+1]; + char* nmcsrc = local_src; + strncpy( nmcsrc, *src, nmc ); + nmcsrc[nmc] = '\0'; + const size_t result = mbsrtowcs( dst, const_cast<const char **>(&nmcsrc), len, ps ); + // propagate error + if( nmcsrc == NULL ) + *src = NULL; + delete[] local_src; + return result; +} +// FIXME: use wcrtomb and avoid copy +// use wcsrtombs which is available, first copy first nwc elements of src +size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src, + size_t nwc, size_t len, mbstate_t *__restrict ps ) +{ + wchar_t* local_src = new wchar_t[nwc]; + wchar_t* nwcsrc = local_src; + wcsncpy(nwcsrc, *src, nwc); + nwcsrc[nwc] = '\0'; + const size_t result = wcsrtombs( dst, const_cast<const wchar_t **>(&nwcsrc), len, ps ); + // propogate error + if( nwcsrc == NULL ) + *src = NULL; + delete[] nwcsrc; + return result; +} diff --git a/system/lib/libcxx/thread.cpp b/system/lib/libcxx/thread.cpp index b07f8f85..447eca7b 100644 --- a/system/lib/libcxx/thread.cpp +++ b/system/lib/libcxx/thread.cpp @@ -11,10 +11,15 @@ #include "exception" #include "vector" #include "future" +#include "limits" #include <sys/types.h> #if !_WIN32 +#if !__sun__ && !__linux__ #include <sys/sysctl.h> -#endif // _WIN32 +#else +#include <unistd.h> +#endif // !__sun__ && !__linux__ +#endif // !_WIN32 _LIBCPP_BEGIN_NAMESPACE_STD @@ -52,14 +57,23 @@ thread::detach() } unsigned -thread::hardware_concurrency() +thread::hardware_concurrency() _NOEXCEPT { #if defined(CTL_HW) && defined(HW_NCPU) - int n; + unsigned n; int mib[2] = {CTL_HW, HW_NCPU}; std::size_t s = sizeof(n); sysctl(mib, 2, &n, &s, 0, 0); return n; +#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && defined(_SC_NPROCESSORS_ONLN) + long result = sysconf(_SC_NPROCESSORS_ONLN); + // sysconf returns -1 if the name is invalid, the option does not exist or + // does not have a definite limit. + // if sysconf returns some other negative number, we have no idea + // what is going on. Default to something safe. + if (result < 0) + return 0; + return static_cast<unsigned>(result); #else // defined(CTL_HW) && defined(HW_NCPU) // TODO: grovel through /proc or check cpuid on x86 and similar // instructions on other architectures. @@ -74,11 +88,22 @@ void sleep_for(const chrono::nanoseconds& ns) { using namespace chrono; - if (ns >= nanoseconds::zero()) + if (ns > nanoseconds::zero()) { + seconds s = duration_cast<seconds>(ns); timespec ts; - ts.tv_sec = static_cast<decltype(ts.tv_sec)>(duration_cast<seconds>(ns).count()); - ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns - seconds(ts.tv_sec)).count()); + typedef decltype(ts.tv_sec) ts_sec; + _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max(); + if (s.count() < ts_sec_max) + { + ts.tv_sec = static_cast<ts_sec>(s.count()); + ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns-s).count()); + } + else + { + ts.tv_sec = ts_sec_max; + ts.tv_nsec = giga::num - 1; + } nanosleep(&ts, 0); } } diff --git a/system/lib/libcxx/typeinfo.cpp b/system/lib/libcxx/typeinfo.cpp index 9ca03a18..6bab0771 100644 --- a/system/lib/libcxx/typeinfo.cpp +++ b/system/lib/libcxx/typeinfo.cpp @@ -7,12 +7,21 @@ // //===----------------------------------------------------------------------===// #include <stdlib.h> + +#ifndef __has_include +#define __has_include(inc) 0 +#endif + #if __APPLE__ #include <cxxabi.h> +#elif defined(LIBCXXRT) || __has_include(<cxxabi.h>) +#include <cxxabi.h> #endif #include "typeinfo" +#if !(defined(_LIBCPPABI_VERSION) || defined(LIBCXXRT)) + std::bad_cast::bad_cast() _NOEXCEPT { } @@ -48,3 +57,4 @@ std::bad_typeid::what() const _NOEXCEPT void __cxxabiv1::__cxa_bad_cast() { throw std::bad_cast(); } #endif +#endif // _LIBCPPABI_VERSION diff --git a/system/lib/libcxx/utility.cpp b/system/lib/libcxx/utility.cpp index 7dccffb7..e9830e7c 100644 --- a/system/lib/libcxx/utility.cpp +++ b/system/lib/libcxx/utility.cpp @@ -7,6 +7,7 @@ // //===----------------------------------------------------------------------===// +#define _LIBCPP_BUILDING_UTILITY #include "utility" _LIBCPP_BEGIN_NAMESPACE_STD |