diff options
Diffstat (limited to 'dep/fmt/src')
-rw-r--r-- | dep/fmt/src/fmt.cc | 99 | ||||
-rw-r--r-- | dep/fmt/src/format.cc | 96 | ||||
-rw-r--r-- | dep/fmt/src/os.cc | 153 |
3 files changed, 217 insertions, 131 deletions
diff --git a/dep/fmt/src/fmt.cc b/dep/fmt/src/fmt.cc new file mode 100644 index 00000000000..80e77e26af8 --- /dev/null +++ b/dep/fmt/src/fmt.cc @@ -0,0 +1,99 @@ +module; +#ifndef __cpp_modules +# error Module not supported. +#endif + +// put all implementation-provided headers into the global module fragment +// to prevent attachment to this module +#if !defined(_CRT_SECURE_NO_WARNINGS) && defined(_MSC_VER) +# define _CRT_SECURE_NO_WARNINGS +#endif +#if !defined(WIN32_LEAN_AND_MEAN) && defined(_WIN32) +# define WIN32_LEAN_AND_MEAN +#endif + +#include <algorithm> +#include <cctype> +#include <cerrno> +#include <chrono> +#include <climits> +#include <clocale> +#include <cmath> +#include <cstdarg> +#include <cstddef> +#include <cstdint> +#include <cstdio> +#include <cstdlib> +#include <cstring> +#include <ctime> +#include <cwchar> +#include <exception> +#include <functional> +#include <iterator> +#include <limits> +#include <locale> +#include <memory> +#include <ostream> +#include <sstream> +#include <stdexcept> +#include <string> +#include <string_view> +#include <system_error> +#include <type_traits> +#include <utility> +#include <vector> + +#if _MSC_VER +# include <intrin.h> +#endif +#if defined __APPLE__ || defined(__FreeBSD__) +# include <xlocale.h> +#endif +#if __has_include(<winapifamily.h>) +# include <winapifamily.h> +#endif +#if (__has_include(<fcntl.h>) || defined(__APPLE__) || \ + defined(__linux__)) && \ + (!defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)) +# include <fcntl.h> +# include <sys/stat.h> +# include <sys/types.h> +# ifndef _WIN32 +# include <unistd.h> +# else +# include <io.h> +# endif +#endif +#ifdef _WIN32 +# include <windows.h> +#endif + +export module fmt; + +#define FMT_MODULE_EXPORT export +#define FMT_MODULE_EXPORT_BEGIN export { +#define FMT_MODULE_EXPORT_END } +#define FMT_BEGIN_DETAIL_NAMESPACE \ + } \ + namespace detail { +#define FMT_END_DETAIL_NAMESPACE \ + } \ + export { +// all library-provided declarations and definitions +// must be in the module purview to be exported +#include "fmt/args.h" +#include "fmt/chrono.h" +#include "fmt/color.h" +#include "fmt/compile.h" +#include "fmt/format.h" +#include "fmt/os.h" +#include "fmt/printf.h" +#include "fmt/xchar.h" + +// gcc doesn't yet implement private module fragments +#if !FMT_GCC_VERSION +module : private; +#endif + +#include "format.cc" +#include "os.cc" diff --git a/dep/fmt/src/format.cc b/dep/fmt/src/format.cc index 6141d964a79..99b7e9dd471 100644 --- a/dep/fmt/src/format.cc +++ b/dep/fmt/src/format.cc @@ -10,90 +10,38 @@ FMT_BEGIN_NAMESPACE namespace detail { -template <typename T> -int format_float(char* buf, std::size_t size, const char* format, int precision, - T value) { -#ifdef FMT_FUZZ - if (precision > 100000) - throw std::runtime_error( - "fuzz mode - avoid large allocation inside snprintf"); -#endif - // Suppress the warning about nonliteral format string. - int (*snprintf_ptr)(char*, size_t, const char*, ...) = FMT_SNPRINTF; - return precision < 0 ? snprintf_ptr(buf, size, format, value) - : snprintf_ptr(buf, size, format, precision, value); -} - -template FMT_API dragonbox::decimal_fp<float> dragonbox::to_decimal(float x) - FMT_NOEXCEPT; -template FMT_API dragonbox::decimal_fp<double> dragonbox::to_decimal(double x) - FMT_NOEXCEPT; - -// DEPRECATED! This function exists for ABI compatibility. -template <typename Char> -typename basic_format_context<std::back_insert_iterator<buffer<Char>>, - Char>::iterator -vformat_to(buffer<Char>& buf, basic_string_view<Char> format_str, - basic_format_args<basic_format_context< - std::back_insert_iterator<buffer<type_identity_t<Char>>>, - type_identity_t<Char>>> - args) { - using iterator = std::back_insert_iterator<buffer<char>>; - using context = basic_format_context< - std::back_insert_iterator<buffer<type_identity_t<Char>>>, - type_identity_t<Char>>; - auto out = iterator(buf); - format_handler<iterator, Char, context> h(out, format_str, args, {}); - parse_format_string<false>(format_str, h); - return out; -} -template basic_format_context<std::back_insert_iterator<buffer<char>>, - char>::iterator -vformat_to(buffer<char>&, string_view, - basic_format_args<basic_format_context< - std::back_insert_iterator<buffer<type_identity_t<char>>>, - type_identity_t<char>>>); -} // namespace detail - -template struct FMT_INSTANTIATION_DEF_API detail::basic_data<void>; - -// Workaround a bug in MSVC2013 that prevents instantiation of format_float. -int (*instantiate_format_float)(double, int, detail::float_specs, - detail::buffer<char>&) = detail::format_float; +template FMT_API auto dragonbox::to_decimal(float x) noexcept + -> dragonbox::decimal_fp<float>; +template FMT_API auto dragonbox::to_decimal(double x) noexcept + -> dragonbox::decimal_fp<double>; #ifndef FMT_STATIC_THOUSANDS_SEPARATOR -template FMT_API detail::locale_ref::locale_ref(const std::locale& loc); -template FMT_API std::locale detail::locale_ref::get<std::locale>() const; +template FMT_API locale_ref::locale_ref(const std::locale& loc); +template FMT_API auto locale_ref::get<std::locale>() const -> std::locale; #endif // Explicit instantiations for char. -template FMT_API std::string detail::grouping_impl<char>(locale_ref); -template FMT_API char detail::thousands_sep_impl(locale_ref); -template FMT_API char detail::decimal_point_impl(locale_ref); - -template FMT_API void detail::buffer<char>::append(const char*, const char*); +template FMT_API auto thousands_sep_impl(locale_ref) + -> thousands_sep_result<char>; +template FMT_API auto decimal_point_impl(locale_ref) -> char; -template FMT_API void detail::vformat_to( - detail::buffer<char>&, string_view, - basic_format_args<FMT_BUFFER_CONTEXT(char)>, detail::locale_ref); +template FMT_API void buffer<char>::append(const char*, const char*); -template FMT_API int detail::snprintf_float(double, int, detail::float_specs, - detail::buffer<char>&); -template FMT_API int detail::snprintf_float(long double, int, - detail::float_specs, - detail::buffer<char>&); -template FMT_API int detail::format_float(double, int, detail::float_specs, - detail::buffer<char>&); -template FMT_API int detail::format_float(long double, int, detail::float_specs, - detail::buffer<char>&); +// DEPRECATED! +// There is no correspondent extern template in format.h because of +// incompatibility between clang and gcc (#2377). +template FMT_API void vformat_to(buffer<char>&, string_view, + basic_format_args<FMT_BUFFER_CONTEXT(char)>, + locale_ref); // Explicit instantiations for wchar_t. -template FMT_API std::string detail::grouping_impl<wchar_t>(locale_ref); -template FMT_API wchar_t detail::thousands_sep_impl(locale_ref); -template FMT_API wchar_t detail::decimal_point_impl(locale_ref); +template FMT_API auto thousands_sep_impl(locale_ref) + -> thousands_sep_result<wchar_t>; +template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t; -template FMT_API void detail::buffer<wchar_t>::append(const wchar_t*, - const wchar_t*); +template FMT_API void buffer<wchar_t>::append(const wchar_t*, const wchar_t*); + +} // namespace detail FMT_END_NAMESPACE diff --git a/dep/fmt/src/os.cc b/dep/fmt/src/os.cc index 6850024588f..f388ead0191 100644 --- a/dep/fmt/src/os.cc +++ b/dep/fmt/src/os.cc @@ -25,21 +25,24 @@ # define WIN32_LEAN_AND_MEAN # endif # include <io.h> -# include <windows.h> - -# define O_CREAT _O_CREAT -# define O_TRUNC _O_TRUNC # ifndef S_IRUSR # define S_IRUSR _S_IREAD # endif - # ifndef S_IWUSR # define S_IWUSR _S_IWRITE # endif - -# ifdef __MINGW32__ -# define _SH_DENYNO 0x40 +# ifndef S_IRGRP +# define S_IRGRP 0 +# endif +# ifndef S_IWGRP +# define S_IWGRP 0 +# endif +# ifndef S_IROTH +# define S_IROTH 0 +# endif +# ifndef S_IWOTH +# define S_IWOTH 0 # endif # endif // _WIN32 #endif // FMT_USE_FCNTL @@ -48,14 +51,10 @@ # include <windows.h> #endif -#ifdef fileno -# undef fileno -#endif - namespace { #ifdef _WIN32 // Return type of read and write functions. -using RWResult = int; +using rwresult = int; // On Windows the count argument to read and write is unsigned, so convert // it from size_t preventing integer overflow. @@ -64,7 +63,7 @@ inline unsigned convert_rwcount(std::size_t count) { } #elif FMT_USE_FCNTL // Return type of read and write functions. -using RWResult = ssize_t; +using rwresult = ssize_t; inline std::size_t convert_rwcount(std::size_t count) { return count; } #endif @@ -73,14 +72,14 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; } FMT_BEGIN_NAMESPACE #ifdef _WIN32 -detail::utf16_to_utf8::utf16_to_utf8(wstring_view s) { +detail::utf16_to_utf8::utf16_to_utf8(basic_string_view<wchar_t> s) { if (int error_code = convert(s)) { FMT_THROW(windows_error(error_code, "cannot convert string from UTF-16 to UTF-8")); } } -int detail::utf16_to_utf8::convert(wstring_view s) { +int detail::utf16_to_utf8::convert(basic_string_view<wchar_t> s) { if (s.size() > INT_MAX) return ERROR_INVALID_PARAMETER; int s_size = static_cast<int>(s.size()); if (s_size == 0) { @@ -101,51 +100,90 @@ int detail::utf16_to_utf8::convert(wstring_view s) { return 0; } -void windows_error::init(int err_code, string_view format_str, - format_args args) { - error_code_ = err_code; - memory_buffer buffer; - detail::format_windows_error(buffer, err_code, vformat(format_str, args)); - std::runtime_error& base = *this; - base = std::runtime_error(to_string(buffer)); +namespace detail { + +class system_message { + system_message(const system_message&) = delete; + void operator=(const system_message&) = delete; + + unsigned long result_; + wchar_t* message_; + + static bool is_whitespace(wchar_t c) noexcept { + return c == L' ' || c == L'\n' || c == L'\r' || c == L'\t' || c == L'\0'; + } + + public: + explicit system_message(unsigned long error_code) + : result_(0), message_(nullptr) { + result_ = FormatMessageW( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast<wchar_t*>(&message_), 0, nullptr); + if (result_ != 0) { + while (result_ != 0 && is_whitespace(message_[result_ - 1])) { + --result_; + } + } + } + ~system_message() { LocalFree(message_); } + explicit operator bool() const noexcept { return result_ != 0; } + operator basic_string_view<wchar_t>() const noexcept { + return basic_string_view<wchar_t>(message_, result_); + } +}; + +class utf8_system_category final : public std::error_category { + public: + const char* name() const noexcept override { return "system"; } + std::string message(int error_code) const override { + system_message msg(error_code); + if (msg) { + utf16_to_utf8 utf8_message; + if (utf8_message.convert(msg) == ERROR_SUCCESS) { + return utf8_message.str(); + } + } + return "unknown error"; + } +}; + +} // namespace detail + +FMT_API const std::error_category& system_category() noexcept { + static const detail::utf8_system_category category; + return category; +} + +std::system_error vwindows_error(int err_code, string_view format_str, + format_args args) { + auto ec = std::error_code(err_code, system_category()); + return std::system_error(ec, vformat(format_str, args)); } void detail::format_windows_error(detail::buffer<char>& out, int error_code, - string_view message) FMT_NOEXCEPT { + const char* message) noexcept { FMT_TRY { - wmemory_buffer buf; - buf.resize(inline_buffer_size); - for (;;) { - wchar_t* system_message = &buf[0]; - int result = FormatMessageW( - FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, - error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), system_message, - static_cast<uint32_t>(buf.size()), nullptr); - if (result != 0) { - utf16_to_utf8 utf8_message; - if (utf8_message.convert(system_message) == ERROR_SUCCESS) { - format_to(buffer_appender<char>(out), "{}: {}", message, - utf8_message); - return; - } - break; + system_message msg(error_code); + if (msg) { + utf16_to_utf8 utf8_message; + if (utf8_message.convert(msg) == ERROR_SUCCESS) { + fmt::format_to(buffer_appender<char>(out), "{}: {}", message, utf8_message); + return; } - if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) - break; // Can't get error message, report error code instead. - buf.resize(buf.size() * 2); } } FMT_CATCH(...) {} format_error_code(out, error_code, message); } -void report_windows_error(int error_code, - fmt::string_view message) FMT_NOEXCEPT { +void report_windows_error(int error_code, const char* message) noexcept { report_error(detail::format_windows_error, error_code, message); } #endif // _WIN32 -buffered_file::~buffered_file() FMT_NOEXCEPT { +buffered_file::~buffered_file() noexcept { if (file_ && FMT_SYSTEM(fclose(file_)) != 0) report_system_error(errno, "cannot close file"); } @@ -164,18 +202,19 @@ void buffered_file::close() { if (result != 0) FMT_THROW(system_error(errno, "cannot close file")); } -// A macro used to prevent expansion of fileno on broken versions of MinGW. -#define FMT_ARGS - -int buffered_file::fileno() const { - int fd = FMT_POSIX_CALL(fileno FMT_ARGS(file_)); +int buffered_file::descriptor() const { + int fd = FMT_POSIX_CALL(fileno(file_)); if (fd == -1) FMT_THROW(system_error(errno, "cannot get file descriptor")); return fd; } #if FMT_USE_FCNTL file::file(cstring_view path, int oflag) { - int mode = S_IRUSR | S_IWUSR; +# ifdef _WIN32 + using mode_t = int; +# endif + constexpr mode_t mode = + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; # if defined(_WIN32) && !defined(__MINGW32__) fd_ = -1; FMT_POSIX_CALL(sopen_s(&fd_, path.c_str(), oflag, _SH_DENYNO, mode)); @@ -186,7 +225,7 @@ file::file(cstring_view path, int oflag) { FMT_THROW(system_error(errno, "cannot open file {}", path.c_str())); } -file::~file() FMT_NOEXCEPT { +file::~file() noexcept { // Don't retry close in case of EINTR! // See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0) @@ -229,14 +268,14 @@ long long file::size() const { } std::size_t file::read(void* buffer, std::size_t count) { - RWResult result = 0; + rwresult result = 0; FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count)))); if (result < 0) FMT_THROW(system_error(errno, "cannot read from file")); return detail::to_unsigned(result); } std::size_t file::write(const void* buffer, std::size_t count) { - RWResult result = 0; + rwresult result = 0; FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count)))); if (result < 0) FMT_THROW(system_error(errno, "cannot write to file")); return detail::to_unsigned(result); @@ -260,10 +299,10 @@ void file::dup2(int fd) { } } -void file::dup2(int fd, error_code& ec) FMT_NOEXCEPT { +void file::dup2(int fd, std::error_code& ec) noexcept { int result = 0; FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd))); - if (result == -1) ec = error_code(errno); + if (result == -1) ec = std::error_code(errno, std::generic_category()); } void file::pipe(file& read_end, file& write_end) { |