1 //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the implementation of formatted_raw_ostream.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/FormattedStream.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/raw_ostream.h"
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.
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
;
28 // Keep track of the current column and line by scanning the string for
30 for (const char *End
= Ptr
+ Size
; Ptr
!= End
; ++Ptr
) {
40 // Assumes tab stop = 8 characters.
41 Column
+= (8 - (Column
& 0x7)) & 0x7;
47 /// ComputePosition - Examine the current output and update line and column
49 void formatted_raw_ostream::ComputePosition(const char *Ptr
, size_t Size
) {
50 // If our previous scan pointer is inside the buffer, assume we already
51 // scanned those bytes. This depends on raw_ostream to not change our buffer
52 // in unexpected ways.
53 if (Ptr
<= Scanned
&& Scanned
<= Ptr
+ Size
)
54 // Scan all characters added since our last scan to determine the new
56 UpdatePosition(Position
, Scanned
, Size
- (Scanned
- Ptr
));
58 UpdatePosition(Position
, Ptr
, Size
);
60 // Update the scanning pointer.
64 /// PadToColumn - Align the output to some column number.
66 /// \param NewCol - The column to move to.
68 formatted_raw_ostream
&formatted_raw_ostream::PadToColumn(unsigned NewCol
) {
69 // Figure out what's in the buffer and add it to the column count.
70 ComputePosition(getBufferStart(), GetNumBytesInBuffer());
72 // Output spaces until we reach the desired column.
73 indent(std::max(int(NewCol
- getColumn()), 1));
77 void formatted_raw_ostream::write_impl(const char *Ptr
, size_t Size
) {
78 // Figure out what's in the buffer and add it to the column count.
79 ComputePosition(Ptr
, Size
);
81 // Write the data to the underlying stream (which is unbuffered, so
82 // the data will be immediately written out).
83 TheStream
->write(Ptr
, Size
);
85 // Reset the scanning pointer.
89 /// fouts() - This returns a reference to a formatted_raw_ostream for
90 /// standard output. Use it like: fouts() << "foo" << "bar";
91 formatted_raw_ostream
&llvm::fouts() {
92 static formatted_raw_ostream
S(outs());
96 /// ferrs() - This returns a reference to a formatted_raw_ostream for
97 /// standard error. Use it like: ferrs() << "foo" << "bar";
98 formatted_raw_ostream
&llvm::ferrs() {
99 static formatted_raw_ostream
S(errs());
103 /// fdbgs() - This returns a reference to a formatted_raw_ostream for
104 /// the debug stream. Use it like: fdbgs() << "foo" << "bar";
105 formatted_raw_ostream
&llvm::fdbgs() {
106 static formatted_raw_ostream
S(dbgs());