[llvm] Revert "[ADT] Teach StringRef to derive from std::string_view" (PR #113767)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 26 10:31:46 PDT 2024


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/113767

Reverts llvm/llvm-project#113752

Many build bot failures

>From 2fc9c0dda365e578aa807b770bd16bd2316cab1b Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sat, 26 Oct 2024 10:31:26 -0700
Subject: [PATCH] Revert "[ADT] Teach StringRef to derive from std::string_view
 (#113752)"

This reverts commit a3181b11b5b758cfa852c0d27465f84ab3b079bb.
---
 llvm/include/llvm/ADT/StringRef.h | 41 +++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 49b6b8ff52abec..d5f30b88c4c6a2 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -48,9 +48,7 @@ namespace llvm {
   /// situations where the character data resides in some other buffer, whose
   /// lifetime extends past that of the StringRef. For this reason, it is not in
   /// general safe to store a StringRef.
-  class LLVM_GSL_POINTER StringRef : public std::string_view {
-    using Base = std::string_view;
-
+  class LLVM_GSL_POINTER StringRef {
   public:
     static constexpr size_t npos = ~size_t(0);
 
@@ -62,6 +60,12 @@ namespace llvm {
     using const_reverse_iterator = std::reverse_iterator<const_iterator>;
 
   private:
+    /// The start of the string, in an external buffer.
+    const char *Data = nullptr;
+
+    /// The length of the string.
+    size_t Length = 0;
+
     // Workaround memcmp issue with null pointers (undefined behavior)
     // by providing a specialized version
     static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
@@ -82,25 +86,27 @@ namespace llvm {
 
     /// Construct a string ref from a cstring.
     /*implicit*/ constexpr StringRef(const char *Str)
-        : Base(Str, Str ?
+        : 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)
+                                __builtin_strlen(Str)
 #else
-                        std::char_traits<char>::length(Str)
+                                std::char_traits<char>::length(Str)
 #endif
-                        : 0) {
+                                : 0) {
     }
 
     /// Construct a string ref from a pointer and length.
     /*implicit*/ constexpr StringRef(const char *data, size_t length)
-        : Base(data, length) {}
+        : Data(data), Length(length) {}
 
     /// Construct a string ref from an std::string.
-    /*implicit*/ StringRef(const std::string &Str) : Base(Str) {}
+    /*implicit*/ StringRef(const std::string &Str)
+        : Data(Str.data()), Length(Str.length()) {}
 
     /// Construct a string ref from an std::string_view.
-    /*implicit*/ constexpr StringRef(std::string_view Str) : Base(Str) {}
+    /*implicit*/ constexpr StringRef(std::string_view Str)
+        : Data(Str.data()), Length(Str.size()) {}
 
     /// @}
     /// @name Iterators
@@ -132,9 +138,16 @@ namespace llvm {
     /// @name String Operations
     /// @{
 
+    /// data - Get a pointer to the start of the string (which may not be null
+    /// terminated).
+    [[nodiscard]] constexpr const char *data() const { return Data; }
+
     /// empty - Check if the string is empty.
     [[nodiscard]] constexpr bool empty() const { return size() == 0; }
 
+    /// size - Get the string size.
+    [[nodiscard]] constexpr size_t size() const { return Length; }
+
     /// front - Get the first character in the string.
     [[nodiscard]] char front() const {
       assert(!empty());
@@ -235,6 +248,14 @@ namespace llvm {
     std::enable_if_t<std::is_same<T, std::string>::value, StringRef> &
     operator=(T &&Str) = delete;
 
+    /// @}
+    /// @name Type Conversions
+    /// @{
+
+    constexpr operator std::string_view() const {
+      return std::string_view(data(), size());
+    }
+
     /// @}
     /// @name String Predicates
     /// @{



More information about the llvm-commits mailing list