LLVM
8.0.1
|
Twine - A lightweight data structure for efficiently representing the concatenation of temporary values as strings. More...
#include "llvm/ADT/Twine.h"
Public Member Functions | |
Predicate Operations | |
bool | isTriviallyEmpty () const |
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is empty. More... | |
bool | isSingleStringRef () const |
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStringRef(). More... | |
String Operations | |
Twine | concat (const Twine &Suffix) const |
Output & Conversion. | |
std::string | str () const |
Return the twine contents as a std::string. More... | |
void | toVector (SmallVectorImpl< char > &Out) const |
Append the concatenated string into the given SmallString or SmallVector. More... | |
StringRef | getSingleStringRef () const |
This returns the twine as a single StringRef. More... | |
StringRef | toStringRef (SmallVectorImpl< char > &Out) const |
This returns the twine as a single StringRef if it can be represented as such. More... | |
StringRef | toNullTerminatedStringRef (SmallVectorImpl< char > &Out) const |
This returns the twine as a single null terminated StringRef if it can be represented as such. More... | |
void | print (raw_ostream &OS) const |
Write the concatenated string represented by this twine to the stream OS . More... | |
void | dump () const |
Dump the concatenated string represented by this twine to stderr. More... | |
void | printRepr (raw_ostream &OS) const |
Write the representation of this twine to the stream OS . More... | |
void | dumpRepr () const |
Dump the representation of this twine to stderr. More... | |
Static Public Member Functions | |
Numeric Conversions | |
static Twine | utohexstr (const uint64_t &Val) |
Constructors | |
Twine () | |
Construct from an empty string. More... | |
Twine (const Twine &)=default | |
Twine (const char *Str) | |
Construct from a C string. More... | |
Twine (const std::string &Str) | |
Construct from an std::string. More... | |
Twine (const StringRef &Str) | |
Construct from a StringRef. More... | |
Twine (const SmallVectorImpl< char > &Str) | |
Construct from a SmallString. More... | |
Twine (const formatv_object_base &Fmt) | |
Construct from a formatv_object_base. More... | |
Twine (char Val) | |
Construct from a char. More... | |
Twine (signed char Val) | |
Construct from a signed char. More... | |
Twine (unsigned char Val) | |
Construct from an unsigned char. More... | |
Twine (unsigned Val) | |
Construct a twine to print Val as an unsigned decimal integer. More... | |
Twine (int Val) | |
Construct a twine to print Val as a signed decimal integer. More... | |
Twine (const unsigned long &Val) | |
Construct a twine to print Val as an unsigned decimal integer. More... | |
Twine (const long &Val) | |
Construct a twine to print Val as a signed decimal integer. More... | |
Twine (const unsigned long long &Val) | |
Construct a twine to print Val as an unsigned decimal integer. More... | |
Twine (const long long &Val) | |
Construct a twine to print Val as a signed decimal integer. More... | |
Twine (const char *LHS, const StringRef &RHS) | |
Construct as the concatenation of a C string and a StringRef. More... | |
Twine (const StringRef &LHS, const char *RHS) | |
Construct as the concatenation of a StringRef and a C string. More... | |
Twine & | operator= (const Twine &)=delete |
Since the intended use of twines is as temporary objects, assignments when concatenating might cause undefined behavior or stack corruptions. More... | |
static Twine | createNull () |
Create a 'null' string, which is an empty string that always concatenates to form another empty string. More... | |
Twine - A lightweight data structure for efficiently representing the concatenation of temporary values as strings.
A Twine is a kind of rope, it represents a concatenated string using a binary-tree, where the string is the preorder of the nodes. Since the Twine can be efficiently rendered into a buffer when its result is used, it avoids the cost of generating temporary values for intermediate string results – particularly in cases when the Twine result is never required. By explicitly tracking the type of leaf nodes, we can also avoid the creation of temporary strings for conversions operations (such as appending an integer to a string).
A Twine is not intended for use directly and should not be stored, its implementation relies on the ability to store pointers to temporary stack objects which may be deallocated at the end of a statement. Twines should only be used accepted as const references in arguments, when an API wishes to accept possibly-concatenated strings.
Twines support a special 'null' value, which always concatenates to form itself, and renders as an empty string. This can be returned from APIs to effectively nullify any concatenations performed on the result.
Implementation
Given the nature of a Twine, it is not possible for the Twine's concatenation method to construct interior nodes; the result must be represented inside the returned value. For this reason a Twine object actually holds two values, the left- and right-hand sides of a concatenation. We also have nullary Twine objects, which are effectively sentinel values that represent empty strings.
Thus, a Twine can effectively have zero, one, or two children. The
We maintain a number of invariants on Twine objects (FIXME: Why):
These invariants are check by
Efficiency Considerations
The Twine is designed to yield efficient and small code for common situations. For this reason, the concat() method is inlined so that concatenations of leaf nodes can be optimized into stores directly into a single stack allocated object.
In practice, not all compilers can be trusted to optimize concat() fully, so we provide two additional methods (and accompanying operator+ overloads) to guarantee that particularly important cases (cstring plus StringRef) codegen as desired.
|
inline |
Construct from an empty string.
Definition at line 257 of file Twine.h.
References assert().
Referenced by concat(), createNull(), llvm::operator+(), and utohexstr().
|
inline |
|
inline |
|
inline |
|
inlineexplicit |
|
inlineexplicit |
|
inlineexplicit |
|
inlineexplicit |
|
inlineexplicit |
|
inlineexplicit |
Construct as the concatenation of a StringRef and a C string.
Definition at line 363 of file Twine.h.
References assert(), and operator=().
Definition at line 486 of file Twine.h.
References Twine().
Referenced by encodeBase64StringEntry(), isSingleStringRef(), and llvm::operator+().
|
inlinestatic |
LLVM_DUMP_METHOD void Twine::dump | ( | ) | const |
Dump the concatenated string represented by this twine to stderr.
Definition at line 178 of file Twine.cpp.
References llvm::dbgs(), and print().
Referenced by toStringRef().
LLVM_DUMP_METHOD void Twine::dumpRepr | ( | ) | const |
Dump the representation of this twine to stderr.
Definition at line 182 of file Twine.cpp.
References llvm::dbgs(), and printRepr().
Referenced by toStringRef().
|
inline |
This returns the twine as a single StringRef.
This method is only valid if isSingleStringRef() is true.
Definition at line 437 of file Twine.h.
References assert(), isSingleStringRef(), and llvm_unreachable.
Referenced by llvm::sys::path::native(), toStringRef(), and truncateToSize().
|
inline |
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStringRef().
Definition at line 404 of file Twine.h.
References concat(), str(), and toVector().
Referenced by getSingleStringRef(), llvm::sys::path::native(), and toStringRef().
|
inline |
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is empty.
Definition at line 398 of file Twine.h.
Referenced by llvm::sys::path::append(), llvm::ModuleSummaryIndex::dumpSCCs(), llvm::MCContext::getELFSection(), llvm::Value::getName(), and llvm::MCContext::getWasmSection().
Since the intended use of twines is as temporary objects, assignments when concatenating might cause undefined behavior or stack corruptions.
Referenced by Twine().
void Twine::print | ( | raw_ostream & | OS | ) | const |
Write the concatenated string represented by this twine to the stream OS
.
Definition at line 164 of file Twine.cpp.
Referenced by dump(), llvm::DiagnosticPrinterRawOStream::operator<<(), llvm::operator<<(), toStringRef(), and toVector().
void Twine::printRepr | ( | raw_ostream & | OS | ) | const |
Write the representation of this twine to the stream OS
.
Definition at line 169 of file Twine.cpp.
Referenced by dumpRepr(), and toStringRef().
std::string Twine::str | ( | ) | const |
Return the twine contents as a std::string.
Definition at line 18 of file Twine.cpp.
References llvm::StringRef::str(), and toStringRef().
Referenced by canRenameComdat(), llvm::IndexedInstrProfReader::create(), llvm::MCContext::createELFRelSection(), llvm::DiagnosticInfoIROptimization::DiagnosticInfoIROptimization(), llvm::CFGMST< Edge, BBInfo >::dumpEdges(), llvm::ModuleSummaryIndex::dumpSCCs(), llvm::BufferByteStreamer::EmitInt8(), llvm::BufferByteStreamer::EmitSLEB128(), llvm::BufferByteStreamer::EmitULEB128(), llvm::MIRParserImpl::error(), llvm::format_provider< Twine >::format(), formatPax(), llvm::bfi_detail::getBlockName(), llvm::MCContext::getELFSection(), llvm::SourceMgr::GetMessage(), getNMDOps(), llvm::vfs::OverlayFileSystem::getRealPath(), getRedirectedFileStatus(), llvm::MCContext::getWasmSection(), isSingleStringRef(), malformedError(), llvm::Triple::normalize(), pathHasTraversal(), printLine(), readTriple(), llvm::report_fatal_error(), llvm::StringSaver::save(), llvm::UniqueStringSaver::save(), llvm::VPBlockBase::setName(), llvm::VPlan::setName(), llvm::HexagonTargetObjectFile::shouldPutJumpTableInFunctionSection(), toNullTerminatedStringRef(), and llvm::WriteGraph().
StringRef Twine::toNullTerminatedStringRef | ( | SmallVectorImpl< char > & | Out | ) | const |
This returns the twine as a single null terminated 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.
The returned StringRef's size does not include the null terminator.
Definition at line 38 of file Twine.cpp.
References llvm::SmallVectorTemplateCommon< T, typename >::data(), Kind, llvm::SmallVectorTemplateBase< T, bool >::pop_back(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::SmallVectorBase::size(), str(), toVector(), and llvm::raw_ostream::write_hex().
Referenced by llvm::sys::fs::createTemporaryFile(), llvm::detail::to_float(), and toStringRef().
|
inline |
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.
Definition at line 453 of file Twine.h.
References llvm::SmallVectorTemplateCommon< T, typename >::data(), dump(), dumpRepr(), getSingleStringRef(), isSingleStringRef(), print(), printRepr(), llvm::SmallVectorBase::size(), toNullTerminatedStringRef(), and toVector().
Referenced by llvm::sys::path::append(), llvm::sys::fs::create_directories(), llvm::sys::fs::directory_iterator::directory_iterator(), llvm::MCStreamer::EmitRawText(), llvm::MemoryBuffer::getFileOrSTDIN(), llvm::Value::getName(), llvm::Module::getNamedMetadata(), getNameWithPrefixImpl(), llvm::WritableMemoryBuffer::getNewUninitMemBuffer(), llvm::MCContext::getOrCreateSymbol(), llvm::sys::path::has_extension(), llvm::sys::path::has_filename(), llvm::sys::path::has_parent_path(), llvm::sys::path::has_relative_path(), llvm::sys::path::has_root_directory(), llvm::sys::path::has_root_name(), llvm::sys::path::has_root_path(), llvm::sys::path::has_stem(), llvm::sys::path::is_absolute(), llvm::MCContext::lookupSymbol(), llvm::opt::ArgList::MakeArgString(), llvm::sys::path::replace_extension(), and str().
void Twine::toVector | ( | SmallVectorImpl< char > & | Out | ) | const |
Append the concatenated string into the given SmallString or SmallVector.
Definition at line 33 of file Twine.cpp.
References print().
Referenced by llvm::MCAsmParser::addErrorSuffix(), createUniqueEntity(), encodeBase64StringEntry(), llvm::MCAsmParser::Error(), llvm::FileRemover::FileRemover(), llvm::vfs::InMemoryFileSystem::getRealPath(), isSingleStringRef(), llvm::vfs::lookupInMemoryNode(), llvm::vfs::RedirectingFileSystem::lookupPath(), llvm::sys::fs::make_absolute(), llvm::sys::path::native(), llvm::vfs::InMemoryFileSystem::setCurrentWorkingDirectory(), llvm::FileRemover::setFile(), toNullTerminatedStringRef(), llvm::vfs::InMemoryFileSystem::toString(), and toStringRef().
Definition at line 385 of file Twine.h.
References Twine().
Referenced by llvm::BTFDebug::BTFDebug(), llvm::GVNExpression::Expression::dump(), llvm::AppleAcceleratorTable::dump(), llvm::DWARFDebugNames::Abbrev::dump(), llvm::DWARFDebugNames::NameIndex::dump(), llvm::AsmPrinter::emitDwarfDIE(), llvm::BTFTypeBase::emitType(), llvm::BTFTypeInt::emitType(), llvm::BTFTypeStruct::emitType(), GetAEABIUnwindPersonalityName(), llvm::DWARFDebugNames::NameIndex::getHashArrayEntry(), llvm::object::ExportEntry::moveNext(), llvm::object::MachORebaseEntry::moveNext(), llvm::object::MachOBindEntry::moveNext(), llvm::DemandedBits::print(), llvm::MipsAsmPrinter::PrintAsmOperand(), llvm::MCSectionELF::PrintSwitchToSection(), llvm::AppleAcceleratorTable::readAtoms(), llvm::DemandedBitsWrapperPass::releaseMemory(), and llvm::AMDGPU::PALMD::toString().