X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FFormattedStream.h;h=4a135cd23174f007c21cc0fc64c6e144f9f85ab9;hp=24a3546200ac78bab3c2d19e4f7771786d1d34d6;hb=d84fffe1de546b956429595d652f54f0529dbe07;hpb=05866f7963d37ede6b99445b5b079874413b1ec7 diff --git a/include/llvm/Support/FormattedStream.h b/include/llvm/Support/FormattedStream.h index 24a3546200a..4a135cd2317 100644 --- a/include/llvm/Support/FormattedStream.h +++ b/include/llvm/Support/FormattedStream.h @@ -1,4 +1,4 @@ -//===-- llvm/CodeGen/FormattedStream.h - Formatted streams ------*- C++ -*-===// +//===-- llvm/Support/FormattedStream.h - Formatted streams ------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -16,125 +16,133 @@ #define LLVM_SUPPORT_FORMATTEDSTREAM_H #include "llvm/Support/raw_ostream.h" +#include -namespace llvm -{ - /// formatted_raw_ostream - Formatted raw_fd_ostream to handle - /// asm-specific constructs. +namespace llvm { + +/// formatted_raw_ostream - A raw_ostream that wraps another one and keeps track +/// of line and column position, allowing padding out to specific column +/// boundaries and querying the number of lines written to the stream. +/// +class formatted_raw_ostream : public raw_ostream { + /// TheStream - The real stream we output to. We set it to be + /// unbuffered, since we're already doing our own buffering. /// - class formatted_raw_ostream : public raw_ostream { - public: - /// DELETE_STREAM - Tell the destructor to delete the held stream. - /// - static const bool DELETE_STREAM = true; - - /// PRESERVE_STREAM - Tell the destructor to not delete the held - /// stream. - /// - static const bool PRESERVE_STREAM = false; - - private: - /// TheStream - The real stream we output to. We set it to be - /// unbuffered, since we're already doing our own buffering. - /// - raw_ostream *TheStream; - - /// DeleteStream - Do we need to delete TheStream in the - /// destructor? - /// - bool DeleteStream; - - /// ColumnScanned - The current output column of the data that's - /// been flushed and the portion of the buffer that's been - /// scanned. The column scheme is zero-based. - /// - unsigned ColumnScanned; - - /// Scanned - This points to one past the last character in the - /// buffer we've scanned. - /// - const char *Scanned; - - virtual void write_impl(const char *Ptr, size_t Size); - - /// current_pos - Return the current position within the stream, - /// not counting the bytes currently in the buffer. - virtual uint64_t current_pos() { - // This has the same effect as calling TheStream.current_pos(), - // but that interface is private. - return TheStream->tell() - TheStream->GetNumBytesInBuffer(); - } - - /// ComputeColumn - Examine the given output buffer and figure out which - /// column we end up in after output. - /// - void ComputeColumn(const char *Ptr, size_t size); - - public: - /// formatted_raw_ostream - Open the specified file for - /// writing. If an error occurs, information about the error is - /// put into ErrorInfo, and the stream should be immediately - /// destroyed; the string will be empty if no error occurred. - /// - /// As a side effect, the given Stream is set to be Unbuffered. - /// This is because formatted_raw_ostream does its own buffering, - /// so it doesn't want another layer of buffering to be happening - /// underneath it. - /// - formatted_raw_ostream(raw_ostream &Stream, bool Delete = false) - : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) { - setStream(Stream, Delete); - } - explicit formatted_raw_ostream() - : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) { - Scanned = 0; - } - - ~formatted_raw_ostream() { - flush(); - releaseStream(); - } - - void setStream(raw_ostream &Stream, bool Delete = false) { - releaseStream(); - - TheStream = &Stream; - DeleteStream = Delete; - - // This formatted_raw_ostream inherits from raw_ostream, so it'll do its - // own buffering, and it doesn't need or want TheStream to do another - // layer of buffering underneath. Resize the buffer to what TheStream - // had been using, and tell TheStream not to do its own buffering. - if (size_t BufferSize = TheStream->GetBufferSize()) - SetBufferSize(BufferSize); - else - SetUnbuffered(); - TheStream->SetUnbuffered(); + raw_ostream *TheStream; + + /// Position - The current output column and line of the data that's + /// been flushed and the portion of the buffer that's been + /// scanned. The line and column scheme is zero-based. + /// + std::pair Position; + + /// Scanned - This points to one past the last character in the + /// buffer we've scanned. + /// + const char *Scanned; + + void write_impl(const char *Ptr, size_t Size) override; - Scanned = 0; - } - - /// PadToColumn - Align the output to some column number. If the current - /// column is already equal to or more than NewCol, PadToColumn inserts one - /// space. - /// - /// \param NewCol - The column to move to. - void PadToColumn(unsigned NewCol); - - private: - void releaseStream() { - // Delete the stream if needed. Otherwise, transfer the buffer - // settings from this raw_ostream back to the underlying stream. - if (!TheStream) - return; - if (DeleteStream) - delete TheStream; - else if (size_t BufferSize = GetBufferSize()) - TheStream->SetBufferSize(BufferSize); - else - TheStream->SetUnbuffered(); - } - }; + /// current_pos - Return the current position within the stream, + /// not counting the bytes currently in the buffer. + uint64_t current_pos() const override { + // Our current position in the stream is all the contents which have been + // written to the underlying stream (*not* the current position of the + // underlying stream). + return TheStream->tell(); + } + + /// ComputePosition - Examine the given output buffer and figure out the new + /// position after output. + /// + void ComputePosition(const char *Ptr, size_t size); + + void setStream(raw_ostream &Stream) { + releaseStream(); + + TheStream = &Stream; + + // This formatted_raw_ostream inherits from raw_ostream, so it'll do its + // own buffering, and it doesn't need or want TheStream to do another + // layer of buffering underneath. Resize the buffer to what TheStream + // had been using, and tell TheStream not to do its own buffering. + if (size_t BufferSize = TheStream->GetBufferSize()) + SetBufferSize(BufferSize); + else + SetUnbuffered(); + TheStream->SetUnbuffered(); + + Scanned = nullptr; + } + +public: + /// formatted_raw_ostream - Open the specified file for + /// writing. If an error occurs, information about the error is + /// put into ErrorInfo, and the stream should be immediately + /// destroyed; the string will be empty if no error occurred. + /// + /// As a side effect, the given Stream is set to be Unbuffered. + /// This is because formatted_raw_ostream does its own buffering, + /// so it doesn't want another layer of buffering to be happening + /// underneath it. + /// + formatted_raw_ostream(raw_ostream &Stream) + : TheStream(nullptr), Position(0, 0) { + setStream(Stream); + } + explicit formatted_raw_ostream() : TheStream(nullptr), Position(0, 0) { + Scanned = nullptr; + } + + ~formatted_raw_ostream() override { + flush(); + releaseStream(); + } + + /// PadToColumn - Align the output to some column number. If the current + /// column is already equal to or more than NewCol, PadToColumn inserts one + /// space. + /// + /// \param NewCol - The column to move to. + formatted_raw_ostream &PadToColumn(unsigned NewCol); + + /// getColumn - Return the column number + unsigned getColumn() { return Position.first; } + + /// getLine - Return the line number + unsigned getLine() { return Position.second; } + + raw_ostream &resetColor() override { + TheStream->resetColor(); + return *this; + } + + raw_ostream &reverseColor() override { + TheStream->reverseColor(); + return *this; + } + + raw_ostream &changeColor(enum Colors Color, bool Bold, bool BG) override { + TheStream->changeColor(Color, Bold, BG); + return *this; + } + + bool is_displayed() const override { + return TheStream->is_displayed(); + } + +private: + void releaseStream() { + // Transfer the buffer settings from this raw_ostream back to the underlying + // stream. + if (!TheStream) + return; + if (size_t BufferSize = GetBufferSize()) + TheStream->SetBufferSize(BufferSize); + else + TheStream->SetUnbuffered(); + } +}; /// fouts() - This returns a reference to a formatted_raw_ostream for /// standard output. Use it like: fouts() << "foo" << "bar"; @@ -144,6 +152,10 @@ formatted_raw_ostream &fouts(); /// standard error. Use it like: ferrs() << "foo" << "bar"; formatted_raw_ostream &ferrs(); +/// fdbgs() - This returns a reference to a formatted_raw_ostream for +/// debug output. Use it like: fdbgs() << "foo" << "bar"; +formatted_raw_ostream &fdbgs(); + } // end llvm namespace