[libcxx-commits] [libcxx] 148ef80 - [libc++] Add GCC workaround in std::char_traits<char>::length()
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Dec 16 05:28:58 PST 2021
Author: Nikolas Klauser
Date: 2021-12-16T14:27:51+01:00
New Revision: 148ef80f895297975b95800f6fa989674767c5a2
URL: https://github.com/llvm/llvm-project/commit/148ef80f895297975b95800f6fa989674767c5a2
DIFF: https://github.com/llvm/llvm-project/commit/148ef80f895297975b95800f6fa989674767c5a2.diff
LOG: [libc++] Add GCC workaround in std::char_traits<char>::length()
GCC currently does not allow `__builtin_strlen()` during constant evaluation. This PR adds a workaround in `std::char_traits<char>::length()`
Reviewed By: Quuxplusone, ldionne, #libc, Mordante
Spies: Mordante, libcxx-commits
Differential Revision: https://reviews.llvm.org/D115795
Added:
Modified:
libcxx/include/__string
libcxx/test/std/strings/string.view/string.view.ops/copy.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/__string b/libcxx/include/__string
index e3fd0c270b2bc..2840819529ce0 100644
--- a/libcxx/include/__string
+++ b/libcxx/include/__string
@@ -337,8 +337,21 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char>
static _LIBCPP_CONSTEXPR_AFTER_CXX14
int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
- static inline size_t _LIBCPP_CONSTEXPR_AFTER_CXX14
- length(const char_type* __s) _NOEXCEPT {return __builtin_strlen(__s);}
+
+ static inline size_t _LIBCPP_CONSTEXPR_AFTER_CXX14 length(const char_type* __s) _NOEXCEPT {
+ // GCC currently does not support __builtin_strlen during constant evaluation.
+ // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70816
+#ifdef _LIBCPP_COMPILER_GCC
+ if (__libcpp_is_constant_evaluated()) {
+ size_t __i = 0;
+ for (; __s[__i] != char_type('\0'); ++__i)
+ ;
+ return __i;
+ }
+#endif
+ return __builtin_strlen(__s);
+ }
+
static _LIBCPP_CONSTEXPR_AFTER_CXX14
const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
diff --git a/libcxx/test/std/strings/string.view/string.view.ops/copy.pass.cpp b/libcxx/test/std/strings/string.view/string.view.ops/copy.pass.cpp
index d07e0b0d5255a..8e612db365275 100644
--- a/libcxx/test/std/strings/string.view/string.view.ops/copy.pass.cpp
+++ b/libcxx/test/std/strings/string.view/string.view.ops/copy.pass.cpp
@@ -6,9 +6,6 @@
//
//===----------------------------------------------------------------------===//
-// GCC's __builtin_strlen isn't constexpr yet
-// XFAIL: gcc-11 && !(c++11 || c++14 || c++17)
-
// <string_view>
// size_type copy(charT* s, size_type n, size_type pos = 0) const;
More information about the libcxx-commits
mailing list