From 3e9fdb3a1fd931ea43229f8fc860d0ce67444597 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 10 Sep 2024 17:01:23 -0700 Subject: [PATCH] Cleanup --- include/fmt/base.h | 134 +++++++++++++------------------------------ include/fmt/format.h | 26 +++++++++ 2 files changed, 67 insertions(+), 93 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index a040f2f9..60c27565 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -143,21 +143,6 @@ # define FMT_CONSTEXPR20 #endif -#if defined(FMT_USE_NONTYPE_TEMPLATE_ARGS) -// Use the provided definition. -#elif defined(__NVCOMPILER) -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0 -#elif FMT_GCC_VERSION >= 903 && FMT_CPLUSPLUS >= 201709L -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 -#elif defined(__cpp_nontype_template_args) && \ - __cpp_nontype_template_args >= 201911L -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 -#elif FMT_CLANG_VERSION >= 1200 && FMT_CPLUSPLUS >= 202002L -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 -#else -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0 -#endif - // Check if exceptions are disabled. #ifdef FMT_EXCEPTIONS // Use the provided definition. @@ -176,17 +161,6 @@ # define FMT_CATCH(x) if (false) #endif -// Check if RTTI is disabled. -#ifdef FMT_USE_RTTI -// Use the provided definition. -#elif defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || defined(_CPPRTTI) || \ - defined(__INTEL_RTTI__) || defined(__RTTI) -// __RTTI is for EDG compilers. _CPPRTTI is for MSVC. -# define FMT_USE_RTTI 1 -#else -# define FMT_USE_RTTI 0 -#endif - #if FMT_HAS_CPP17_ATTRIBUTE(fallthrough) # define FMT_FALLTHROUGH [[fallthrough]] #elif defined(__clang__) @@ -1564,7 +1538,6 @@ FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end, ++begin; break; case '+': - FMT_FALLTHROUGH; case ' ': specs.set_sign(c == ' ' ? sign::space : sign::plus); FMT_FALLTHROUGH; @@ -2228,50 +2201,38 @@ template class value { }; constexpr FMT_INLINE value() : no_value() {} - constexpr FMT_INLINE value(int val) : int_value(val) {} - constexpr FMT_INLINE value(unsigned val FMT_BUILTIN) : uint_value(val) {} - FMT_CONSTEXPR FMT_INLINE value(long val FMT_BUILTIN) { - if (sizeof(long) == sizeof(int)) - int_value = static_cast(val); - else - long_long_value = val; - } - FMT_CONSTEXPR FMT_INLINE value(unsigned long val FMT_BUILTIN) { - if (sizeof(long) == sizeof(int)) - uint_value = static_cast(val); - else - ulong_long_value = val; - } - constexpr FMT_INLINE value(long long val FMT_BUILTIN) - : long_long_value(val) {} - constexpr FMT_INLINE value(unsigned long long val FMT_BUILTIN) - : ulong_long_value(val) {} + constexpr FMT_INLINE value(int x) : int_value(x) {} + constexpr FMT_INLINE value(unsigned x FMT_BUILTIN) : uint_value(x) {} + FMT_CONSTEXPR FMT_INLINE value(long x FMT_BUILTIN) : value(long_type(x)) {} + FMT_CONSTEXPR FMT_INLINE value(unsigned long x FMT_BUILTIN) + : value(ulong_type(x)) {} + constexpr FMT_INLINE value(long long x FMT_BUILTIN) : long_long_value(x) {} + constexpr FMT_INLINE value(unsigned long long x FMT_BUILTIN) + : ulong_long_value(x) {} template - constexpr FMT_INLINE value(bitint val FMT_BUILTIN) - : long_long_value(val) {} + constexpr FMT_INLINE value(bitint x FMT_BUILTIN) : long_long_value(x) {} template - constexpr FMT_INLINE value(ubitint val FMT_BUILTIN) - : ulong_long_value(val) {} - FMT_INLINE value(int128_opt val FMT_BUILTIN) : int128_value(val) {} - FMT_INLINE value(uint128_opt val FMT_BUILTIN) : uint128_value(val) {} - constexpr FMT_INLINE value(float val FMT_BUILTIN) : float_value(val) {} - constexpr FMT_INLINE value(double val FMT_BUILTIN) : double_value(val) {} - FMT_INLINE value(long double val FMT_BUILTIN) : long_double_value(val) {} - constexpr FMT_INLINE value(bool val FMT_BUILTIN) : bool_value(val) {} + constexpr FMT_INLINE value(ubitint x FMT_BUILTIN) : ulong_long_value(x) {} + FMT_INLINE value(int128_opt x FMT_BUILTIN) : int128_value(x) {} + FMT_INLINE value(uint128_opt x FMT_BUILTIN) : uint128_value(x) {} + constexpr FMT_INLINE value(float x FMT_BUILTIN) : float_value(x) {} + constexpr FMT_INLINE value(double x FMT_BUILTIN) : double_value(x) {} + FMT_INLINE value(long double x FMT_BUILTIN) : long_double_value(x) {} + constexpr FMT_INLINE value(bool x FMT_BUILTIN) : bool_value(x) {} template ::value)> - constexpr FMT_INLINE value(T val FMT_BUILTIN) : char_value(val) { + constexpr FMT_INLINE value(T x FMT_BUILTIN) : char_value(x) { static_assert(std::is_same::value, "mixing character types is disallowed"); } - FMT_CONSTEXPR FMT_INLINE value(const char_type* val FMT_BUILTIN) { - string.data = val; + FMT_CONSTEXPR FMT_INLINE value(const char_type* x FMT_BUILTIN) { + string.data = x; if (is_constant_evaluated()) string.size = {}; } - FMT_CONSTEXPR FMT_INLINE value(basic_string_view val FMT_BUILTIN) { - string.data = val.data(); - string.size = val.size(); + FMT_CONSTEXPR FMT_INLINE value(basic_string_view x FMT_BUILTIN) { + string.data = x.data(); + string.size = x.size(); } - FMT_INLINE value(const void* val FMT_BUILTIN) : pointer(val) {} + FMT_INLINE value(const void* x FMT_BUILTIN) : pointer(x) {} // We can't use mapped_t because of a bug in MSVC 2017. template < @@ -2279,8 +2240,8 @@ template class value { typename M = decltype(arg_mapper::map(std::declval())), FMT_ENABLE_IF(!std::is_same::value && !std::is_integral>::value)> - FMT_CONSTEXPR20 FMT_INLINE value(T&& val) { - *this = arg_mapper::map(val); + FMT_CONSTEXPR20 FMT_INLINE value(T&& x) { + *this = arg_mapper::map(x); } template < @@ -2288,7 +2249,7 @@ template class value { typename M = decltype(arg_mapper::map(std::declval())), FMT_ENABLE_IF(std::is_same::value && !std::is_integral>::value)> - FMT_CONSTEXPR20 FMT_INLINE value(T&& val) { + FMT_CONSTEXPR20 FMT_INLINE value(T&& x) { // Use enum instead of constexpr because the latter may generate code. enum { formattable_char = !std::is_same::value }; static_assert(formattable_char, "mixing character types is disallowed"); @@ -2309,13 +2270,13 @@ template class value { if constexpr (!formattable) type_is_unformattable_for _; if constexpr (std::is_same::value) { - int_value = val; // int is always handled as a built-in type. + int_value = x; // int is always handled as a built-in type. return; } // T may overload operator& e.g. std::vector::reference in libc++. - if constexpr (std::is_same*>::value) - custom.value = const_cast(&val); + if constexpr (std::is_same*>::value) + custom.value = const_cast(&x); #endif static_assert( @@ -2325,7 +2286,7 @@ template class value { if (!is_constant_evaluated()) custom.value = - const_cast(&reinterpret_cast(val)); + const_cast(&reinterpret_cast(x)); // Get the formatter type through the context to allow different contexts // have different extension points, e.g. `formatter` for `format` and // `printf_formatter` for `printf`. @@ -2415,9 +2376,7 @@ template args[ARGS_ARRAY_SIZE]; + arg_t args[1 + (NUM_ARGS != 0 ? NUM_ARGS : +1)]; named_arg_info named_args[NUM_NAMED_ARGS]; template @@ -2430,7 +2389,8 @@ struct named_arg_store { named_arg_store(named_arg_store&& rhs) { args[0] = {named_args, NUM_NAMED_ARGS}; - for (size_t i = 1; i < ARGS_ARRAY_SIZE; ++i) args[i] = rhs.args[i]; + for (size_t i = 1; i < sizeof(args) / sizeof(*args); ++i) + args[i] = rhs.args[i]; for (size_t i = 0; i < NUM_NAMED_ARGS; ++i) named_args[i] = rhs.named_args[i]; } @@ -2438,6 +2398,7 @@ struct named_arg_store { named_arg_store(const named_arg_store& rhs) = delete; named_arg_store& operator=(const named_arg_store& rhs) = delete; named_arg_store& operator=(named_arg_store&& rhs) = delete; + operator const arg_t*() const { return args + 1; } }; // An array of references to arguments. It can be implicitly converted to @@ -2694,35 +2655,23 @@ template class basic_format_args { /// Constructs a `basic_format_args` object from `format_arg_store`. template + FMT_ENABLE_IF(NUM_ARGS <= detail::max_packed_args)> constexpr FMT_ALWAYS_INLINE basic_format_args( const store& s) - : desc_(DESC), values_(s.args) {} + : desc_(DESC | (NUM_NAMED_ARGS != 0 ? +detail::has_named_args_bit : 0)), + values_(s.args) {} template + FMT_ENABLE_IF(NUM_ARGS > detail::max_packed_args)> constexpr basic_format_args(const store& s) - : desc_(DESC | detail::has_named_args_bit), values_(s.args.args + 1) {} - - template detail::max_packed_args && - NUM_NAMED_ARGS == 0)> - constexpr basic_format_args(const store& s) - : desc_(DESC), args_(s.args) {} - - template detail::max_packed_args && - NUM_NAMED_ARGS != 0)> - constexpr basic_format_args(const store& s) - : desc_(DESC | detail::has_named_args_bit), args_(s.args.args + 1) {} + : desc_(DESC | (NUM_NAMED_ARGS != 0 ? +detail::has_named_args_bit : 0)), + args_(s.args) {} /// Constructs a `basic_format_args` object from a dynamic list of arguments. constexpr basic_format_args(const format_arg* args, int count, bool has_named = false) : desc_(detail::is_unpacked_bit | detail::to_unsigned(count) | - (has_named ? +detail::has_named_args_bit : 0ULL)), + (has_named ? +detail::has_named_args_bit : 0)), args_(args) {} /// Returns the argument with the specified id. @@ -2880,7 +2829,6 @@ using is_formattable = bool_constant< detail::mapped_t::value, detail::unformattable, T>, Char>>::value>; - #ifdef __cpp_concepts template concept formattable = is_formattable, Char>::value; diff --git a/include/fmt/format.h b/include/fmt/format.h index aadc9b66..63af76c7 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -68,12 +68,38 @@ # endif #endif // FMT_MODULE +#if defined(FMT_USE_NONTYPE_TEMPLATE_ARGS) +// Use the provided definition. +#elif defined(__NVCOMPILER) +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0 +#elif FMT_GCC_VERSION >= 903 && FMT_CPLUSPLUS >= 201709L +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 +#elif defined(__cpp_nontype_template_args) && \ + __cpp_nontype_template_args >= 201911L +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 +#elif FMT_CLANG_VERSION >= 1200 && FMT_CPLUSPLUS >= 202002L +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 +#else +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0 +#endif + #if defined __cpp_inline_variables && __cpp_inline_variables >= 201606L # define FMT_INLINE_VARIABLE inline #else # define FMT_INLINE_VARIABLE #endif +// Check if RTTI is disabled. +#ifdef FMT_USE_RTTI +// Use the provided definition. +#elif defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || defined(_CPPRTTI) || \ + defined(__INTEL_RTTI__) || defined(__RTTI) +// __RTTI is for EDG compilers. _CPPRTTI is for MSVC. +# define FMT_USE_RTTI 1 +#else +# define FMT_USE_RTTI 0 +#endif + // Visibility when compiled as a shared library/object. #if defined(FMT_LIB_EXPORT) || defined(FMT_SHARED) # define FMT_SO_VISIBILITY(value) FMT_VISIBILITY(value)