[PATCH] D115421: [Support] No longer require .flush()ing raw_string_ostream
Logan Smith via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 9 12:47:45 PST 2021
logan-5 updated this revision to Diff 393258.
logan-5 marked 3 inline comments as done.
logan-5 added a comment.
Addressed comments
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D115421/new/
https://reviews.llvm.org/D115421
Files:
llvm/include/llvm/Support/raw_ostream.h
llvm/lib/Support/raw_ostream.cpp
llvm/unittests/Support/raw_ostream_test.cpp
Index: llvm/unittests/Support/raw_ostream_test.cpp
===================================================================
--- llvm/unittests/Support/raw_ostream_test.cpp
+++ llvm/unittests/Support/raw_ostream_test.cpp
@@ -21,11 +21,10 @@
template<typename T> std::string printToString(const T &Value) {
std::string res;
- {
- llvm::raw_string_ostream OS(res);
- OS.SetBuffered();
- OS << Value;
- }
+ llvm::raw_string_ostream OS(res);
+ OS.SetBuffered();
+ OS << Value;
+ OS.flush();
return res;
}
@@ -141,7 +140,8 @@
OS << "hello";
OS << 1;
OS << 'w' << 'o' << 'r' << 'l' << 'd';
- EXPECT_EQ("hello1world", OS.str());
+ OS.flush();
+ EXPECT_EQ("hello1world", Str);
}
TEST(raw_ostreamTest, WriteEscaped) {
@@ -457,6 +457,9 @@
TiedTo << "y";
TiedStream << "0";
EXPECT_EQ("acegostuv", TiedToBuffer);
+
+ TiedTo.flush();
+ TiedStream.flush();
}
TEST(raw_ostreamTest, reserve_stream) {
Index: llvm/lib/Support/raw_ostream.cpp
===================================================================
--- llvm/lib/Support/raw_ostream.cpp
+++ llvm/lib/Support/raw_ostream.cpp
@@ -937,10 +937,6 @@
// raw_string_ostream
//===----------------------------------------------------------------------===//
-raw_string_ostream::~raw_string_ostream() {
- flush();
-}
-
void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
OS.append(Ptr, Size);
}
Index: llvm/include/llvm/Support/raw_ostream.h
===================================================================
--- llvm/include/llvm/Support/raw_ostream.h
+++ llvm/include/llvm/Support/raw_ostream.h
@@ -622,6 +622,9 @@
/// A raw_ostream that writes to an std::string. This is a simple adaptor
/// class. This class does not encounter output errors.
+/// raw_string_ostream operates without a buffer, delegating all memory
+/// management to the std::string. Thus the std::string is always up-to-date,
+/// may be used directly and there is no need to call flush().
class raw_string_ostream : public raw_ostream {
std::string &OS;
@@ -636,14 +639,9 @@
explicit raw_string_ostream(std::string &O) : OS(O) {
SetUnbuffered();
}
- ~raw_string_ostream() override;
- /// Flushes the stream contents to the target string and returns the string's
- /// reference.
- std::string& str() {
- flush();
- return OS;
- }
+ /// Returns the string's reference.
+ std::string &str() { return OS; }
void reserveExtraSpace(uint64_t ExtraSize) override {
OS.reserve(tell() + ExtraSize);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115421.393258.patch
Type: text/x-patch
Size: 2525 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211209/bd9e9c7b/attachment.bin>
More information about the llvm-commits
mailing list