[llvm] 9697e46 - [ADT] Simplify StringRef(const char *) (NFC) (#160038)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 22 07:53:02 PDT 2025


Author: Kazu Hirata
Date: 2025-09-22T07:52:58-07:00
New Revision: 9697e4605cb1ca13d3d4b0e0bc91a69652c9a719

URL: https://github.com/llvm/llvm-project/commit/9697e4605cb1ca13d3d4b0e0bc91a69652c9a719
DIFF: https://github.com/llvm/llvm-project/commit/9697e4605cb1ca13d3d4b0e0bc91a69652c9a719.diff

LOG: [ADT] Simplify StringRef(const char *) (NFC) (#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.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringRef.h

Removed: 
    


################################################################################
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