X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FTwine.cpp;h=75cea2961a9dc68d8faccbbd4a6e99681c69283f;hb=84be958ed8db1ba2e0caca8964b7077761694f92;hp=2b0cf062ec875ddeecf55a270887a4a5d937ba3e;hpb=0165a2ca897598bb95baec031362921565e24f2b;p=oota-llvm.git diff --git a/lib/Support/Twine.cpp b/lib/Support/Twine.cpp index 2b0cf062ec8..75cea2961a9 100644 --- a/lib/Support/Twine.cpp +++ b/lib/Support/Twine.cpp @@ -8,65 +8,90 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/Twine.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; std::string Twine::str() const { - std::string Res; - raw_string_ostream OS(Res); - print(OS); - return Res; + SmallString<256> Vec; + return toStringRef(Vec).str(); } void Twine::toVector(SmallVectorImpl &Out) const { - // FIXME: This is very inefficient, since we are creating a large raw_ostream - // buffer -- hitting malloc, which we were supposed to avoid -- all when we - // have this pretty little small vector available. - // - // The best way to fix this is to make raw_svector_ostream do the right thing - // and be efficient, by augmenting the base raw_ostream with the ability to - // have the buffer managed by a concrete implementation. raw_svector_ostream OS(Out); print(OS); } -void Twine::printOneChild(raw_ostream &OS, const void *Ptr, +StringRef Twine::toStringRef(SmallVectorImpl &Out) const { + if (isSingleStringRef()) + return getSingleStringRef(); + toVector(Out); + return StringRef(Out.data(), Out.size()); +} + +StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl &Out) const { + if (isUnary()) { + switch (getLHSKind()) { + case CStringKind: + // Already null terminated, yay! + return StringRef(static_cast(LHS)); + case StdStringKind: { + const std::string *str = static_cast(LHS); + return StringRef(str->c_str(), str->size()); + } + default: + break; + } + } + toVector(Out); + Out.push_back(0); + Out.pop_back(); + return StringRef(Out.data(), Out.size()); +} + +void Twine::printOneChild(raw_ostream &OS, const void *Ptr, NodeKind Kind) const { switch (Kind) { case Twine::NullKind: break; case Twine::EmptyKind: break; case Twine::TwineKind: - static_cast(Ptr)->print(OS); + static_cast(Ptr)->print(OS); break; - case Twine::CStringKind: - OS << static_cast(Ptr); + case Twine::CStringKind: + OS << static_cast(Ptr); break; case Twine::StdStringKind: - OS << *static_cast(Ptr); + OS << *static_cast(Ptr); break; case Twine::StringRefKind: - OS << *static_cast(Ptr); + OS << *static_cast(Ptr); + break; + case Twine::DecUIKind: + OS << (unsigned)(uintptr_t)Ptr; + break; + case Twine::DecIKind: + OS << (int)(intptr_t)Ptr; break; - case Twine::UDec32Kind: - OS << *static_cast(Ptr); + case Twine::DecULKind: + OS << *static_cast(Ptr); break; - case Twine::SDec32Kind: - OS << *static_cast(Ptr); + case Twine::DecLKind: + OS << *static_cast(Ptr); break; - case Twine::UDec64Kind: - OS << *static_cast(Ptr); + case Twine::DecULLKind: + OS << *static_cast(Ptr); break; - case Twine::SDec64Kind: - OS << *static_cast(Ptr); + case Twine::DecLLKind: + OS << *static_cast(Ptr); break; case Twine::UHexKind: - // FIXME: Add raw_ostream functionality for this. - OS << ::utohexstr(*static_cast(Ptr)); + OS.write_hex(*static_cast(Ptr)); break; } } -void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, +void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, NodeKind Kind) const { switch (Kind) { case Twine::NullKind: @@ -89,20 +114,26 @@ void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, OS << "stringref:\"" << static_cast(Ptr) << "\""; break; - case Twine::UDec32Kind: - OS << "udec32:" << static_cast(Ptr) << "\""; + case Twine::DecUIKind: + OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\""; + break; + case Twine::DecIKind: + OS << "decI:\"" << (int)(intptr_t)Ptr << "\""; + break; + case Twine::DecULKind: + OS << "decUL:\"" << *static_cast(Ptr) << "\""; break; - case Twine::SDec32Kind: - OS << "sdec32:" << static_cast(Ptr) << "\""; + case Twine::DecLKind: + OS << "decL:\"" << *static_cast(Ptr) << "\""; break; - case Twine::UDec64Kind: - OS << "udec64:" << static_cast(Ptr) << "\""; + case Twine::DecULLKind: + OS << "decULL:\"" << *static_cast(Ptr) << "\""; break; - case Twine::SDec64Kind: - OS << "sdec64:" << static_cast(Ptr) << "\""; + case Twine::DecLLKind: + OS << "decLL:\"" << *static_cast(Ptr) << "\""; break; case Twine::UHexKind: - OS << "uhex:" << static_cast(Ptr) << "\""; + OS << "uhex:\"" << static_cast(Ptr) << "\""; break; } } @@ -121,9 +152,9 @@ void Twine::printRepr(raw_ostream &OS) const { } void Twine::dump() const { - print(llvm::errs()); + print(llvm::dbgs()); } void Twine::dumpRepr() const { - printRepr(llvm::errs()); + printRepr(llvm::dbgs()); }