diff --git a/include/llvm/Support/FormattedStream.h b/include/llvm/Support/FormattedStream.h index 4b13ff2d5ab..8d6dca66541 100644 --- a/include/llvm/Support/FormattedStream.h +++ b/include/llvm/Support/FormattedStream.h @@ -49,14 +49,21 @@ namespace llvm /// bool DeleteStream; - /// Column - The current output column of the stream. The column - /// scheme is zero-based. + /// 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 Column; + unsigned ColumnScanned; + + /// Scanned - This points to one past the last character in the + /// buffer we've scanned. + /// + iterator Scanned; virtual void write_impl(const char *Ptr, size_t Size) { - ComputeColumn(Ptr, Size); + ComputeColumn(); TheStream->write(Ptr, Size); + Scanned = begin(); } /// current_pos - Return the current position within the stream, @@ -70,7 +77,7 @@ namespace llvm /// ComputeColumn - Examine the current output and figure out /// which column we end up in after output. /// - void ComputeColumn(const char *Ptr, size_t Size); + void ComputeColumn(); public: /// formatted_raw_ostream - Open the specified file for @@ -84,13 +91,16 @@ namespace llvm /// underneath it. /// formatted_raw_ostream(raw_ostream &Stream, bool Delete = false) - : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) { + : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) { setStream(Stream, Delete); } explicit formatted_raw_ostream() - : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {} + : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) { + Scanned = begin(); + } ~formatted_raw_ostream() { + flush(); if (DeleteStream) delete TheStream; } @@ -110,6 +120,8 @@ namespace llvm if (size_t BufferSize = TheStream->GetNumBytesInBuffer()) SetBufferSize(BufferSize); TheStream->SetUnbuffered(); + + Scanned = begin(); } /// PadToColumn - Align the output to some column number. diff --git a/lib/Support/FormattedStream.cpp b/lib/Support/FormattedStream.cpp index 1796f9f9568..e57b17ec45d 100644 --- a/lib/Support/FormattedStream.cpp +++ b/lib/Support/FormattedStream.cpp @@ -19,16 +19,23 @@ using namespace llvm; /// ComputeColumn - Examine the current output and figure out which /// column we end up in after output. /// -void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) { +void formatted_raw_ostream::ComputeColumn() { // Keep track of the current column by scanning the string for // special characters - for (const char *epos = Ptr + Size; Ptr != epos; ++Ptr) { - ++Column; - if (*Ptr == '\n' || *Ptr == '\r') - Column = 0; - else if (*Ptr == '\t') - Column += (8 - (Column & 0x7)) & 0x7; + // The buffer may have been allocated underneath us. + if (Scanned == 0 && GetNumBytesInBuffer() != 0) { + Scanned = begin(); + } + + while (Scanned != end()) { + ++ColumnScanned; + if (*Scanned == '\n' || *Scanned == '\r') + ColumnScanned = 0; + else if (*Scanned == '\t') + // Assumes tab stop = 8 characters. + ColumnScanned += (8 - (ColumnScanned & 0x7)) & 0x7; + ++Scanned; } } @@ -38,21 +45,18 @@ void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) { /// \param MinPad - The minimum space to give after the most recent /// I/O, even if the current column + minpad > newcol. /// -void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) { - flush(); +void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) { + // Figure out what's in the buffer and add it to the column count. + ComputeColumn(); // Output spaces until we reach the desired column. - unsigned num = NewCol - Column; - if (NewCol < Column || num < MinPad) + unsigned num = NewCol - ColumnScanned; + if (NewCol < ColumnScanned || num < MinPad) num = MinPad; // Keep a buffer of spaces handy to speed up processing. - static char Spaces[MAX_COLUMN_PAD]; - static bool Initialized = false; - if (!Initialized) { - std::fill_n(Spaces, MAX_COLUMN_PAD, ' '), - Initialized = true; - } + const char *Spaces = " " + " "; assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding");