From b71ec88103d51b40bff30661bd19416b3738d22a Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 12 Jan 2024 04:42:28 -0800 Subject: [PATCH] Cleanup conseval detection --- include/fmt/base.h | 116 +++++++++++++++++++--------------------- test/compile-fp-test.cc | 8 ++- test/compile-test.cc | 11 ++-- 3 files changed, 61 insertions(+), 74 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 416d4e10248da..b98be79b7023e 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -54,24 +54,6 @@ # define FMT_LIBCPP_VERSION 0 #endif -// Check if exceptions are disabled. -#ifdef FMT_EXCEPTIONS -// Use the provided definition. -#elif defined(__GNUC__) && !defined(__EXCEPTIONS) -# define FMT_EXCEPTIONS 0 -#elif FMT_MSC_VERSION && !_HAS_EXCEPTIONS -# define FMT_EXCEPTIONS 0 -#else -# define FMT_EXCEPTIONS 1 -#endif -#if FMT_EXCEPTIONS -# define FMT_TRY try -# define FMT_CATCH(x) catch (x) -#else -# define FMT_TRY if (true) -# define FMT_CATCH(x) if (false) -#endif - #ifdef _MSVC_LANG # define FMT_CPLUSPLUS _MSVC_LANG #else @@ -121,49 +103,54 @@ # define FMT_CONSTEXPR #endif -// Detect C++20 extensions to constexpr and std::is_constant_evaluated. -#ifndef __cpp_lib_is_constant_evaluated -# define FMT_CONSTEXPR20 +// Detect consteval, C++20 constexpr extensions and std::is_constant_evaluated. +#if !defined(__cpp_lib_is_constant_evaluated) +# define FMT_USE_CONSTEVAL 0 +#elif FMT_CPLUSPLUS < 201709L +# define FMT_USE_CONSTEVAL 0 #elif FMT_GLIBCXX_RELEASE && FMT_GLIBCXX_RELEASE < 10 -# define FMT_CONSTEXPR20 +# define FMT_USE_CONSTEVAL 0 #elif FMT_LIBCPP_VERSION && FMT_LIBCPP_VERSION < 10000 -# define FMT_CONSTEXPR20 -#elif FMT_MSC_VERSION && FMT_MSC_VERSION < 1928 -# define FMT_CONSTEXPR20 -#elif FMT_CPLUSPLUS >= 202002L -# define FMT_CONSTEXPR20 constexpr -#elif FMT_CPLUSPLUS >= 201709L && FMT_GCC_VERSION >= 1002 +# define FMT_USE_CONSTEVAL 0 +#elif defined(__apple_build_version__) && __apple_build_version__ < 14000029L +# define FMT_USE_CONSTEVAL 0 // consteval is broken in Apple clang < 14. +#elif FMT_MSC_VERSION && FMT_MSC_VERSION < 1929 +# define FMT_USE_CONSTEVAL 0 // consteval is broken in MSVC VS2019 < 16.10. +#elif defined(__cpp_consteval) +# define FMT_USE_CONSTEVAL 1 +#elif FMT_GCC_VERSION >= 1002 || FMT_CLANG_VERSION >= 1101 +# define FMT_USE_CONSTEVAL 1 +#else +# define FMT_USE_CONSTEVAL 0 +#endif +#if FMT_USE_CONSTEVAL +# define FMT_CONSTEVAL consteval # define FMT_CONSTEXPR20 constexpr #else +# define FMT_CONSTEVAL # define FMT_CONSTEXPR20 #endif -#ifndef FMT_CONSTEVAL -# if ((FMT_GCC_VERSION >= 1000 || FMT_CLANG_VERSION >= 1101) && \ - (!defined(__apple_build_version__) || \ - __apple_build_version__ >= 14000029L) && \ - FMT_CPLUSPLUS >= 202002L) || \ - (defined(__cpp_consteval) && \ - (!FMT_MSC_VERSION || FMT_MSC_VERSION >= 1929)) -// consteval is broken in MSVC before VS2019 version 16.10 and Apple clang -// before 14. -# define FMT_CONSTEVAL consteval -# define FMT_HAS_CONSTEVAL -# else -# define FMT_CONSTEVAL -# endif -#endif - -#ifdef FMT_DEPRECATED +// Check if exceptions are disabled. +#ifdef FMT_EXCEPTIONS // Use the provided definition. -#elif FMT_HAS_CPP14_ATTRIBUTE(deprecated) -# define FMT_DEPRECATED [[deprecated]] +#elif defined(__GNUC__) && !defined(__EXCEPTIONS) +# define FMT_EXCEPTIONS 0 +#elif FMT_MSC_VERSION && !_HAS_EXCEPTIONS +# define FMT_EXCEPTIONS 0 #else -# define FMT_DEPRECATED /* deprecated */ +# define FMT_EXCEPTIONS 1 +#endif +#if FMT_EXCEPTIONS +# define FMT_TRY try +# define FMT_CATCH(x) catch (x) +#else +# define FMT_TRY if (true) +# define FMT_CATCH(x) if (false) #endif // Disable [[noreturn]] on MSVC/NVCC because of bogus unreachable code warnings. -#if FMT_EXCEPTIONS && FMT_HAS_CPP_ATTRIBUTE(noreturn) && !FMT_MSC_VERSION && \ +#if FMT_HAS_CPP_ATTRIBUTE(noreturn) && FMT_EXCEPTIONS && !FMT_MSC_VERSION && \ !defined(__NVCC__) # define FMT_NORETURN [[noreturn]] #else @@ -178,6 +165,14 @@ # endif #endif +#ifdef FMT_DEPRECATED +// Use the provided definition. +#elif FMT_HAS_CPP14_ATTRIBUTE(deprecated) +# define FMT_DEPRECATED [[deprecated]] +#else +# define FMT_DEPRECATED /* deprecated */ +#endif + #ifdef FMT_INLINE // Use the provided definition. #elif FMT_GCC_VERSION || FMT_CLANG_VERSION @@ -186,6 +181,12 @@ # define FMT_INLINE inline #endif +#if FMT_GCC_VERSION || FMT_CLANG_VERSION +# define FMT_VISIBILITY(value) __attribute__((visibility(value))) +#else +# define FMT_VISIBILITY(value) +#endif + #ifndef FMT_GCC_PRAGMA // Workaround a _Pragma bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59884 // and an nvhpc warning: https://github.com/fmtlib/fmt/pull/2582. @@ -195,16 +196,13 @@ # define FMT_GCC_PRAGMA(arg) # endif #endif + #if FMT_MSC_VERSION # define FMT_MSC_WARNING(...) __pragma(warning(__VA_ARGS__)) -#else -# define FMT_MSC_WARNING(...) -#endif - -#ifdef _MSC_VER # define FMT_UNCHECKED_ITERATOR(It) \ using _Unchecked_type = It // Mark iterator as checked. #else +# define FMT_MSC_WARNING(...) # define FMT_UNCHECKED_ITERATOR(It) using unchecked_type = It #endif @@ -223,12 +221,6 @@ # define FMT_END_EXPORT #endif -#if FMT_GCC_VERSION || FMT_CLANG_VERSION -# define FMT_VISIBILITY(value) __attribute__((visibility(value))) -#else -# define FMT_VISIBILITY(value) -#endif - #if !defined(FMT_HEADER_ONLY) && defined(_WIN32) # if defined(FMT_LIB_EXPORT) # define FMT_API __declspec(dllexport) @@ -1272,7 +1264,7 @@ template class value { template FMT_CONSTEXPR20 FMT_INLINE value(T& val) { using value_type = remove_const_t; // T may overload operator& e.g. std::vector::reference in libc++. -#ifdef __cpp_if_constexpr +#if defined(__cpp_if_constexpr) if constexpr (std::is_same::value) custom.value = const_cast(&val); #endif @@ -2749,7 +2741,7 @@ template class basic_format_string { (std::is_base_of>::value && std::is_reference::value)...>() == 0, "passing views as lvalues is disallowed"); -#ifdef FMT_HAS_CONSTEVAL +#if FMT_USE_CONSTEVAL if constexpr (detail::count_named_args() == detail::count_statically_named_args()) { using checker = diff --git a/test/compile-fp-test.cc b/test/compile-fp-test.cc index 5b46d74bcd0ca..92c57af26ad06 100644 --- a/test/compile-fp-test.cc +++ b/test/compile-fp-test.cc @@ -8,10 +8,7 @@ #include "fmt/compile.h" #include "gmock/gmock.h" -#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806 && \ - defined(__cpp_constexpr) && __cpp_constexpr >= 201907 && \ - defined(__cpp_constexpr_dynamic_alloc) && \ - __cpp_constexpr_dynamic_alloc >= 201907 && FMT_CPLUSPLUS >= 202002L +#if FMT_USE_CONSTEVAL template struct test_string { template constexpr bool operator==(const T& rhs) const noexcept { @@ -60,4 +57,5 @@ TEST(compile_time_formatting_test, floating_point) { EXPECT_EQ("+inf", test_format<5>(FMT_COMPILE("{:+}"), inf)); EXPECT_EQ("-inf", test_format<5>(FMT_COMPILE("{}"), -inf)); } -#endif + +#endif // FMT_USE_CONSTEVAL diff --git a/test/compile-test.cc b/test/compile-test.cc index 4d83033402d23..a37dc7fac8625 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -290,18 +290,15 @@ TEST(compile_test, compile_format_string_literal) { } #endif -// MSVS 2019 19.29.30145.0 - Support C++20 and OK. +// MSVS 2019 19.29.30145.0 - OK // MSVS 2022 19.32.31332.0, 19.37.32826.1 - compile-test.cc(362,3): fatal error // C1001: Internal compiler error. // (compiler file // 'D:\a\_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\constexpr\constexpr.cpp', // line 8635) -#if (FMT_CPLUSPLUS >= 202002L || \ - (FMT_CPLUSPLUS >= 201709L && FMT_GCC_VERSION >= 1002)) && \ - ((!FMT_GLIBCXX_RELEASE || FMT_GLIBCXX_RELEASE >= 10) && \ - (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 10000) && \ - (!FMT_MSC_VERSION || \ - (FMT_MSC_VERSION >= 1928 && FMT_MSC_VERSION < 1930))) && \ +#if FMT_USE_CONSTEVAL && \ + (!FMT_MSC_VERSION || \ + (FMT_MSC_VERSION >= 1928 && FMT_MSC_VERSION < 1930))) && \ defined(__cpp_lib_is_constant_evaluated) template struct test_string { template constexpr bool operator==(const T& rhs) const noexcept {