summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAlp Toker <alp@nuanti.com>2014-06-26 00:00:48 +0000
committerAlp Toker <alp@nuanti.com>2014-06-26 00:00:48 +0000
commit255907042245b77779e3e38c5ce66901866cabe5 (patch)
tree92db65aa99c37ab5f68cd6adaae1d8cf6d629b8f /include
parentce6e7c7a59e8595ea3f30d87cbad4af278cb5ef3 (diff)
downloadllvm-255907042245b77779e3e38c5ce66901866cabe5.tar.gz
llvm-255907042245b77779e3e38c5ce66901866cabe5.tar.bz2
llvm-255907042245b77779e3e38c5ce66901866cabe5.tar.xz
Introduce a string_ostream string builder facilty
string_ostream is a safe and efficient string builder that combines opaque stack storage with a built-in ostream interface. small_string_ostream<bytes> additionally permits an explicit stack storage size other than the default 128 bytes to be provided. Beyond that, storage is transferred to the heap. This convenient class can be used in most places an std::string+raw_string_ostream pair or SmallString<>+raw_svector_ostream pair would previously have been used, in order to guarantee consistent access without byte truncation. The patch also converts much of LLVM to use the new facility. These changes include several probable bug fixes for truncated output, a programming error that's no longer possible with the new interface. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211749 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r--include/llvm/Analysis/CFGPrinter.h10
-rw-r--r--include/llvm/ExecutionEngine/ObjectBuffer.h11
-rw-r--r--include/llvm/Support/GraphWriter.h3
-rw-r--r--include/llvm/Support/YAMLTraits.h3
-rw-r--r--include/llvm/Support/raw_ostream.h28
-rw-r--r--include/llvm/TableGen/StringToOffsetTable.h4
6 files changed, 38 insertions, 21 deletions
diff --git a/include/llvm/Analysis/CFGPrinter.h b/include/llvm/Analysis/CFGPrinter.h
index e6d2ed1a68..759afcaee1 100644
--- a/include/llvm/Analysis/CFGPrinter.h
+++ b/include/llvm/Analysis/CFGPrinter.h
@@ -36,9 +36,7 @@ struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
if (!Node->getName().empty())
return Node->getName().str();
- std::string Str;
- raw_string_ostream OS(Str);
-
+ string_ostream OS;
Node->printAsOperand(OS, false);
return OS.str();
}
@@ -46,8 +44,7 @@ struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
static std::string getCompleteNodeLabel(const BasicBlock *Node,
const Function *) {
enum { MaxColumns = 80 };
- std::string Str;
- raw_string_ostream OS(Str);
+ string_ostream OS;
if (Node->getName().empty()) {
Node->printAsOperand(OS, false);
@@ -109,8 +106,7 @@ struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
if (SuccNo == 0) return "def";
- std::string Str;
- raw_string_ostream OS(Str);
+ string_ostream OS;
SwitchInst::ConstCaseIt Case =
SwitchInst::ConstCaseIt::fromSuccessorIndex(SI, SuccNo);
OS << Case.getCaseValue()->getValue();
diff --git a/include/llvm/ExecutionEngine/ObjectBuffer.h b/include/llvm/ExecutionEngine/ObjectBuffer.h
index 071a42b6b7..81dc88dcd0 100644
--- a/include/llvm/ExecutionEngine/ObjectBuffer.h
+++ b/include/llvm/ExecutionEngine/ObjectBuffer.h
@@ -58,23 +58,18 @@ protected:
class ObjectBufferStream : public ObjectBuffer {
void anchor() override;
public:
- ObjectBufferStream() : OS(SV) {}
+ ObjectBufferStream() {}
virtual ~ObjectBufferStream() {}
raw_ostream &getOStream() { return OS; }
void flush()
{
- OS.flush();
-
// Make the data accessible via the ObjectBuffer::Buffer
- Buffer.reset(MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()),
- "",
- false));
+ Buffer.reset(MemoryBuffer::getMemBuffer(OS.str(), "", false));
}
protected:
- SmallVector<char, 4096> SV; // Working buffer into which we JIT.
- raw_svector_ostream OS; // streaming wrapper
+ small_string_ostream<4096> OS; // Working buffer into which we JIT.
};
} // namespace llvm
diff --git a/include/llvm/Support/GraphWriter.h b/include/llvm/Support/GraphWriter.h
index 2f02aa7a1c..a6bd1afa16 100644
--- a/include/llvm/Support/GraphWriter.h
+++ b/include/llvm/Support/GraphWriter.h
@@ -184,8 +184,7 @@ public:
O << "|" << DOT::EscapeString(NodeDesc);
}
- std::string edgeSourceLabels;
- raw_string_ostream EdgeSourceLabels(edgeSourceLabels);
+ string_ostream EdgeSourceLabels;
bool hasEdgeSourceLabels = getEdgeSourceLabels(EdgeSourceLabels, Node);
if (hasEdgeSourceLabels) {
diff --git a/include/llvm/Support/YAMLTraits.h b/include/llvm/Support/YAMLTraits.h
index a23faf65bb..14ca2db932 100644
--- a/include/llvm/Support/YAMLTraits.h
+++ b/include/llvm/Support/YAMLTraits.h
@@ -612,8 +612,7 @@ template<typename T>
typename std::enable_if<has_ScalarTraits<T>::value,void>::type
yamlize(IO &io, T &Val, bool) {
if ( io.outputting() ) {
- std::string Storage;
- llvm::raw_string_ostream Buffer(Storage);
+ llvm::string_ostream Buffer;
ScalarTraits<T>::output(Val, io.getContext(), Buffer);
StringRef Str = Buffer.str();
io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h
index 34fbe082cd..77e473f2f6 100644
--- a/include/llvm/Support/raw_ostream.h
+++ b/include/llvm/Support/raw_ostream.h
@@ -15,6 +15,7 @@
#define LLVM_SUPPORT_RAW_OSTREAM_H
#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
@@ -461,6 +462,14 @@ class raw_svector_ostream : public raw_ostream {
/// current_pos - Return the current position within the stream, not
/// counting the bytes currently in the buffer.
uint64_t current_pos() const override;
+
+protected:
+ // This constructor is specified not to access \p O provided for storage as it
+ // may not yet be initialized at construction time.
+ explicit raw_svector_ostream(SmallVectorImpl<char> &O, std::nullptr_t)
+ : OS(O){};
+ void init();
+
public:
/// Construct a new raw_svector_ostream.
///
@@ -493,6 +502,25 @@ public:
~raw_null_ostream();
};
+/// string_ostream - A raw_ostream that builds a string. This is a
+/// raw_svector_ostream with storage.
+template <unsigned InternalLen>
+class small_string_ostream : public raw_svector_ostream {
+ SmallVector<char, InternalLen> Buffer;
+ // There's no need to flush explicitly.
+ using raw_svector_ostream::flush;
+
+public:
+ small_string_ostream() : raw_svector_ostream(Buffer, nullptr) { init(); }
+
+ void clear() {
+ flush();
+ Buffer.clear();
+ }
+};
+
+typedef small_string_ostream<128> string_ostream;
+
} // end llvm namespace
#endif
diff --git a/include/llvm/TableGen/StringToOffsetTable.h b/include/llvm/TableGen/StringToOffsetTable.h
index c924bd8ec5..01829a10b2 100644
--- a/include/llvm/TableGen/StringToOffsetTable.h
+++ b/include/llvm/TableGen/StringToOffsetTable.h
@@ -42,8 +42,8 @@ public:
void EmitString(raw_ostream &O) {
// Escape the string.
- SmallString<256> Str;
- raw_svector_ostream(Str).write_escaped(AggregateString);
+ small_string_ostream<256> Str;
+ Str.write_escaped(AggregateString);
AggregateString = Str.str();
O << " \"";