diff options
-rw-r--r-- | include/llvm/Support/raw_ostream.h | 8 | ||||
-rw-r--r-- | lib/Support/raw_ostream.cpp | 10 |
2 files changed, 13 insertions, 5 deletions
diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h index ed06dbf42e..dc496d46e0 100644 --- a/include/llvm/Support/raw_ostream.h +++ b/include/llvm/Support/raw_ostream.h @@ -454,10 +454,14 @@ class raw_svector_ostream : public raw_ostream { public: /// Construct a new raw_svector_ostream. /// - /// \arg O - The vector to write to; this *must* have at least 128 bytes of - /// free space in it. + /// \arg O - The vector to write to; this should generally have at least 128 + /// bytes free to avoid any extraneous memory overhead. explicit raw_svector_ostream(SmallVectorImpl<char> &O); ~raw_svector_ostream(); + + /// str - Flushes the stream contents to the target vector and return a + /// StringRef for the vector contents. + StringRef str(); }; /// raw_null_ostream - A raw_ostream that discards all output. diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index 30bc76b7fd..917e6be669 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -487,12 +487,11 @@ void raw_string_ostream::write_impl(const char *Ptr, size_t Size) { // and we only need to set the vector size when the data is flushed. raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) { - // Set up the initial external buffer. We enforce that the buffer must have at + // Set up the initial external buffer. We make sure that the buffer has at // least 128 bytes free; raw_ostream itself only requires 64, but we want to // make sure that we don't grow the buffer unnecessarily on destruction (when // the data is flushed). See the FIXME below. - if (OS.capacity() - OS.size() < 128) - llvm_report_error("Invalid argument, must have at least 128 bytes free!"); + OS.reserve(OS.size() + 128); SetBuffer(OS.end(), OS.capacity() - OS.size()); } @@ -519,6 +518,11 @@ void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) { uint64_t raw_svector_ostream::current_pos() { return OS.size(); } +StringRef raw_svector_ostream::str() { + flush(); + return StringRef(OS.begin(), OS.size()); +} + //===----------------------------------------------------------------------===// // raw_null_ostream //===----------------------------------------------------------------------===// |