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