[Lldb-commits] [PATCH] D50161: Add raw_ostream wrapper to the Stream class

Raphael Isemann via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed Aug 1 15:11:09 PDT 2018


teemperor created this revision.

This wrapper will allow us in the future to reuse LLVM methods from within the
Stream class.

Currently no test as this is intended to be an internal class that shouldn't have any
NFC. The test for this change will be the follow up patch that migrates LLDB's
LEB128 implementation to the one from LLVM.


https://reviews.llvm.org/D50161

Files:
  include/lldb/Utility/Stream.h
  source/Utility/Stream.cpp


Index: source/Utility/Stream.cpp
===================================================================
--- source/Utility/Stream.cpp
+++ source/Utility/Stream.cpp
@@ -23,11 +23,11 @@
 
 Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order)
     : m_flags(flags), m_addr_size(addr_size), m_byte_order(byte_order),
-      m_indent_level(0) {}
+      m_indent_level(0), m_forward(*this) {}
 
 Stream::Stream()
     : m_flags(0), m_addr_size(4), m_byte_order(endian::InlHostByteOrder()),
-      m_indent_level(0) {}
+      m_indent_level(0), m_forward(*this) {}
 
 //------------------------------------------------------------------
 // Destructor
Index: include/lldb/Utility/Stream.h
===================================================================
--- include/lldb/Utility/Stream.h
+++ include/lldb/Utility/Stream.h
@@ -15,6 +15,7 @@
 #include "lldb/lldb-enumerations.h" // for ByteOrder::eByteOrderInvalid
 #include "llvm/ADT/StringRef.h"     // for StringRef
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/raw_ostream.h"
 
 #include <stdarg.h>
 #include <stddef.h>    // for size_t
@@ -52,6 +53,17 @@
   //------------------------------------------------------------------
   Stream();
 
+  // FIXME: Streams should not be copyable.
+  Stream(const Stream &other) : m_forward(*this) { (*this) = other; }
+
+  Stream &operator=(const Stream &rhs) {
+    m_flags = rhs.m_flags;
+    m_addr_size = rhs.m_addr_size;
+    m_byte_order = rhs.m_byte_order;
+    m_indent_level = rhs.m_indent_level;
+    return *this;
+  }
+
   //------------------------------------------------------------------
   /// Destructor
   //------------------------------------------------------------------
@@ -548,6 +560,34 @@
   ///     The number of bytes that were appended to the stream.
   //------------------------------------------------------------------
   virtual size_t WriteImpl(const void *src, size_t src_len) = 0;
+
+  //----------------------------------------------------------------------
+  /// @class RawOstreamForward Stream.h "lldb/Utility/Stream.h"
+  /// This is a wrapper class that exposes a raw_ostream interface that just
+  /// forwards to an LLDB stream, allowing to reuse LLVM algorithms that take
+  /// a raw_ostream within the LLDB code base.
+  //----------------------------------------------------------------------
+  class RawOstreamForward : public llvm::raw_ostream {
+    // Note: This stream must *not* maintain its own buffer, but instead
+    // directly write everything to the internal Stream class. Without this,
+    // we would run into the problem that the Stream written byte count would
+    // differ from the actually written bytes by the size of the internal
+    // raw_ostream buffer.
+
+    Stream &m_forward_to;
+    void write_impl(const char *Ptr, size_t Size) override {
+      m_forward_to.Write(Ptr, Size);
+    }
+
+    uint64_t current_pos() const override {
+      return m_forward_to.GetWrittenBytes();
+    }
+
+  public:
+    RawOstreamForward(Stream &forward_to)
+        : llvm::raw_ostream(/*unbuffered*/ true), m_forward_to(forward_to) {}
+  };
+  RawOstreamForward m_forward;
 };
 
 } // namespace lldb_private


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50161.158647.patch
Type: text/x-patch
Size: 3202 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20180801/cf2002a4/attachment.bin>


More information about the lldb-commits mailing list