LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FormattedStream.cpp
Go to the documentation of this file.
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/Debug.h"
16 #include <algorithm>
17 
18 using namespace llvm;
19 
20 /// UpdatePosition - Examine the given char sequence and figure out which
21 /// column we end up in after output, and how many line breaks are contained.
22 ///
23 static void UpdatePosition(std::pair<unsigned, unsigned> &Position, const char *Ptr, size_t Size) {
24  unsigned &Column = Position.first;
25  unsigned &Line = Position.second;
26 
27  // Keep track of the current column and line by scanning the string for
28  // special characters
29  for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
30  ++Column;
31  switch (*Ptr) {
32  case '\n':
33  Line += 1;
34  case '\r':
35  Column = 0;
36  break;
37  case '\t':
38  // Assumes tab stop = 8 characters.
39  Column += (8 - (Column & 0x7)) & 0x7;
40  break;
41  }
42  }
43 }
44 
45 /// ComputePosition - Examine the current output and update line and column
46 /// counts.
47 void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
48  // If our previous scan pointer is inside the buffer, assume we already
49  // scanned those bytes. This depends on raw_ostream to not change our buffer
50  // in unexpected ways.
51  if (Ptr <= Scanned && Scanned <= Ptr + Size)
52  // Scan all characters added since our last scan to determine the new
53  // column.
54  UpdatePosition(Position, Scanned, Size - (Scanned - Ptr));
55  else
56  UpdatePosition(Position, Ptr, Size);
57 
58  // Update the scanning pointer.
59  Scanned = Ptr + Size;
60 }
61 
62 /// PadToColumn - Align the output to some column number.
63 ///
64 /// \param NewCol - The column to move to.
65 ///
67  // Figure out what's in the buffer and add it to the column count.
68  ComputePosition(getBufferStart(), GetNumBytesInBuffer());
69 
70  // Output spaces until we reach the desired column.
71  indent(std::max(int(NewCol - getColumn()), 1));
72  return *this;
73 }
74 
75 void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
76  // Figure out what's in the buffer and add it to the column count.
77  ComputePosition(Ptr, Size);
78 
79  // Write the data to the underlying stream (which is unbuffered, so
80  // the data will be immediately written out).
81  TheStream->write(Ptr, Size);
82 
83  // Reset the scanning pointer.
84  Scanned = 0;
85 }
86 
87 /// fouts() - This returns a reference to a formatted_raw_ostream for
88 /// standard output. Use it like: fouts() << "foo" << "bar";
90  static formatted_raw_ostream S(outs());
91  return S;
92 }
93 
94 /// ferrs() - This returns a reference to a formatted_raw_ostream for
95 /// standard error. Use it like: ferrs() << "foo" << "bar";
97  static formatted_raw_ostream S(errs());
98  return S;
99 }
100 
101 /// fdbgs() - This returns a reference to a formatted_raw_ostream for
102 /// the debug stream. Use it like: fdbgs() << "foo" << "bar";
104  static formatted_raw_ostream S(dbgs());
105  return S;
106 }
raw_ostream & errs()
const char * getBufferStart() const
Definition: raw_ostream.h:285
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
formatted_raw_ostream & PadToColumn(unsigned NewCol)
unsigned getColumn()
getColumn - Return the column number
raw_ostream & outs()
raw_ostream & write(unsigned char C)
formatted_raw_ostream & fdbgs()
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
static void UpdatePosition(std::pair< unsigned, unsigned > &Position, const char *Ptr, size_t Size)
formatted_raw_ostream & ferrs()
formatted_raw_ostream & fouts()
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:121