1 //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the implementation of formatted_raw_ostream.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/FormattedStream.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
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.
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
;
27 // Keep track of the current column and line by scanning the string for
29 for (const char *End
= Ptr
+ Size
; Ptr
!= End
; ++Ptr
) {
39 // Assumes tab stop = 8 characters.
40 Column
+= (8 - (Column
& 0x7)) & 0x7;
46 /// ComputePosition - Examine the current output and update line and column
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
55 UpdatePosition(Position
, Scanned
, Size
- (Scanned
- Ptr
));
57 UpdatePosition(Position
, Ptr
, Size
);
59 // Update the scanning pointer.
63 /// PadToColumn - Align the output to some column number.
65 /// \param NewCol - The column to move to.
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());
71 // Output spaces until we reach the desired column.
72 indent(std::max(int(NewCol
- getColumn()), 1));
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
);
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
);
84 // Reset the scanning pointer.
88 /// fouts() - This returns a reference to a formatted_raw_ostream for
89 /// standard output. Use it like: fouts() << "foo" << "bar";
90 formatted_raw_ostream
&llvm::fouts() {
91 static formatted_raw_ostream
S(outs());
95 /// ferrs() - This returns a reference to a formatted_raw_ostream for
96 /// standard error. Use it like: ferrs() << "foo" << "bar";
97 formatted_raw_ostream
&llvm::ferrs() {
98 static formatted_raw_ostream
S(errs());
102 /// fdbgs() - This returns a reference to a formatted_raw_ostream for
103 /// the debug stream. Use it like: fdbgs() << "foo" << "bar";
104 formatted_raw_ostream
&llvm::fdbgs() {
105 static formatted_raw_ostream
S(dbgs());