Have asm printers use formatted_raw_ostream directly to avoid a
[oota-llvm.git] / lib / Support / FormattedStream.cpp
1 //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the implementation of formatted_raw_ostream and
11 // friends.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Support/FormattedStream.h"
16
17 using namespace llvm;
18
19 /// ComputeColumn - Examine the current output and figure out which
20 /// column we end up in after output.
21 ///
22 void formatted_raw_ostream::ComputeColumn(const char *Ptr, unsigned Size)
23 {
24   // Keep track of the current column by scanning the string for
25   // special characters
26
27   for (const char *epos = Ptr + Size; Ptr != epos; ++Ptr) {
28     ++Column;
29     if (*Ptr == '\n' || *Ptr == '\r')
30       Column = 0;
31     else if (*Ptr == '\t')
32       Column += (8 - (Column & 0x7)) & 0x7;
33   }
34 }
35
36 /// PadToColumn - Align the output to some column number.
37 ///
38 /// \param NewCol - The column to move to.
39 /// \param MinPad - The minimum space to give after the most recent
40 /// I/O, even if the current column + minpad > newcol.
41 ///
42 void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) 
43 {
44   flush();
45
46   // Output spaces until we reach the desired column.
47   unsigned num = NewCol - Column;
48   if (NewCol < Column || num < MinPad) {
49     num = MinPad;
50   }
51
52   // TODO: Write a whole string at a time.
53   while (num-- > 0) {
54     write(' ');
55   }
56 }
57
58 /// fouts() - This returns a reference to a formatted_raw_ostream for
59 /// standard output.  Use it like: fouts() << "foo" << "bar";
60 formatted_raw_ostream &llvm::fouts() {
61   static formatted_raw_ostream S(outs());
62   return S;
63 }
64
65 /// ferrs() - This returns a reference to a formatted_raw_ostream for
66 /// standard error.  Use it like: ferrs() << "foo" << "bar";
67 formatted_raw_ostream &llvm::ferrs() {
68   static formatted_raw_ostream S(errs());
69   return S;
70 }