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 | |
| 14 | #include "llvm/Support/FormattedStream.h" |
David Greene | ed0e2ad | 2009-07-23 23:21:10 +0000 | [diff] [blame^] | 15 | #include <algorithm> |
| 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 | |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 19 | /// ComputeColumn - Examine the current output and figure out which |
| 20 | /// column we end up in after output. |
| 21 | /// |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 22 | void formatted_raw_ostream::ComputeColumn(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 | |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 26 | for (const char *epos = Ptr + Size; Ptr != epos; ++Ptr) { |
| 27 | ++Column; |
| 28 | if (*Ptr == '\n' || *Ptr == '\r') |
| 29 | Column = 0; |
| 30 | else if (*Ptr == '\t') |
| 31 | Column += (8 - (Column & 0x7)) & 0x7; |
David Greene | 62fe47a | 2009-07-10 21:14:44 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 34 | |
| 35 | /// PadToColumn - Align the output to some column number. |
| 36 | /// |
| 37 | /// \param NewCol - The column to move to. |
| 38 | /// \param MinPad - The minimum space to give after the most recent |
| 39 | /// I/O, even if the current column + minpad > newcol. |
| 40 | /// |
Chris Lattner | 1f318e0 | 2009-07-14 23:14:10 +0000 | [diff] [blame] | 41 | void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) { |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 42 | flush(); |
| 43 | |
| 44 | // Output spaces until we reach the desired column. |
| 45 | unsigned num = NewCol - Column; |
Chris Lattner | 1f318e0 | 2009-07-14 23:14:10 +0000 | [diff] [blame] | 46 | if (NewCol < Column || num < MinPad) |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 47 | num = MinPad; |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 48 | |
David Greene | ed0e2ad | 2009-07-23 23:21:10 +0000 | [diff] [blame^] | 49 | // Keep a buffer of spaces handy to speed up processing. |
| 50 | static char Spaces[MAX_COLUMN_PAD]; |
| 51 | static bool Initialized = false; |
| 52 | if (!Initialized) { |
| 53 | std::fill_n(Spaces, MAX_COLUMN_PAD, ' '), |
| 54 | Initialized = true; |
| 55 | } |
| 56 | |
| 57 | assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding"); |
| 58 | |
| 59 | write(Spaces, num); |
David Greene | 191cf28 | 2009-07-13 16:49:27 +0000 | [diff] [blame] | 60 | } |
| 61 | |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 62 | /// fouts() - This returns a reference to a formatted_raw_ostream for |
| 63 | /// standard output. Use it like: fouts() << "foo" << "bar"; |
| 64 | formatted_raw_ostream &llvm::fouts() { |
| 65 | static formatted_raw_ostream S(outs()); |
| 66 | return S; |
| 67 | } |
| 68 | |
| 69 | /// ferrs() - This returns a reference to a formatted_raw_ostream for |
| 70 | /// standard error. Use it like: ferrs() << "foo" << "bar"; |
| 71 | formatted_raw_ostream &llvm::ferrs() { |
| 72 | static formatted_raw_ostream S(errs()); |
| 73 | return S; |
| 74 | } |