diff --git a/llvm/include/llvm/ADT/Twine.h b/llvm/include/llvm/ADT/Twine.h --- a/llvm/include/llvm/ADT/Twine.h +++ b/llvm/include/llvm/ADT/Twine.h @@ -96,18 +96,9 @@ /// A pointer to a C string instance. CStringKind, - /// 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, SmallString and std::string. Can't use a StringRef here + /// because they are not trivally constructible. PtrAndLengthKind, /// A pointer to a formatv_object_base instance. @@ -149,9 +140,6 @@ const char *ptr; size_t length; } ptrAndLength; - const std::string *stdString; - const StringRef *stringRef; - const SmallVectorImpl *smallString; const formatv_object_base *formatvObject; char character; unsigned int decUI; @@ -290,8 +278,9 @@ /*implicit*/ Twine(std::nullptr_t) = delete; /// Construct from an std::string. - /*implicit*/ Twine(const std::string &Str) : LHSKind(StdStringKind) { - LHS.stdString = &Str; + /*implicit*/ Twine(const std::string &Str) : LHSKind(PtrAndLengthKind) { + LHS.ptrAndLength.ptr = Str.data(); + LHS.ptrAndLength.length = Str.length(); assert(isValid() && "Invalid twine!"); } @@ -309,15 +298,17 @@ #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 &Str) - : LHSKind(SmallStringKind) { - LHS.smallString = &Str; + : LHSKind(PtrAndLengthKind) { + LHS.ptrAndLength.ptr = Str.data(); + LHS.ptrAndLength.length = Str.size(); assert(isValid() && "Invalid twine!"); } @@ -380,16 +371,18 @@ /// 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!"); } @@ -434,9 +427,6 @@ switch (getLHSKind()) { case EmptyKind: case CStringKind: - case StdStringKind: - case StringRefKind: - case SmallStringKind: case PtrAndLengthKind: return true; default: @@ -470,12 +460,6 @@ return StringRef(); case CStringKind: 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 --- a/llvm/lib/Support/Twine.cpp +++ b/llvm/lib/Support/Twine.cpp @@ -15,10 +15,6 @@ using namespace llvm; std::string Twine::str() const { - // If we're storing only a std::string, just return it. - if (LHSKind == StdStringKind && RHSKind == EmptyKind) - return *LHS.stdString; - // If we're storing a formatv_object, we can avoid an extra copy by formatting // it immediately and returning the result. if (LHSKind == FormatvObjectKind && RHSKind == EmptyKind) @@ -35,18 +31,9 @@ } StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl &Out) const { - if (isUnary()) { - switch (getLHSKind()) { - case CStringKind: - // Already null terminated, yay! - return StringRef(LHS.cString); - case StdStringKind: { - const std::string *str = LHS.stdString; - return StringRef(str->c_str(), str->size()); - } - default: - break; - } + if (isUnary() && getLHSKind() == CStringKind) { + // Already null terminated, yay! + return StringRef(LHS.cString); } toVector(Out); Out.push_back(0); @@ -68,15 +55,6 @@ 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; - break; case Twine::FormatvObjectKind: OS << *Ptr.formatvObject; break; @@ -126,17 +104,6 @@ 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 << "\""; - break; case Twine::FormatvObjectKind: OS << "formatv:\"" << *Ptr.formatvObject << "\""; break; diff --git a/llvm/unittests/ADT/TwineTest.cpp b/llvm/unittests/ADT/TwineTest.cpp --- a/llvm/unittests/ADT/TwineTest.cpp +++ b/llvm/unittests/ADT/TwineTest.cpp @@ -68,13 +68,13 @@ 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 @@ 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 Pt:\"b\" cstring:\"c\"))", + repr(Twine("a").concat(Twine(SmallString<3>("b")).concat(Twine("c"))))); } TEST(TwineTest, toNullTerminatedStringRef) {