blob: e57b17ec45de89260cb291fd432d0afbc4985537 [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 Greeneeb857282009-07-29 16:08:27 +000022void formatted_raw_ostream::ComputeColumn() {
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 Greeneeb857282009-07-29 16:08:27 +000026 // The buffer may have been allocated underneath us.
27 if (Scanned == 0 && GetNumBytesInBuffer() != 0) {
28 Scanned = begin();
29 }
30
31 while (Scanned != end()) {
32 ++ColumnScanned;
33 if (*Scanned == '\n' || *Scanned == '\r')
34 ColumnScanned = 0;
35 else if (*Scanned == '\t')
36 // Assumes tab stop = 8 characters.
37 ColumnScanned += (8 - (ColumnScanned & 0x7)) & 0x7;
38 ++Scanned;
David Greene62fe47a2009-07-10 21:14:44 +000039 }
40}
David Greene191cf282009-07-13 16:49:27 +000041
42/// PadToColumn - Align the output to some column number.
43///
44/// \param NewCol - The column to move to.
45/// \param MinPad - The minimum space to give after the most recent
46/// I/O, even if the current column + minpad > newcol.
47///
David Greeneeb857282009-07-29 16:08:27 +000048void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) {
49 // Figure out what's in the buffer and add it to the column count.
50 ComputeColumn();
David Greene191cf282009-07-13 16:49:27 +000051
52 // Output spaces until we reach the desired column.
David Greeneeb857282009-07-29 16:08:27 +000053 unsigned num = NewCol - ColumnScanned;
54 if (NewCol < ColumnScanned || num < MinPad)
David Greene191cf282009-07-13 16:49:27 +000055 num = MinPad;
David Greene191cf282009-07-13 16:49:27 +000056
David Greeneed0e2ad2009-07-23 23:21:10 +000057 // Keep a buffer of spaces handy to speed up processing.
David Greeneeb857282009-07-29 16:08:27 +000058 const char *Spaces = " "
59 " ";
David Greeneed0e2ad2009-07-23 23:21:10 +000060
61 assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding");
62
63 write(Spaces, num);
David Greene191cf282009-07-13 16:49:27 +000064}
65
David Greene71847812009-07-14 20:18:05 +000066/// fouts() - This returns a reference to a formatted_raw_ostream for
67/// standard output. Use it like: fouts() << "foo" << "bar";
68formatted_raw_ostream &llvm::fouts() {
69 static formatted_raw_ostream S(outs());
70 return S;
71}
72
73/// ferrs() - This returns a reference to a formatted_raw_ostream for
74/// standard error. Use it like: ferrs() << "foo" << "bar";
75formatted_raw_ostream &llvm::ferrs() {
76 static formatted_raw_ostream S(errs());
77 return S;
78}