From a30146643ccf52afef2713f347172b66bc800ffd Mon Sep 17 00:00:00 2001 From: Michael Winterberg Date: Thu, 17 May 2018 18:03:43 -0700 Subject: [PATCH] MSVC improvements and data truncation cleanup. MSVC is timid about evaluating constexpr functions unless it has to, so the "TYPES" variables end up in read-write memory even though the optimizer removes the initializer. Making TYPES constexpr causes MSVC to try harder to initialize these variables at compile time, which also ends up completely removing the (named) variable from the final compiled binary. Fixed a data truncation warning being reported in ostream-test. --- include/fmt/core.h | 18 ++++++++++++++---- include/fmt/format.h | 12 +++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 66597eed..5cc84994 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -73,7 +73,8 @@ # endif #endif -#if FMT_HAS_FEATURE(cxx_explicit_conversions) +#if FMT_HAS_FEATURE(cxx_explicit_conversions) || \ + FMT_MSC_VER >= 1800 # define FMT_EXPLICIT explicit #else # define FMT_EXPLICIT @@ -986,8 +987,17 @@ class format_arg_store { friend class basic_format_args; + static FMT_CONSTEXPR uint64_t get_types() { + return IS_PACKED ? internal::get_types() + : -static_cast(NUM_ARGS); + } + public: +#if FMT_USE_CONSTEXPR + static constexpr uint64_t TYPES = get_types(); +#else static const uint64_t TYPES; +#endif #if FMT_GCC_VERSION && FMT_GCC_VERSION <= 405 // Workaround an array initialization bug in gcc 4.5 and earlier. @@ -1000,10 +1010,10 @@ class format_arg_store { #endif }; +#if !FMT_USE_CONSTEXPR template -const uint64_t format_arg_store::TYPES = IS_PACKED ? - internal::get_types() : - -static_cast(NUM_ARGS); +const uint64_t format_arg_store::TYPES = get_types(); +#endif /** \rst diff --git a/include/fmt/format.h b/include/fmt/format.h index 07c644aa..817e890a 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1381,13 +1381,13 @@ class float_type_checker : private ErrorHandler { } }; -template +template class char_specs_checker : public ErrorHandler { private: - char type_; + CharType type_; public: - FMT_CONSTEXPR char_specs_checker(char type, ErrorHandler eh) + FMT_CONSTEXPR char_specs_checker(CharType type, ErrorHandler eh) : ErrorHandler(eh), type_(type) {} FMT_CONSTEXPR void on_int() { @@ -3110,8 +3110,10 @@ struct formatter< type_spec, internal::int_type_checker(eh)); break; case internal::char_type: - handle_char_specs(specs_, internal::char_specs_checker( - type_spec, eh)); + handle_char_specs( + specs_, + internal::char_specs_checker( + type_spec, eh)); break; case internal::double_type: case internal::long_double_type: