From 795af9c7de9d3118da4d5e097759ca7f80dff3f9 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Mon, 14 Dec 2020 17:46:11 -0800 Subject: [PATCH] Frontend: Simplify handling of non-seeking streams in CompilerInstance, NFC Add a new `raw_pwrite_ostream` variant, `buffer_unique_ostream`, which is like `buffer_ostream` but with unique ownership of the stream it's wrapping. Use this in CompilerInstance to simplify the ownership of non-seeking output streams, avoiding logic sprawled around to deal with them specially. This also simplifies future work to encapsulate output files in a different class. Differential Revision: https://reviews.llvm.org/D93260 --- include/llvm/Support/raw_ostream.h | 12 ++++++++++++ lib/Support/raw_ostream.cpp | 2 ++ 2 files changed, 14 insertions(+) diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h index bd15f97a13a..7d572fe06f6 100644 --- a/include/llvm/Support/raw_ostream.h +++ b/include/llvm/Support/raw_ostream.h @@ -687,6 +687,18 @@ public: ~buffer_ostream() override { OS << str(); } }; +class buffer_unique_ostream : public raw_svector_ostream { + std::unique_ptr OS; + SmallVector Buffer; + + virtual void anchor() override; + +public: + buffer_unique_ostream(std::unique_ptr OS) + : raw_svector_ostream(Buffer), OS(std::move(OS)) {} + ~buffer_unique_ostream() override { *OS << str(); } +}; + } // end namespace llvm #endif // LLVM_SUPPORT_RAW_OSTREAM_H diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index 48b42fec0ac..8f10d136bc3 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -987,3 +987,5 @@ void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size, void raw_pwrite_stream::anchor() {} void buffer_ostream::anchor() {} + +void buffer_unique_ostream::anchor() {}