Index: include/llvm/ADT/Twine.h =================================================================== --- include/llvm/ADT/Twine.h +++ include/llvm/ADT/Twine.h @@ -7,12 +7,13 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_ADT_TWINE_H -#define LLVM_ADT_TWINE_H - -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/DataTypes.h" -#include "llvm/Support/ErrorHandling.h" +#ifndef LLVM_ADT_TWINE_H +#define LLVM_ADT_TWINE_H + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/DataTypes.h" +#include "llvm/Support/ErrorHandling.h" #include #include @@ -97,12 +98,15 @@ /// A pointer to an std::string instance. StdStringKind, - /// A pointer to a StringRef instance. - StringRefKind, - - /// A char value reinterpreted as a pointer, to render as a character. - CharKind, - + /// A pointer to a StringRef instance. + StringRefKind, + + /// A pointer to a SmallString instance. + SmallStringKind, + + /// A char value reinterpreted as a pointer, to render as a character. + CharKind, + /// An unsigned int value reinterpreted as a pointer, to render as an /// unsigned decimal integer. DecUIKind, @@ -133,12 +137,13 @@ union Child { const Twine *twine; - const char *cString; - const std::string *stdString; - const StringRef *stringRef; - char character; - unsigned int decUI; - int decI; + const char *cString; + const std::string *stdString; + const StringRef *stringRef; + const SmallVectorImpl *smallString; + char character; + unsigned int decUI; + int decI; const unsigned long *decUL; const long *decL; const unsigned long long *decULL; @@ -286,12 +291,19 @@ /*implicit*/ Twine(const StringRef &Str) : LHSKind(StringRefKind), RHSKind(EmptyKind) { LHS.stringRef = &Str; - assert(isValid() && "Invalid twine!"); - } - - /// Construct from a char. - explicit Twine(char Val) - : LHSKind(CharKind), RHSKind(EmptyKind) { + assert(isValid() && "Invalid twine!"); + } + + /// Construct from a SmallString. + /*implicit*/ Twine(const SmallVectorImpl &Str) + : LHSKind(SmallStringKind), RHSKind(EmptyKind) { + LHS.smallString = &Str; + assert(isValid() && "Invalid twine!"); + } + + /// Construct from a char. + explicit Twine(char Val) + : LHSKind(CharKind), RHSKind(EmptyKind) { LHS.character = Val; } @@ -399,12 +411,13 @@ switch (getLHSKind()) { case EmptyKind: - case CStringKind: - case StdStringKind: - case StringRefKind: - return true; - default: - return false; + case CStringKind: + case StdStringKind: + case StringRefKind: + case SmallStringKind: + return true; + default: + return false; } } @@ -432,12 +445,13 @@ switch (getLHSKind()) { default: llvm_unreachable("Out of sync with isSingleStringRef"); case EmptyKind: return StringRef(); - case CStringKind: return StringRef(LHS.cString); - case StdStringKind: return StringRef(*LHS.stdString); - case StringRefKind: return *LHS.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()); + } + } + /// toStringRef - This returns the twine as a single StringRef if it can be /// represented as such. Otherwise the twine is written into the given /// SmallVector and a StringRef to the SmallVector's data is returned. Index: include/llvm/Support/raw_ostream.h =================================================================== --- include/llvm/Support/raw_ostream.h +++ include/llvm/Support/raw_ostream.h @@ -14,6 +14,7 @@ #ifndef LLVM_SUPPORT_RAW_OSTREAM_H #define LLVM_SUPPORT_RAW_OSTREAM_H +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/DataTypes.h" @@ -185,6 +186,10 @@ return write(Str.data(), Str.length()); } + raw_ostream &operator<<(const llvm::SmallVectorImpl &Str) { + return write(Str.data(), Str.size()); + } + raw_ostream &operator<<(unsigned long N); raw_ostream &operator<<(long N); raw_ostream &operator<<(unsigned long long N); Index: lib/Support/Twine.cpp =================================================================== --- lib/Support/Twine.cpp +++ lib/Support/Twine.cpp @@ -69,12 +69,15 @@ case Twine::StdStringKind: OS << *Ptr.stdString; break; - case Twine::StringRefKind: - OS << *Ptr.stringRef; - break; - case Twine::CharKind: - OS << Ptr.character; - break; + case Twine::StringRefKind: + OS << *Ptr.stringRef; + break; + case Twine::SmallStringKind: + OS << *Ptr.smallString; + break; + case Twine::CharKind: + OS << Ptr.character; + break; case Twine::DecUIKind: OS << Ptr.decUI; break; @@ -119,12 +122,16 @@ << Ptr.stdString << "\""; break; case Twine::StringRefKind: - OS << "stringref:\"" - << Ptr.stringRef << "\""; - break; - case Twine::CharKind: - OS << "char:\"" << Ptr.character << "\""; - break; + OS << "stringref:\"" + << Ptr.stringRef << "\""; + break; + case Twine::SmallStringKind: + OS << "smallstring:\"" + << Ptr.smallString << "\""; + break; + case Twine::CharKind: + OS << "char:\"" << Ptr.character << "\""; + break; case Twine::DecUIKind: OS << "decUI:\"" << Ptr.decUI << "\""; break;