[llvm] [ADT] Simplify StringRef(const char *) (NFC) (PR #160038)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 21 23:56:14 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/160038

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.


>From 4e4259805e12b62313e53d8a848b05b652f93ce2 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 21 Sep 2025 22:38:11 -0700
Subject: [PATCH] [ADT] Simplify StringRef(const char *) (NFC)

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.
---
 llvm/include/llvm/ADT/StringRef.h | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

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,



More information about the llvm-commits mailing list