Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions llvm/include/llvm/Support/raw_ostream_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace llvm {
/// template instantions.
class raw_ostream_proxy_adaptor_base {
protected:
raw_ostream_proxy_adaptor_base() = delete;
raw_ostream_proxy_adaptor_base(const raw_ostream_proxy_adaptor_base &) =
delete;

Expand Down Expand Up @@ -72,8 +71,7 @@ class raw_ostream_proxy_adaptor_base {
/// changeColor(), resetColor(), and \a reverseColor() are not forwarded, since
/// they need to call \a flush() and the buffer lives in the proxy.
template <class RawOstreamT = raw_ostream>
class raw_ostream_proxy_adaptor : public RawOstreamT,
public raw_ostream_proxy_adaptor_base {
class raw_ostream_proxy_adaptor : public RawOstreamT {
void write_impl(const char *Ptr, size_t Size) override {
getProxiedOS().write(Ptr, Size);
}
Expand All @@ -92,23 +90,39 @@ class raw_ostream_proxy_adaptor : public RawOstreamT,
RawOstreamT::enable_colors(enable);
getProxiedOS().enable_colors(enable);
}
bool hasProxiedOS() const { return OS; }
raw_ostream &getProxiedOS() const {
assert(OS && "raw_ostream_proxy_adaptor use after reset");
return *OS;
}
size_t getPreferredBufferSize() const { return PreferredBufferSize; }

~raw_ostream_proxy_adaptor() override { resetProxiedOS(); }

protected:
template <class... ArgsT>
explicit raw_ostream_proxy_adaptor(raw_ostream &OS, ArgsT &&...Args)
: RawOstreamT(std::forward<ArgsT>(Args)...),
raw_ostream_proxy_adaptor_base(OS) {}
: RawOstreamT(std::forward<ArgsT>(Args)...), OS(&OS),
PreferredBufferSize(OS.GetBufferSize()) {
// Drop OS's buffer to make this->flush() forward. This proxy will add a
// buffer in its place.
OS.SetUnbuffered();
}

/// Stop proxying the stream. Flush and set up a crash for future writes.
///
/// For example, this can simplify logic when a subclass might have a longer
/// lifetime than the stream it proxies.
void resetProxiedOS() {
raw_ostream_proxy_adaptor_base::resetProxiedOS(*this);
OS->SetUnbuffered();
OS = nullptr;
}
void resetProxiedOS(raw_ostream &) = delete;

private:
raw_ostream *OS;

/// Caches the value of OS->GetBufferSize() at construction time.
size_t PreferredBufferSize;
};

/// Adaptor for creating a stream that proxies a \a raw_pwrite_stream.
Expand All @@ -134,7 +148,7 @@ class raw_pwrite_stream_proxy_adaptor
}
};

/// Non-owning proxy for a \a raw_ostream. Enables passing a stream into of an
/// Non-owning proxy for a \a raw_ostream. Enables passing a stream into an
/// API that takes ownership.
class raw_ostream_proxy : public raw_ostream_proxy_adaptor<> {
void anchor() override;
Expand All @@ -144,7 +158,7 @@ class raw_ostream_proxy : public raw_ostream_proxy_adaptor<> {
};

/// Non-owning proxy for a \a raw_pwrite_stream. Enables passing a stream
/// into of an API that takes ownership.
/// into an API that takes ownership.
class raw_pwrite_stream_proxy : public raw_pwrite_stream_proxy_adaptor<> {
void anchor() override;

Expand Down
6 changes: 3 additions & 3 deletions llvm/unittests/Support/raw_ostream_proxy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BufferedNoPwriteSmallVectorStream : public raw_ostream {
public:
// Choose a strange buffer size to ensure it doesn't collide with the default
// on \a raw_ostream.
constexpr static const size_t PreferredBufferSize = 63;
static constexpr size_t PreferredBufferSize = 63;

size_t preferred_buffer_size() const override { return PreferredBufferSize; }
uint64_t current_pos() const override { return Vector.size(); }
Expand All @@ -40,7 +40,7 @@ class BufferedNoPwriteSmallVectorStream : public raw_ostream {
bool IsDisplayed = false;
};

constexpr const size_t BufferedNoPwriteSmallVectorStream::PreferredBufferSize;
constexpr size_t BufferedNoPwriteSmallVectorStream::PreferredBufferSize;

TEST(raw_ostream_proxyTest, write) {
// Besides confirming that "write" works, this test confirms that the proxy
Expand Down Expand Up @@ -157,7 +157,7 @@ TEST(raw_ostream_proxyTest, resetProxiedOS) {
EXPECT_EQ(0u, ProxyOS.GetBufferSize());
EXPECT_FALSE(ProxyOS.hasProxiedOS());

#if GTEST_HAS_DEATH_TEST
#if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
EXPECT_DEATH(ProxyOS << "e", "use after reset");
EXPECT_DEATH(ProxyOS.getProxiedOS(), "use after reset");
#endif
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.