blob: 1198ebf08e53ad7c8cdbd287cd53b094039c3ba7 [file] [log] [blame]
David Greene191cf282009-07-13 16:49:27 +00001//===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
David Greene62fe47a2009-07-10 21:14:44 +00002//
3// The LLVM Compiler Infrastructure
4//
David Greene191cf282009-07-13 16:49:27 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
David Greene62fe47a2009-07-10 21:14:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner1f318e02009-07-14 23:14:10 +000010// This file contains the implementation of formatted_raw_ostream.
David Greene62fe47a2009-07-10 21:14:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/FormattedStream.h"
David Greeneed0e2ad2009-07-23 23:21:10 +000015#include <algorithm>
16
David Greene191cf282009-07-13 16:49:27 +000017using namespace llvm;
David Greene62fe47a2009-07-10 21:14:44 +000018
David Greene191cf282009-07-13 16:49:27 +000019/// ComputeColumn - Examine the current output and figure out which
20/// column we end up in after output.
21///
David Greene7aaad712009-07-28 23:26:34 +000022void formatted_raw_ostream::ComputeColumn(unsigned &Column) {
David Greene191cf282009-07-13 16:49:27 +000023 // Keep track of the current column by scanning the string for
24 // special characters
David Greene62fe47a2009-07-10 21:14:44 +000025
David Greene7aaad712009-07-28 23:26:34 +000026 for (const char *Ptr = begin(); Ptr != end(); ++Ptr) {
David Greene191cf282009-07-13 16:49:27 +000027 ++Column;
28 if (*Ptr == '\n' || *Ptr == '\r')
29 Column = 0;
30 else if (*Ptr == '\t')
31 Column += (8 - (Column & 0x7)) & 0x7;
David Greene62fe47a2009-07-10 21:14:44 +000032 }
33}
David Greene191cf282009-07-13 16:49:27 +000034
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///
David Greene7aaad712009-07-28 23:26:34 +000041void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) {
42 // Start out from the last flush position.
43 unsigned Column = ColumnFlushed;
44
45 // Now figure out what's in the buffer and add it to the column
46 // count.
47 ComputeColumn(Column);
David Greene191cf282009-07-13 16:49:27 +000048
49 // Output spaces until we reach the desired column.
50 unsigned num = NewCol - Column;
Chris Lattner1f318e02009-07-14 23:14:10 +000051 if (NewCol < Column || num < MinPad)
David Greene191cf282009-07-13 16:49:27 +000052 num = MinPad;
David Greene191cf282009-07-13 16:49:27 +000053
David Greeneed0e2ad2009-07-23 23:21:10 +000054 // Keep a buffer of spaces handy to speed up processing.
55 static char Spaces[MAX_COLUMN_PAD];
56 static bool Initialized = false;
57 if (!Initialized) {
58 std::fill_n(Spaces, MAX_COLUMN_PAD, ' '),
59 Initialized = true;
60 }
61
62 assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding");
63
64 write(Spaces, num);
David Greene191cf282009-07-13 16:49:27 +000065}
66
David Greene71847812009-07-14 20:18:05 +000067/// fouts() - This returns a reference to a formatted_raw_ostream for
68/// standard output. Use it like: fouts() << "foo" << "bar";
69formatted_raw_ostream &llvm::fouts() {
70 static formatted_raw_ostream S(outs());
71 return S;
72}
73
74/// ferrs() - This returns a reference to a formatted_raw_ostream for
75/// standard error. Use it like: ferrs() << "foo" << "bar";
76formatted_raw_ostream &llvm::ferrs() {
77 static formatted_raw_ostream S(errs());
78 return S;
79}