David Greene | 20f6ac0 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 1 | //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===// |
David Greene | c97b778b | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
David Greene | 20f6ac0 | 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 | c97b778b | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | ca99c34 | 2009-07-14 23:14:10 +0000 | [diff] [blame] | 10 | // This file contains the implementation of formatted_raw_ostream. |
David Greene | c97b778b | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
David Greene | 93a522b | 2010-01-05 01:28:43 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Debug.h" |
David Greene | c97b778b | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 15 | #include "llvm/Support/FormattedStream.h" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 16 | #include <algorithm> |
David Greene | 8e621f0 | 2009-07-23 23:21:10 +0000 | [diff] [blame] | 17 | |
David Greene | 20f6ac0 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 18 | using namespace llvm; |
David Greene | c97b778b | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 19 | |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 20 | /// UpdatePosition - Examine the given char sequence and figure out which |
| 21 | /// column we end up in after output, and how many line breaks are contained. |
David Greene | 20f6ac0 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 22 | /// |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 23 | static void UpdatePosition(std::pair<unsigned, unsigned> &Position, const char *Ptr, size_t Size) { |
| 24 | unsigned &Column = Position.first; |
| 25 | unsigned &Line = Position.second; |
David Greene | c97b778b | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 26 | |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 27 | // Keep track of the current column and line by scanning the string for |
| 28 | // special characters |
Dan Gohman | 186b85d | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 29 | for (const char *End = Ptr + Size; Ptr != End; ++Ptr) { |
| 30 | ++Column; |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 31 | switch (*Ptr) { |
| 32 | case '\n': |
| 33 | Line += 1; |
| 34 | case '\r': |
Dan Gohman | 186b85d | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 35 | Column = 0; |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 36 | break; |
| 37 | case '\t': |
Dan Gohman | 186b85d | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 38 | // Assumes tab stop = 8 characters. |
| 39 | Column += (8 - (Column & 0x7)) & 0x7; |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 40 | break; |
| 41 | } |
David Greene | ea2f1ce | 2009-07-29 16:08:27 +0000 | [diff] [blame] | 42 | } |
Dan Gohman | 186b85d | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 45 | /// ComputePosition - Examine the current output and update line and column |
| 46 | /// counts. |
| 47 | void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) { |
Daniel Dunbar | 17a6fd2 | 2009-08-18 23:36:04 +0000 | [diff] [blame] | 48 | // If our previous scan pointer is inside the buffer, assume we already |
| 49 | // scanned those bytes. This depends on raw_ostream to not change our buffer |
| 50 | // in unexpected ways. |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 51 | if (Ptr <= Scanned && Scanned <= Ptr + Size) |
Daniel Dunbar | 17a6fd2 | 2009-08-18 23:36:04 +0000 | [diff] [blame] | 52 | // Scan all characters added since our last scan to determine the new |
| 53 | // column. |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 54 | UpdatePosition(Position, Scanned, Size - (Scanned - Ptr)); |
| 55 | else |
| 56 | UpdatePosition(Position, Ptr, Size); |
Daniel Dunbar | 17a6fd2 | 2009-08-18 23:36:04 +0000 | [diff] [blame] | 57 | |
| 58 | // Update the scanning pointer. |
| 59 | Scanned = Ptr + Size; |
David Greene | c97b778b | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 60 | } |
David Greene | 20f6ac0 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 61 | |
| 62 | /// PadToColumn - Align the output to some column number. |
| 63 | /// |
| 64 | /// \param NewCol - The column to move to. |
David Greene | 20f6ac0 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 65 | /// |
Chris Lattner | f733d75 | 2010-02-15 02:17:50 +0000 | [diff] [blame] | 66 | formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) { |
David Greene | ea2f1ce | 2009-07-29 16:08:27 +0000 | [diff] [blame] | 67 | // Figure out what's in the buffer and add it to the column count. |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 68 | ComputePosition(getBufferStart(), GetNumBytesInBuffer()); |
David Greene | 20f6ac0 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 69 | |
| 70 | // Output spaces until we reach the desired column. |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 71 | indent(std::max(int(NewCol - getColumn()), 1)); |
Chris Lattner | f733d75 | 2010-02-15 02:17:50 +0000 | [diff] [blame] | 72 | return *this; |
David Greene | 20f6ac0 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Dan Gohman | 250635e | 2009-08-15 02:01:04 +0000 | [diff] [blame] | 75 | void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) { |
Dan Gohman | 186b85d | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 76 | // Figure out what's in the buffer and add it to the column count. |
Daniel Malea | f83beab | 2013-05-08 20:29:10 +0000 | [diff] [blame] | 77 | ComputePosition(Ptr, Size); |
Dan Gohman | 186b85d | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 78 | |
| 79 | // Write the data to the underlying stream (which is unbuffered, so |
| 80 | // the data will be immediately written out). |
Dan Gohman | 250635e | 2009-08-15 02:01:04 +0000 | [diff] [blame] | 81 | TheStream->write(Ptr, Size); |
Dan Gohman | 186b85d | 2009-08-15 02:02:59 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | 17a6fd2 | 2009-08-18 23:36:04 +0000 | [diff] [blame] | 83 | // Reset the scanning pointer. |
| 84 | Scanned = 0; |
Dan Gohman | 250635e | 2009-08-15 02:01:04 +0000 | [diff] [blame] | 85 | } |
| 86 | |
David Greene | a31f96c | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 87 | /// fouts() - This returns a reference to a formatted_raw_ostream for |
| 88 | /// standard output. Use it like: fouts() << "foo" << "bar"; |
| 89 | formatted_raw_ostream &llvm::fouts() { |
| 90 | static formatted_raw_ostream S(outs()); |
| 91 | return S; |
| 92 | } |
| 93 | |
| 94 | /// ferrs() - This returns a reference to a formatted_raw_ostream for |
| 95 | /// standard error. Use it like: ferrs() << "foo" << "bar"; |
| 96 | formatted_raw_ostream &llvm::ferrs() { |
| 97 | static formatted_raw_ostream S(errs()); |
| 98 | return S; |
| 99 | } |
David Greene | 93a522b | 2010-01-05 01:28:43 +0000 | [diff] [blame] | 100 | |
| 101 | /// fdbgs() - This returns a reference to a formatted_raw_ostream for |
| 102 | /// the debug stream. Use it like: fdbgs() << "foo" << "bar"; |
| 103 | formatted_raw_ostream &llvm::fdbgs() { |
| 104 | static formatted_raw_ostream S(dbgs()); |
| 105 | return S; |
| 106 | } |