From e75a5955b226d1c9fe45797b7581d7d5d55149ea Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 21 Feb 2024 14:33:59 +0100 Subject: [PATCH] Rename qIsConstantEvaluated() to q20::is_constant_evaluated() We should really try to avoid another almost-std-compatible-but-not-quite idiom. When qIsConstantEvaluated() was added, the rationale was given that this cannot be q20, because we can't implement it in all compilers. But we can: returning false is a perfectly legal implementation, and makes most users actually simpler because the #ifdef'ery can be dropped. There are only two users that still require the macro, because either they do different fallbacks depending on whether the implementation is trivial, or because they direct expected test outcomes. The INTEGRITY compiler complains "calling __has_builtin() in a constant expression", which we currently don't understand. To unblock this patch, and therefore the 6.7 release, hard-code INTEGRITY to return false. Amends 95e6fac0a5a1eee3aa23e4da0a93c6c25e32fb98. Change-Id: If6cae902ff434f2ccceb6057cb053b7f304a604c Reviewed-by: Thiago Macieira Reviewed-by: Ivan Solovev Reviewed-by: Ahmad Samir Reviewed-by: Qt CI Bot (cherry picked from commit 4b806d678e68c786f4be3809c72d396ae15bb04c) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/global/q20type_traits.h | 25 +++++++++++++++++++++++++ src/corelib/global/qttypetraits.h | 17 ----------------- src/corelib/text/qanystringview.h | 12 ++---------- src/corelib/text/qstringalgorithms.h | 6 ++++-- src/corelib/text/qstringview.h | 4 +--- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/corelib/global/q20type_traits.h b/src/corelib/global/q20type_traits.h index 80ac1fa4313..63a453daca8 100644 --- a/src/corelib/global/q20type_traits.h +++ b/src/corelib/global/q20type_traits.h @@ -3,6 +3,8 @@ #ifndef Q20TYPE_TRAITS_H #define Q20TYPE_TRAITS_H +#include +#include #include // @@ -25,6 +27,29 @@ QT_BEGIN_NAMESPACE +namespace q20 { +// like std::is_constant_evaluated +#ifdef __cpp_lib_is_constant_evaluated +using std::is_constant_evaluated; +#define QT_SUPPORTS_IS_CONSTANT_EVALUATED +#else +constexpr bool is_constant_evaluated() noexcept +{ +#ifdef Q_OS_INTEGRITY + // Integrity complains "calling __has_builtin() from a constant expression". + // Avoid the __has_builtin check until we know what's going on. + return false; +#elif __has_builtin(__builtin_is_constant_evaluated) || \ + (defined(Q_CC_MSVC_ONLY) /* >= 1925, but we require 1927 in qglobal.h */) +# define QT_SUPPORTS_IS_CONSTANT_EVALUATED + return __builtin_is_constant_evaluated(); +#else + return false; +#endif +} +#endif // __cpp_lib_is_constant_evaluated +} + namespace q20 { // like std::remove_cvref(_t) #ifdef __cpp_lib_remove_cvref diff --git a/src/corelib/global/qttypetraits.h b/src/corelib/global/qttypetraits.h index 9129f536aa8..49f2728e9f5 100644 --- a/src/corelib/global/qttypetraits.h +++ b/src/corelib/global/qttypetraits.h @@ -17,23 +17,6 @@ QT_BEGIN_NAMESPACE -// like std::is_constant_evaluated -#define QT_SUPPORTS_IS_CONSTANT_EVALUATED -#ifdef __cpp_lib_is_constant_evaluated -constexpr bool qIsConstantEvaluated() noexcept -{ - return std::is_constant_evaluated(); -} -#elif __has_builtin(__builtin_is_constant_evaluated) || \ - (defined(Q_CC_MSVC_ONLY) /* >= 1925, but we require 1927 in qglobal.h */) -constexpr bool qIsConstantEvaluated() noexcept -{ - return __builtin_is_constant_evaluated(); -} -#else -# undef QT_SUPPORTS_IS_CONSTANT_EVALUATED -#endif - // like std::to_underlying template constexpr std::underlying_type_t qToUnderlying(Enum e) noexcept diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index 0d064c6cd38..4a5e80fa18e 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -101,12 +101,7 @@ private: static constexpr bool isAsciiOnlyCharsAtCompileTime(Char *str, qsizetype sz) noexcept { // do not perform check if not at compile time -#if !defined(QT_SUPPORTS_IS_CONSTANT_EVALUATED) - Q_UNUSED(str); - Q_UNUSED(sz); - return false; -#else - if (!qIsConstantEvaluated()) + if (!q20::is_constant_evaluated()) return false; if constexpr (sizeof(Char) != sizeof(char)) { Q_UNUSED(str); @@ -119,7 +114,6 @@ private: } return true; } -#endif } template @@ -137,10 +131,8 @@ private: template static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept { -#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED - if (qIsConstantEvaluated()) + if (q20::is_constant_evaluated()) return qsizetype(std::char_traits::length(str)); -#endif if constexpr (sizeof(Char) == sizeof(char16_t)) return QtPrivate::qustrlen(reinterpret_cast(str)); else diff --git a/src/corelib/text/qstringalgorithms.h b/src/corelib/text/qstringalgorithms.h index dada744f07a..50edff2e479 100644 --- a/src/corelib/text/qstringalgorithms.h +++ b/src/corelib/text/qstringalgorithms.h @@ -16,6 +16,8 @@ #include // for memchr +#include // q20::is_constant_evaluated + QT_BEGIN_NAMESPACE namespace QtPrivate { @@ -165,7 +167,7 @@ lengthHelperContainer(const Char (&str)[N]) return str[0] == Char(0) ? 0 : 1; } else if constexpr (N > RuntimeThreshold) { #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED - if (!qIsConstantEvaluated()) + if (!q20::is_constant_evaluated()) return QtPrivate::qustrnlen(reinterpret_cast(str), N); #endif } @@ -177,7 +179,7 @@ template [[nodiscard]] constexpr inline std::enable_if_t lengthHelperContainer(const Char (&str)[N]) { #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED - if (!qIsConstantEvaluated()) + if (!q20::is_constant_evaluated()) return qstrnlen(reinterpret_cast(str), N); #endif diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 3d1853c7a00..e91bb6978c6 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -103,10 +103,8 @@ private: template static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept { -#if defined(QT_SUPPORTS_IS_CONSTANT_EVALUATED) - if (qIsConstantEvaluated()) + if (q20::is_constant_evaluated()) return std::char_traits::length(str); -#endif return QtPrivate::qustrlen(reinterpret_cast(str)); } static qsizetype lengthHelperPointer(const QChar *str) noexcept