diff options
author | 2018-09-11 18:18:58 +0000 | |
---|---|---|
committer | 2018-09-11 18:18:58 +0000 | |
commit | 820e1f31efc1d6ed04795ba2e79f3044e1907492 (patch) | |
tree | 815cebb3734784074b661935c33f00bd5eb4d862 /lib/libcxx/src | |
parent | Nuke unused LIST() ieee80211com_head. (diff) | |
download | wireguard-openbsd-820e1f31efc1d6ed04795ba2e79f3044e1907492.tar.xz wireguard-openbsd-820e1f31efc1d6ed04795ba2e79f3044e1907492.zip |
import of libc++ 6.0.0
Diffstat (limited to 'lib/libcxx/src')
43 files changed, 2711 insertions, 1185 deletions
diff --git a/lib/libcxx/src/algorithm.cpp b/lib/libcxx/src/algorithm.cpp index e9752b0653e..f036eb7abe1 100644 --- a/lib/libcxx/src/algorithm.cpp +++ b/lib/libcxx/src/algorithm.cpp @@ -48,7 +48,7 @@ template bool __insertion_sort_incomplete<__less<long double>&, long double*>(lo template unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&); #ifndef _LIBCPP_HAS_NO_THREADS -static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER; +_LIBCPP_SAFE_STATIC static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER; #endif unsigned __rs_default::__c_ = 0; diff --git a/lib/libcxx/src/any.cpp b/lib/libcxx/src/any.cpp index f7768457890..45b2337eb35 100644 --- a/lib/libcxx/src/any.cpp +++ b/lib/libcxx/src/any.cpp @@ -7,12 +7,17 @@ // //===----------------------------------------------------------------------===// +#include "any" #include "experimental/any" -_LIBCPP_BEGIN_NAMESPACE_LFTS - +namespace std { const char* bad_any_cast::what() const _NOEXCEPT { return "bad any cast"; } +} +_LIBCPP_BEGIN_NAMESPACE_LFTS +const char* bad_any_cast::what() const _NOEXCEPT { + return "bad any cast"; +} _LIBCPP_END_NAMESPACE_LFTS diff --git a/lib/libcxx/src/chrono.cpp b/lib/libcxx/src/chrono.cpp index 62149fbf420..d0e184ad32f 100644 --- a/lib/libcxx/src/chrono.cpp +++ b/lib/libcxx/src/chrono.cpp @@ -12,14 +12,45 @@ #include "system_error" // __throw_system_error #include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME -#if !defined(CLOCK_REALTIME) -#include <sys/time.h> // for gettimeofday and timeval +#if (__APPLE__) +#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) +#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200 +#define _LIBCXX_USE_CLOCK_GETTIME +#endif +#elif defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) +#if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 100000 +#define _LIBCXX_USE_CLOCK_GETTIME +#endif +#elif defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) +#if __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 100000 +#define _LIBCXX_USE_CLOCK_GETTIME +#endif +#elif defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) +#if __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 30000 +#define _LIBCXX_USE_CLOCK_GETTIME +#endif +#endif // __ENVIRONMENT_.*_VERSION_MIN_REQUIRED__ +#else +#define _LIBCXX_USE_CLOCK_GETTIME +#endif // __APPLE__ + +#if defined(_LIBCPP_WIN32API) +#define WIN32_LEAN_AND_MEAN +#define VC_EXTRA_LEAN +#include <windows.h> +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 +#include <winapifamily.h> #endif +#else +#if !defined(CLOCK_REALTIME) || !defined(_LIBCXX_USE_CLOCK_GETTIME) +#include <sys/time.h> // for gettimeofday and timeval +#endif // !defined(CLOCK_REALTIME) +#endif // defined(_LIBCPP_WIN32API) -#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK) && !defined(CLOCK_MONOTONIC) +#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK) #if __APPLE__ #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t -#else +#elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC) #error "Monotonic clock not implemented" #endif #endif @@ -36,16 +67,42 @@ const bool system_clock::is_steady; system_clock::time_point system_clock::now() _NOEXCEPT { -#ifdef CLOCK_REALTIME +#if defined(_LIBCPP_WIN32API) + // FILETIME is in 100ns units + using filetime_duration = + _VSTD::chrono::duration<__int64, + _VSTD::ratio_multiply<_VSTD::ratio<100, 1>, + nanoseconds::period>>; + + // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970. + static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600}; + + FILETIME ft; +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) + GetSystemTimePreciseAsFileTime(&ft); +#else + GetSystemTimeAsFileTime(&ft); +#endif +#else + GetSystemTimeAsFileTime(&ft); +#endif + + filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | + static_cast<__int64>(ft.dwLowDateTime)}; + return time_point(duration_cast<duration>(d - nt_to_unix_epoch)); +#else +#if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME) struct timespec tp; if (0 != clock_gettime(CLOCK_REALTIME, &tp)) __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000)); -#else // !CLOCK_REALTIME +#else timeval tv; gettimeofday(&tv, 0); return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); -#endif // CLOCK_REALTIME +#endif // _LIBCXX_USE_CLOCK_GETTIME && CLOCK_REALTIME +#endif } time_t @@ -69,19 +126,20 @@ system_clock::from_time_t(time_t t) _NOEXCEPT const bool steady_clock::is_steady; -#ifdef CLOCK_MONOTONIC +#if defined(__APPLE__) +// Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW +#if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW) steady_clock::time_point steady_clock::now() _NOEXCEPT { struct timespec tp; - if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) - __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); + if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp)) + __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed"); return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); } -#elif defined(__APPLE__) - +#else // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom // are run time constants supplied by the OS. This clock has no relationship @@ -90,8 +148,6 @@ steady_clock::now() _NOEXCEPT // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize // for that case as an optimization. -#pragma GCC visibility push(hidden) - static steady_clock::rep steady_simplified() @@ -129,14 +185,46 @@ init_steady_clock() return &steady_full; } -#pragma GCC visibility pop - steady_clock::time_point steady_clock::now() _NOEXCEPT { static FP fp = init_steady_clock(); return time_point(duration(fp())); } +#endif // defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW) + +#elif defined(_LIBCPP_WIN32API) + +steady_clock::time_point +steady_clock::now() _NOEXCEPT +{ + static LARGE_INTEGER freq; + static BOOL initialized = FALSE; + if (!initialized) + initialized = QueryPerformanceFrequency(&freq); // always succceeds + + LARGE_INTEGER counter; + QueryPerformanceCounter(&counter); + return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart)); +} + +#elif defined(CLOCK_MONOTONIC) + +// On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to +// time functions in the nanosecond range. Thus, they are the only acceptable +// implementations of steady_clock. +#ifdef __APPLE__ +#error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms" +#endif + +steady_clock::time_point +steady_clock::now() _NOEXCEPT +{ + struct timespec tp; + if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) + __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); + return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); +} #else #error "Monotonic clock not implemented" diff --git a/lib/libcxx/src/condition_variable.cpp b/lib/libcxx/src/condition_variable.cpp index bfb4bf3925f..2200aefb804 100644 --- a/lib/libcxx/src/condition_variable.cpp +++ b/lib/libcxx/src/condition_variable.cpp @@ -14,7 +14,7 @@ #include "condition_variable" #include "thread" #include "system_error" -#include "cassert" +#include "__undef_macros" _LIBCPP_BEGIN_NAMESPACE_STD @@ -79,6 +79,12 @@ condition_variable::__do_timed_wait(unique_lock<mutex>& lk, void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk) { + auto& tl_ptr = __thread_local_data(); + // If this thread was not created using std::thread then it will not have + // previously allocated. + if (tl_ptr.get() == nullptr) { + tl_ptr.set_pointer(new __thread_struct); + } __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release()); } diff --git a/lib/libcxx/src/debug.cpp b/lib/libcxx/src/debug.cpp index b1a16e6e72d..f2fc1ceb495 100644 --- a/lib/libcxx/src/debug.cpp +++ b/lib/libcxx/src/debug.cpp @@ -7,16 +7,79 @@ // //===----------------------------------------------------------------------===// -#define _LIBCPP_DEBUG 1 #include "__config" #include "__debug" #include "functional" #include "algorithm" +#include "string" +#include "cstdio" #include "__hash_table" #include "mutex" _LIBCPP_BEGIN_NAMESPACE_STD +static std::string make_what_str(__libcpp_debug_info const& info) { + string msg = info.__file_; + msg += ":" + to_string(info.__line_) + ": _LIBCPP_ASSERT '"; + msg += info.__pred_; + msg += "' failed. "; + msg += info.__msg_; + return msg; +} + +_LIBCPP_SAFE_STATIC __libcpp_debug_function_type + __libcpp_debug_function = __libcpp_abort_debug_function; + +bool __libcpp_set_debug_function(__libcpp_debug_function_type __func) { + __libcpp_debug_function = __func; + return true; +} + +_LIBCPP_NORETURN void __libcpp_abort_debug_function(__libcpp_debug_info const& info) { + std::fprintf(stderr, "%s\n", make_what_str(info).c_str()); + std::abort(); +} + +_LIBCPP_NORETURN void __libcpp_throw_debug_function(__libcpp_debug_info const& info) { +#ifndef _LIBCPP_NO_EXCEPTIONS + throw __libcpp_debug_exception(info); +#else + __libcpp_abort_debug_function(info); +#endif +} + +struct __libcpp_debug_exception::__libcpp_debug_exception_imp { + __libcpp_debug_info __info_; + std::string __what_str_; +}; + +__libcpp_debug_exception::__libcpp_debug_exception() _NOEXCEPT + : __imp_(nullptr) { +} + +__libcpp_debug_exception::__libcpp_debug_exception( + __libcpp_debug_info const& info) : __imp_(new __libcpp_debug_exception_imp) +{ + __imp_->__info_ = info; + __imp_->__what_str_ = make_what_str(info); +} +__libcpp_debug_exception::__libcpp_debug_exception( + __libcpp_debug_exception const& other) : __imp_(nullptr) { + if (other.__imp_) + __imp_ = new __libcpp_debug_exception_imp(*other.__imp_); +} + +__libcpp_debug_exception::~__libcpp_debug_exception() _NOEXCEPT { + if (__imp_) + delete __imp_; +} + +const char* __libcpp_debug_exception::what() const _NOEXCEPT { + if (__imp_) + return __imp_->__what_str_.c_str(); + return "__libcpp_debug_exception"; +} + _LIBCPP_FUNC_VIS __libcpp_db* __get_db() @@ -152,11 +215,8 @@ __libcpp_db::__insert_c(void* __c) size_t nc = __next_prime(2*static_cast<size_t>(__cend_ - __cbeg_) + 1); __c_node** cbeg = static_cast<__c_node**>(calloc(nc, sizeof(void*))); if (cbeg == nullptr) -#ifndef _LIBCPP_NO_EXCEPTIONS - throw bad_alloc(); -#else - abort(); -#endif + __throw_bad_alloc(); + for (__c_node** p = __cbeg_; p != __cend_; ++p) { __c_node* q = *p; @@ -178,11 +238,8 @@ __libcpp_db::__insert_c(void* __c) __c_node* r = __cbeg_[hc] = static_cast<__c_node*>(malloc(sizeof(__c_node))); if (__cbeg_[hc] == nullptr) -#ifndef _LIBCPP_NO_EXCEPTIONS - throw bad_alloc(); -#else - abort(); -#endif + __throw_bad_alloc(); + r->__c_ = __c; r->__next_ = p; ++__csz_; @@ -475,11 +532,8 @@ __c_node::__add(__i_node* i) __i_node** beg = static_cast<__i_node**>(malloc(nc * sizeof(__i_node*))); if (beg == nullptr) -#ifndef _LIBCPP_NO_EXCEPTIONS - throw bad_alloc(); -#else - abort(); -#endif + __throw_bad_alloc(); + if (nc > 1) memcpy(beg, beg_, nc/2*sizeof(__i_node*)); free(beg_); @@ -501,11 +555,8 @@ __libcpp_db::__insert_iterator(void* __i) size_t nc = __next_prime(2*static_cast<size_t>(__iend_ - __ibeg_) + 1); __i_node** ibeg = static_cast<__i_node**>(calloc(nc, sizeof(void*))); if (ibeg == nullptr) -#ifndef _LIBCPP_NO_EXCEPTIONS - throw bad_alloc(); -#else - abort(); -#endif + __throw_bad_alloc(); + for (__i_node** p = __ibeg_; p != __iend_; ++p) { __i_node* q = *p; @@ -527,11 +578,8 @@ __libcpp_db::__insert_iterator(void* __i) __i_node* r = __ibeg_[hi] = static_cast<__i_node*>(malloc(sizeof(__i_node))); if (r == nullptr) -#ifndef _LIBCPP_NO_EXCEPTIONS - throw bad_alloc(); -#else - abort(); -#endif + __throw_bad_alloc(); + ::new(r) __i_node(__i, p, nullptr); ++__isz_; return r; diff --git a/lib/libcxx/src/exception.cpp b/lib/libcxx/src/exception.cpp index e172f642d48..3d2dcfd5b10 100644 --- a/lib/libcxx/src/exception.cpp +++ b/lib/libcxx/src/exception.cpp @@ -6,304 +6,32 @@ // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// -#include <stdlib.h> -#include <stdio.h> #include "exception" #include "new" +#include "typeinfo" -#if defined(__APPLE__) && !defined(LIBCXXRT) +#if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI) || \ + (defined(__APPLE__) && !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)) #include <cxxabi.h> - using namespace __cxxabiv1; #define HAVE_DEPENDENT_EH_ABI 1 - #ifndef _LIBCPPABI_VERSION - using namespace __cxxabiapple; - // On Darwin, there are two STL shared libraries and a lower level ABI - // shared library. 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) || defined(LIBCXX_BUILDING_LIBCXXABI) - #include <cxxabi.h> - using namespace __cxxabiv1; - #if defined(LIBCXXRT) || defined(_LIBCPPABI_VERSION) - #define HAVE_DEPENDENT_EH_ABI 1 - #endif -#elif !defined(__GLIBCXX__) // defined(LIBCXX_BUILDING_LIBCXXABI) - static std::terminate_handler __terminate_handler; - static std::unexpected_handler __unexpected_handler; -#endif // defined(LIBCXX_BUILDING_LIBCXXABI) - -namespace std -{ - -#if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) - -// libcxxrt provides implementations of these functions itself. -unexpected_handler -set_unexpected(unexpected_handler func) _NOEXCEPT -{ - return __sync_lock_test_and_set(&__unexpected_handler, func); -} - -unexpected_handler -get_unexpected() _NOEXCEPT -{ - return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); -} - -_LIBCPP_NORETURN -void -unexpected() -{ - (*get_unexpected())(); - // unexpected handler should not return - terminate(); -} - -terminate_handler -set_terminate(terminate_handler func) _NOEXCEPT -{ - return __sync_lock_test_and_set(&__terminate_handler, func); -} - -terminate_handler -get_terminate() _NOEXCEPT -{ - return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); -} - -#ifndef __EMSCRIPTEN__ // We provide this in JS -_LIBCPP_NORETURN -void -terminate() _NOEXCEPT -{ -#ifndef _LIBCPP_NO_EXCEPTIONS - try - { -#endif // _LIBCPP_NO_EXCEPTIONS - (*get_terminate())(); - // handler should not return - fprintf(stderr, "terminate_handler unexpectedly returned\n"); - ::abort(); -#ifndef _LIBCPP_NO_EXCEPTIONS - } - catch (...) - { - // handler should not throw exception - fprintf(stderr, "terminate_handler unexpectedly threw an exception\n"); - ::abort(); - } -#endif // _LIBCPP_NO_EXCEPTIONS -} -#endif // !__EMSCRIPTEN__ -#endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) - -#if !defined(LIBCXXRT) && !defined(__GLIBCXX__) && !defined(__EMSCRIPTEN__) -bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } - -int uncaught_exceptions() _NOEXCEPT -{ -#if defined(__APPLE__) || defined(_LIBCPPABI_VERSION) - // on Darwin, there is a helper function so __cxa_get_globals is private -# if _LIBCPPABI_VERSION > 1101 - return __cxa_uncaught_exceptions(); -# else - return __cxa_uncaught_exception() ? 1 : 0; -# endif -#else // __APPLE__ -# if defined(_MSC_VER) && ! defined(__clang__) - _LIBCPP_WARNING("uncaught_exceptions not yet implemented") -# else -# warning uncaught_exception not yet implemented -# endif - fprintf(stderr, "uncaught_exceptions not yet implemented\n"); - ::abort(); -#endif // __APPLE__ -} - - -#ifndef _LIBCPPABI_VERSION - -exception::~exception() _NOEXCEPT -{ -} - -const char* exception::what() const _NOEXCEPT -{ - return "std::exception"; -} - -#endif // _LIBCPPABI_VERSION -#endif //LIBCXXRT -#if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) - -bad_exception::~bad_exception() _NOEXCEPT -{ -} - -const char* bad_exception::what() const _NOEXCEPT -{ - return "std::bad_exception"; -} - #endif -#if defined(__GLIBCXX__) - -// libsupc++ does not implement the dependent EH ABI and the functionality -// it uses to implement std::exception_ptr (which it declares as an alias of -// std::__exception_ptr::exception_ptr) is not directly exported to clients. So -// we have little choice but to hijack std::__exception_ptr::exception_ptr's -// (which fortunately has the same layout as our std::exception_ptr) copy -// constructor, assignment operator and destructor (which are part of its -// stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr) -// function. - -namespace __exception_ptr -{ - -struct exception_ptr -{ - void* __ptr_; - - exception_ptr(const exception_ptr&) _NOEXCEPT; - exception_ptr& operator=(const exception_ptr&) _NOEXCEPT; - ~exception_ptr() _NOEXCEPT; -}; - -} - -_LIBCPP_NORETURN void rethrow_exception(__exception_ptr::exception_ptr); - -#endif - -exception_ptr::~exception_ptr() _NOEXCEPT -{ -#if HAVE_DEPENDENT_EH_ABI - __cxa_decrement_exception_refcount(__ptr_); -#elif defined(__GLIBCXX__) - reinterpret_cast<__exception_ptr::exception_ptr*>(this)->~exception_ptr(); -#else -# if defined(_MSC_VER) && ! defined(__clang__) - _LIBCPP_WARNING("exception_ptr not yet implemented") -# else -# warning exception_ptr not yet implemented -# endif - fprintf(stderr, "exception_ptr not yet implemented\n"); - ::abort(); -#endif -} - -exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT - : __ptr_(other.__ptr_) -{ -#if HAVE_DEPENDENT_EH_ABI - __cxa_increment_exception_refcount(__ptr_); -#elif defined(__GLIBCXX__) - new (reinterpret_cast<void*>(this)) __exception_ptr::exception_ptr( - reinterpret_cast<const __exception_ptr::exception_ptr&>(other)); -#else -# if defined(_MSC_VER) && ! defined(__clang__) - _LIBCPP_WARNING("exception_ptr not yet implemented") -# else -# warning exception_ptr not yet implemented -# endif - fprintf(stderr, "exception_ptr not yet implemented\n"); - ::abort(); -#endif -} - -exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT -{ -#if HAVE_DEPENDENT_EH_ABI - if (__ptr_ != other.__ptr_) - { - __cxa_increment_exception_refcount(other.__ptr_); - __cxa_decrement_exception_refcount(__ptr_); - __ptr_ = other.__ptr_; - } - return *this; -#elif defined(__GLIBCXX__) - *reinterpret_cast<__exception_ptr::exception_ptr*>(this) = - reinterpret_cast<const __exception_ptr::exception_ptr&>(other); - return *this; -#else -# if defined(_MSC_VER) && ! defined(__clang__) - _LIBCPP_WARNING("exception_ptr not yet implemented") -# else -# warning exception_ptr not yet implemented -# endif - fprintf(stderr, "exception_ptr not yet implemented\n"); - ::abort(); -#endif -} - -nested_exception::nested_exception() _NOEXCEPT - : __ptr_(current_exception()) -{ -} - -#if !defined(__GLIBCXX__) - -nested_exception::~nested_exception() _NOEXCEPT -{ -} - -#endif - -_LIBCPP_NORETURN -void -nested_exception::rethrow_nested() const -{ - if (__ptr_ == nullptr) - terminate(); - rethrow_exception(__ptr_); -} - -#if !defined(__GLIBCXX__) - -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()); - exception_ptr ptr; - ptr.__ptr_ = __cxa_current_primary_exception(); - return ptr; -#else -# if defined(_MSC_VER) && ! defined(__clang__) - _LIBCPP_WARNING( "exception_ptr not yet implemented" ) -# else -# warning exception_ptr not yet implemented -# endif - fprintf(stderr, "exception_ptr not yet implemented\n"); - ::abort(); -#endif -} - -#endif // !__GLIBCXX__ - -_LIBCPP_NORETURN -void rethrow_exception(exception_ptr p) -{ -#if HAVE_DEPENDENT_EH_ABI - __cxa_rethrow_primary_exception(p.__ptr_); - // if p.__ptr_ is NULL, above returns so we terminate - terminate(); +#if defined(_LIBCPP_ABI_MICROSOFT) +#include "support/runtime/exception_msvc.ipp" +#include "support/runtime/exception_pointer_msvc.ipp" +#elif defined(_LIBCPPABI_VERSION) +#include "support/runtime/exception_libcxxabi.ipp" +#include "support/runtime/exception_pointer_cxxabi.ipp" +#elif defined(LIBCXXRT) +#include "support/runtime/exception_libcxxrt.ipp" +#include "support/runtime/exception_pointer_cxxabi.ipp" #elif defined(__GLIBCXX__) - rethrow_exception(reinterpret_cast<__exception_ptr::exception_ptr&>(p)); +#include "support/runtime/exception_glibcxx.ipp" +#include "support/runtime/exception_pointer_glibcxx.ipp" #else -# if defined(_MSC_VER) && ! defined(__clang__) - _LIBCPP_WARNING("exception_ptr not yet implemented") -# else -# warning exception_ptr not yet implemented -# endif - fprintf(stderr, "exception_ptr not yet implemented\n"); - ::abort(); +#include "include/atomic_support.h" +#include "support/runtime/exception_fallback.ipp" +#include "support/runtime/exception_pointer_unimplemented.ipp" #endif -} -} // std diff --git a/lib/libcxx/src/experimental/filesystem/directory_iterator.cpp b/lib/libcxx/src/experimental/filesystem/directory_iterator.cpp index fa217ba7a12..a552fdc4461 100644 --- a/lib/libcxx/src/experimental/filesystem/directory_iterator.cpp +++ b/lib/libcxx/src/experimental/filesystem/directory_iterator.cpp @@ -1,28 +1,32 @@ +//===------------------ directory_iterator.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 "experimental/filesystem" +#include "__config" +#if defined(_LIBCPP_WIN32API) +#define WIN32_LEAN_AND_MEAN +#include <Windows.h> +#else #include <dirent.h> +#endif #include <errno.h> _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM namespace { namespace detail { +#if !defined(_LIBCPP_WIN32API) inline error_code capture_errno() { _LIBCPP_ASSERT(errno, "Expected errno to be non-zero"); return error_code{errno, std::generic_category()}; } - -template <class ...Args> -inline bool capture_error_or_throw(std::error_code* user_ec, - const char* msg, Args&&... args) -{ - std::error_code my_ec = capture_errno(); - if (user_ec) { - *user_ec = my_ec; - return true; - } - __libcpp_throw(filesystem_error(msg, std::forward<Args>(args)..., my_ec)); - return false; -} +#endif template <class ...Args> inline bool set_or_throw(std::error_code& my_ec, @@ -33,29 +37,91 @@ inline bool set_or_throw(std::error_code& my_ec, *user_ec = my_ec; return true; } - __libcpp_throw(filesystem_error(msg, std::forward<Args>(args)..., my_ec)); + __throw_filesystem_error(msg, std::forward<Args>(args)..., my_ec); return false; } -typedef path::string_type string_type; - - -inline string_type posix_readdir(DIR *dir_stream, error_code& ec) { +#if !defined(_LIBCPP_WIN32API) +inline path::string_type posix_readdir(DIR *dir_stream, error_code& ec) { struct dirent* dir_entry_ptr = nullptr; errno = 0; // zero errno in order to detect errors + ec.clear(); if ((dir_entry_ptr = ::readdir(dir_stream)) == nullptr) { - ec = capture_errno(); + if (errno) + ec = capture_errno(); return {}; } else { - ec.clear(); return dir_entry_ptr->d_name; } } +#endif }} // namespace detail using detail::set_or_throw; +#if defined(_LIBCPP_WIN32API) +class __dir_stream { +public: + __dir_stream() = delete; + __dir_stream& operator=(const __dir_stream&) = delete; + + __dir_stream(__dir_stream&& __ds) noexcept + : __stream_(__ds.__stream_), __root_(std::move(__ds.__root_)), + __entry_(std::move(__ds.__entry_)) { + __ds.__stream_ = INVALID_HANDLE_VALUE; + } + + __dir_stream(const path& root, directory_options opts, error_code& ec) + : __stream_(INVALID_HANDLE_VALUE), __root_(root) { + __stream_ = ::FindFirstFile(root.c_str(), &__data_); + if (__stream_ == INVALID_HANDLE_VALUE) { + ec = error_code(::GetLastError(), std::generic_category()); + const bool ignore_permission_denied = + bool(opts & directory_options::skip_permission_denied); + if (ignore_permission_denied && ec.value() == ERROR_ACCESS_DENIED) + ec.clear(); + return; + } + } + + ~__dir_stream() noexcept { + if (__stream_ == INVALID_HANDLE_VALUE) + return; + close(); + } + + bool good() const noexcept { return __stream_ != INVALID_HANDLE_VALUE; } + + bool advance(error_code& ec) { + while (::FindNextFile(__stream_, &__data_)) { + if (!strcmp(__data_.cFileName, ".") || strcmp(__data_.cFileName, "..")) + continue; + __entry_.assign(__root_ / __data_.cFileName); + return true; + } + ec = error_code(::GetLastError(), std::generic_category()); + close(); + return false; + } + +private: + std::error_code close() noexcept { + std::error_code ec; + if (!::FindClose(__stream_)) + ec = error_code(::GetLastError(), std::generic_category()); + __stream_ = INVALID_HANDLE_VALUE; + return ec; + } + + HANDLE __stream_{INVALID_HANDLE_VALUE}; + WIN32_FIND_DATA __data_; + +public: + path __root_; + directory_entry __entry_; +}; +#else class __dir_stream { public: __dir_stream() = delete; @@ -117,6 +183,7 @@ public: path __root_; directory_entry __entry_; }; +#endif // directory_iterator @@ -149,7 +216,7 @@ directory_iterator& directory_iterator::__increment(error_code *ec) } -directory_entry const& directory_iterator::__deref() const { +directory_entry const& directory_iterator::__dereference() const { _LIBCPP_ASSERT(__imp_, "Attempting to dereference an invalid iterator"); return __imp_->__entry_; } @@ -195,7 +262,7 @@ int recursive_directory_iterator::depth() const { return __imp_->__stack_.size() - 1; } -const directory_entry& recursive_directory_iterator::__deref() const { +const directory_entry& recursive_directory_iterator::__dereference() const { return __imp_->__stack_.top().__entry_; } @@ -229,24 +296,43 @@ void recursive_directory_iterator::__advance(error_code* ec) { } bool recursive_directory_iterator::__try_recursion(error_code *ec) { - bool rec_sym = bool(options() & directory_options::follow_directory_symlink); + auto& curr_it = __imp_->__stack_.top(); - if (is_directory(curr_it.__entry_.status()) && - (!is_symlink(curr_it.__entry_.symlink_status()) || rec_sym)) - { - std::error_code m_ec; + bool skip_rec = false; + std::error_code m_ec; + if (!rec_sym) { + file_status st = curr_it.__entry_.symlink_status(m_ec); + if (m_ec && status_known(st)) + m_ec.clear(); + if (m_ec || is_symlink(st) || !is_directory(st)) + skip_rec = true; + } else { + file_status st = curr_it.__entry_.status(m_ec); + if (m_ec && status_known(st)) + m_ec.clear(); + if (m_ec || !is_directory(st)) + skip_rec = true; + } + + if (!skip_rec) { __dir_stream new_it(curr_it.__entry_.path(), __imp_->__options_, m_ec); if (new_it.good()) { __imp_->__stack_.push(_VSTD::move(new_it)); return true; } - if (m_ec) { - __imp_.reset(); - set_or_throw(m_ec, ec, - "recursive_directory_iterator::operator++()"); + } + if (m_ec) { + const bool allow_eacess = bool(__imp_->__options_ + & directory_options::skip_permission_denied); + if (m_ec.value() == EACCES && allow_eacess) { + if (ec) ec->clear(); + } else { + __imp_.reset(); + set_or_throw(m_ec, ec, + "recursive_directory_iterator::operator++()"); } } return false; diff --git a/lib/libcxx/src/experimental/filesystem/filesystem_time_helper.h b/lib/libcxx/src/experimental/filesystem/filesystem_time_helper.h new file mode 100644 index 00000000000..a60fdef5f0d --- /dev/null +++ b/lib/libcxx/src/experimental/filesystem/filesystem_time_helper.h @@ -0,0 +1,173 @@ +//===----------------------------------------------------------------------===//// +// +// 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. +// +//===----------------------------------------------------------------------===//// + +#ifndef FILESYSTEM_TIME_HELPER_H +#define FILESYSTEM_TIME_HELPER_H + +#include "experimental/__config" +#include "chrono" +#include "cstdlib" +#include "climits" + +#include <unistd.h> +#include <sys/stat.h> +#if !defined(UTIME_OMIT) +#include <sys/time.h> // for ::utimes as used in __last_write_time +#endif + +_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM + +namespace time_detail { namespace { + +using namespace chrono; + +template <class FileTimeT, + bool IsFloat = is_floating_point<typename FileTimeT::rep>::value> +struct fs_time_util_base { + static constexpr auto max_seconds = + duration_cast<seconds>(FileTimeT::duration::max()).count(); + + static constexpr auto max_nsec = + duration_cast<nanoseconds>(FileTimeT::duration::max() - + seconds(max_seconds)) + .count(); + + static constexpr auto min_seconds = + duration_cast<seconds>(FileTimeT::duration::min()).count(); + + static constexpr auto min_nsec_timespec = + duration_cast<nanoseconds>( + (FileTimeT::duration::min() - seconds(min_seconds)) + seconds(1)) + .count(); + + // Static assert that these values properly round trip. + static_assert((seconds(min_seconds) + + duration_cast<microseconds>(nanoseconds(min_nsec_timespec))) - + duration_cast<microseconds>(seconds(1)) == + FileTimeT::duration::min(), + ""); +}; + +template <class FileTimeT> +struct fs_time_util_base<FileTimeT, true> { + static const long long max_seconds; + static const long long max_nsec; + static const long long min_seconds; + static const long long min_nsec_timespec; +}; + +template <class FileTimeT> +const long long fs_time_util_base<FileTimeT, true>::max_seconds = + duration_cast<seconds>(FileTimeT::duration::max()).count(); + +template <class FileTimeT> +const long long fs_time_util_base<FileTimeT, true>::max_nsec = + duration_cast<nanoseconds>(FileTimeT::duration::max() - + seconds(max_seconds)) + .count(); + +template <class FileTimeT> +const long long fs_time_util_base<FileTimeT, true>::min_seconds = + duration_cast<seconds>(FileTimeT::duration::min()).count(); + +template <class FileTimeT> +const long long fs_time_util_base<FileTimeT, true>::min_nsec_timespec = + duration_cast<nanoseconds>((FileTimeT::duration::min() - + seconds(min_seconds)) + + seconds(1)) + .count(); + +template <class FileTimeT, class TimeT, class TimeSpecT> +struct fs_time_util : fs_time_util_base<FileTimeT> { + using Base = fs_time_util_base<FileTimeT>; + using Base::max_nsec; + using Base::max_seconds; + using Base::min_nsec_timespec; + using Base::min_seconds; + +public: + template <class CType, class ChronoType> + static bool checked_set(CType* out, ChronoType time) { + using Lim = numeric_limits<CType>; + if (time > Lim::max() || time < Lim::min()) + return false; + *out = static_cast<CType>(time); + return true; + } + + static _LIBCPP_CONSTEXPR_AFTER_CXX11 bool is_representable(TimeSpecT tm) { + if (tm.tv_sec >= 0) { + return (tm.tv_sec < max_seconds) || + (tm.tv_sec == max_seconds && tm.tv_nsec <= max_nsec); + } else if (tm.tv_sec == (min_seconds - 1)) { + return tm.tv_nsec >= min_nsec_timespec; + } else { + return (tm.tv_sec >= min_seconds); + } + } + + static _LIBCPP_CONSTEXPR_AFTER_CXX11 bool is_representable(FileTimeT tm) { + auto secs = duration_cast<seconds>(tm.time_since_epoch()); + auto nsecs = duration_cast<nanoseconds>(tm.time_since_epoch() - secs); + if (nsecs.count() < 0) { + secs = secs + seconds(1); + nsecs = nsecs + seconds(1); + } + using TLim = numeric_limits<TimeT>; + if (secs.count() >= 0) + return secs.count() <= TLim::max(); + return secs.count() >= TLim::min(); + } + + static _LIBCPP_CONSTEXPR_AFTER_CXX11 FileTimeT + convert_timespec(TimeSpecT tm) { + auto adj_msec = duration_cast<microseconds>(nanoseconds(tm.tv_nsec)); + if (tm.tv_sec >= 0) { + auto Dur = seconds(tm.tv_sec) + microseconds(adj_msec); + return FileTimeT(Dur); + } else if (duration_cast<microseconds>(nanoseconds(tm.tv_nsec)).count() == + 0) { + return FileTimeT(seconds(tm.tv_sec)); + } else { // tm.tv_sec < 0 + auto adj_subsec = + duration_cast<microseconds>(seconds(1) - nanoseconds(tm.tv_nsec)); + auto Dur = seconds(tm.tv_sec + 1) - adj_subsec; + return FileTimeT(Dur); + } + } + + template <class SubSecDurT, class SubSecT> + static bool set_times_checked(TimeT* sec_out, SubSecT* subsec_out, + FileTimeT tp) { + using namespace chrono; + auto dur = tp.time_since_epoch(); + auto sec_dur = duration_cast<seconds>(dur); + auto subsec_dur = duration_cast<SubSecDurT>(dur - sec_dur); + // The tv_nsec and tv_usec fields must not be negative so adjust accordingly + if (subsec_dur.count() < 0) { + if (sec_dur.count() > min_seconds) { + sec_dur -= seconds(1); + subsec_dur += seconds(1); + } else { + subsec_dur = SubSecDurT::zero(); + } + } + return checked_set(sec_out, sec_dur.count()) && + checked_set(subsec_out, subsec_dur.count()); + } +}; + +} // end namespace +} // end namespace time_detail + +using time_detail::fs_time_util; + +_LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM + +#endif // FILESYSTEM_TIME_HELPER_H diff --git a/lib/libcxx/src/experimental/filesystem/operations.cpp b/lib/libcxx/src/experimental/filesystem/operations.cpp index 369996fcbe6..662fa7b8626 100644 --- a/lib/libcxx/src/experimental/filesystem/operations.cpp +++ b/lib/libcxx/src/experimental/filesystem/operations.cpp @@ -15,6 +15,8 @@ #include "cstdlib" #include "climits" +#include "filesystem_time_helper.h" + #include <unistd.h> #include <sys/stat.h> #include <sys/statvfs.h> @@ -35,12 +37,9 @@ namespace detail { namespace { using value_type = path::value_type; using string_type = path::string_type; - - inline std::error_code capture_errno() { - _LIBCPP_ASSERT(errno, "Expected errno to be non-zero"); - std::error_code m_ec(errno, std::generic_category()); - return m_ec; + _LIBCPP_ASSERT(errno, "Expected errno to be non-zero"); + return std::error_code(errno, std::generic_category()); } void set_or_throw(std::error_code const& m_ec, std::error_code* ec, @@ -51,7 +50,7 @@ void set_or_throw(std::error_code const& m_ec, std::error_code* ec, } else { string msg_s("std::experimental::filesystem::"); msg_s += msg; - __libcpp_throw(filesystem_error(msg_s, p, p2, m_ec)); + __throw_filesystem_error(msg_s, p, p2, m_ec); } } @@ -183,20 +182,20 @@ void __copy(const path& from, const path& to, copy_options options, const bool sym_status2 = bool(options & copy_options::copy_symlinks); - std::error_code m_ec; + std::error_code m_ec1; struct ::stat f_st = {}; const file_status f = sym_status || sym_status2 - ? detail::posix_lstat(from, f_st, &m_ec) - : detail::posix_stat(from, f_st, &m_ec); - if (m_ec) - return set_or_throw(m_ec, ec, "copy", from, to); + ? detail::posix_lstat(from, f_st, &m_ec1) + : detail::posix_stat(from, f_st, &m_ec1); + if (m_ec1) + return set_or_throw(m_ec1, ec, "copy", from, to); struct ::stat t_st = {}; - const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec) - : detail::posix_stat(to, t_st, &m_ec); + const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1) + : detail::posix_stat(to, t_st, &m_ec1); if (not status_known(t)) - return set_or_throw(m_ec, ec, "copy", from, to); + return set_or_throw(m_ec1, ec, "copy", from, to); if (!exists(f) || is_other(f) || is_other(t) || (is_directory(f) && is_regular_file(t)) @@ -236,12 +235,11 @@ void __copy(const path& from, const path& to, copy_options options, } return; } - else if (is_directory(f)) { - if (not bool(copy_options::recursive & options) && - bool(copy_options::__in_recursive_copy & options)) - { - return; - } + else if (is_directory(f) && bool(copy_options::create_symlinks & options)) { + return set_or_throw(make_error_code(errc::is_a_directory), ec, "copy"); + } + else if (is_directory(f) && (bool(copy_options::recursive & options) || + copy_options::none == options)) { if (!exists(t)) { // create directory to with attributes from 'from'. @@ -251,9 +249,9 @@ void __copy(const path& from, const path& to, copy_options options, directory_iterator it = ec ? directory_iterator(from, *ec) : directory_iterator(from); if (ec && *ec) { return; } - std::error_code m_ec; - for (; it != directory_iterator(); it.increment(m_ec)) { - if (m_ec) return set_or_throw(m_ec, ec, "copy", from, to); + std::error_code m_ec2; + for (; it != directory_iterator(); it.increment(m_ec2)) { + if (m_ec2) return set_or_throw(m_ec2, ec, "copy", from, to); __copy(it->path(), to / it->path().filename(), options | copy_options::__in_recursive_copy, ec); if (ec && *ec) { return; } @@ -283,6 +281,10 @@ bool __copy_file(const path& from, const path& to, copy_options options, } const bool to_exists = exists(to_st); + if (to_exists && !is_regular_file(to_st)) { + set_or_throw(make_error_code(errc::not_supported), ec, "copy_file", from, to); + return false; + } if (to_exists && bool(copy_options::skip_existing & options)) { return false; } @@ -303,6 +305,8 @@ bool __copy_file(const path& from, const path& to, copy_options options, set_or_throw(make_error_code(errc::file_exists), ec, "copy", from, to); return false; } + + _LIBCPP_UNREACHABLE(); } void __copy_symlink(const path& existing_symlink, const path& new_symlink, @@ -424,17 +428,20 @@ void __current_path(const path& p, std::error_code *ec) { bool __equivalent(const path& p1, const path& p2, std::error_code *ec) { + auto make_unsupported_error = [&]() { + set_or_throw(make_error_code(errc::not_supported), ec, + "equivalent", p1, p2); + return false; + }; std::error_code ec1, ec2; struct ::stat st1 = {}; struct ::stat st2 = {}; auto s1 = detail::posix_stat(p1.native(), st1, &ec1); + if (!exists(s1)) + return make_unsupported_error(); auto s2 = detail::posix_stat(p2.native(), st2, &ec2); - - if ((!exists(s1) && !exists(s2)) || (is_other(s1) && is_other(s2))) { - set_or_throw(make_error_code(errc::not_supported), ec, - "equivalent", p1, p2); - return false; - } + if (!exists(s2)) + return make_unsupported_error(); if (ec) ec->clear(); return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); } @@ -476,55 +483,50 @@ bool __fs_is_empty(const path& p, std::error_code *ec) std::error_code m_ec; struct ::stat pst; auto st = detail::posix_stat(p, pst, &m_ec); - if (is_directory(st)) - return directory_iterator(p) == directory_iterator{}; + if (m_ec) { + set_or_throw(m_ec, ec, "is_empty", p); + return false; + } + else if (!is_directory(st) && !is_regular_file(st)) { + m_ec = make_error_code(errc::not_supported); + set_or_throw(m_ec, ec, "is_empty"); + return false; + } + else if (is_directory(st)) { + auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p); + if (ec && *ec) + return false; + return it == directory_iterator{}; + } else if (is_regular_file(st)) return static_cast<std::uintmax_t>(pst.st_size) == 0; - // else - set_or_throw(m_ec, ec, "is_empty", p); - return false; + + _LIBCPP_UNREACHABLE(); } namespace detail { namespace { -template <class CType, class ChronoType> -bool checked_set(CType* out, ChronoType time) { - using Lim = numeric_limits<CType>; - if (time > Lim::max() || time < Lim::min()) - return false; - *out = static_cast<CType>(time); - return true; -} - -constexpr long long min_seconds = file_time_type::duration::min().count() - / file_time_type::period::den; - -template <class SubSecDurT, class SubSecT> -bool set_times_checked(time_t* sec_out, SubSecT* subsec_out, file_time_type tp) { - using namespace chrono; - auto dur = tp.time_since_epoch(); - auto sec_dur = duration_cast<seconds>(dur); - auto subsec_dur = duration_cast<SubSecDurT>(dur - sec_dur); - // The tv_nsec and tv_usec fields must not be negative so adjust accordingly - if (subsec_dur.count() < 0) { - if (sec_dur.count() > min_seconds) { +using TimeSpec = struct timespec; +using StatT = struct stat; - sec_dur -= seconds(1); - subsec_dur += seconds(1); - } else { - subsec_dur = SubSecDurT::zero(); - } - } - return checked_set(sec_out, sec_dur.count()) - && checked_set(subsec_out, subsec_dur.count()); -} +#if defined(__APPLE__) +TimeSpec extract_mtime(StatT const& st) { return st.st_mtimespec; } +__attribute__((unused)) // Suppress warning +TimeSpec extract_atime(StatT const& st) { return st.st_atimespec; } +#else +TimeSpec extract_mtime(StatT const& st) { return st.st_mtim; } +__attribute__((unused)) // Suppress warning +TimeSpec extract_atime(StatT const& st) { return st.st_atim; } +#endif }} // end namespace detail +using FSTime = fs_time_util<file_time_type, time_t, struct timespec>; file_time_type __last_write_time(const path& p, std::error_code *ec) { + using namespace ::std::chrono; std::error_code m_ec; struct ::stat st; detail::posix_stat(p, st, &m_ec); @@ -533,7 +535,13 @@ file_time_type __last_write_time(const path& p, std::error_code *ec) return file_time_type::min(); } if (ec) ec->clear(); - return file_time_type::clock::from_time_t(st.st_mtime); + auto ts = detail::extract_mtime(st); + if (!FSTime::is_representable(ts)) { + set_or_throw(error_code(EOVERFLOW, generic_category()), ec, + "last_write_time", p); + return file_time_type::min(); + } + return FSTime::convert_timespec(ts); } void __last_write_time(const path& p, file_time_type new_time, @@ -554,10 +562,11 @@ void __last_write_time(const path& p, file_time_type new_time, set_or_throw(m_ec, ec, "last_write_time", p); return; } + auto atime = detail::extract_atime(st); struct ::timeval tbuf[2]; - tbuf[0].tv_sec = st.st_atime; - tbuf[0].tv_usec = 0; - const bool overflowed = !detail::set_times_checked<microseconds>( + tbuf[0].tv_sec = atime.tv_sec; + tbuf[0].tv_usec = duration_cast<microseconds>(nanoseconds(atime.tv_nsec)).count(); + const bool overflowed = !FSTime::set_times_checked<microseconds>( &tbuf[1].tv_sec, &tbuf[1].tv_usec, new_time); if (overflowed) { @@ -573,7 +582,7 @@ void __last_write_time(const path& p, file_time_type new_time, tbuf[0].tv_sec = 0; tbuf[0].tv_nsec = UTIME_OMIT; - const bool overflowed = !detail::set_times_checked<nanoseconds>( + const bool overflowed = !FSTime::set_times_checked<nanoseconds>( &tbuf[1].tv_sec, &tbuf[1].tv_nsec, new_time); if (overflowed) { set_or_throw(make_error_code(errc::invalid_argument), @@ -701,7 +710,7 @@ void __rename(const path& from, const path& to, std::error_code *ec) { } void __resize_file(const path& p, std::uintmax_t size, std::error_code *ec) { - if (::truncate(p.c_str(), static_cast<long>(size)) == -1) + if (::truncate(p.c_str(), static_cast<::off_t>(size)) == -1) set_or_throw(ec, "resize_file", p); else if (ec) ec->clear(); @@ -720,7 +729,7 @@ space_info __space(const path& p, std::error_code *ec) { // Multiply with overflow checking. auto do_mult = [&](std::uintmax_t& out, std::uintmax_t other) { out = other * m_svfs.f_frsize; - if (out / other != m_svfs.f_frsize || other == 0) + if (other == 0 || out / other != m_svfs.f_frsize) out = static_cast<std::uintmax_t>(-1); }; do_mult(si.capacity, m_svfs.f_blocks); @@ -742,23 +751,28 @@ path __system_complete(const path& p, std::error_code *ec) { return absolute(p, current_path()); } -path __temp_directory_path(std::error_code *ec) { - const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}; - const char* ret = nullptr; - for (auto & ep : env_paths) { - if ((ret = std::getenv(ep))) - break; - } - path p(ret ? ret : "/tmp"); - std::error_code m_ec; - if (is_directory(p, m_ec)) { - if (ec) ec->clear(); - return p; - } +path __temp_directory_path(std::error_code* ec) { + const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}; + const char* ret = nullptr; + + for (auto& ep : env_paths) + if ((ret = std::getenv(ep))) + break; + if (ret == nullptr) + ret = "/tmp"; + + path p(ret); + std::error_code m_ec; + if (!exists(p, m_ec) || !is_directory(p, m_ec)) { if (!m_ec || m_ec == make_error_code(errc::no_such_file_or_directory)) - m_ec = make_error_code(errc::not_a_directory); + m_ec = make_error_code(errc::not_a_directory); set_or_throw(m_ec, ec, "temp_directory_path"); return {}; + } + + if (ec) + ec->clear(); + return p; } // An absolute path is composed according to the table in [fs.op.absolute]. diff --git a/lib/libcxx/src/experimental/filesystem/path.cpp b/lib/libcxx/src/experimental/filesystem/path.cpp index 38c449832f6..dd4026cfe13 100644 --- a/lib/libcxx/src/experimental/filesystem/path.cpp +++ b/lib/libcxx/src/experimental/filesystem/path.cpp @@ -7,241 +7,284 @@ // //===----------------------------------------------------------------------===// #include "experimental/filesystem" -#include "experimental/string_view" +#include "string_view" #include "utility" -_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM - -_LIBCPP_CONSTEXPR path::value_type path::preferred_separator; - namespace { namespace parser { +using namespace std; +using namespace std::experimental::filesystem; + +using string_view_t = path::__string_view; +using string_view_pair = pair<string_view_t, string_view_t>; +using PosPtr = path::value_type const*; + +struct PathParser { + enum ParserState : unsigned char { + // Zero is a special sentinel value used by default constructed iterators. + PS_BeforeBegin = 1, + PS_InRootName, + PS_InRootDir, + PS_InFilenames, + PS_InTrailingSep, + PS_AtEnd + }; -using string_type = string_view; -using value_type = path::value_type; - -using string_view_pair = pair<string_view, string_view>; - -// status reporting -constexpr size_t npos = static_cast<size_t>(-1); - -inline bool good(size_t pos) { return pos != npos; } - -// lexical elements -constexpr value_type preferred_separator = path::preferred_separator; -constexpr value_type const * preferred_separator_str = "/"; -constexpr value_type const * dot = "."; - -// forward // -bool is_separator(string_type const &, size_t); -bool is_root_name(const string_type&, size_t); -bool is_root_directory(string_type const &, size_t); -bool is_trailing_separator(string_type const &, size_t); - -size_t start_of(string_type const &, size_t); -size_t end_of(string_type const &, size_t); - -size_t root_name_start(const string_type& s); -size_t root_name_end(const string_type&); - -size_t root_directory_start(string_type const &); -size_t root_directory_end(string_type const &); + const string_view_t Path; + string_view_t RawEntry; + ParserState State; -string_view_pair separate_filename(string_type const &); -string_view extract_raw(string_type const &, size_t); -string_view extract_preferred(string_type const &, size_t); +private: + PathParser(string_view_t P, ParserState State) noexcept + : Path(P), State(State) {} -inline bool is_separator(const string_type& s, size_t pos) { - return (pos < s.size() && s[pos] == preferred_separator); -} +public: + PathParser(string_view_t P, string_view_t E, unsigned char S) + : Path(P), RawEntry(E), State(static_cast<ParserState>(S)) { + // S cannot be '0' or PS_BeforeBegin. + } -inline bool is_root_name(const string_type& s, size_t pos) { - return good(pos) && pos == 0 ? root_name_start(s) == pos : false; -} + static PathParser CreateBegin(string_view_t P) noexcept { + PathParser PP(P, PS_BeforeBegin); + PP.increment(); + return PP; + } -inline bool is_root_directory(const string_type& s, size_t pos) { - return good(pos) ? root_directory_start(s) == pos : false; -} + static PathParser CreateEnd(string_view_t P) noexcept { + PathParser PP(P, PS_AtEnd); + return PP; + } -inline bool is_trailing_separator(const string_type& s, size_t pos) { - return (pos < s.size() && is_separator(s, pos) && - end_of(s, pos) == s.size()-1 && - !is_root_directory(s, pos) && !is_root_name(s, pos)); -} + PosPtr peek() const noexcept { + auto TkEnd = getNextTokenStartPos(); + auto End = getAfterBack(); + return TkEnd == End ? nullptr : TkEnd; + } -size_t start_of(const string_type& s, size_t pos) { - if (pos >= s.size()) return npos; - bool in_sep = (s[pos] == preferred_separator); - while (pos - 1 < s.size() && - (s[pos-1] == preferred_separator) == in_sep) - { --pos; } - if (pos == 2 && !in_sep && s[0] == preferred_separator && - s[1] == preferred_separator) - { return 0; } - return pos; -} + void increment() noexcept { + const PosPtr End = getAfterBack(); + const PosPtr Start = getNextTokenStartPos(); + if (Start == End) + return makeState(PS_AtEnd); + + switch (State) { + case PS_BeforeBegin: { + PosPtr TkEnd = consumeSeparator(Start, End); + // If we consumed exactly two separators we have a root name. + if (TkEnd && TkEnd == Start + 2) { + // FIXME Do we need to consume a name or is '//' a root name on its own? + // what about '//.', '//..', '//...'? + auto NameEnd = consumeName(TkEnd, End); + if (NameEnd) + TkEnd = NameEnd; + return makeState(PS_InRootName, Start, TkEnd); + } + else if (TkEnd) + return makeState(PS_InRootDir, Start, TkEnd); + else + return makeState(PS_InFilenames, Start, consumeName(Start, End)); + } -size_t end_of(const string_type& s, size_t pos) { - if (pos >= s.size()) return npos; - // special case for root name - if (pos == 0 && is_root_name(s, pos)) return root_name_end(s); - bool in_sep = (s[pos] == preferred_separator); - while (pos + 1 < s.size() && (s[pos+1] == preferred_separator) == in_sep) - { ++pos; } - return pos; -} + case PS_InRootName: + return makeState(PS_InRootDir, Start, consumeSeparator(Start, End)); + case PS_InRootDir: + return makeState(PS_InFilenames, Start, consumeName(Start, End)); + + case PS_InFilenames: { + PosPtr SepEnd = consumeSeparator(Start, End); + if (SepEnd != End) { + PosPtr TkEnd = consumeName(SepEnd, End); + if (TkEnd) + return makeState(PS_InFilenames, SepEnd, TkEnd); + } + return makeState(PS_InTrailingSep, Start, SepEnd); + } -inline size_t root_name_start(const string_type& s) { - return good(root_name_end(s)) ? 0 : npos; -} + case PS_InTrailingSep: + return makeState(PS_AtEnd); -size_t root_name_end(const string_type& s) { - if (s.size() < 2 || s[0] != preferred_separator - || s[1] != preferred_separator) { - return npos; + case PS_AtEnd: + _LIBCPP_UNREACHABLE(); } - if (s.size() == 2) { - return 1; + } + + void decrement() noexcept { + const PosPtr REnd = getBeforeFront(); + const PosPtr RStart = getCurrentTokenStartPos() - 1; + + switch (State) { + case PS_AtEnd: { + // Try to consume a trailing separator or root directory first. + if (PosPtr SepEnd = consumeSeparator(RStart, REnd)) { + if (SepEnd == REnd) + return makeState((RStart == REnd + 2) ? PS_InRootName : PS_InRootDir, + Path.data(), RStart + 1); + // Check if we're seeing the root directory separator + auto PP = CreateBegin(Path); + bool InRootDir = PP.State == PS_InRootName && + &PP.RawEntry.back() == SepEnd; + return makeState(InRootDir ? PS_InRootDir : PS_InTrailingSep, + SepEnd + 1, RStart + 1); + } else { + PosPtr TkStart = consumeName(RStart, REnd); + if (TkStart == REnd + 2 && consumeSeparator(TkStart, REnd) == REnd) + return makeState(PS_InRootName, Path.data(), RStart + 1); + else + return makeState(PS_InFilenames, TkStart + 1, RStart + 1); + } } - size_t index = 2; // current position - if (s[index] == preferred_separator) { - return npos; + case PS_InTrailingSep: + return makeState(PS_InFilenames, consumeName(RStart, REnd) + 1, RStart + 1); + case PS_InFilenames: { + PosPtr SepEnd = consumeSeparator(RStart, REnd); + if (SepEnd == REnd) + return makeState((RStart == REnd + 2) ? PS_InRootName : PS_InRootDir, + Path.data(), RStart + 1); + PosPtr TkEnd = consumeName(SepEnd, REnd); + if (TkEnd == REnd + 2 && consumeSeparator(TkEnd, REnd) == REnd) + return makeState(PS_InRootDir, SepEnd + 1, RStart + 1); + return makeState(PS_InFilenames, TkEnd + 1, SepEnd + 1); } - while (index + 1 < s.size() && s[index+1] != preferred_separator) { - ++index; + case PS_InRootDir: + return makeState(PS_InRootName, Path.data(), RStart + 1); + case PS_InRootName: + case PS_BeforeBegin: + _LIBCPP_UNREACHABLE(); } - return index; -} - -size_t root_directory_start(const string_type& s) { - size_t e = root_name_end(s); - if (!good(e)) - return is_separator(s, 0) ? 0 : npos; - return is_separator(s, e + 1) ? e + 1 : npos; -} - -size_t root_directory_end(const string_type& s) { - size_t st = root_directory_start(s); - if (!good(st)) return npos; - size_t index = st; - while (index + 1 < s.size() && s[index + 1] == preferred_separator) - { ++index; } - return index; -} - -string_view_pair separate_filename(string_type const & s) { - if (s == "." || s == ".." || s.empty()) return string_view_pair{s, ""}; - auto pos = s.find_last_of('.'); - if (pos == string_type::npos) return string_view_pair{s, string_view{}}; - return string_view_pair{s.substr(0, pos), s.substr(pos)}; -} - -inline string_view extract_raw(const string_type& s, size_t pos) { - size_t end_i = end_of(s, pos); - if (!good(end_i)) return string_view{}; - return string_view(s).substr(pos, end_i - pos + 1); -} - -string_view extract_preferred(const string_type& s, size_t pos) { - string_view raw = extract_raw(s, pos); - if (raw.empty()) - return raw; - if (is_trailing_separator(s, pos)) - return string_view{dot}; - if (is_separator(s, pos) && !is_root_name(s, pos)) - return string_view(preferred_separator_str); - return raw; -} - -}} // namespace parser - - -//////////////////////////////////////////////////////////////////////////////// -// path_view_iterator -//////////////////////////////////////////////////////////////////////////////// -namespace { - -struct path_view_iterator { - const string_view __s_; - size_t __pos_; + } - explicit path_view_iterator(string_view const& __s) : __s_(__s), __pos_(__s_.empty() ? parser::npos : 0) {} - explicit path_view_iterator(string_view const& __s, size_t __p) : __s_(__s), __pos_(__p) {} + /// \brief Return a view with the "preferred representation" of the current + /// element. For example trailing separators are represented as a '.' + string_view_t operator*() const noexcept { + switch (State) { + case PS_BeforeBegin: + case PS_AtEnd: + return ""; + case PS_InRootDir: + return "/"; + case PS_InTrailingSep: + return "."; + case PS_InRootName: + case PS_InFilenames: + return RawEntry; + } + _LIBCPP_UNREACHABLE(); + } - string_view operator*() const { - return parser::extract_preferred(__s_, __pos_); + explicit operator bool() const noexcept { + return State != PS_BeforeBegin && State != PS_AtEnd; } - path_view_iterator& operator++() { + PathParser& operator++() noexcept { increment(); return *this; } - path_view_iterator& operator--() { + PathParser& operator--() noexcept { decrement(); return *this; } - void increment() { - if (__pos_ == parser::npos) return; - while (! set_position(parser::end_of(__s_, __pos_)+1)) - ; - return; +private: + void makeState(ParserState NewState, PosPtr Start, PosPtr End) noexcept { + State = NewState; + RawEntry = string_view_t(Start, End - Start); + } + void makeState(ParserState NewState) noexcept { + State = NewState; + RawEntry = {}; } - void decrement() { - if (__pos_ == 0) { - set_position(0); - } - else if (__pos_ == parser::npos) { - auto const str_size = __s_.size(); - set_position(parser::start_of( - __s_, str_size != 0 ? str_size - 1 : str_size)); - } else { - while (!set_position(parser::start_of(__s_, __pos_-1))) - ; - } + PosPtr getAfterBack() const noexcept { + return Path.data() + Path.size(); + } + + PosPtr getBeforeFront() const noexcept { + return Path.data() - 1; } - bool set_position(size_t pos) { - if (pos >= __s_.size()) { - __pos_ = parser::npos; - } else { - __pos_ = pos; + /// \brief Return a pointer to the first character after the currently + /// lexed element. + PosPtr getNextTokenStartPos() const noexcept { + switch (State) { + case PS_BeforeBegin: + return Path.data(); + case PS_InRootName: + case PS_InRootDir: + case PS_InFilenames: + return &RawEntry.back() + 1; + case PS_InTrailingSep: + case PS_AtEnd: + return getAfterBack(); } - return valid_iterator_position(); + _LIBCPP_UNREACHABLE(); } - bool valid_iterator_position() const { - if (__pos_ == parser::npos) return true; // end position is valid - return (!parser::is_separator (__s_, __pos_) || - parser::is_root_directory (__s_, __pos_) || - parser::is_trailing_separator(__s_, __pos_) || - parser::is_root_name (__s_, __pos_)); + /// \brief Return a pointer to the first character in the currently lexed + /// element. + PosPtr getCurrentTokenStartPos() const noexcept { + switch (State) { + case PS_BeforeBegin: + case PS_InRootName: + return &Path.front(); + case PS_InRootDir: + case PS_InFilenames: + case PS_InTrailingSep: + return &RawEntry.front(); + case PS_AtEnd: + return &Path.back() + 1; + } + _LIBCPP_UNREACHABLE(); } - bool is_end() const { return __pos_ == parser::npos; } + PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept { + if (P == End || *P != '/') + return nullptr; + const int Inc = P < End ? 1 : -1; + P += Inc; + while (P != End && *P == '/') + P += Inc; + return P; + } - inline bool operator==(path_view_iterator const& __p) { - return __pos_ == __p.__pos_; + PosPtr consumeName(PosPtr P, PosPtr End) const noexcept { + if (P == End || *P == '/') + return nullptr; + const int Inc = P < End ? 1 : -1; + P += Inc; + while (P != End && *P != '/') + P += Inc; + return P; } }; -path_view_iterator pbegin(path const& p) { - return path_view_iterator(p.native()); +string_view_pair separate_filename(string_view_t const & s) { + if (s == "." || s == ".." || s.empty()) return string_view_pair{s, ""}; + auto pos = s.find_last_of('.'); + if (pos == string_view_t::npos) + return string_view_pair{s, string_view_t{}}; + return string_view_pair{s.substr(0, pos), s.substr(pos)}; } -path_view_iterator pend(path const& p) { - path_view_iterator __p(p.native()); - __p.__pos_ = parser::npos; - return __p; +string_view_t createView(PosPtr S, PosPtr E) noexcept { + return {S, static_cast<size_t>(E - S) + 1}; } -} // end namespace +}} // namespace parser + +_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM + +using parser::string_view_t; +using parser::string_view_pair; +using parser::PathParser; +using parser::createView; + /////////////////////////////////////////////////////////////////////////////// // path definitions /////////////////////////////////////////////////////////////////////////////// +constexpr path::value_type path::preferred_separator; + path & path::replace_extension(path const & replacement) { path p = extension(); @@ -260,131 +303,145 @@ path & path::replace_extension(path const & replacement) /////////////////////////////////////////////////////////////////////////////// // path.decompose -string_view path::__root_name() const +string_view_t path::__root_name() const { - return parser::is_root_name(__pn_, 0) - ? parser::extract_preferred(__pn_, 0) - : string_view{}; + auto PP = PathParser::CreateBegin(__pn_); + if (PP.State == PathParser::PS_InRootName) + return *PP; + return {}; } -string_view path::__root_directory() const +string_view_t path::__root_directory() const { - auto start_i = parser::root_directory_start(__pn_); - if(!parser::good(start_i)) { - return {}; - } - return parser::extract_preferred(__pn_, start_i); + auto PP = PathParser::CreateBegin(__pn_); + if (PP.State == PathParser::PS_InRootName) + ++PP; + if (PP.State == PathParser::PS_InRootDir) + return *PP; + return {}; } -string_view path::__relative_path() const +string_view_t path::__root_path_raw() const { - if (empty()) { - return {__pn_}; - } - auto end_i = parser::root_directory_end(__pn_); - if (not parser::good(end_i)) { - end_i = parser::root_name_end(__pn_); + auto PP = PathParser::CreateBegin(__pn_); + if (PP.State == PathParser::PS_InRootName) { + auto NextCh = PP.peek(); + if (NextCh && *NextCh == '/') { + ++PP; + return createView(__pn_.data(), &PP.RawEntry.back()); + } + return PP.RawEntry; } - if (not parser::good(end_i)) { - return {__pn_}; - } - return string_view(__pn_).substr(end_i+1); + if (PP.State == PathParser::PS_InRootDir) + return *PP; + return {}; } -string_view path::__parent_path() const +string_view_t path::__relative_path() const { - if (empty() || pbegin(*this) == --pend(*this)) { - return {}; - } - auto end_it = --(--pend(*this)); - auto end_i = parser::end_of(__pn_, end_it.__pos_); - return string_view(__pn_).substr(0, end_i+1); + auto PP = PathParser::CreateBegin(__pn_); + while (PP.State <= PathParser::PS_InRootDir) + ++PP; + if (PP.State == PathParser::PS_AtEnd) + return {}; + return createView(PP.RawEntry.data(), &__pn_.back()); } -string_view path::__filename() const +string_view_t path::__parent_path() const { - return empty() ? string_view{} : *--pend(*this); + if (empty()) + return {}; + auto PP = PathParser::CreateEnd(__pn_); + --PP; + if (PP.RawEntry.data() == __pn_.data()) + return {}; + --PP; + return createView(__pn_.data(), &PP.RawEntry.back()); } -string_view path::__stem() const +string_view_t path::__filename() const +{ + if (empty()) return {}; + return *(--PathParser::CreateEnd(__pn_)); +} + +string_view_t path::__stem() const { return parser::separate_filename(__filename()).first; } -string_view path::__extension() const +string_view_t path::__extension() const { return parser::separate_filename(__filename()).second; } //////////////////////////////////////////////////////////////////////////// // path.comparisons -int path::__compare(const value_type* __s) const { - path_view_iterator thisIter(this->native()); - path_view_iterator sIter(__s); - while (!thisIter.is_end() && !sIter.is_end()) { - int res = (*thisIter).compare(*sIter); +int path::__compare(string_view_t __s) const { + auto PP = PathParser::CreateBegin(__pn_); + auto PP2 = PathParser::CreateBegin(__s); + while (PP && PP2) { + int res = (*PP).compare(*PP2); if (res != 0) return res; - ++thisIter; ++sIter; + ++PP; ++PP2; } - if (thisIter.is_end() && sIter.is_end()) + if (PP.State == PP2.State && PP.State == PathParser::PS_AtEnd) return 0; - if (thisIter.is_end()) + if (PP.State == PathParser::PS_AtEnd) return -1; return 1; } //////////////////////////////////////////////////////////////////////////// // path.nonmembers -size_t hash_value(const path& __p) _NOEXCEPT { - path_view_iterator thisIter(__p.native()); - struct HashPairT { - size_t first; - size_t second; - }; - HashPairT hp = {0, 0}; - std::hash<string_view> hasher; - std::__scalar_hash<decltype(hp)> pair_hasher; - while (!thisIter.is_end()) { - hp.second = hasher(*thisIter); - hp.first = pair_hasher(hp); - ++thisIter; +size_t hash_value(const path& __p) noexcept { + auto PP = PathParser::CreateBegin(__p.native()); + size_t hash_value = 0; + std::hash<string_view_t> hasher; + while (PP) { + hash_value = __hash_combine(hash_value, hasher(*PP)); + ++PP; } - return hp.first; + return hash_value; } //////////////////////////////////////////////////////////////////////////// // path.itr path::iterator path::begin() const { - path_view_iterator pit = pbegin(*this); + auto PP = PathParser::CreateBegin(__pn_); iterator it; it.__path_ptr_ = this; - it.__pos_ = pit.__pos_; - it.__elem_.__assign_view(*pit); + it.__state_ = PP.State; + it.__entry_ = PP.RawEntry; + it.__stashed_elem_.__assign_view(*PP); return it; } path::iterator path::end() const { iterator it{}; + it.__state_ = PathParser::PS_AtEnd; it.__path_ptr_ = this; - it.__pos_ = parser::npos; return it; } path::iterator& path::iterator::__increment() { - path_view_iterator it(__path_ptr_->native(), __pos_); - it.increment(); - __pos_ = it.__pos_; - __elem_.__assign_view(*it); + static_assert(__at_end == PathParser::PS_AtEnd, ""); + PathParser PP(__path_ptr_->native(), __entry_, __state_); + ++PP; + __state_ = PP.State; + __entry_ = PP.RawEntry; + __stashed_elem_.__assign_view(*PP); return *this; } path::iterator& path::iterator::__decrement() { - path_view_iterator it(__path_ptr_->native(), __pos_); - it.decrement(); - __pos_ = it.__pos_; - __elem_.__assign_view(*it); + PathParser PP(__path_ptr_->native(), __entry_, __state_); + --PP; + __state_ = PP.State; + __entry_ = PP.RawEntry; + __stashed_elem_.__assign_view(*PP); return *this; } diff --git a/lib/libcxx/src/experimental/memory_resource.cpp b/lib/libcxx/src/experimental/memory_resource.cpp index c01eb0823ba..c4dc1ca894a 100644 --- a/lib/libcxx/src/experimental/memory_resource.cpp +++ b/lib/libcxx/src/experimental/memory_resource.cpp @@ -23,7 +23,7 @@ _LIBCPP_BEGIN_NAMESPACE_LFTS_PMR // new_delete_resource() -class _LIBCPP_TYPE_VIS_ONLY __new_delete_memory_resource_imp +class _LIBCPP_TYPE_VIS __new_delete_memory_resource_imp : public memory_resource { public: @@ -34,7 +34,7 @@ protected: { return __allocate(__size); } virtual void do_deallocate(void * __p, size_t, size_t) - { __deallocate(__p); } + { _VSTD::__libcpp_deallocate(__p); } virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT { return &__other == this; } @@ -42,7 +42,7 @@ protected: // null_memory_resource() -class _LIBCPP_TYPE_VIS_ONLY __null_memory_resource_imp +class _LIBCPP_TYPE_VIS __null_memory_resource_imp : public memory_resource { public: @@ -50,11 +50,7 @@ public: protected: virtual void* do_allocate(size_t, size_t) { -#ifndef _LIBCPP_NO_EXCEPTIONS - throw std::bad_alloc(); -#else - abort(); -#endif + __throw_bad_alloc(); } virtual void do_deallocate(void *, size_t, size_t) {} virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT @@ -74,7 +70,10 @@ union ResourceInitHelper { }; // When compiled in C++14 this initialization should be a constant expression. // Only in C++11 is "init_priority" needed to ensure initialization order. -ResourceInitHelper res_init __attribute__((init_priority (101))); +#if _LIBCPP_STD_VER > 11 +_LIBCPP_SAFE_STATIC +#endif +ResourceInitHelper res_init __attribute__((init_priority (101))); } // end namespace @@ -93,7 +92,7 @@ static memory_resource * __default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT { #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER - static atomic<memory_resource*> __res = + _LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res = ATOMIC_VAR_INIT(&res_init.resources.new_delete_res); if (set) { new_res = new_res ? new_res : new_delete_resource(); @@ -106,7 +105,7 @@ __default_memory_resource(bool set = false, memory_resource * new_res = nullptr) &__res, memory_order::memory_order_acquire); } #elif !defined(_LIBCPP_HAS_NO_THREADS) - static memory_resource * res = &res_init.resources.new_delete_res; + _LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res; static mutex res_lock; if (set) { new_res = new_res ? new_res : new_delete_resource(); @@ -119,7 +118,7 @@ __default_memory_resource(bool set = false, memory_resource * new_res = nullptr) return res; } #else - static memory_resource* res = &res_init.resources.new_delete_res; + _LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res; if (set) { new_res = new_res ? new_res : new_delete_resource(); memory_resource * old_res = res; @@ -141,4 +140,4 @@ memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT return __default_memory_resource(true, __new_res); } -_LIBCPP_END_NAMESPACE_LFTS_PMR
\ No newline at end of file +_LIBCPP_END_NAMESPACE_LFTS_PMR diff --git a/lib/libcxx/src/functional.cpp b/lib/libcxx/src/functional.cpp new file mode 100644 index 00000000000..5c2646f01b2 --- /dev/null +++ b/lib/libcxx/src/functional.cpp @@ -0,0 +1,26 @@ +//===----------------------- functional.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 "functional" + +_LIBCPP_BEGIN_NAMESPACE_STD + +#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION +bad_function_call::~bad_function_call() _NOEXCEPT +{ +} + +const char* +bad_function_call::what() const _NOEXCEPT +{ + return "std::bad_function_call"; +} +#endif + +_LIBCPP_END_NAMESPACE_STD diff --git a/lib/libcxx/src/include/atomic_support.h b/lib/libcxx/src/include/atomic_support.h index 8b719c5f221..ccd8d78d3d7 100644 --- a/lib/libcxx/src/include/atomic_support.h +++ b/lib/libcxx/src/include/atomic_support.h @@ -16,6 +16,7 @@ #if defined(__clang__) && __has_builtin(__atomic_load_n) \ && __has_builtin(__atomic_store_n) \ && __has_builtin(__atomic_add_fetch) \ + && __has_builtin(__atomic_exchange_n) \ && __has_builtin(__atomic_compare_exchange_n) \ && defined(__ATOMIC_RELAXED) \ && defined(__ATOMIC_CONSUME) \ @@ -29,7 +30,7 @@ #endif #if !defined(_LIBCPP_HAS_ATOMIC_BUILTINS) && !defined(_LIBCPP_HAS_NO_THREADS) -# if defined(_MSC_VER) && !defined(__clang__) +# if defined(_LIBCPP_WARNING) _LIBCPP_WARNING("Building libc++ without __atomic builtins is unsupported") # else # warning Building libc++ without __atomic builtins is unsupported @@ -45,7 +46,7 @@ namespace { enum __libcpp_atomic_order { _AO_Relaxed = __ATOMIC_RELAXED, _AO_Consume = __ATOMIC_CONSUME, - _AO_Aquire = __ATOMIC_ACQUIRE, + _AO_Acquire = __ATOMIC_ACQUIRE, _AO_Release = __ATOMIC_RELEASE, _AO_Acq_Rel = __ATOMIC_ACQ_REL, _AO_Seq = __ATOMIC_SEQ_CST @@ -84,6 +85,14 @@ _ValueType __libcpp_atomic_add(_ValueType* __val, _AddType __a, template <class _ValueType> inline _LIBCPP_INLINE_VISIBILITY +_ValueType __libcpp_atomic_exchange(_ValueType* __target, + _ValueType __value, int __order = _AO_Seq) +{ + return __atomic_exchange_n(__target, __value, __order); +} + +template <class _ValueType> +inline _LIBCPP_INLINE_VISIBILITY bool __libcpp_atomic_compare_exchange(_ValueType* __val, _ValueType* __expected, _ValueType __after, int __success_order = _AO_Seq, @@ -137,6 +146,16 @@ _ValueType __libcpp_atomic_add(_ValueType* __val, _AddType __a, template <class _ValueType> inline _LIBCPP_INLINE_VISIBILITY +_ValueType __libcpp_atomic_exchange(_ValueType* __target, + _ValueType __value, int __order = _AO_Seq) +{ + _ValueType old = *__target; + *__target = __value; + return old; +} + +template <class _ValueType> +inline _LIBCPP_INLINE_VISIBILITY bool __libcpp_atomic_compare_exchange(_ValueType* __val, _ValueType* __expected, _ValueType __after, int = 0, int = 0) diff --git a/lib/libcxx/src/include/config_elast.h b/lib/libcxx/src/include/config_elast.h index 9d6a76b0c00..4c4d853c2a6 100644 --- a/lib/libcxx/src/include/config_elast.h +++ b/lib/libcxx/src/include/config_elast.h @@ -10,7 +10,9 @@ #ifndef _LIBCPP_CONFIG_ELAST #define _LIBCPP_CONFIG_ELAST -#if defined(_WIN32) +#include <__config> + +#if defined(_LIBCPP_MSVCRT) #include <stdlib.h> #else #include <errno.h> @@ -20,14 +22,16 @@ #define _LIBCPP_ELAST ELAST #elif defined(_NEWLIB_VERSION) #define _LIBCPP_ELAST __ELASTERROR -#elif defined(__linux__) +#elif defined(__Fuchsia__) +// No _LIBCPP_ELAST needed on Fuchsia +#elif defined(__linux__) || defined(_LIBCPP_HAS_MUSL_LIBC) #define _LIBCPP_ELAST 4095 #elif defined(__APPLE__) // No _LIBCPP_ELAST needed on Apple #elif defined(__sun__) #define _LIBCPP_ELAST ESTALE -#elif defined(_WIN32) -#define _LIBCPP_ELAST _sys_nerr +#elif defined(_LIBCPP_MSVCRT) +#define _LIBCPP_ELAST (_sys_nerr - 1) #else // Warn here so that the person doing the libcxx port has an easier time: #warning ELAST for this platform not yet implemented diff --git a/lib/libcxx/src/include/refstring.h b/lib/libcxx/src/include/refstring.h new file mode 100644 index 00000000000..702f2b7388d --- /dev/null +++ b/lib/libcxx/src/include/refstring.h @@ -0,0 +1,128 @@ +//===------------------------ __refstring ---------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_REFSTRING_H +#define _LIBCPP_REFSTRING_H + +#include <__config> +#include <stdexcept> +#include <cstddef> +#include <cstring> +#ifdef __APPLE__ +#include <dlfcn.h> +#include <mach-o/dyld.h> +#endif +#include "atomic_support.h" + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace __refstring_imp { namespace { +typedef int count_t; + +struct _Rep_base { + std::size_t len; + std::size_t cap; + count_t count; +}; + +inline _Rep_base* rep_from_data(const char *data_) noexcept { + char *data = const_cast<char *>(data_); + return reinterpret_cast<_Rep_base *>(data - sizeof(_Rep_base)); +} + +inline char * data_from_rep(_Rep_base *rep) noexcept { + char *data = reinterpret_cast<char *>(rep); + return data + sizeof(*rep); +} + +#if defined(__APPLE__) +inline +const char* compute_gcc_empty_string_storage() _NOEXCEPT +{ + void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD); + if (handle == nullptr) + return nullptr; + void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE"); + if (sym == nullptr) + return nullptr; + return data_from_rep(reinterpret_cast<_Rep_base *>(sym)); +} + +inline +const char* +get_gcc_empty_string_storage() _NOEXCEPT +{ + static const char* p = compute_gcc_empty_string_storage(); + return p; +} +#endif + +}} // namespace __refstring_imp + +using namespace __refstring_imp; + +inline +__libcpp_refstring::__libcpp_refstring(const char* msg) { + std::size_t len = strlen(msg); + _Rep_base* rep = static_cast<_Rep_base *>(::operator new(sizeof(*rep) + len + 1)); + rep->len = len; + rep->cap = len; + rep->count = 0; + char *data = data_from_rep(rep); + std::memcpy(data, msg, len + 1); + __imp_ = data; +} + +inline +__libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) _NOEXCEPT + : __imp_(s.__imp_) +{ + if (__uses_refcount()) + __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1); +} + +inline +__libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) _NOEXCEPT { + bool adjust_old_count = __uses_refcount(); + struct _Rep_base *old_rep = rep_from_data(__imp_); + __imp_ = s.__imp_; + if (__uses_refcount()) + __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1); + if (adjust_old_count) + { + if (__libcpp_atomic_add(&old_rep->count, count_t(-1)) < 0) + { + ::operator delete(old_rep); + } + } + return *this; +} + +inline +__libcpp_refstring::~__libcpp_refstring() { + if (__uses_refcount()) { + _Rep_base* rep = rep_from_data(__imp_); + if (__libcpp_atomic_add(&rep->count, count_t(-1)) < 0) { + ::operator delete(rep); + } + } +} + +inline +bool __libcpp_refstring::__uses_refcount() const { +#ifdef __APPLE__ + return __imp_ != get_gcc_empty_string_storage(); +#else + return true; +#endif +} + +_LIBCPP_END_NAMESPACE_STD + +#endif //_LIBCPP_REFSTRING_H diff --git a/lib/libcxx/src/ios.cpp b/lib/libcxx/src/ios.cpp index 23e3ee0ca04..0f1d88e378f 100644 --- a/lib/libcxx/src/ios.cpp +++ b/lib/libcxx/src/ios.cpp @@ -22,22 +22,23 @@ #include "new" #include "streambuf" #include "string" +#include "__undef_macros" _LIBCPP_BEGIN_NAMESPACE_STD -template class basic_ios<char>; -template class basic_ios<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ios<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ios<wchar_t>; -template class basic_streambuf<char>; -template class basic_streambuf<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_streambuf<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_streambuf<wchar_t>; -template class basic_istream<char>; -template class basic_istream<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_istream<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_istream<wchar_t>; -template class basic_ostream<char>; -template class basic_ostream<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ostream<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ostream<wchar_t>; -template class basic_iostream<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_iostream<char>; class _LIBCPP_HIDDEN __iostream_category : public __do_message diff --git a/lib/libcxx/src/iostream.cpp b/lib/libcxx/src/iostream.cpp index e073aec6ead..2b47cf25b64 100644 --- a/lib/libcxx/src/iostream.cpp +++ b/lib/libcxx/src/iostream.cpp @@ -11,35 +11,71 @@ #include "string" #include "new" +#define _str(s) #s +#define str(s) _str(s) +#define _LIBCPP_NAMESPACE_STR str(_LIBCPP_NAMESPACE) + _LIBCPP_BEGIN_NAMESPACE_STD #ifndef _LIBCPP_HAS_NO_STDIN -_ALIGNAS_TYPE (istream) _LIBCPP_FUNC_VIS char cin [sizeof(istream)]; -_ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin [sizeof(__stdinbuf <char>)]; +_ALIGNAS_TYPE (istream) _LIBCPP_FUNC_VIS char cin[sizeof(istream)] +#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) +__asm__("?cin@" _LIBCPP_NAMESPACE_STR "@std@@3V?$basic_istream@DU?$char_traits@D@" _LIBCPP_NAMESPACE_STR "@std@@@12@A") +#endif +; +_ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin[sizeof(__stdinbuf <char>)]; static mbstate_t mb_cin; -_ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin [sizeof(wistream)]; -_ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin [sizeof(__stdinbuf <wchar_t>)]; +_ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin[sizeof(wistream)] +#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) +__asm__("?wcin@" _LIBCPP_NAMESPACE_STR "@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_NAMESPACE_STR "@std@@@12@A") +#endif +; +_ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin[sizeof(__stdinbuf <wchar_t>)]; static mbstate_t mb_wcin; #endif #ifndef _LIBCPP_HAS_NO_STDOUT -_ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cout[sizeof(ostream)]; +_ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cout[sizeof(ostream)] +#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) +__asm__("?cout@" _LIBCPP_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_NAMESPACE_STR "@std@@@12@A") +#endif +; _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)]; static mbstate_t mb_cout; -_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)]; +_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)] +#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) +__asm__("?wcout@" _LIBCPP_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_NAMESPACE_STR "@std@@@12@A") +#endif +; _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)]; static mbstate_t mb_wcout; #endif -_ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)]; +_ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)] +#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) +__asm__("?cerr@" _LIBCPP_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_NAMESPACE_STR "@std@@@12@A") +#endif +; _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)]; static mbstate_t mb_cerr; -_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)]; +_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)] +#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) +__asm__("?wcerr@" _LIBCPP_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_NAMESPACE_STR "@std@@@12@A") +#endif +; _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)]; static mbstate_t mb_wcerr; -_ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char clog[sizeof(ostream)]; -_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)]; +_ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char clog[sizeof(ostream)] +#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) +__asm__("?clog@" _LIBCPP_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_NAMESPACE_STR "@std@@@12@A") +#endif +; +_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)] +#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) +__asm__("?wclog@" _LIBCPP_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_NAMESPACE_STR "@std@@@12@A") +#endif +; ios_base::Init __start_std_streams; diff --git a/lib/libcxx/src/locale.cpp b/lib/libcxx/src/locale.cpp index da2fd11678d..a6cb0e97d12 100644 --- a/lib/libcxx/src/locale.cpp +++ b/lib/libcxx/src/locale.cpp @@ -24,15 +24,20 @@ #endif #include "clocale" #include "cstring" +#if defined(_LIBCPP_MSVCRT) +#define _CTYPE_DISABLE_MACROS +#endif #include "cwctype" #include "__sso_allocator" #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) #include "support/win32/locale_win32.h" -#elif !defined(__ANDROID__) +#elif !defined(__BIONIC__) #include <langinfo.h> #endif #include <stdlib.h> #include <stdio.h> +#include "include/atomic_support.h" +#include "__undef_macros" // 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. @@ -42,6 +47,24 @@ _LIBCPP_BEGIN_NAMESPACE_STD +struct __libcpp_unique_locale { + __libcpp_unique_locale(const char* nm) : __loc_(newlocale(LC_ALL_MASK, nm, 0)) {} + + ~__libcpp_unique_locale() { + if (__loc_) + freelocale(__loc_); + } + + explicit operator bool() const { return __loc_; } + + locale_t& get() { return __loc_; } + + locale_t __loc_; +private: + __libcpp_unique_locale(__libcpp_unique_locale const&); + __libcpp_unique_locale& operator=(__libcpp_unique_locale const&); +}; + #ifdef __cloc_defined locale_t __cloc() { // In theory this could create a race condition. In practice @@ -65,8 +88,8 @@ T& make(A0 a0) { static typename aligned_storage<sizeof(T)>::type buf; - ::new (&buf) T(a0); - return *reinterpret_cast<T*>(&buf); + auto *obj = ::new (&buf) T(a0); + return *obj; } template <class T, class A0, class A1> @@ -85,8 +108,8 @@ T& make(A0 a0, A1 a1, A2 a2) { static typename aligned_storage<sizeof(T)>::type buf; - ::new (&buf) T(a0, a1, a2); - return *reinterpret_cast<T*>(&buf); + auto *obj = ::new (&buf) T(a0, a1, a2); + return *obj; } template <typename T, size_t N> @@ -107,6 +130,16 @@ countof(const T * const begin, const T * const end) return static_cast<size_t>(end - begin); } +_LIBCPP_NORETURN static void __throw_runtime_error(const string &msg) +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + throw runtime_error(msg); +#else + (void)msg; + _VSTD::abort(); +#endif +} + } #if defined(_AIX) @@ -127,7 +160,7 @@ class _LIBCPP_HIDDEN locale::__imp : public facet { enum {N = 28}; -#if defined(_LIBCPP_MSVC) +#if defined(_LIBCPP_COMPILER_MSVC) // FIXME: MSVC doesn't support aligned parameters by value. // I can't get the __sso_allocator to work here // for MSVC I think for this reason. @@ -467,8 +500,8 @@ locale::__imp::make_global() { // only one thread can get in here and it only gets in once static aligned_storage<sizeof(locale)>::type buf; - ::new (&buf) locale(locale::classic()); - return *reinterpret_cast<locale*>(&buf); + auto *obj = ::new (&buf) locale(locale::classic()); + return *obj; } locale& @@ -566,10 +599,8 @@ locale::global(const locale& loc) locale& g = __global(); locale r = g; g = loc; -#ifndef __CloudABI__ if (g.name() != "*") setlocale(LC_ALL, g.name().c_str()); -#endif return r; } @@ -637,7 +668,7 @@ locale::id::__get() void locale::id::__init() { - __id_ = __sync_add_and_fetch(&__next_id, 1); + __id_ = __libcpp_atomic_add(&__next_id, 1); } // template <> class collate_byname<char> @@ -646,22 +677,18 @@ collate_byname<char>::collate_byname(const char* n, size_t refs) : collate<char>(refs), __l(newlocale(LC_ALL_MASK, n, 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__l == 0) - throw runtime_error("collate_byname<char>::collate_byname" + __throw_runtime_error("collate_byname<char>::collate_byname" " failed to construct for " + string(n)); -#endif // _LIBCPP_NO_EXCEPTIONS } collate_byname<char>::collate_byname(const string& name, size_t refs) : collate<char>(refs), __l(newlocale(LC_ALL_MASK, name.c_str(), 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__l == 0) - throw runtime_error("collate_byname<char>::collate_byname" + __throw_runtime_error("collate_byname<char>::collate_byname" " failed to construct for " + name); -#endif // _LIBCPP_NO_EXCEPTIONS } collate_byname<char>::~collate_byname() @@ -698,22 +725,18 @@ collate_byname<wchar_t>::collate_byname(const char* n, size_t refs) : collate<wchar_t>(refs), __l(newlocale(LC_ALL_MASK, n, 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__l == 0) - throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)" + __throw_runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)" " failed to construct for " + string(n)); -#endif // _LIBCPP_NO_EXCEPTIONS } collate_byname<wchar_t>::collate_byname(const string& name, size_t refs) : collate<wchar_t>(refs), __l(newlocale(LC_ALL_MASK, name.c_str(), 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__l == 0) - throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)" + __throw_runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)" " failed to construct for " + name); -#endif // _LIBCPP_NO_EXCEPTIONS } collate_byname<wchar_t>::~collate_byname() @@ -1106,9 +1129,7 @@ ctype<char>::classic_table() _NOEXCEPT #elif __sun__ return __ctype_mask; #elif defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) - 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... + return __pctype_func(); #elif defined(__EMSCRIPTEN__) return *__ctype_b_loc(); #elif defined(_NEWLIB_VERSION) @@ -1172,22 +1193,18 @@ ctype_byname<char>::ctype_byname(const char* name, size_t refs) : ctype<char>(0, false, refs), __l(newlocale(LC_ALL_MASK, name, 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__l == 0) - throw runtime_error("ctype_byname<char>::ctype_byname" + __throw_runtime_error("ctype_byname<char>::ctype_byname" " failed to construct for " + string(name)); -#endif // _LIBCPP_NO_EXCEPTIONS } ctype_byname<char>::ctype_byname(const string& name, size_t refs) : ctype<char>(0, false, refs), __l(newlocale(LC_ALL_MASK, name.c_str(), 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__l == 0) - throw runtime_error("ctype_byname<char>::ctype_byname" + __throw_runtime_error("ctype_byname<char>::ctype_byname" " failed to construct for " + name); -#endif // _LIBCPP_NO_EXCEPTIONS } ctype_byname<char>::~ctype_byname() @@ -1229,22 +1246,18 @@ ctype_byname<wchar_t>::ctype_byname(const char* name, size_t refs) : ctype<wchar_t>(refs), __l(newlocale(LC_ALL_MASK, name, 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__l == 0) - throw runtime_error("ctype_byname<wchar_t>::ctype_byname" + __throw_runtime_error("ctype_byname<wchar_t>::ctype_byname" " failed to construct for " + string(name)); -#endif // _LIBCPP_NO_EXCEPTIONS } ctype_byname<wchar_t>::ctype_byname(const string& name, size_t refs) : ctype<wchar_t>(refs), __l(newlocale(LC_ALL_MASK, name.c_str(), 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__l == 0) - throw runtime_error("ctype_byname<wchar_t>::ctype_byname" + __throw_runtime_error("ctype_byname<wchar_t>::ctype_byname" " failed to construct for " + name); -#endif // _LIBCPP_NO_EXCEPTIONS } ctype_byname<wchar_t>::~ctype_byname() @@ -1504,11 +1517,9 @@ codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs) : locale::facet(refs), __l(newlocale(LC_ALL_MASK, nm, 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__l == 0) - throw runtime_error("codecvt_byname<wchar_t, char, mbstate_t>::codecvt_byname" + __throw_runtime_error("codecvt_byname<wchar_t, char, mbstate_t>::codecvt_byname" " failed to construct for " + string(nm)); -#endif // _LIBCPP_NO_EXCEPTIONS } codecvt<wchar_t, char, mbstate_t>::~codecvt() @@ -3255,7 +3266,7 @@ __codecvt_utf8<wchar_t>::do_out(state_type&, const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, extern_type* to, extern_type* to_end, extern_type*& to_nxt) const { -#if _WIN32 +#if defined(_LIBCPP_SHORT_WCHAR) const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm); const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end); const uint16_t* _frm_nxt = _frm; @@ -3267,7 +3278,7 @@ __codecvt_utf8<wchar_t>::do_out(state_type&, uint8_t* _to = reinterpret_cast<uint8_t*>(to); uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); uint8_t* _to_nxt = _to; -#if _WIN32 +#if defined(_LIBCPP_SHORT_WCHAR) result r = ucs2_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, _Maxcode_, _Mode_); #else @@ -3287,7 +3298,7 @@ __codecvt_utf8<wchar_t>::do_in(state_type&, const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); const uint8_t* _frm_nxt = _frm; -#if _WIN32 +#if defined(_LIBCPP_SHORT_WCHAR) uint16_t* _to = reinterpret_cast<uint16_t*>(to); uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end); uint16_t* _to_nxt = _to; @@ -4191,6 +4202,54 @@ __widen_from_utf8<32>::~__widen_from_utf8() { } + +static bool checked_string_to_wchar_convert(wchar_t& dest, + const char* ptr, + locale_t loc) { + if (*ptr == '\0') + return false; + mbstate_t mb = {}; + wchar_t out; + size_t ret = __libcpp_mbrtowc_l(&out, ptr, strlen(ptr), &mb, loc); + if (ret == static_cast<size_t>(-1) || ret == static_cast<size_t>(-2)) { + return false; + } + dest = out; + return true; +} + +static bool checked_string_to_char_convert(char& dest, + const char* ptr, + locale_t __loc) { + if (*ptr == '\0') + return false; + if (!ptr[1]) { + dest = *ptr; + return true; + } + // First convert the MBS into a wide char then attempt to narrow it using + // wctob_l. + wchar_t wout; + if (!checked_string_to_wchar_convert(wout, ptr, __loc)) + return false; + int res; + if ((res = __libcpp_wctob_l(wout, __loc)) != char_traits<char>::eof()) { + dest = res; + return true; + } + // FIXME: Work around specific multibyte sequences that we can reasonable + // translate into a different single byte. + switch (wout) { + case L'\u00A0': // non-breaking space + dest = ' '; + return true; + default: + return false; + } + _LIBCPP_UNREACHABLE(); +} + + // numpunct<char> && numpunct<wchar_t> locale::id numpunct< char >::id; @@ -4256,17 +4315,16 @@ numpunct_byname<char>::__init(const char* nm) { if (strcmp(nm, "C") != 0) { - __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (loc == nullptr) - throw runtime_error("numpunct_byname<char>::numpunct_byname" + __libcpp_unique_locale loc(nm); + if (!loc) + __throw_runtime_error("numpunct_byname<char>::numpunct_byname" " failed to construct for " + string(nm)); -#endif // _LIBCPP_NO_EXCEPTIONS + lconv* lc = __libcpp_localeconv_l(loc.get()); - if (*lc->decimal_point) - __decimal_point_ = *lc->decimal_point; - if (*lc->thousands_sep) - __thousands_sep_ = *lc->thousands_sep; + checked_string_to_char_convert(__decimal_point_, lc->decimal_point, + loc.get()); + checked_string_to_char_convert(__thousands_sep_, lc->thousands_sep, + loc.get()); __grouping_ = lc->grouping; // localization for truename and falsename is not available } @@ -4295,19 +4353,18 @@ numpunct_byname<wchar_t>::__init(const char* nm) { if (strcmp(nm, "C") != 0) { - __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (loc == nullptr) - throw runtime_error("numpunct_byname<char>::numpunct_byname" + __libcpp_unique_locale loc(nm); + if (!loc) + __throw_runtime_error("numpunct_byname<wchar_t>::numpunct_byname" " failed to construct for " + string(nm)); -#endif // _LIBCPP_NO_EXCEPTIONS + lconv* lc = __libcpp_localeconv_l(loc.get()); - if (*lc->decimal_point) - __decimal_point_ = *lc->decimal_point; - if (*lc->thousands_sep) - __thousands_sep_ = *lc->thousands_sep; + checked_string_to_wchar_convert(__decimal_point_, lc->decimal_point, + loc.get()); + checked_string_to_wchar_convert(__thousands_sep_, lc->thousands_sep, + loc.get()); __grouping_ = lc->grouping; - // locallization for truename and falsename is not available + // localization for truename and falsename is not available } } @@ -4703,21 +4760,17 @@ __time_get_c_storage<wchar_t>::__r() const __time_get::__time_get(const char* nm) : __loc_(newlocale(LC_ALL_MASK, nm, 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__loc_ == 0) - throw runtime_error("time_get_byname" + __throw_runtime_error("time_get_byname" " failed to construct for " + string(nm)); -#endif // _LIBCPP_NO_EXCEPTIONS } __time_get::__time_get(const string& nm) : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__loc_ == 0) - throw runtime_error("time_get_byname" + __throw_runtime_error("time_get_byname" " failed to construct for " + nm); -#endif // _LIBCPP_NO_EXCEPTIONS } __time_get::~__time_get() @@ -5363,21 +5416,17 @@ __time_get_storage<wchar_t>::__do_date_order() const __time_put::__time_put(const char* nm) : __loc_(newlocale(LC_ALL_MASK, nm, 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__loc_ == 0) - throw runtime_error("time_put_byname" + __throw_runtime_error("time_put_byname" " failed to construct for " + string(nm)); -#endif // _LIBCPP_NO_EXCEPTIONS } __time_put::__time_put(const string& nm) : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0)) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (__loc_ == 0) - throw runtime_error("time_put_byname" + __throw_runtime_error("time_put_byname" " failed to construct for " + nm); -#endif // _LIBCPP_NO_EXCEPTIONS } __time_put::~__time_put() @@ -5791,21 +5840,21 @@ void 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 == nullptr) - throw runtime_error("moneypunct_byname" + __libcpp_unique_locale loc(nm); + if (!loc) + __throw_runtime_error("moneypunct_byname" " failed to construct for " + string(nm)); -#endif // _LIBCPP_NO_EXCEPTIONS + lconv* lc = __libcpp_localeconv_l(loc.get()); - if (*lc->mon_decimal_point) - __decimal_point_ = *lc->mon_decimal_point; - else - __decimal_point_ = base::do_decimal_point(); - if (*lc->mon_thousands_sep) - __thousands_sep_ = *lc->mon_thousands_sep; - else - __thousands_sep_ = base::do_thousands_sep(); + if (!checked_string_to_char_convert(__decimal_point_, + lc->mon_decimal_point, + loc.get())) + __decimal_point_ = base::do_decimal_point(); + if (!checked_string_to_char_convert(__thousands_sep_, + lc->mon_thousands_sep, + loc.get())) + __thousands_sep_ = base::do_thousands_sep(); + __grouping_ = lc->mon_grouping; __curr_symbol_ = lc->currency_symbol; if (lc->frac_digits != CHAR_MAX) @@ -5835,21 +5884,20 @@ void 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 == nullptr) - throw runtime_error("moneypunct_byname" + __libcpp_unique_locale loc(nm); + if (!loc) + __throw_runtime_error("moneypunct_byname" " failed to construct for " + string(nm)); -#endif // _LIBCPP_NO_EXCEPTIONS + lconv* lc = __libcpp_localeconv_l(loc.get()); - if (*lc->mon_decimal_point) - __decimal_point_ = *lc->mon_decimal_point; - else - __decimal_point_ = base::do_decimal_point(); - if (*lc->mon_thousands_sep) - __thousands_sep_ = *lc->mon_thousands_sep; - else - __thousands_sep_ = base::do_thousands_sep(); + if (!checked_string_to_char_convert(__decimal_point_, + lc->mon_decimal_point, + loc.get())) + __decimal_point_ = base::do_decimal_point(); + if (!checked_string_to_char_convert(__thousands_sep_, + lc->mon_thousands_sep, + loc.get())) + __thousands_sep_ = base::do_thousands_sep(); __grouping_ = lc->mon_grouping; __curr_symbol_ = lc->int_curr_symbol; if (lc->int_frac_digits != CHAR_MAX) @@ -5896,21 +5944,19 @@ void 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 == nullptr) - throw runtime_error("moneypunct_byname" + __libcpp_unique_locale loc(nm); + if (!loc) + __throw_runtime_error("moneypunct_byname" " failed to construct for " + string(nm)); -#endif // _LIBCPP_NO_EXCEPTIONS lconv* lc = __libcpp_localeconv_l(loc.get()); - if (*lc->mon_decimal_point) - __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point); - else - __decimal_point_ = base::do_decimal_point(); - if (*lc->mon_thousands_sep) - __thousands_sep_ = static_cast<wchar_t>(*lc->mon_thousands_sep); - else - __thousands_sep_ = base::do_thousands_sep(); + if (!checked_string_to_wchar_convert(__decimal_point_, + lc->mon_decimal_point, + loc.get())) + __decimal_point_ = base::do_decimal_point(); + if (!checked_string_to_wchar_convert(__thousands_sep_, + lc->mon_thousands_sep, + loc.get())) + __thousands_sep_ = base::do_thousands_sep(); __grouping_ = lc->mon_grouping; wchar_t wbuf[100]; mbstate_t mb = {0}; @@ -5963,21 +6009,20 @@ void 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 == nullptr) - throw runtime_error("moneypunct_byname" + __libcpp_unique_locale loc(nm); + if (!loc) + __throw_runtime_error("moneypunct_byname" " failed to construct for " + string(nm)); -#endif // _LIBCPP_NO_EXCEPTIONS + lconv* lc = __libcpp_localeconv_l(loc.get()); - if (*lc->mon_decimal_point) - __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point); - else - __decimal_point_ = base::do_decimal_point(); - if (*lc->mon_thousands_sep) - __thousands_sep_ = static_cast<wchar_t>(*lc->mon_thousands_sep); - else - __thousands_sep_ = base::do_thousands_sep(); + if (!checked_string_to_wchar_convert(__decimal_point_, + lc->mon_decimal_point, + loc.get())) + __decimal_point_ = base::do_decimal_point(); + if (!checked_string_to_wchar_convert(__thousands_sep_, + lc->mon_thousands_sep, + loc.get())) + __thousands_sep_ = base::do_thousands_sep(); __grouping_ = lc->mon_grouping; wchar_t wbuf[100]; mbstate_t mb = {0}; @@ -6050,69 +6095,68 @@ void __throw_runtime_error(const char* msg) throw runtime_error(msg); #else (void)msg; + _VSTD::abort(); #endif } -template class collate<char>; -template class collate<wchar_t>; - -template class num_get<char>; -template class num_get<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS collate<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS collate<wchar_t>; -template struct __num_get<char>; -template struct __num_get<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS num_get<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS num_get<wchar_t>; -template class num_put<char>; -template class num_put<wchar_t>; +template struct _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __num_get<char>; +template struct _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __num_get<wchar_t>; -template struct __num_put<char>; -template struct __num_put<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS num_put<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS num_put<wchar_t>; -template class time_get<char>; -template class time_get<wchar_t>; +template struct _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __num_put<char>; +template struct _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __num_put<wchar_t>; -template class time_get_byname<char>; -template class time_get_byname<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_get<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_get<wchar_t>; -template class time_put<char>; -template class time_put<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_get_byname<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_get_byname<wchar_t>; -template class time_put_byname<char>; -template class time_put_byname<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_put<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_put<wchar_t>; -template class moneypunct<char, false>; -template class moneypunct<char, true>; -template class moneypunct<wchar_t, false>; -template class moneypunct<wchar_t, true>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_put_byname<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_put_byname<wchar_t>; -template class moneypunct_byname<char, false>; -template class moneypunct_byname<char, true>; -template class moneypunct_byname<wchar_t, false>; -template class moneypunct_byname<wchar_t, true>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct<char, false>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct<char, true>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct<wchar_t, false>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct<wchar_t, true>; -template class money_get<char>; -template class money_get<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct_byname<char, false>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct_byname<char, true>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct_byname<wchar_t, false>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct_byname<wchar_t, true>; -template class __money_get<char>; -template class __money_get<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS money_get<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS money_get<wchar_t>; -template class money_put<char>; -template class money_put<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __money_get<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __money_get<wchar_t>; -template class __money_put<char>; -template class __money_put<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS money_put<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS money_put<wchar_t>; -template class messages<char>; -template class messages<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __money_put<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __money_put<wchar_t>; -template class messages_byname<char>; -template class messages_byname<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS messages<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS messages<wchar_t>; -template class codecvt_byname<char, char, mbstate_t>; -template class codecvt_byname<wchar_t, char, mbstate_t>; -template class codecvt_byname<char16_t, char, mbstate_t>; -template class codecvt_byname<char32_t, char, mbstate_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS messages_byname<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS messages_byname<wchar_t>; -template class __vector_base_common<true>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS codecvt_byname<char, char, mbstate_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS codecvt_byname<wchar_t, char, mbstate_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS codecvt_byname<char16_t, char, mbstate_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS codecvt_byname<char32_t, char, mbstate_t>; _LIBCPP_END_NAMESPACE_STD diff --git a/lib/libcxx/src/memory.cpp b/lib/libcxx/src/memory.cpp index 15e947fc0ff..4e0d3af9167 100644 --- a/lib/libcxx/src/memory.cpp +++ b/lib/libcxx/src/memory.cpp @@ -17,28 +17,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD -namespace -{ - -// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively) -// should be sufficient for thread safety. -// See https://llvm.org/bugs/show_bug.cgi?id=22803 -template <class T> -inline T -increment(T& t) _NOEXCEPT -{ - return __libcpp_atomic_add(&t, 1, _AO_Relaxed); -} - -template <class T> -inline T -decrement(T& t) _NOEXCEPT -{ - return __libcpp_atomic_add(&t, -1, _AO_Acq_Rel); -} - -} // namespace - const allocator_arg_t allocator_arg = allocator_arg_t(); bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {} @@ -53,16 +31,21 @@ __shared_count::~__shared_count() { } +__shared_weak_count::~__shared_weak_count() +{ +} + +#if defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) void __shared_count::__add_shared() _NOEXCEPT { - increment(__shared_owners_); + __libcpp_atomic_refcount_increment(__shared_owners_); } bool __shared_count::__release_shared() _NOEXCEPT { - if (decrement(__shared_owners_) == -1) + if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) { __on_zero_shared(); return true; @@ -70,10 +53,6 @@ __shared_count::__release_shared() _NOEXCEPT return false; } -__shared_weak_count::~__shared_weak_count() -{ -} - void __shared_weak_count::__add_shared() _NOEXCEPT { @@ -83,7 +62,7 @@ __shared_weak_count::__add_shared() _NOEXCEPT void __shared_weak_count::__add_weak() _NOEXCEPT { - increment(__shared_weak_owners_); + __libcpp_atomic_refcount_increment(__shared_weak_owners_); } void @@ -93,10 +72,40 @@ __shared_weak_count::__release_shared() _NOEXCEPT __release_weak(); } +#endif // _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS + void __shared_weak_count::__release_weak() _NOEXCEPT { - if (decrement(__shared_weak_owners_) == -1) + // NOTE: The acquire load here is an optimization of the very + // common case where a shared pointer is being destructed while + // having no other contended references. + // + // BENEFIT: We avoid expensive atomic stores like XADD and STREX + // in a common case. Those instructions are slow and do nasty + // things to caches. + // + // IS THIS SAFE? Yes. During weak destruction, if we see that we + // are the last reference, we know that no-one else is accessing + // us. If someone were accessing us, then they would be doing so + // while the last shared / weak_ptr was being destructed, and + // that's undefined anyway. + // + // If we see anything other than a 0, then we have possible + // contention, and need to use an atomicrmw primitive. + // The same arguments don't apply for increment, where it is legal + // (though inadvisable) to share shared_ptr references between + // threads, and have them all get copied at once. The argument + // also doesn't apply for __release_shared, because an outstanding + // weak_ptr::lock() could read / modify the shared count. + if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0) + { + // no need to do this store, because we are about + // to destroy everything. + //__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release); + __on_zero_shared_weak(); + } + else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1) __on_zero_shared_weak(); } @@ -111,7 +120,7 @@ __shared_weak_count::lock() _NOEXCEPT object_owners+1)) return this; } - return 0; + return nullptr; } #if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC) @@ -119,15 +128,15 @@ __shared_weak_count::lock() _NOEXCEPT const void* __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT { - return 0; + return nullptr; } #endif // _LIBCPP_NO_RTTI #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) -static const std::size_t __sp_mut_count = 16; -static __libcpp_mutex_t mut_back_imp[__sp_mut_count] = +_LIBCPP_SAFE_STATIC static const std::size_t __sp_mut_count = 16; +_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut_back[__sp_mut_count] = { _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, @@ -135,8 +144,6 @@ static __libcpp_mutex_t mut_back_imp[__sp_mut_count] = _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER }; -static mutex* mut_back = reinterpret_cast<std::mutex*>(mut_back_imp); - _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT : __lx(p) { @@ -145,13 +152,13 @@ _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT void __sp_mut::lock() _NOEXCEPT { - mutex& m = *static_cast<mutex*>(__lx); + auto m = static_cast<__libcpp_mutex_t*>(__lx); unsigned count = 0; - while (!m.try_lock()) + while (!__libcpp_mutex_trylock(m)) { if (++count > 16) { - m.lock(); + __libcpp_mutex_lock(m); break; } this_thread::yield(); @@ -161,13 +168,13 @@ __sp_mut::lock() _NOEXCEPT void __sp_mut::unlock() _NOEXCEPT { - static_cast<mutex*>(__lx)->unlock(); + __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx)); } __sp_mut& __get_sp_mut(const void* p) { - static __sp_mut muts[__sp_mut_count] + 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], @@ -194,11 +201,12 @@ undeclare_no_pointers(char*, size_t) { } -pointer_safety -get_pointer_safety() _NOEXCEPT +#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) +pointer_safety get_pointer_safety() _NOEXCEPT { return pointer_safety::relaxed; } +#endif void* __undeclare_reachable(void* p) diff --git a/lib/libcxx/src/mutex.cpp b/lib/libcxx/src/mutex.cpp index 9f808ca5076..c36bd5549da 100644 --- a/lib/libcxx/src/mutex.cpp +++ b/lib/libcxx/src/mutex.cpp @@ -11,8 +11,8 @@ #include "mutex" #include "limits" #include "system_error" -#include "cassert" #include "include/atomic_support.h" +#include "__undef_macros" _LIBCPP_BEGIN_NAMESPACE_STD #ifndef _LIBCPP_HAS_NO_THREADS @@ -37,7 +37,7 @@ mutex::lock() bool mutex::try_lock() _NOEXCEPT { - return __libcpp_mutex_trylock(&__m_) == 0; + return __libcpp_mutex_trylock(&__m_); } void @@ -45,7 +45,7 @@ mutex::unlock() _NOEXCEPT { int ec = __libcpp_mutex_unlock(&__m_); (void)ec; - assert(ec == 0); + _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed"); } // recursive_mutex @@ -59,15 +59,15 @@ recursive_mutex::recursive_mutex() recursive_mutex::~recursive_mutex() { - int e = __libcpp_mutex_destroy(&__m_); + int e = __libcpp_recursive_mutex_destroy(&__m_); (void)e; - assert(e == 0); + _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed"); } void recursive_mutex::lock() { - int ec = __libcpp_mutex_lock(&__m_); + int ec = __libcpp_recursive_mutex_lock(&__m_); if (ec) __throw_system_error(ec, "recursive_mutex lock failed"); } @@ -75,15 +75,15 @@ recursive_mutex::lock() void recursive_mutex::unlock() _NOEXCEPT { - int e = __libcpp_mutex_unlock(&__m_); + int e = __libcpp_recursive_mutex_unlock(&__m_); (void)e; - assert(e == 0); + _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed"); } bool recursive_mutex::try_lock() _NOEXCEPT { - return __libcpp_mutex_trylock(&__m_) == 0; + return __libcpp_recursive_mutex_trylock(&__m_); } // timed_mutex @@ -195,13 +195,10 @@ recursive_timed_mutex::unlock() _NOEXCEPT // keep in sync with: 7741191. #ifndef _LIBCPP_HAS_NO_THREADS -static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER; -static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER; +_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER; +_LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER; #endif -/// NOTE: Changes to flag are done via relaxed atomic stores -/// even though the accesses are protected by a mutex because threads -/// just entering 'call_once` concurrently read from flag. void __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*)) { @@ -238,7 +235,7 @@ __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*)) __libcpp_mutex_unlock(&mut); func(arg); __libcpp_mutex_lock(&mut); - __libcpp_relaxed_store(&flag, ~0ul); + __libcpp_atomic_store(&flag, ~0ul, _AO_Release); __libcpp_mutex_unlock(&mut); __libcpp_condvar_broadcast(&cv); #ifndef _LIBCPP_NO_EXCEPTIONS diff --git a/lib/libcxx/src/new.cpp b/lib/libcxx/src/new.cpp index f4f73d86803..e228a0d83d8 100644 --- a/lib/libcxx/src/new.cpp +++ b/lib/libcxx/src/new.cpp @@ -12,37 +12,61 @@ #include <stdlib.h> #include "new" +#include "include/atomic_support.h" -#if defined(__APPLE__) && !defined(LIBCXXRT) - #include <cxxabi.h> - - #ifndef _LIBCPPABI_VERSION - // On Darwin, there are two STL shared libraries and a lower level ABI - // shared library. 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__ - #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI) - #include <cxxabi.h> - #endif // defined(LIBCXX_BUILDING_LIBCXXABI) - #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) - static std::new_handler __new_handler; - #endif // _LIBCPPABI_VERSION +#if defined(_LIBCPP_ABI_MICROSOFT) +#if defined(_LIBCPP_NO_VCRUNTIME) +#include "support/runtime/new_handler_fallback.ipp" +#endif +#elif defined(LIBCXX_BUILDING_LIBCXXABI) +#include <cxxabi.h> +#elif defined(LIBCXXRT) +#include <cxxabi.h> +#include "support/runtime/new_handler_fallback.ipp" +#elif defined(__GLIBCXX__) +// nothing todo +#else +# if defined(__APPLE__) && !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) +# include <cxxabi.h> // FIXME: remove this once buildit is gone. +# else +# include "support/runtime/new_handler_fallback.ipp" +# endif #endif +namespace std +{ + #ifndef __GLIBCXX__ +const nothrow_t nothrow = {}; +#endif + +#ifndef LIBSTDCXX + +void +__throw_bad_alloc() +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + throw bad_alloc(); +#else + _VSTD::abort(); +#endif +} + +#endif // !LIBSTDCXX + +} // std + +#if !defined(__GLIBCXX__) && \ + (!defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)) && \ + !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS) // Implement all new and delete operators as weak definitions // in this shared library, so that they can be overridden by programs // that define non-weak copies of the functions. -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void * -operator new(std::size_t size) -#if !__has_feature(cxx_noexcept) - throw(std::bad_alloc) -#endif +operator new(std::size_t size) _THROW_BAD_ALLOC { if (size == 0) size = 1; @@ -64,7 +88,7 @@ operator new(std::size_t size) return p; } -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void* operator new(size_t size, const std::nothrow_t&) _NOEXCEPT { @@ -83,17 +107,14 @@ operator new(size_t size, const std::nothrow_t&) _NOEXCEPT return p; } -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void* -operator new[](size_t size) -#if !__has_feature(cxx_noexcept) - throw(std::bad_alloc) -#endif +operator new[](size_t size) _THROW_BAD_ALLOC { return ::operator new(size); } -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void* operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT { @@ -112,7 +133,7 @@ operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT return p; } -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void operator delete(void* ptr) _NOEXCEPT { @@ -120,130 +141,166 @@ operator delete(void* ptr) _NOEXCEPT ::free(ptr); } -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT { ::operator delete(ptr); } -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void operator delete(void* ptr, size_t) _NOEXCEPT { ::operator delete(ptr); } -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void operator delete[] (void* ptr) _NOEXCEPT { ::operator delete(ptr); } -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT { ::operator delete[](ptr); } -_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS +_LIBCPP_WEAK void operator delete[] (void* ptr, size_t) _NOEXCEPT { ::operator delete[](ptr); } -#endif // !__GLIBCXX__ +#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) -namespace std +_LIBCPP_WEAK +void * +operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC { - -#ifndef __GLIBCXX__ -const nothrow_t nothrow = {}; + if (size == 0) + size = 1; + if (static_cast<size_t>(alignment) < sizeof(void*)) + alignment = std::align_val_t(sizeof(void*)); + void* p; +#if defined(_LIBCPP_MSVCRT_LIKE) + while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr) +#else + while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0) #endif - -#ifndef _LIBCPPABI_VERSION - -#ifndef __GLIBCXX__ - -new_handler -set_new_handler(new_handler handler) _NOEXCEPT -{ - return __sync_lock_test_and_set(&__new_handler, handler); -} - -new_handler -get_new_handler() _NOEXCEPT -{ - return __sync_fetch_and_add(&__new_handler, nullptr); -} - -#endif // !__GLIBCXX__ - -#ifndef LIBCXXRT - -bad_alloc::bad_alloc() _NOEXCEPT -{ + { + // If posix_memalign fails and there is a new_handler, + // call it to try free up memory. + std::new_handler nh = std::get_new_handler(); + if (nh) + nh(); + else { +#ifndef _LIBCPP_NO_EXCEPTIONS + throw std::bad_alloc(); +#else + p = nullptr; // posix_memalign doesn't initialize 'p' on failure + break; +#endif + } + } + return p; } -#ifndef __GLIBCXX__ - -bad_alloc::~bad_alloc() _NOEXCEPT +_LIBCPP_WEAK +void* +operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT { + void* p = 0; +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + p = ::operator new(size, alignment); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + } +#endif // _LIBCPP_NO_EXCEPTIONS + return p; } -const char* -bad_alloc::what() const _NOEXCEPT +_LIBCPP_WEAK +void* +operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC { - return "std::bad_alloc"; + return ::operator new(size, alignment); } -#endif // !__GLIBCXX__ - -bad_array_new_length::bad_array_new_length() _NOEXCEPT +_LIBCPP_WEAK +void* +operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT { + void* p = 0; +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + p = ::operator new[](size, alignment); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + } +#endif // _LIBCPP_NO_EXCEPTIONS + return p; } -bad_array_new_length::~bad_array_new_length() _NOEXCEPT +_LIBCPP_WEAK +void +operator delete(void* ptr, std::align_val_t) _NOEXCEPT { + if (ptr) +#if defined(_LIBCPP_MSVCRT_LIKE) + ::_aligned_free(ptr); +#else + ::free(ptr); +#endif } -const char* -bad_array_new_length::what() const _NOEXCEPT +_LIBCPP_WEAK +void +operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT { - return "bad_array_new_length"; + ::operator delete(ptr, alignment); } -#endif //LIBCXXRT - -const char* -bad_array_length::what() const _NOEXCEPT +_LIBCPP_WEAK +void +operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT { - return "bad_array_length"; + ::operator delete(ptr, alignment); } -bad_array_length::bad_array_length() _NOEXCEPT +_LIBCPP_WEAK +void +operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT { + ::operator delete(ptr, alignment); } -bad_array_length::~bad_array_length() _NOEXCEPT +_LIBCPP_WEAK +void +operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT { + ::operator delete[](ptr, alignment); } -#endif // _LIBCPPABI_VERSION - -#ifndef LIBSTDCXX - +_LIBCPP_WEAK void -__throw_bad_alloc() +operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT { -#ifndef _LIBCPP_NO_EXCEPTIONS - throw bad_alloc(); -#endif + ::operator delete[](ptr, alignment); } -#endif // !LIBSTDCXX - -} // std +#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION +#endif // !__GLIBCXX__ && (!_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME) && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS diff --git a/lib/libcxx/src/optional.cpp b/lib/libcxx/src/optional.cpp index 8c5dd76d86d..2877d175bc1 100644 --- a/lib/libcxx/src/optional.cpp +++ b/lib/libcxx/src/optional.cpp @@ -7,18 +7,22 @@ // //===----------------------------------------------------------------------===// +#include "optional" #include "experimental/optional" -_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL +namespace std +{ -#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS +bad_optional_access::~bad_optional_access() _NOEXCEPT = default; -bad_optional_access::~bad_optional_access() _NOEXCEPT {} +const char* bad_optional_access::what() const _NOEXCEPT { + return "bad_optional_access"; + } -#else +} // std -bad_optional_access::~bad_optional_access() _NOEXCEPT = default; +_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL -#endif +bad_optional_access::~bad_optional_access() _NOEXCEPT = default; _LIBCPP_END_NAMESPACE_EXPERIMENTAL diff --git a/lib/libcxx/src/random.cpp b/lib/libcxx/src/random.cpp index 4ab424eaa6e..4a2468368d0 100644 --- a/lib/libcxx/src/random.cpp +++ b/lib/libcxx/src/random.cpp @@ -7,6 +7,8 @@ // //===----------------------------------------------------------------------===// +#include <__config> + #if defined(_LIBCPP_USING_WIN32_RANDOM) // Must be defined before including stdlib.h to enable rand_s(). #define _CRT_RAND_S @@ -23,7 +25,9 @@ #include <stdio.h> #include <stdlib.h> -#if defined(_LIBCPP_USING_DEV_RANDOM) +#if defined(_LIBCPP_USING_GETENTROPY) +#include <sys/random.h> +#elif defined(_LIBCPP_USING_DEV_RANDOM) #include <fcntl.h> #include <unistd.h> #elif defined(_LIBCPP_USING_NACL_RANDOM) @@ -33,7 +37,30 @@ _LIBCPP_BEGIN_NAMESPACE_STD -#if defined(_LIBCPP_USING_ARC4_RANDOM) +#if defined(_LIBCPP_USING_GETENTROPY) + +random_device::random_device(const string& __token) +{ + if (__token != "/dev/urandom") + __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); +} + +random_device::~random_device() +{ +} + +unsigned +random_device::operator()() +{ + unsigned r; + size_t n = sizeof(r); + int err = getentropy(&r, n); + if (err) + __throw_system_error(errno, "random_device getentropy failed"); + return r; +} + +#elif defined(_LIBCPP_USING_ARC4_RANDOM) random_device::random_device(const string& __token) { diff --git a/lib/libcxx/src/stdexcept.cpp b/lib/libcxx/src/stdexcept.cpp index 0a08bfec27e..5e06e521e40 100644 --- a/lib/libcxx/src/stdexcept.cpp +++ b/lib/libcxx/src/stdexcept.cpp @@ -7,19 +7,21 @@ // //===----------------------------------------------------------------------===// -#include "__refstring" #include "stdexcept" #include "new" #include "string" #include "system_error" +#include "include/refstring.h" /* For _LIBCPPABI_VERSION */ -#if defined(LIBCXX_BUILDING_LIBCXXABI) || defined(__APPLE__) || defined(LIBCXXRT) +#if !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) && \ + (defined(LIBCXX_BUILDING_LIBCXXABI) || defined(__APPLE__) || defined(LIBCXXRT)) #include <cxxabi.h> #endif static_assert(sizeof(std::__libcpp_refstring) == sizeof(const char *), ""); + namespace std // purposefully not using versioning namespace { diff --git a/lib/libcxx/src/string.cpp b/lib/libcxx/src/string.cpp index d3f29df639f..d7ebdd3e5c9 100644 --- a/lib/libcxx/src/string.cpp +++ b/lib/libcxx/src/string.cpp @@ -13,17 +13,14 @@ #include "cerrno" #include "limits" #include "stdexcept" -#ifdef _LIBCPP_MSVCRT -#include "support/win32/support.h" -#endif // _LIBCPP_MSVCRT #include <stdio.h> _LIBCPP_BEGIN_NAMESPACE_STD -template class __basic_string_common<true>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __basic_string_common<true>; -template class basic_string<char>; -template class basic_string<wchar_t>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<char>; +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<wchar_t>; template string @@ -40,7 +37,7 @@ void throw_helper( const string& msg ) throw T( msg ); #else fprintf(stderr, "%s\n", msg.c_str()); - abort(); + _VSTD::abort(); #endif } @@ -430,7 +427,7 @@ get_swprintf() #ifndef _LIBCPP_MSVCRT return swprintf; #else - return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(swprintf); + return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf); #endif } diff --git a/lib/libcxx/src/strstream.cpp b/lib/libcxx/src/strstream.cpp index 0e2d7ff21bb..8b8521f76af 100644 --- a/lib/libcxx/src/strstream.cpp +++ b/lib/libcxx/src/strstream.cpp @@ -11,7 +11,9 @@ #include "algorithm" #include "climits" #include "cstring" +#include "cstdlib" #include "__debug" +#include "__undef_macros" _LIBCPP_BEGIN_NAMESPACE_STD @@ -184,7 +186,7 @@ strstreambuf::overflow(int_type __c) } setg(buf, buf + ninp, buf + einp); setp(buf + einp, buf + new_size); - pbump(static_cast<int>(nout)); + __pbump(nout); __strmode_ |= __allocated; } *pptr() = static_cast<char>(__c); @@ -266,6 +268,8 @@ strstreambuf::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmod case ios::end: newoff = seekhigh - eback(); break; + default: + _LIBCPP_UNREACHABLE(); } newoff += __off; if (0 <= newoff && newoff <= seekhigh - eback()) @@ -278,7 +282,7 @@ strstreambuf::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmod // min(pbase, newpos), newpos, epptr() __off = epptr() - newpos; setp(min(pbase(), newpos), epptr()); - pbump(static_cast<int>((epptr() - pbase()) - __off)); + __pbump((epptr() - pbase()) - __off); } __p = newoff; } @@ -308,7 +312,7 @@ strstreambuf::seekpos(pos_type __sp, ios_base::openmode __which) // min(pbase, newpos), newpos, epptr() off_type temp = epptr() - newpos; setp(min(pbase(), newpos), epptr()); - pbump(static_cast<int>((epptr() - pbase()) - temp)); + __pbump((epptr() - pbase()) - temp); } __p = newoff; } diff --git a/lib/libcxx/src/support/runtime/exception_fallback.ipp b/lib/libcxx/src/support/runtime/exception_fallback.ipp new file mode 100644 index 00000000000..664e7f48c09 --- /dev/null +++ b/lib/libcxx/src/support/runtime/exception_fallback.ipp @@ -0,0 +1,181 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 <cstdio> + +namespace std { + +_LIBCPP_SAFE_STATIC static std::terminate_handler __terminate_handler; +_LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler; + + +// libcxxrt provides implementations of these functions itself. +unexpected_handler +set_unexpected(unexpected_handler func) _NOEXCEPT +{ + return __libcpp_atomic_exchange(&__unexpected_handler, func); +} + +unexpected_handler +get_unexpected() _NOEXCEPT +{ + return __libcpp_atomic_load(&__unexpected_handler); + +} + +_LIBCPP_NORETURN +void unexpected() +{ + (*get_unexpected())(); + // unexpected handler should not return + terminate(); +} + +terminate_handler +set_terminate(terminate_handler func) _NOEXCEPT +{ + return __libcpp_atomic_exchange(&__terminate_handler, func); +} + +terminate_handler +get_terminate() _NOEXCEPT +{ + return __libcpp_atomic_load(&__terminate_handler); +} + +#ifndef __EMSCRIPTEN__ // We provide this in JS +_LIBCPP_NORETURN +void +terminate() _NOEXCEPT +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + (*get_terminate())(); + // handler should not return + fprintf(stderr, "terminate_handler unexpectedly returned\n"); + ::abort(); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + // handler should not throw exception + fprintf(stderr, "terminate_handler unexpectedly threw an exception\n"); + ::abort(); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} +#endif // !__EMSCRIPTEN__ + +#if !defined(__EMSCRIPTEN__) +bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } + +int uncaught_exceptions() _NOEXCEPT +{ +#warning uncaught_exception not yet implemented + fprintf(stderr, "uncaught_exceptions not yet implemented\n"); + ::abort(); +} +#endif // !__EMSCRIPTEN__ + + +exception::~exception() _NOEXCEPT +{ +} + +const char* exception::what() const _NOEXCEPT +{ + return "std::exception"; +} + +bad_exception::~bad_exception() _NOEXCEPT +{ +} + +const char* bad_exception::what() const _NOEXCEPT +{ + return "std::bad_exception"; +} + + +bad_alloc::bad_alloc() _NOEXCEPT +{ +} + +bad_alloc::~bad_alloc() _NOEXCEPT +{ +} + +const char* +bad_alloc::what() const _NOEXCEPT +{ + return "std::bad_alloc"; +} + +bad_array_new_length::bad_array_new_length() _NOEXCEPT +{ +} + +bad_array_new_length::~bad_array_new_length() _NOEXCEPT +{ +} + +const char* +bad_array_new_length::what() const _NOEXCEPT +{ + return "bad_array_new_length"; +} + + +bad_array_length::bad_array_length() _NOEXCEPT +{ +} + +bad_array_length::~bad_array_length() _NOEXCEPT +{ +} + +const char* +bad_array_length::what() const _NOEXCEPT +{ + return "bad_array_length"; +} + + +bad_cast::bad_cast() _NOEXCEPT +{ +} + +bad_typeid::bad_typeid() _NOEXCEPT +{ +} + +bad_cast::~bad_cast() _NOEXCEPT +{ +} + +const char* +bad_cast::what() const _NOEXCEPT +{ + return "std::bad_cast"; +} + +bad_typeid::~bad_typeid() _NOEXCEPT +{ +} + +const char* +bad_typeid::what() const _NOEXCEPT +{ + return "std::bad_typeid"; +} + +} // namespace std diff --git a/lib/libcxx/src/support/runtime/exception_glibcxx.ipp b/lib/libcxx/src/support/runtime/exception_glibcxx.ipp new file mode 100644 index 00000000000..0f78932f6f9 --- /dev/null +++ b/lib/libcxx/src/support/runtime/exception_glibcxx.ipp @@ -0,0 +1,38 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#ifndef __GLIBCXX__ +#error header can only be used when targeting libstdc++ or libsupc++ +#endif + +namespace std { + +bad_alloc::bad_alloc() _NOEXCEPT +{ +} + +bad_array_new_length::bad_array_new_length() _NOEXCEPT +{ +} + +bad_array_length::bad_array_length() _NOEXCEPT +{ +} + + +bad_cast::bad_cast() _NOEXCEPT +{ +} + +bad_typeid::bad_typeid() _NOEXCEPT +{ +} + +} // namespace std diff --git a/lib/libcxx/src/support/runtime/exception_libcxxabi.ipp b/lib/libcxx/src/support/runtime/exception_libcxxabi.ipp new file mode 100644 index 00000000000..c3dcf1ec591 --- /dev/null +++ b/lib/libcxx/src/support/runtime/exception_libcxxabi.ipp @@ -0,0 +1,28 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPPABI_VERSION +#error this header can only be used with libc++abi +#endif + +namespace std { + +bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } + +int uncaught_exceptions() _NOEXCEPT +{ +# if _LIBCPPABI_VERSION > 1101 + return __cxa_uncaught_exceptions(); +# else + return __cxa_uncaught_exception() ? 1 : 0; +# endif +} + +} // namespace std diff --git a/lib/libcxx/src/support/runtime/exception_libcxxrt.ipp b/lib/libcxx/src/support/runtime/exception_libcxxrt.ipp new file mode 100644 index 00000000000..6d9e0cff58d --- /dev/null +++ b/lib/libcxx/src/support/runtime/exception_libcxxrt.ipp @@ -0,0 +1,41 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#ifndef LIBCXXRT +#error this header may only be used when targeting libcxxrt +#endif + +namespace std { + +bad_exception::~bad_exception() _NOEXCEPT +{ +} + +const char* bad_exception::what() const _NOEXCEPT +{ + return "std::bad_exception"; +} + + +bad_array_length::bad_array_length() _NOEXCEPT +{ +} + +bad_array_length::~bad_array_length() _NOEXCEPT +{ +} + +const char* +bad_array_length::what() const _NOEXCEPT +{ + return "bad_array_length"; +} + +} // namespace std diff --git a/lib/libcxx/src/support/runtime/exception_msvc.ipp b/lib/libcxx/src/support/runtime/exception_msvc.ipp new file mode 100644 index 00000000000..d5bf5b726ea --- /dev/null +++ b/lib/libcxx/src/support/runtime/exception_msvc.ipp @@ -0,0 +1,190 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_ABI_MICROSOFT +#error this header can only be used when targeting the MSVC ABI +#endif + +#include <stdio.h> +#include <stdlib.h> + +#if !defined(_ACRTIMP) +#define _ACRTIMP __declspec(dllimport) +#endif + +#if !defined(_VCRTIMP) +#define _VCRTIMP __declspec(dllimport) +#endif + +#if !defined(__CRTDECL) +#define __CRTDECL __cdecl +#endif + +extern "C" { +typedef void (__CRTDECL* terminate_handler)(); +_ACRTIMP terminate_handler __cdecl set_terminate( + terminate_handler _NewTerminateHandler) throw(); +_ACRTIMP terminate_handler __cdecl _get_terminate(); + +typedef void (__CRTDECL* unexpected_handler)(); +_VCRTIMP unexpected_handler __cdecl set_unexpected( + unexpected_handler _NewUnexpectedHandler) throw(); +_VCRTIMP unexpected_handler __cdecl _get_unexpected(); + +_VCRTIMP int __cdecl __uncaught_exceptions(); +} + +namespace std { + +unexpected_handler +set_unexpected(unexpected_handler func) _NOEXCEPT { + return ::set_unexpected(func); +} + +unexpected_handler get_unexpected() _NOEXCEPT { + return ::_get_unexpected(); +} + +_LIBCPP_NORETURN +void unexpected() { + (*get_unexpected())(); + // unexpected handler should not return + terminate(); +} + +terminate_handler set_terminate(terminate_handler func) _NOEXCEPT { + return ::set_terminate(func); +} + +terminate_handler get_terminate() _NOEXCEPT { + return ::_get_terminate(); +} + +_LIBCPP_NORETURN +void terminate() _NOEXCEPT +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + (*get_terminate())(); + // handler should not return + fprintf(stderr, "terminate_handler unexpectedly returned\n"); + ::abort(); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + // handler should not throw exception + fprintf(stderr, "terminate_handler unexpectedly threw an exception\n"); + ::abort(); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} + +bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } + +int uncaught_exceptions() _NOEXCEPT { + return __uncaught_exceptions(); +} + +bad_array_length::bad_array_length() _NOEXCEPT +{ +} + +bad_array_length::~bad_array_length() _NOEXCEPT +{ +} + +const char* +bad_array_length::what() const _NOEXCEPT +{ + return "bad_array_length"; +} + +bad_cast::bad_cast() _NOEXCEPT +{ +} + +bad_cast::~bad_cast() _NOEXCEPT +{ +} + +const char * +bad_cast::what() const _NOEXCEPT +{ + return "std::bad_cast"; +} + +bad_typeid::bad_typeid() _NOEXCEPT +{ +} + +bad_typeid::~bad_typeid() _NOEXCEPT +{ +} + +const char * +bad_typeid::what() const _NOEXCEPT +{ + return "std::bad_typeid"; +} + +#if defined(_LIBCPP_NO_VCRUNTIME) +exception::~exception() _NOEXCEPT +{ +} + +const char* exception::what() const _NOEXCEPT +{ + return "std::exception"; +} + + +bad_exception::~bad_exception() _NOEXCEPT +{ +} + +const char* bad_exception::what() const _NOEXCEPT +{ + return "std::bad_exception"; +} + + +bad_alloc::bad_alloc() _NOEXCEPT +{ +} + +bad_alloc::~bad_alloc() _NOEXCEPT +{ +} + +const char* +bad_alloc::what() const _NOEXCEPT +{ + return "std::bad_alloc"; +} + +bad_array_new_length::bad_array_new_length() _NOEXCEPT +{ +} + +bad_array_new_length::~bad_array_new_length() _NOEXCEPT +{ +} + +const char* +bad_array_new_length::what() const _NOEXCEPT +{ + return "bad_array_new_length"; +} +#endif // _LIBCPP_NO_VCRUNTIME + +} // namespace std diff --git a/lib/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/lib/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp new file mode 100644 index 00000000000..dfac8648c49 --- /dev/null +++ b/lib/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp @@ -0,0 +1,74 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#ifndef HAVE_DEPENDENT_EH_ABI +#error this header may only be used with libc++abi or libcxxrt +#endif + +namespace std { + +exception_ptr::~exception_ptr() _NOEXCEPT { + __cxa_decrement_exception_refcount(__ptr_); +} + +exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT + : __ptr_(other.__ptr_) +{ + __cxa_increment_exception_refcount(__ptr_); +} + +exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT +{ + if (__ptr_ != other.__ptr_) + { + __cxa_increment_exception_refcount(other.__ptr_); + __cxa_decrement_exception_refcount(__ptr_); + __ptr_ = other.__ptr_; + } + return *this; +} + +nested_exception::nested_exception() _NOEXCEPT + : __ptr_(current_exception()) +{ +} + +nested_exception::~nested_exception() _NOEXCEPT +{ +} + +_LIBCPP_NORETURN +void +nested_exception::rethrow_nested() const +{ + if (__ptr_ == nullptr) + terminate(); + rethrow_exception(__ptr_); +} + +exception_ptr current_exception() _NOEXCEPT +{ + // 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()); + exception_ptr ptr; + ptr.__ptr_ = __cxa_current_primary_exception(); + return ptr; +} + +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) +{ + __cxa_rethrow_primary_exception(p.__ptr_); + // if p.__ptr_ is NULL, above returns so we terminate + terminate(); +} + +} // namespace std diff --git a/lib/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp b/lib/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp new file mode 100644 index 00000000000..9d20dfe4862 --- /dev/null +++ b/lib/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp @@ -0,0 +1,78 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// libsupc++ does not implement the dependent EH ABI and the functionality +// it uses to implement std::exception_ptr (which it declares as an alias of +// std::__exception_ptr::exception_ptr) is not directly exported to clients. So +// we have little choice but to hijack std::__exception_ptr::exception_ptr's +// (which fortunately has the same layout as our std::exception_ptr) copy +// constructor, assignment operator and destructor (which are part of its +// stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr) +// function. + +namespace std { + +namespace __exception_ptr +{ + +struct exception_ptr +{ + void* __ptr_; + + exception_ptr(const exception_ptr&) _NOEXCEPT; + exception_ptr& operator=(const exception_ptr&) _NOEXCEPT; + ~exception_ptr() _NOEXCEPT; +}; + +} + +_LIBCPP_NORETURN void rethrow_exception(__exception_ptr::exception_ptr); + +exception_ptr::~exception_ptr() _NOEXCEPT +{ + reinterpret_cast<__exception_ptr::exception_ptr*>(this)->~exception_ptr(); +} + +exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT + : __ptr_(other.__ptr_) +{ + new (reinterpret_cast<void*>(this)) __exception_ptr::exception_ptr( + reinterpret_cast<const __exception_ptr::exception_ptr&>(other)); +} + +exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT +{ + *reinterpret_cast<__exception_ptr::exception_ptr*>(this) = + reinterpret_cast<const __exception_ptr::exception_ptr&>(other); + return *this; +} + +nested_exception::nested_exception() _NOEXCEPT + : __ptr_(current_exception()) +{ +} + + +_LIBCPP_NORETURN +void +nested_exception::rethrow_nested() const +{ + if (__ptr_ == nullptr) + terminate(); + rethrow_exception(__ptr_); +} + +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) +{ + rethrow_exception(reinterpret_cast<__exception_ptr::exception_ptr&>(p)); +} + +} // namespace std diff --git a/lib/libcxx/src/support/runtime/exception_pointer_msvc.ipp b/lib/libcxx/src/support/runtime/exception_pointer_msvc.ipp new file mode 100644 index 00000000000..5ca7519a600 --- /dev/null +++ b/lib/libcxx/src/support/runtime/exception_pointer_msvc.ipp @@ -0,0 +1,101 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 <stdio.h> +#include <stdlib.h> + +#if !defined(_CRTIMP2_PURE) +#define _CRTIMP2_PURE __declspec(dllimport) +#endif + +#if !defined(__CLRCALL_PURE_OR_CDECL) +#define __CLRCALL_PURE_OR_CDECL __cdecl +#endif + +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(void*, + const void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL +__ExceptionPtrAssign(void*, const void*); +_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL +__ExceptionPtrCompare(const void*, const void*); +_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL +__ExceptionPtrToBool(const void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(void*, void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL +__ExceptionPtrCurrentException(void*); +[[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL +__ExceptionPtrRethrow(const void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL +__ExceptionPtrCopyException(void*, const void*, const void*); + +namespace std { + +exception_ptr::exception_ptr() _NOEXCEPT { __ExceptionPtrCreate(this); } +exception_ptr::exception_ptr(nullptr_t) _NOEXCEPT { __ExceptionPtrCreate(this); } + +exception_ptr::exception_ptr(const exception_ptr& __other) _NOEXCEPT { + __ExceptionPtrCopy(this, &__other); +} +exception_ptr& exception_ptr::operator=(const exception_ptr& __other) _NOEXCEPT { + __ExceptionPtrAssign(this, &__other); + return *this; +} + +exception_ptr& exception_ptr::operator=(nullptr_t) _NOEXCEPT { + exception_ptr dummy; + __ExceptionPtrAssign(this, &dummy); + return *this; +} + +exception_ptr::~exception_ptr() _NOEXCEPT { __ExceptionPtrDestroy(this); } + +exception_ptr::operator bool() const _NOEXCEPT { + return __ExceptionPtrToBool(this); +} + +bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { + return __ExceptionPtrCompare(&__x, &__y); +} + + +void swap(exception_ptr& lhs, exception_ptr& rhs) _NOEXCEPT { + __ExceptionPtrSwap(&rhs, &lhs); +} + +exception_ptr __copy_exception_ptr(void* __except, const void* __ptr) { + exception_ptr __ret = nullptr; + if (__ptr) + __ExceptionPtrCopyException(&__ret, __except, __ptr); + return __ret; +} + +exception_ptr current_exception() _NOEXCEPT { + exception_ptr __ret; + __ExceptionPtrCurrentException(&__ret); + return __ret; +} + +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) { __ExceptionPtrRethrow(&p); } + +nested_exception::nested_exception() _NOEXCEPT : __ptr_(current_exception()) {} + +nested_exception::~nested_exception() _NOEXCEPT {} + +_LIBCPP_NORETURN +void nested_exception::rethrow_nested() const { + if (__ptr_ == nullptr) + terminate(); + rethrow_exception(__ptr_); +} + +} // namespace std diff --git a/lib/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp b/lib/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp new file mode 100644 index 00000000000..21c182c8597 --- /dev/null +++ b/lib/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp @@ -0,0 +1,80 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 <stdio.h> +#include <stdlib.h> + +namespace std { + +exception_ptr::~exception_ptr() _NOEXCEPT +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT + : __ptr_(other.__ptr_) +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +nested_exception::nested_exception() _NOEXCEPT + : __ptr_(current_exception()) +{ +} + +#if !defined(__GLIBCXX__) + +nested_exception::~nested_exception() _NOEXCEPT +{ +} + +#endif + +_LIBCPP_NORETURN +void +nested_exception::rethrow_nested() const +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +#if 0 + if (__ptr_ == nullptr) + terminate(); + rethrow_exception(__ptr_); +#endif // FIXME +} + +exception_ptr current_exception() _NOEXCEPT +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +} // namespace std diff --git a/lib/libcxx/src/support/runtime/new_handler_fallback.ipp b/lib/libcxx/src/support/runtime/new_handler_fallback.ipp new file mode 100644 index 00000000000..ec3f52354ca --- /dev/null +++ b/lib/libcxx/src/support/runtime/new_handler_fallback.ipp @@ -0,0 +1,27 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +namespace std { + +_LIBCPP_SAFE_STATIC static std::new_handler __new_handler; + +new_handler +set_new_handler(new_handler handler) _NOEXCEPT +{ + return __libcpp_atomic_exchange(&__new_handler, handler); +} + +new_handler +get_new_handler() _NOEXCEPT +{ + return __libcpp_atomic_load(&__new_handler); +} + +} // namespace std diff --git a/lib/libcxx/src/support/solaris/xlocale.cpp b/lib/libcxx/src/support/solaris/xlocale.cpp new file mode 100644 index 00000000000..6eaf317c768 --- /dev/null +++ b/lib/libcxx/src/support/solaris/xlocale.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#ifdef __sun__ + +#include "support/solaris/xlocale.h" +#include <stdarg.h> +#include <stdio.h> +#include <sys/localedef.h> + +extern "C" { + +int isxdigit_l(int __c, locale_t __l) { + return isxdigit(__c); +} + +int iswxdigit_l(wint_t __c, locale_t __l) { + return isxdigit(__c); +} + +// FIXME: This disregards the locale, which is Very Wrong +#define vsnprintf_l(__s, __n, __l, __format, __va) \ + vsnprintf(__s, __n, __format, __va) + +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; +} + +size_t mbrtowc_l(wchar_t *__pwc, const char *__pmb, + size_t __max, mbstate_t *__ps, locale_t __loc) { + return mbrtowc(__pwc, __pmb, __max, __ps); +} + +struct lconv *localeconv_l(locale_t __l) { + return localeconv(); +} + +}; + +#endif // __sun__ diff --git a/lib/libcxx/src/support/win32/locale_win32.cpp b/lib/libcxx/src/support/win32/locale_win32.cpp index 5a43743470d..fdca7efff06 100644 --- a/lib/libcxx/src/support/win32/locale_win32.cpp +++ b/lib/libcxx/src/support/win32/locale_win32.cpp @@ -10,79 +10,83 @@ #include <locale> #include <cstdarg> // va_start, va_end +#include <memory> +#include <type_traits> + +using std::__libcpp_locale_guard; // 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 ); + return {_create_locale( LC_ALL, locale ), locale}; } -locale_t uselocale( locale_t newloc ) + +decltype(MB_CUR_MAX) MB_CUR_MAX_L( locale_t __l ) { - locale_t old_locale = _get_current_locale(); - if ( newloc == NULL ) - return old_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; +#if defined(_LIBCPP_MSVCRT) + return ___mb_cur_max_l_func(__l); +#else + __libcpp_locale_guard __current(__l); + return MB_CUR_MAX; +#endif } + + lconv *localeconv_l( locale_t loc ) { - __locale_raii __current( uselocale(loc), uselocale ); + __libcpp_locale_guard __current(loc); 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 ); + __libcpp_locale_guard __current(loc); 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 ); + __libcpp_locale_guard __current(loc); 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 ); + __libcpp_locale_guard __current(loc); 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 ); + __libcpp_locale_guard __current(loc); 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 ); + __libcpp_locale_guard __current(loc); 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 ); + __libcpp_locale_guard __current(loc); return wcsnrtombs( dst, src, nwc, len, ps ); } wint_t btowc_l( int c, locale_t loc ) { - __locale_raii __current( uselocale(loc), uselocale ); + __libcpp_locale_guard __current(loc); return btowc( c ); } int wctob_l( wint_t c, locale_t loc ) { - __locale_raii __current( uselocale(loc), uselocale ); + __libcpp_locale_guard __current(loc); return wctob( c ); } int snprintf_l(char *ret, size_t n, locale_t loc, const char *format, ...) { - __locale_raii __current( uselocale(loc), uselocale ); + __libcpp_locale_guard __current(loc); va_list ap; va_start( ap, format ); int result = vsnprintf( ret, n, format, ap ); @@ -100,6 +104,18 @@ int asprintf_l( char **ret, locale_t loc, const char *format, ... ) } int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap ) { - __locale_raii __current( uselocale(loc), uselocale ); + __libcpp_locale_guard __current(loc); return vasprintf( ret, format, ap ); } + +#if !defined(_LIBCPP_MSVCRT) +float strtof_l(const char* nptr, char** endptr, locale_t loc) { + __libcpp_locale_guard __current(loc); + return strtof(nptr, endptr); +} + +long double strtold_l(const char* nptr, char** endptr, locale_t loc) { + __libcpp_locale_guard __current(loc); + return strtold(nptr, endptr); +} +#endif diff --git a/lib/libcxx/src/support/win32/support.cpp b/lib/libcxx/src/support/win32/support.cpp index e989681a6d5..dbd1c4c418a 100644 --- a/lib/libcxx/src/support/win32/support.cpp +++ b/lib/libcxx/src/support/win32/support.cpp @@ -15,17 +15,6 @@ #include <cstring> // strcpy, wcsncpy #include <cwchar> // mbstate_t -// Some of these functions aren't standard or if they conform, the name does not. - -int asprintf(char **sptr, const char *__restrict format, ...) -{ - va_list ap; - va_start(ap, format); - int result; - result = vasprintf(sptr, format, ap); - va_end(ap); - return result; -} // Like sprintf, but when return value >= 0 it returns // a pointer to a malloc'd string in *sptr. diff --git a/lib/libcxx/src/system_error.cpp b/lib/libcxx/src/system_error.cpp index 87f35ae37f3..72623ea6bc8 100644 --- a/lib/libcxx/src/system_error.cpp +++ b/lib/libcxx/src/system_error.cpp @@ -17,9 +17,9 @@ #include "cstring" #include "cstdio" #include "cstdlib" -#include "cassert" #include "string" #include "string.h" +#include "__debug" #if defined(__ANDROID__) #include <android/api-level.h> @@ -29,9 +29,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD // class error_category +#if defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) error_category::error_category() _NOEXCEPT { } +#endif error_category::~error_category() _NOEXCEPT { @@ -55,6 +57,7 @@ error_category::equivalent(const error_code& code, int condition) const _NOEXCEP return *this == code.category() && code.value() == condition; } +#if !defined(_LIBCPP_HAS_NO_THREADS) namespace { // GLIBC also uses 1024 as the maximum buffer size internally. @@ -62,40 +65,69 @@ constexpr size_t strerror_buff_size = 1024; string do_strerror_r(int ev); -#if defined(__linux__) && !defined(_LIBCPP_HAS_MUSL_LIBC) \ - && (!defined(__ANDROID__) || __ANDROID_API__ >= 23) -// GNU Extended version +#if defined(_LIBCPP_MSVCRT_LIKE) string do_strerror_r(int ev) { - char buffer[strerror_buff_size]; - char* ret = ::strerror_r(ev, buffer, strerror_buff_size); - return string(ret); + char buffer[strerror_buff_size]; + if (::strerror_s(buffer, strerror_buff_size, ev) == 0) + return string(buffer); + std::snprintf(buffer, strerror_buff_size, "unknown error %d", ev); + return string(buffer); } #else -// POSIX version + +// Only one of the two following functions will be used, depending on +// the return type of strerror_r: + +// For the GNU variant, a char* return value: +__attribute__((unused)) const char * +handle_strerror_r_return(char *strerror_return, char *buffer) { + // GNU always returns a string pointer in its return value. The + // string might point to either the input buffer, or a static + // buffer, but we don't care which. + return strerror_return; +} + +// For the POSIX variant: an int return value. +__attribute__((unused)) const char * +handle_strerror_r_return(int strerror_return, char *buffer) { + // The POSIX variant either: + // - fills in the provided buffer and returns 0 + // - returns a positive error value, or + // - returns -1 and fills in errno with an error value. + if (strerror_return == 0) + return buffer; + + // Only handle EINVAL. Other errors abort. + int new_errno = strerror_return == -1 ? errno : strerror_return; + if (new_errno == EINVAL) + return ""; + + _LIBCPP_ASSERT(new_errno == ERANGE, "unexpected error from ::strerror_r"); + // FIXME maybe? 'strerror_buff_size' is likely to exceed the + // maximum error size so ERANGE shouldn't be returned. + std::abort(); +} + +// This function handles both GNU and POSIX variants, dispatching to +// one of the two above functions. string do_strerror_r(int ev) { char buffer[strerror_buff_size]; + // Preserve errno around the call. (The C++ standard requires that + // system_error functions not modify errno). const int old_errno = errno; - int ret; - if ((ret = ::strerror_r(ev, buffer, strerror_buff_size)) != 0) { - // If `ret == -1` then the error is specified using `errno`, otherwise - // `ret` represents the error. - const int new_errno = ret == -1 ? errno : ret; - errno = old_errno; - if (new_errno == EINVAL) { - std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev); - return string(buffer); - } else { - assert(new_errno == ERANGE); - // FIXME maybe? 'strerror_buff_size' is likely to exceed the - // maximum error size so ERANGE shouldn't be returned. - std::abort(); - } + const char *error_message = handle_strerror_r_return( + ::strerror_r(ev, buffer, strerror_buff_size), buffer); + // If we didn't get any message, print one now. + if (!error_message[0]) { + std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev); + error_message = buffer; } - return string(buffer); + errno = old_errno; + return string(error_message); } #endif - } // end namespace +#endif string __do_message::message(int ev) const @@ -258,6 +290,7 @@ __throw_system_error(int ev, const char* what_arg) #else (void)ev; (void)what_arg; + _VSTD::abort(); #endif } diff --git a/lib/libcxx/src/typeinfo.cpp b/lib/libcxx/src/typeinfo.cpp index 5c0a609b5e5..0cb193b77d9 100644 --- a/lib/libcxx/src/typeinfo.cpp +++ b/lib/libcxx/src/typeinfo.cpp @@ -6,63 +6,52 @@ // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// -#include <stdlib.h> - -#if defined(__APPLE__) || defined(LIBCXXRT) || \ - defined(LIBCXX_BUILDING_LIBCXXABI) -#include <cxxabi.h> -#endif #include "typeinfo" -#if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) +#if defined(_LIBCPP_ABI_MICROSOFT) +#include <string.h> -std::bad_cast::bad_cast() _NOEXCEPT -{ +int std::type_info::__compare(const type_info &__rhs) const _NOEXCEPT { + if (&__data == &__rhs.__data) + return 0; + return strcmp(&__data.__decorated_name[1], &__rhs.__data.__decorated_name[1]); } -std::bad_typeid::bad_typeid() _NOEXCEPT -{ +const char *std::type_info::name() const _NOEXCEPT { + // TODO(compnerd) cache demangled &__data.__decorated_name[1] + return &__data.__decorated_name[1]; } -#ifndef __GLIBCXX__ +size_t std::type_info::hash_code() const _NOEXCEPT { +#if defined(_WIN64) + constexpr size_t fnv_offset_basis = 14695981039346656037ull; + constexpr size_t fnv_prime = 10995116282110ull; +#else + constexpr size_t fnv_offset_basis = 2166136261ull; + constexpr size_t fnv_prime = 16777619ull; +#endif -std::bad_cast::~bad_cast() _NOEXCEPT -{ -} + size_t value = fnv_offset_basis; + for (const char* c = &__data.__decorated_name[1]; *c; ++c) { + value ^= static_cast<size_t>(static_cast<unsigned char>(*c)); + value *= fnv_prime; + } -const char* -std::bad_cast::what() const _NOEXCEPT -{ - return "std::bad_cast"; -} +#if defined(_WIN64) + value ^= value >> 32; +#endif -std::bad_typeid::~bad_typeid() _NOEXCEPT -{ + return value; } - -const char* -std::bad_typeid::what() const _NOEXCEPT +#endif // _LIBCPP_ABI_MICROSOFT + +// FIXME: Remove __APPLE__ default here once buildit is gone. +// FIXME: Remove the _LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY configuration. +#if (!defined(LIBCXX_BUILDING_LIBCXXABI) && !defined(LIBCXXRT) && \ + !defined(__GLIBCXX__) && !defined(__APPLE__)) || \ + defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) +std::type_info::~type_info() { - return "std::bad_typeid"; } - -#ifdef __APPLE__ - // On Darwin, the cxa_bad_* functions cannot be in the lower level library - // because bad_cast and bad_typeid are defined in his higher level library - void __cxxabiv1::__cxa_bad_typeid() - { -#ifndef _LIBCPP_NO_EXCEPTIONS - throw std::bad_typeid(); #endif - } - void __cxxabiv1::__cxa_bad_cast() - { -#ifndef _LIBCPP_NO_EXCEPTIONS - throw std::bad_cast(); -#endif - } -#endif - -#endif // !__GLIBCXX__ -#endif // !LIBCXXRT && !_LIBCPPABI_VERSION diff --git a/lib/libcxx/src/variant.cpp b/lib/libcxx/src/variant.cpp new file mode 100644 index 00000000000..5bb508c3f46 --- /dev/null +++ b/lib/libcxx/src/variant.cpp @@ -0,0 +1,18 @@ +//===------------------------ variant.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 "variant" + +namespace std { + +const char* bad_variant_access::what() const noexcept { + return "bad_variant_access"; +} + +} // namespace std diff --git a/lib/libcxx/src/vector.cpp b/lib/libcxx/src/vector.cpp new file mode 100644 index 00000000000..300adaed5f4 --- /dev/null +++ b/lib/libcxx/src/vector.cpp @@ -0,0 +1,16 @@ +//===------------------------- vector.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 "vector" + +_LIBCPP_BEGIN_NAMESPACE_STD + +template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __vector_base_common<true>; + +_LIBCPP_END_NAMESPACE_STD |