David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 1 | //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===// |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 1f318e0 | 2009-07-14 23:14:10 +0000 | [diff] [blame] | 10 | // This file contains the implementation of formatted_raw_ostream. |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
David Greene | 8f1929b | 2010-01-05 01:28:43 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Debug.h" |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 15 | #include "llvm/Support/FormattedStream.h" |
David Greene | ed0e2ad | 2009-07-23 23:21:10 +0000 | [diff] [blame] | 16 | |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 17 | using namespace llvm; |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 18 | |
Dan Gohman | fbcb5b6 | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 19 | /// CountColumns - Examine the given char sequence and figure out which |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 20 | /// column we end up in after output. |
| 21 | /// |
Dan Gohman | fbcb5b6 | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 22 | static unsigned CountColumns(unsigned Column, const char *Ptr, size_t Size) { |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 23 | // Keep track of the current column by scanning the string for |
| 24 | // special characters |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 25 | |
Dan Gohman | fbcb5b6 | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 26 | for (const char *End = Ptr + Size; Ptr != End; ++Ptr) { |
| 27 | ++Column; |
| 28 | if (*Ptr == '\n' || *Ptr == '\r') |
| 29 | Column = 0; |
| 30 | else if (*Ptr == '\t') |
| 31 | // Assumes tab stop = 8 characters. |
| 32 | Column += (8 - (Column & 0x7)) & 0x7; |
David Greene | eb85728 | 2009-07-29 16:08:27 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Dan Gohman | fbcb5b6 | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 35 | return Column; |
| 36 | } |
| 37 | |
| 38 | /// ComputeColumn - Examine the current output and figure out which |
| 39 | /// column we end up in after output. |
Daniel Dunbar | ab81027 | 2009-08-18 23:36:04 +0000 | [diff] [blame] | 40 | void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) { |
| 41 | // If our previous scan pointer is inside the buffer, assume we already |
| 42 | // scanned those bytes. This depends on raw_ostream to not change our buffer |
| 43 | // in unexpected ways. |
| 44 | if (Ptr <= Scanned && Scanned <= Ptr + Size) { |
| 45 | // Scan all characters added since our last scan to determine the new |
| 46 | // column. |
| 47 | ColumnScanned = CountColumns(ColumnScanned, Scanned, |
| 48 | Size - (Scanned - Ptr)); |
| 49 | } else |
| 50 | ColumnScanned = CountColumns(ColumnScanned, Ptr, Size); |
| 51 | |
| 52 | // Update the scanning pointer. |
| 53 | Scanned = Ptr + Size; |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 54 | } |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 55 | |
| 56 | /// PadToColumn - Align the output to some column number. |
| 57 | /// |
| 58 | /// \param NewCol - The column to move to. |
| 59 | /// \param MinPad - The minimum space to give after the most recent |
| 60 | /// I/O, even if the current column + minpad > newcol. |
| 61 | /// |
Chris Lattner | efbdaa6 | 2010-02-15 02:17:50 +0000 | [diff] [blame^] | 62 | formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) { |
David Greene | eb85728 | 2009-07-29 16:08:27 +0000 | [diff] [blame] | 63 | // Figure out what's in the buffer and add it to the column count. |
Daniel Dunbar | ab81027 | 2009-08-18 23:36:04 +0000 | [diff] [blame] | 64 | ComputeColumn(getBufferStart(), GetNumBytesInBuffer()); |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 65 | |
| 66 | // Output spaces until we reach the desired column. |
Chris Lattner | de51ded | 2009-08-22 23:16:09 +0000 | [diff] [blame] | 67 | indent(std::max(int(NewCol - ColumnScanned), 1)); |
Chris Lattner | efbdaa6 | 2010-02-15 02:17:50 +0000 | [diff] [blame^] | 68 | return *this; |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dan Gohman | a4a68c1 | 2009-08-15 02:01:04 +0000 | [diff] [blame] | 71 | void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) { |
Dan Gohman | fbcb5b6 | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 72 | // Figure out what's in the buffer and add it to the column count. |
Daniel Dunbar | ab81027 | 2009-08-18 23:36:04 +0000 | [diff] [blame] | 73 | ComputeColumn(Ptr, Size); |
Dan Gohman | fbcb5b6 | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 74 | |
| 75 | // Write the data to the underlying stream (which is unbuffered, so |
| 76 | // the data will be immediately written out). |
Dan Gohman | a4a68c1 | 2009-08-15 02:01:04 +0000 | [diff] [blame] | 77 | TheStream->write(Ptr, Size); |
Dan Gohman | fbcb5b6 | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 78 | |
Daniel Dunbar | ab81027 | 2009-08-18 23:36:04 +0000 | [diff] [blame] | 79 | // Reset the scanning pointer. |
| 80 | Scanned = 0; |
Dan Gohman | a4a68c1 | 2009-08-15 02:01:04 +0000 | [diff] [blame] | 81 | } |
| 82 | |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 83 | /// fouts() - This returns a reference to a formatted_raw_ostream for |
| 84 | /// standard output. Use it like: fouts() << "foo" << "bar"; |
| 85 | formatted_raw_ostream &llvm::fouts() { |
| 86 | static formatted_raw_ostream S(outs()); |
| 87 | return S; |
| 88 | } |
| 89 | |
| 90 | /// ferrs() - This returns a reference to a formatted_raw_ostream for |
| 91 | /// standard error. Use it like: ferrs() << "foo" << "bar"; |
| 92 | formatted_raw_ostream &llvm::ferrs() { |
| 93 | static formatted_raw_ostream S(errs()); |
| 94 | return S; |
| 95 | } |
David Greene | 8f1929b | 2010-01-05 01:28:43 +0000 | [diff] [blame] | 96 | |
| 97 | /// fdbgs() - This returns a reference to a formatted_raw_ostream for |
| 98 | /// the debug stream. Use it like: fdbgs() << "foo" << "bar"; |
| 99 | formatted_raw_ostream &llvm::fdbgs() { |
| 100 | static formatted_raw_ostream S(dbgs()); |
| 101 | return S; |
| 102 | } |