Skip to content

Commit 5217495

Browse files
committed
Cleanup conseval detection
1 parent b71d987 commit 5217495

File tree

3 files changed

+61
-74
lines changed

3 files changed

+61
-74
lines changed

include/fmt/base.h

+54-62
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,6 @@
5454
# define FMT_LIBCPP_VERSION 0
5555
#endif
5656

57-
// Check if exceptions are disabled.
58-
#ifdef FMT_EXCEPTIONS
59-
// Use the provided definition.
60-
#elif defined(__GNUC__) && !defined(__EXCEPTIONS)
61-
# define FMT_EXCEPTIONS 0
62-
#elif FMT_MSC_VERSION && !_HAS_EXCEPTIONS
63-
# define FMT_EXCEPTIONS 0
64-
#else
65-
# define FMT_EXCEPTIONS 1
66-
#endif
67-
#if FMT_EXCEPTIONS
68-
# define FMT_TRY try
69-
# define FMT_CATCH(x) catch (x)
70-
#else
71-
# define FMT_TRY if (true)
72-
# define FMT_CATCH(x) if (false)
73-
#endif
74-
7557
#ifdef _MSVC_LANG
7658
# define FMT_CPLUSPLUS _MSVC_LANG
7759
#else
@@ -121,49 +103,54 @@
121103
# define FMT_CONSTEXPR
122104
#endif
123105

124-
// Detect C++20 extensions to constexpr and std::is_constant_evaluated.
125-
#ifndef __cpp_lib_is_constant_evaluated
126-
# define FMT_CONSTEXPR20
106+
// Detect consteval, C++20 constexpr extensions and std::is_constant_evaluated.
107+
#if !defined(__cpp_lib_is_constant_evaluated)
108+
# define FMT_USE_CONSTEVAL 0
109+
#elif FMT_CPLUSPLUS < 201709L
110+
# define FMT_USE_CONSTEVAL 0
127111
#elif FMT_GLIBCXX_RELEASE && FMT_GLIBCXX_RELEASE < 10
128-
# define FMT_CONSTEXPR20
112+
# define FMT_USE_CONSTEVAL 0
129113
#elif FMT_LIBCPP_VERSION && FMT_LIBCPP_VERSION < 10000
130-
# define FMT_CONSTEXPR20
131-
#elif FMT_MSC_VERSION && FMT_MSC_VERSION < 1928
132-
# define FMT_CONSTEXPR20
133-
#elif FMT_CPLUSPLUS >= 202002L
134-
# define FMT_CONSTEXPR20 constexpr
135-
#elif FMT_CPLUSPLUS >= 201709L && FMT_GCC_VERSION >= 1002
114+
# define FMT_USE_CONSTEVAL 0
115+
#elif defined(__apple_build_version__) && __apple_build_version__ < 14000029L
116+
# define FMT_USE_CONSTEVAL 0 // consteval is broken in Apple clang < 14.
117+
#elif FMT_MSC_VERSION && FMT_MSC_VERSION < 1929
118+
# define FMT_USE_CONSTEVAL 0 // consteval is broken in MSVC VS2019 < 16.10.
119+
#elif defined(__cpp_consteval)
120+
# define FMT_USE_CONSTEVAL 1
121+
#elif FMT_GCC_VERSION >= 1002 || FMT_CLANG_VERSION >= 1101
122+
# define FMT_USE_CONSTEVAL 1
123+
#else
124+
# define FMT_USE_CONSTEVAL 0
125+
#endif
126+
#if FMT_USE_CONSTEVAL
127+
# define FMT_CONSTEVAL consteval
136128
# define FMT_CONSTEXPR20 constexpr
137129
#else
130+
# define FMT_CONSTEVAL
138131
# define FMT_CONSTEXPR20
139132
#endif
140133

141-
#ifndef FMT_CONSTEVAL
142-
# if ((FMT_GCC_VERSION >= 1000 || FMT_CLANG_VERSION >= 1101) && \
143-
(!defined(__apple_build_version__) || \
144-
__apple_build_version__ >= 14000029L) && \
145-
FMT_CPLUSPLUS >= 202002L) || \
146-
(defined(__cpp_consteval) && \
147-
(!FMT_MSC_VERSION || FMT_MSC_VERSION >= 1929))
148-
// consteval is broken in MSVC before VS2019 version 16.10 and Apple clang
149-
// before 14.
150-
# define FMT_CONSTEVAL consteval
151-
# define FMT_HAS_CONSTEVAL
152-
# else
153-
# define FMT_CONSTEVAL
154-
# endif
155-
#endif
156-
157-
#ifdef FMT_DEPRECATED
134+
// Check if exceptions are disabled.
135+
#ifdef FMT_EXCEPTIONS
158136
// Use the provided definition.
159-
#elif FMT_HAS_CPP14_ATTRIBUTE(deprecated)
160-
# define FMT_DEPRECATED [[deprecated]]
137+
#elif defined(__GNUC__) && !defined(__EXCEPTIONS)
138+
# define FMT_EXCEPTIONS 0
139+
#elif FMT_MSC_VERSION && !_HAS_EXCEPTIONS
140+
# define FMT_EXCEPTIONS 0
161141
#else
162-
# define FMT_DEPRECATED /* deprecated */
142+
# define FMT_EXCEPTIONS 1
143+
#endif
144+
#if FMT_EXCEPTIONS
145+
# define FMT_TRY try
146+
# define FMT_CATCH(x) catch (x)
147+
#else
148+
# define FMT_TRY if (true)
149+
# define FMT_CATCH(x) if (false)
163150
#endif
164151

165152
// Disable [[noreturn]] on MSVC/NVCC because of bogus unreachable code warnings.
166-
#if FMT_EXCEPTIONS && FMT_HAS_CPP_ATTRIBUTE(noreturn) && !FMT_MSC_VERSION && \
153+
#if FMT_HAS_CPP_ATTRIBUTE(noreturn) && FMT_EXCEPTIONS && !FMT_MSC_VERSION && \
167154
!defined(__NVCC__)
168155
# define FMT_NORETURN [[noreturn]]
169156
#else
@@ -178,6 +165,14 @@
178165
# endif
179166
#endif
180167

168+
#ifdef FMT_DEPRECATED
169+
// Use the provided definition.
170+
#elif FMT_HAS_CPP14_ATTRIBUTE(deprecated)
171+
# define FMT_DEPRECATED [[deprecated]]
172+
#else
173+
# define FMT_DEPRECATED /* deprecated */
174+
#endif
175+
181176
#ifdef FMT_INLINE
182177
// Use the provided definition.
183178
#elif FMT_GCC_VERSION || FMT_CLANG_VERSION
@@ -186,6 +181,12 @@
186181
# define FMT_INLINE inline
187182
#endif
188183

184+
#if FMT_GCC_VERSION || FMT_CLANG_VERSION
185+
# define FMT_VISIBILITY(value) __attribute__((visibility(value)))
186+
#else
187+
# define FMT_VISIBILITY(value)
188+
#endif
189+
189190
#ifndef FMT_GCC_PRAGMA
190191
// Workaround a _Pragma bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59884
191192
// and an nvhpc warning: https://github.com/fmtlib/fmt/pull/2582.
@@ -195,16 +196,13 @@
195196
# define FMT_GCC_PRAGMA(arg)
196197
# endif
197198
#endif
199+
198200
#if FMT_MSC_VERSION
199201
# define FMT_MSC_WARNING(...) __pragma(warning(__VA_ARGS__))
200-
#else
201-
# define FMT_MSC_WARNING(...)
202-
#endif
203-
204-
#ifdef _MSC_VER
205202
# define FMT_UNCHECKED_ITERATOR(It) \
206203
using _Unchecked_type = It // Mark iterator as checked.
207204
#else
205+
# define FMT_MSC_WARNING(...)
208206
# define FMT_UNCHECKED_ITERATOR(It) using unchecked_type = It
209207
#endif
210208

@@ -223,12 +221,6 @@
223221
# define FMT_END_EXPORT
224222
#endif
225223

