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