[llvm] edfdb5f - Consolidate string types into ptr and length representations.

Sterling Augustine via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 20 13:30:17 PDT 2021


Author: Sterling Augustine
Date: 2021-07-20T13:29:57-07:00
New Revision: edfdb5fcd10779905ef960c8d4aa61d46b57f451

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

LOG: Consolidate string types into ptr and length representations.

After rGbbbc4f110e35ac709b943efaa1c4c99ec073da30, we can move
any string type that has convenient pointer and length fields
into the PtrAndLengthKind, reducing the amount of code.

Differential Revision: https://reviews.llvm.org/D106381

Added: 
    

Modified: 
    llvm/include/llvm/ADT/Twine.h
    llvm/lib/Support/Twine.cpp
    llvm/unittests/ADT/TwineTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/Twine.h b/llvm/include/llvm/ADT/Twine.h
index 0885f9bb3593..6364a91454d3 100644
--- a/llvm/include/llvm/ADT/Twine.h
+++ b/llvm/include/llvm/ADT/Twine.h
@@ -99,15 +99,9 @@ namespace llvm {
       /// A pointer to an std::string instance.
       StdStringKind,
 
-      /// A pointer to a StringRef instance.
-      StringRefKind,
-
-      /// A pointer to a SmallString instance.
-      SmallStringKind,
-
-      /// A Pointer and Length representation. Used for std::string_view.
-      /// Can't use a StringRef here because they are not trivally
-      /// constructible.
+      /// A Pointer and Length representation. Used for std::string_view,
+      /// StringRef, and SmallString.  Can't use a StringRef here
+      /// because they are not trivally constructible.
       PtrAndLengthKind,
 
       /// A pointer to a formatv_object_base instance.
@@ -145,13 +139,11 @@ namespace llvm {
     {
       const Twine *twine;
       const char *cString;
+      const std::string *stdString;
       struct {
         const char *ptr;
         size_t length;
       } ptrAndLength;
-      const std::string *stdString;
-      const StringRef *stringRef;
-      const SmallVectorImpl<char> *smallString;
       const formatv_object_base *formatvObject;
       char character;
       unsigned int decUI;
@@ -309,15 +301,17 @@ namespace llvm {
 #endif
 
     /// Construct from a StringRef.
-    /*implicit*/ Twine(const StringRef &Str) : LHSKind(StringRefKind) {
-      LHS.stringRef = &Str;
+    /*implicit*/ Twine(const StringRef &Str) : LHSKind(PtrAndLengthKind) {
+      LHS.ptrAndLength.ptr = Str.data();
+      LHS.ptrAndLength.length = Str.size();
       assert(isValid() && "Invalid twine!");
     }
 
     /// Construct from a SmallString.
     /*implicit*/ Twine(const SmallVectorImpl<char> &Str)
-        : LHSKind(SmallStringKind) {
-      LHS.smallString = &Str;
+        : LHSKind(PtrAndLengthKind) {
+      LHS.ptrAndLength.ptr = Str.data();
+      LHS.ptrAndLength.length = Str.size();
       assert(isValid() && "Invalid twine!");
     }
 
@@ -380,16 +374,18 @@ namespace llvm {
 
     /// Construct as the concatenation of a C string and a StringRef.
     /*implicit*/ Twine(const char *LHS, const StringRef &RHS)
-        : LHSKind(CStringKind), RHSKind(StringRefKind) {
+        : LHSKind(CStringKind), RHSKind(PtrAndLengthKind) {
       this->LHS.cString = LHS;
-      this->RHS.stringRef = &RHS;
+      this->RHS.ptrAndLength.ptr = RHS.data();
+      this->RHS.ptrAndLength.length = RHS.size();
       assert(isValid() && "Invalid twine!");
     }
 
     /// Construct as the concatenation of a StringRef and a C string.
     /*implicit*/ Twine(const StringRef &LHS, const char *RHS)
-        : LHSKind(StringRefKind), RHSKind(CStringKind) {
-      this->LHS.stringRef = &LHS;
+        : LHSKind(PtrAndLengthKind), RHSKind(CStringKind) {
+      this->LHS.ptrAndLength.ptr = LHS.data();
+      this->LHS.ptrAndLength.length = LHS.size();
       this->RHS.cString = RHS;
       assert(isValid() && "Invalid twine!");
     }
@@ -435,8 +431,6 @@ namespace llvm {
       case EmptyKind:
       case CStringKind:
       case StdStringKind:
-      case StringRefKind:
-      case SmallStringKind:
       case PtrAndLengthKind:
         return true;
       default:
@@ -472,10 +466,6 @@ namespace llvm {
         return StringRef(LHS.cString);
       case StdStringKind:
         return StringRef(*LHS.stdString);
-      case StringRefKind:
-        return *LHS.stringRef;
-      case SmallStringKind:
-        return StringRef(LHS.smallString->data(), LHS.smallString->size());
       case PtrAndLengthKind:
         return StringRef(LHS.ptrAndLength.ptr, LHS.ptrAndLength.length);
       }

diff  --git a/llvm/lib/Support/Twine.cpp b/llvm/lib/Support/Twine.cpp
index 5cbf8a06da53..8bbfd0815a40 100644
--- a/llvm/lib/Support/Twine.cpp
+++ b/llvm/lib/Support/Twine.cpp
@@ -65,17 +65,11 @@ void Twine::printOneChild(raw_ostream &OS, Child Ptr,
   case Twine::CStringKind:
     OS << Ptr.cString;
     break;
-  case Twine::PtrAndLengthKind:
-    OS << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length);
-    break;
   case Twine::StdStringKind:
     OS << *Ptr.stdString;
     break;
-  case Twine::StringRefKind:
-    OS << *Ptr.stringRef;
-    break;
-  case Twine::SmallStringKind:
-    OS << *Ptr.smallString;
+  case Twine::PtrAndLengthKind:
+    OS << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length);
     break;
   case Twine::FormatvObjectKind:
     OS << *Ptr.formatvObject;
@@ -122,20 +116,13 @@ void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
     OS << "cstring:\""
        << Ptr.cString << "\"";
     break;
-  case Twine::PtrAndLengthKind:
-    OS << "ptrAndLength:\""
-       << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length) << "\"";
-    break;
   case Twine::StdStringKind:
     OS << "std::string:\""
        << Ptr.stdString << "\"";
     break;
-  case Twine::StringRefKind:
-    OS << "stringref:\""
-       << Ptr.stringRef << "\"";
-    break;
-  case Twine::SmallStringKind:
-    OS << "smallstring:\"" << *Ptr.smallString << "\"";
+  case Twine::PtrAndLengthKind:
+    OS << "ptrAndLength:\""
+       << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length) << "\"";
     break;
   case Twine::FormatvObjectKind:
     OS << "formatv:\"" << *Ptr.formatvObject << "\"";

diff  --git a/llvm/unittests/ADT/TwineTest.cpp b/llvm/unittests/ADT/TwineTest.cpp
index e0fb7e55142e..543b4507f27b 100644
--- a/llvm/unittests/ADT/TwineTest.cpp
+++ b/llvm/unittests/ADT/TwineTest.cpp
@@ -68,13 +68,13 @@ TEST(TwineTest, Concat) {
             repr(Twine("hi").concat(Twine())));
   EXPECT_EQ("(Twine cstring:\"hi\" empty)", 
             repr(Twine().concat(Twine("hi"))));
-  EXPECT_EQ("(Twine smallstring:\"hi\" empty)", 
+  EXPECT_EQ("(Twine ptrAndLength:\"hi\" empty)",
             repr(Twine().concat(Twine(SmallString<5>("hi")))));
   EXPECT_EQ("(Twine formatv:\"howdy\" empty)",
             repr(Twine(formatv("howdy")).concat(Twine())));
   EXPECT_EQ("(Twine formatv:\"howdy\" empty)",
             repr(Twine().concat(Twine(formatv("howdy")))));
-  EXPECT_EQ("(Twine smallstring:\"hey\" cstring:\"there\")", 
+  EXPECT_EQ("(Twine ptrAndLength:\"hey\" cstring:\"there\")",
             repr(Twine(SmallString<7>("hey")).concat(Twine("there"))));
 #if __cplusplus > 201402L
   EXPECT_EQ("(Twine ptrAndLength:\"hey\" cstring:\"there\")",
@@ -90,8 +90,9 @@ TEST(TwineTest, Concat) {
             repr(Twine("a").concat(Twine("b")).concat(Twine("c"))));
   EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))",
             repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
-  EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine smallstring:\"b\" cstring:\"c\"))",
-            repr(Twine("a").concat(Twine(SmallString<3>("b")).concat(Twine("c")))));
+  EXPECT_EQ(
+      "(Twine cstring:\"a\" rope:(Twine ptrAndLength:\"b\" cstring:\"c\"))",
+      repr(Twine("a").concat(Twine(SmallString<3>("b")).concat(Twine("c")))));
 }
 
 TEST(TwineTest, toNullTerminatedStringRef) {


        


More information about the llvm-commits mailing list