X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FSupport%2Fraw_ostream.h;h=e5cc40e7d6b22d99790b572b0302378150b99abf;hb=2b762697564ca1e12e0e974e93ceeb4c3420505c;hp=982271397e4ec9e54ccbcf92c8bdeb69d4dcbac5;hpb=271631a0afecebfb806e8d4d67407c919c4e1c0d;p=oota-llvm.git diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h index 982271397e4..e5cc40e7d6b 100644 --- a/include/llvm/Support/raw_ostream.h +++ b/include/llvm/Support/raw_ostream.h @@ -16,7 +16,6 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/Casting.h" #include "llvm/Support/DataTypes.h" #include @@ -68,17 +67,6 @@ private: } BufferMode; public: - enum StreamKind { - SK_FD, - SK_STRING, - SK_SVECTOR, - SK_NULL, - SK_STD_OS, - SK_CIRCULAR, - SK_FORMATTED, - SK_COUNTING - }; - // color order matches ANSI escape sequence, don't change enum Colors { BLACK=0, @@ -92,8 +80,8 @@ public: SAVEDCOLOR }; - explicit raw_ostream(StreamKind Kind, bool unbuffered = false) - : BufferMode(unbuffered ? Unbuffered : InternalBuffer), Kind(Kind) { + explicit raw_ostream(bool unbuffered = false) + : BufferMode(unbuffered ? Unbuffered : InternalBuffer) { // Start out ready to flush. OutBufStart = OutBufEnd = OutBufCur = nullptr; } @@ -177,8 +165,10 @@ public: if (Size > (size_t)(OutBufEnd - OutBufCur)) return write(Str.data(), Size); - memcpy(OutBufCur, Str.data(), Size); - OutBufCur += Size; + if (Size) { + memcpy(OutBufCur, Str.data(), Size); + OutBufCur += Size; + } return *this; } @@ -228,14 +218,13 @@ public: // Formatted output, see the leftJustify() function in Support/Format.h. raw_ostream &operator<<(const FormattedString &); - + // Formatted output, see the formatHex() function in Support/Format.h. raw_ostream &operator<<(const FormattedNumber &); - + /// indent - Insert 'NumSpaces' spaces. raw_ostream &indent(unsigned NumSpaces); - /// Changes the foreground color of text that will be output from this point /// forward. /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to @@ -256,7 +245,7 @@ public: /// outputting colored text, or before program exit. virtual raw_ostream &resetColor() { return *this; } - /// Reverses the forground and background colors. + /// Reverses the foreground and background colors. virtual raw_ostream &reverseColor() { return *this; } /// This function determines if this stream is connected to a "tty" or @@ -271,10 +260,7 @@ public: // Subclass Interface //===--------------------------------------------------------------------===// - StreamKind getKind() const { return Kind; } - private: - StreamKind Kind; /// The is the piece of the class that is implemented by subclasses. This /// writes the \p Size bytes starting at /// \p Ptr to the underlying stream. @@ -328,13 +314,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 useful 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; @@ -348,9 +355,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; } @@ -379,14 +390,14 @@ public: /// this closes the file when the stream is destroyed. raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false); - static bool classof(const raw_ostream *OS) { return OS->getKind() == SK_FD; } - - ~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); @@ -459,13 +470,10 @@ class raw_string_ostream : public raw_ostream { /// Return the current position within the stream, not counting the bytes /// currently in the buffer. uint64_t current_pos() const override { return OS.size(); } -public: - explicit raw_string_ostream(std::string &O) : raw_ostream(SK_STRING), OS(O) {} - ~raw_string_ostream(); - static bool classof(const raw_ostream *OS) { - return OS->getKind() == SK_STRING; - } +public: + explicit raw_string_ostream(std::string &O) : OS(O) {} + ~raw_string_ostream() override; /// Flushes the stream contents to the target string and returns the string's /// reference. @@ -477,54 +485,60 @@ 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 { +/// raw_svector_ostream operates without a buffer, delegating all memory +/// management to the SmallString. Thus the SmallString is always up-to-date, +/// may be used directly and there is no need to call flush(). +class raw_svector_ostream : public raw_pwrite_stream { SmallVectorImpl &OS; /// See raw_ostream::write_impl. void write_impl(const char *Ptr, size_t Size) override; - /// Return the current position within the stream, not counting the bytes - /// currently in the buffer. + void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override; + + /// Return the current position within the stream. uint64_t current_pos() const override; + 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 &O); - ~raw_svector_ostream(); - - static bool classof(const raw_ostream *OS) { - return OS->getKind() == SK_SVECTOR; + explicit raw_svector_ostream(SmallVectorImpl &O) : OS(O) { + SetUnbuffered(); } + ~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 - /// raw_svector_ostream has previously been flushed. - void resync(); + void flush() = delete; - /// Flushes the stream contents to the target vector and return a StringRef - /// for the vector contents. - StringRef str(); + /// Return a StringRef for the vector contents. + StringRef str() { return StringRef(OS.data(), OS.size()); } }; /// 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. uint64_t current_pos() const override; public: - explicit raw_null_ostream() : raw_ostream(SK_NULL) {} - ~raw_null_ostream(); - static bool classof(const raw_ostream *OS) { - return OS->getKind() == SK_NULL; - } + explicit raw_null_ostream() {} + ~raw_null_ostream() override; +}; + +class buffer_ostream : public raw_svector_ostream { + raw_ostream &OS; + SmallVector Buffer; + +public: + buffer_ostream(raw_ostream &OS) : raw_svector_ostream(Buffer), OS(OS) {} + ~buffer_ostream() override { OS << str(); } }; } // end llvm namespace -#endif +#endif // LLVM_SUPPORT_RAW_OSTREAM_H