Skip to content

Conversation

kazutakahirata
Copy link
Contributor

This patch delegates the string length computation to
std::string_view. This way, we don't have to worry about old GCC
versions or call __builtin_strlen on our own.

This patch delegates the string length computation to
std::string_view.  This way, we don't have to worry about old GCC
versions or call __builtin_strlen on our own.
@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

This patch delegates the string length computation to
std::string_view. This way, we don't have to worry about old GCC
versions or call __builtin_strlen on our own.


Full diff: https://github.com/llvm/llvm-project/pull/160038.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/StringRef.h (+1-9)
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 16aca4d45892d..49a52fbe1a6f7 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -90,15 +90,7 @@ namespace llvm {
 
     /// Construct a string ref from a cstring.
     /*implicit*/ constexpr StringRef(const char *Str LLVM_LIFETIME_BOUND)
-        : Data(Str), Length(Str ?
-    // GCC 7 doesn't have constexpr char_traits. Fall back to __builtin_strlen.
-#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
-                                __builtin_strlen(Str)
-#else
-                                std::char_traits<char>::length(Str)
-#endif
-                                : 0) {
-    }
+        : StringRef(Str ? std::string_view(Str) : std::string_view()) {}
 
     /// Construct a string ref from a pointer and length.
     /*implicit*/ constexpr StringRef(const char *data LLVM_LIFETIME_BOUND,

@kazutakahirata kazutakahirata merged commit 9697e46 into llvm:main Sep 22, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250921_ADT_StringRef branch September 22, 2025 14:53
SeongjaeP pushed a commit to SeongjaeP/llvm-project that referenced this pull request Sep 23, 2025
This patch delegates the string length computation to
std::string_view.  This way, we don't have to worry about old GCC
versions or call __builtin_strlen on our own.
YixingZhang007 pushed a commit to YixingZhang007/llvm-project that referenced this pull request Sep 27, 2025
This patch delegates the string length computation to
std::string_view.  This way, we don't have to worry about old GCC
versions or call __builtin_strlen on our own.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants