Fixed/added namespace ending comments using clang-tidy. NFC
[oota-llvm.git] / include / llvm / Support / raw_ostream.h
index dfe25978faf8094ed87036c16a0df7d1fb0ea9f4..4b4f933aa0179cbe38b55e37d727e30b7f9e7676 100644 (file)
@@ -80,8 +80,8 @@ public:
     SAVEDCOLOR
   };
 
-  explicit raw_ostream(bool unbuffered=false)
-    : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
+  explicit raw_ostream(bool unbuffered = false)
+      : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
     // Start out ready to flush.
     OutBufStart = OutBufEnd = OutBufCur = nullptr;
   }
@@ -313,13 +313,34 @@ private:
   void copy_to_buffer(const char *Ptr, size_t Size);
 };
 
+/// An abstract base class for streams implementations that also support a
+/// pwrite operation. This is usefull for code that can mostly stream out data,
+/// but needs to patch in a header that needs to know the output size.
+class raw_pwrite_stream : public raw_ostream {
+  virtual void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) = 0;
+
+public:
+  explicit raw_pwrite_stream(bool Unbuffered = false)
+      : raw_ostream(Unbuffered) {}
+  void pwrite(const char *Ptr, size_t Size, uint64_t Offset) {
+#ifndef NDBEBUG
+    uint64_t Pos = tell();
+    // /dev/null always reports a pos of 0, so we cannot perform this check
+    // in that case.
+    if (Pos)
+      assert(Size + Offset <= Pos && "We don't support extending the stream");
+#endif
+    pwrite_impl(Ptr, Size, Offset);
+  }
+};
+
 //===----------------------------------------------------------------------===//
 // File Output Streams
 //===----------------------------------------------------------------------===//
 
 /// A raw_ostream that writes to a file descriptor.
 ///
-class raw_fd_ostream : public raw_ostream {
+class raw_fd_ostream : public raw_pwrite_stream {
   int FD;
   bool ShouldClose;
 
@@ -333,9 +354,13 @@ class raw_fd_ostream : public raw_ostream {
 
   uint64_t pos;
 
+  bool SupportsSeeking;
+
   /// See raw_ostream::write_impl.
   void write_impl(const char *Ptr, size_t Size) override;
 
+  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
+
   /// Return the current position within the stream, not counting the bytes
   /// currently in the buffer.
   uint64_t current_pos() const override { return pos; }
@@ -364,12 +389,14 @@ public:
   /// this closes the file when the stream is destroyed.
   raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false);
 
-  ~raw_fd_ostream();
+  ~raw_fd_ostream() override;
 
   /// Manually flush the stream and close the file. Note that this does not call
   /// fsync.
   void close();
 
+  bool supportsSeeking() { 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);
@@ -444,7 +471,7 @@ class raw_string_ostream : public raw_ostream {
   uint64_t current_pos() const override { return OS.size(); }
 public:
   explicit raw_string_ostream(std::string &O) : OS(O) {}
-  ~raw_string_ostream();
+  ~raw_string_ostream() override;
 
   /// Flushes the stream contents to the target string and returns  the string's
   /// reference.
@@ -456,22 +483,31 @@ public:
 
 /// A raw_ostream that writes to an SmallVector or SmallString.  This is a
 /// simple adaptor class. This class does not encounter output errors.
-class raw_svector_ostream : public raw_ostream {
+class raw_svector_ostream : public raw_pwrite_stream {
   SmallVectorImpl<char> &OS;
 
   /// See raw_ostream::write_impl.
   void write_impl(const char *Ptr, size_t Size) override;
 
+  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
+
   /// Return the current position within the stream, not counting the bytes
   /// currently in the buffer.
   uint64_t current_pos() const override;
+
+protected:
+  // Like the regular constructor, but doesn't call init.
+  explicit raw_svector_ostream(SmallVectorImpl<char> &O, unsigned);
+  void init();
+
 public:
   /// Construct a new raw_svector_ostream.
   ///
   /// \param O The vector to write to; this should generally have at least 128
   /// bytes free to avoid any extraneous memory overhead.
   explicit raw_svector_ostream(SmallVectorImpl<char> &O);
-  ~raw_svector_ostream();
+  ~raw_svector_ostream() override;
+
 
   /// This is called when the SmallVector we're appending to is changed outside
   /// of the raw_svector_ostream's control.  It is only safe to do this if the
@@ -484,9 +520,10 @@ public:
 };
 
 /// A raw_ostream that discards all output.
-class raw_null_ostream : public raw_ostream {
+class raw_null_ostream : public raw_pwrite_stream {
   /// See raw_ostream::write_impl.
   void write_impl(const char *Ptr, size_t size) override;
+  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
 
   /// Return the current position within the stream, not counting the bytes
   /// currently in the buffer.
@@ -494,9 +531,20 @@ class raw_null_ostream : public raw_ostream {
 
 public:
   explicit raw_null_ostream() {}
-  ~raw_null_ostream();
+  ~raw_null_ostream() override;
+};
+
+class buffer_ostream : public raw_svector_ostream {
+  raw_ostream &OS;
+  SmallVector<char, 0> Buffer;
+
+public:
+  buffer_ostream(raw_ostream &OS) : raw_svector_ostream(Buffer, 0), OS(OS) {
+    init();
+  }
+  ~buffer_ostream() { OS << str(); }
 };
 
-} // end llvm namespace
+} // namespace llvm
 
 #endif