226-
#if FMT_GCC_VERSION || FMT_CLANG_VERSION
227-
# define FMT_VISIBILITY(value) __attribute__((visibility(value)))
228-
#else
229-
# define FMT_VISIBILITY(value)
230-
#endif
231-
232224
#if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
233225
# if defined(FMT_LIB_EXPORT)
234226
# define FMT_API __declspec(dllexport)
@@ -1272,7 +1264,7 @@ template <typename Context> class value {
12721264
template <typename T> FMT_CONSTEXPR20 FMT_INLINE value(T& val) {
12731265
using value_type = remove_const_t<T>;
12741266
// T may overload operator& e.g. std::vector<bool>::reference in libc++.
1275-
#ifdef __cpp_if_constexpr
1267+
#if defined(__cpp_if_constexpr)
12761268
if constexpr (std::is_same<decltype(&val), T*>::value)
12771269
custom.value = const_cast<value_type*>(&val);
12781270
#endif
@@ -2749,7 +2741,7 @@ template <typename Char, typename... Args> class basic_format_string {
27492741
(std::is_base_of<detail::view, remove_reference_t<Args>>::value &&
27502742
std::is_reference<Args>::value)...>() == 0,
27512743
"passing views as lvalues is disallowed");
2752-
#ifdef FMT_HAS_CONSTEVAL
2744+
#if FMT_USE_CONSTEVAL
27532745
if constexpr (detail::count_named_args<Args...>() ==
27542746
detail::count_statically_named_args<Args...>()) {
27552747
using checker =

test/compile-fp-test.cc

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
#include "fmt/compile.h"
99
#include "gmock/gmock.h"
1010

11-
#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806 && \
12-
defined(__cpp_constexpr) && __cpp_constexpr >= 201907 && \
13-
defined(__cpp_constexpr_dynamic_alloc) && \
14-
__cpp_constexpr_dynamic_alloc >= 201907 && FMT_CPLUSPLUS >= 202002L
11+
#if FMT_USE_CONSTEVAL
1512

1613
template <size_t max_string_length, typename Char = char> struct test_string {
1714
template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
@@ -60,4 +57,5 @@ TEST(compile_time_formatting_test, floating_point) {
6057
EXPECT_EQ("+inf", test_format<5>(FMT_COMPILE("{:+}"), inf));
6158
EXPECT_EQ("-inf", test_format<5>(FMT_COMPILE("{}"), -inf));
6259
}
63-
#endif
60+
61+
#endif // FMT_USE_CONSTEVAL

test/compile-test.cc

+4-7
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,15 @@ TEST(compile_test, compile_format_string_literal) {
290290
}
291291
#endif
292292

293-
// MSVS 2019 19.29.30145.0 - Support C++20 and OK.
293+
// MSVS 2019 19.29.30145.0 - OK
294294
// MSVS 2022 19.32.31332.0, 19.37.32826.1 - compile-test.cc(362,3): fatal error
295295
// C1001: Internal compiler error.
296296
// (compiler file
297297
// 'D:\a\_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\constexpr\constexpr.cpp',
298298
// line 8635)
299-
#if (FMT_CPLUSPLUS >= 202002L || \
300-
(FMT_CPLUSPLUS >= 201709L && FMT_GCC_VERSION >= 1002)) && \
301-
((!FMT_GLIBCXX_RELEASE || FMT_GLIBCXX_RELEASE >= 10) && \
302-
(!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 10000) && \
303-
(!FMT_MSC_VERSION || \
304-
(FMT_MSC_VERSION >= 1928 && FMT_MSC_VERSION < 1930))) && \
299+
#if FMT_USE_CONSTEVAL && \
300+
(!FMT_MSC_VERSION || \
301+
(FMT_MSC_VERSION >= 1928 && FMT_MSC_VERSION < 1930)) && \
305302
defined(__cpp_lib_is_constant_evaluated)
306303
template <size_t max_string_length, typename Char = char> struct test_string {
307304
template <typename T> constexpr bool operator==(const T& rhs) const noexcept {

0 commit comments

Comments
 (0)