[PATCH] D65096: Add seek support into llvm::raw_ostream that can be conditionally enabled.

Greg Clayton via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 22 08:31:06 PDT 2019


clayborg created this revision.
clayborg added reviewers: echristo, MaskRay, jakehehrlich, JDevlieghere, aprantl.

While working on a patch for GSYM: https://reviews.llvm.org/D63828 there is a FileWriter class that is being used to write binary data to a stand alone file. The patch was directly using a std::ostream to do the writing, and multiple people in the comments of the patch suggested that we use llvm::raw_ostream. In order to be able to use raw_ostream in the patch we need to be able to seek to locations in the the stream when writing binary data to apply fixes to offsets.

This patch puts the ability to support seeking into llvm::raw_ostream so that we can end up using it in the FileWriter class in the unit tests. It does this by adding two virtual methods to llvm::raw_ostream:

  /// Seek support is optional.
  virtual bool supportsSeeking() { return false; }
   
  /// Optional seek support. Defaults to not changing the file position.
  virtual uint64_t seek(uint64_t off) { return current_pos(); };

There are default implementations that return false and current_pos() respectively so only changes need to be made to raw_ostream subclasses that need to add seek support. It also fixed raw_fd_ostream and raw_os_ostream to override these methods.


https://reviews.llvm.org/D65096

Files:
  include/llvm/Support/raw_os_ostream.h
  include/llvm/Support/raw_ostream.h
  lib/Support/raw_os_ostream.cpp


Index: lib/Support/raw_os_ostream.cpp
===================================================================
--- lib/Support/raw_os_ostream.cpp
+++ lib/Support/raw_os_ostream.cpp
@@ -26,4 +26,9 @@
   OS.write(Ptr, Size);
 }
 
+uint64_t raw_os_ostream::seek(uint64_t off) { 
+  OS.seekp(off); 
+  return current_pos();
+};
+
 uint64_t raw_os_ostream::current_pos() const { return OS.tellp(); }
Index: include/llvm/Support/raw_ostream.h
===================================================================
--- include/llvm/Support/raw_ostream.h
+++ include/llvm/Support/raw_ostream.h
@@ -95,6 +95,12 @@
 
   virtual ~raw_ostream();
 
+  /// Seek support is optional.
+  virtual bool supportsSeeking() { return false; }
+
+  /// Optional seek support. Defaults to not changing the file position.
+  virtual uint64_t seek(uint64_t off) { return current_pos(); };
+
   /// tell - Return the current offset with the file.
   uint64_t tell() const { return current_pos() + GetNumBytesInBuffer(); }
 
@@ -426,11 +432,11 @@
   /// fsync.
   void close();
 
-  bool supportsSeeking() { return SupportsSeeking; }
+  bool supportsSeeking() override { return SupportsSeeking; }
 
   /// Flushes the stream and repositions the underlying file descriptor position
   /// to the offset specified from the beginning of the file.
-  uint64_t seek(uint64_t off);
+  uint64_t seek(uint64_t off) override;
 
   raw_ostream &changeColor(enum Colors colors, bool bold=false,
                            bool bg=false) override;
Index: include/llvm/Support/raw_os_ostream.h
===================================================================
--- include/llvm/Support/raw_os_ostream.h
+++ include/llvm/Support/raw_os_ostream.h
@@ -27,6 +27,12 @@
   /// write_impl - See raw_ostream::write_impl.
   void write_impl(const char *Ptr, size_t Size) override;
 
+  /// Implement optional seek support.
+  bool supportsSeeking() override { return true; }
+
+  /// Implement optional seek support.
+  uint64_t seek(uint64_t off) override;
+
   /// current_pos - Return the current position within the stream, not
   /// counting the bytes currently in the buffer.
   uint64_t current_pos() const override;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65096.211109.patch
Type: text/x-patch
Size: 2169 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190722/f56f324b/attachment.bin>


More information about the llvm-commits mailing list