[Lldb-commits] [lldb] r338901 - Add raw_ostream wrapper to the Stream class

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Fri Aug 3 09:56:33 PDT 2018


Author: teemperor
Date: Fri Aug  3 09:56:33 2018
New Revision: 338901

URL: http://llvm.org/viewvc/llvm-project?rev=338901&view=rev
Log:
Add raw_ostream wrapper to the Stream class

Summary:
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.

This change also adds custom move/assignment methods to Stream, as LLVM
raw_ostream doesn't support these. As our internal stream has anyway no state,
we can just keep the same stream object around.

Reviewers: davide, labath

Reviewed By: labath

Subscribers: xiaobai, labath, lldb-commits

Differential Revision: https://reviews.llvm.org/D50161

Modified:
    lldb/trunk/include/lldb/Utility/Stream.h
    lldb/trunk/source/Utility/Stream.cpp

Modified: lldb/trunk/include/lldb/Utility/Stream.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/Stream.h?rev=338901&r1=338900&r2=338901&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/Stream.h (original)
+++ lldb/trunk/include/lldb/Utility/Stream.h Fri Aug  3 09:56:33 2018
@@ -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 @@ public:
   //------------------------------------------------------------------
   Stream();
 
+  // FIXME: Streams should not be copyable.
+  Stream(const Stream &other) : m_forwarder(*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
   //------------------------------------------------------------------
@@ -520,6 +532,13 @@ public:
   //------------------------------------------------------------------
   size_t PutULEB128(uint64_t uval);
 
+  //------------------------------------------------------------------
+  /// Returns a raw_ostream that forwards the data to this Stream object.
+  //------------------------------------------------------------------
+  llvm::raw_ostream &AsRawOstream() {
+    return m_forwarder;
+  }
+
 protected:
   //------------------------------------------------------------------
   // Member variables
@@ -548,6 +567,34 @@ protected:
   ///     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_target;
+    void write_impl(const char *Ptr, size_t Size) override {
+      m_target.Write(Ptr, Size);
+    }
+
+    uint64_t current_pos() const override {
+      return m_target.GetWrittenBytes();
+    }
+
+  public:
+    RawOstreamForward(Stream &target)
+        : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {}
+  };
+  RawOstreamForward m_forwarder;
 };
 
 } // namespace lldb_private

Modified: lldb/trunk/source/Utility/Stream.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/Stream.cpp?rev=338901&r1=338900&r2=338901&view=diff
==============================================================================
--- lldb/trunk/source/Utility/Stream.cpp (original)
+++ lldb/trunk/source/Utility/Stream.cpp Fri Aug  3 09:56:33 2018
@@ -23,11 +23,11 @@ using namespace lldb_private;
 
 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_forwarder(*this) {}
 
 Stream::Stream()
     : m_flags(0), m_addr_size(4), m_byte_order(endian::InlHostByteOrder()),
-      m_indent_level(0) {}
+      m_indent_level(0), m_forwarder(*this) {}
 
 //------------------------------------------------------------------
 // Destructor




More information about the lldb-commits mailing list