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/ConvertUTF.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/Unicode.h"
17 #include "llvm/Support/raw_ostream.h"
22 /// UpdatePosition - Examine the given char sequence and figure out which
23 /// column we end up in after output, and how many line breaks are contained.
24 /// This assumes that the input string is well-formed UTF-8, and takes into
25 /// account Unicode characters which render as multiple columns wide.
26 void formatted_raw_ostream::UpdatePosition(const char *Ptr
, size_t Size
) {
27 unsigned &Column
= Position
.first
;
28 unsigned &Line
= Position
.second
;
30 auto ProcessUTF8CodePoint
= [&Line
, &Column
](StringRef CP
) {
31 int Width
= sys::unicode::columnWidthUTF8(CP
);
32 if (Width
!= sys::unicode::ErrorNonPrintableCharacter
)
35 // The only special whitespace characters we care about are single-byte.
47 // Assumes tab stop = 8 characters.
48 Column
+= (8 - (Column
& 0x7)) & 0x7;
53 // If we have a partial UTF-8 sequence from the previous buffer, check that
55 if (PartialUTF8Char
.size()) {
56 size_t BytesFromBuffer
=
57 getNumBytesForUTF8(PartialUTF8Char
[0]) - PartialUTF8Char
.size();
58 if (Size
< BytesFromBuffer
) {
59 // If we still don't have enough bytes for a complete code point, just
60 // append what we have.
61 PartialUTF8Char
.append(StringRef(Ptr
, Size
));
64 // The first few bytes from the buffer will complete the code point.
65 // Concatenate them and process their effect on the line and column
67 PartialUTF8Char
.append(StringRef(Ptr
, BytesFromBuffer
));
68 ProcessUTF8CodePoint(PartialUTF8Char
);
69 PartialUTF8Char
.clear();
70 Ptr
+= BytesFromBuffer
;
71 Size
-= BytesFromBuffer
;
75 // Now scan the rest of the buffer.
77 for (const char *End
= Ptr
+ Size
; Ptr
< End
; Ptr
+= NumBytes
) {
78 // Fast path for printable ASCII characters without special handling.
79 if (*Ptr
>= 0x20 && *Ptr
<= 0x7e) {
85 NumBytes
= getNumBytesForUTF8(*Ptr
);
87 // The buffer might end part way through a UTF-8 code unit sequence for a
88 // Unicode scalar value if it got flushed. If this happens, we can't know
89 // the display width until we see the rest of the code point. Stash the
90 // bytes we do have, so that we can reconstruct the whole code point later,
91 // even if the buffer is being flushed.
92 if ((unsigned)(End
- Ptr
) < NumBytes
) {
93 PartialUTF8Char
= StringRef(Ptr
, End
- Ptr
);
97 ProcessUTF8CodePoint(StringRef(Ptr
, NumBytes
));
101 /// ComputePosition - Examine the current output and update line and column
103 void formatted_raw_ostream::ComputePosition(const char *Ptr
, size_t Size
) {
107 // If our previous scan pointer is inside the buffer, assume we already
108 // scanned those bytes. This depends on raw_ostream to not change our buffer
109 // in unexpected ways.
110 if (Ptr
<= Scanned
&& Scanned
<= Ptr
+ Size
)
111 // Scan all characters added since our last scan to determine the new
113 UpdatePosition(Scanned
, Size
- (Scanned
- Ptr
));
115 UpdatePosition(Ptr
, Size
);
117 // Update the scanning pointer.
118 Scanned
= Ptr
+ Size
;
121 /// PadToColumn - Align the output to some column number.
123 /// \param NewCol - The column to move to.
125 formatted_raw_ostream
&formatted_raw_ostream::PadToColumn(unsigned NewCol
) {
126 // Figure out what's in the buffer and add it to the column count.
127 ComputePosition(getBufferStart(), GetNumBytesInBuffer());
129 // Output spaces until we reach the desired column.
130 indent(std::max(int(NewCol
- getColumn()), 1));
134 void formatted_raw_ostream::write_impl(const char *Ptr
, size_t Size
) {
135 // Figure out what's in the buffer and add it to the column count.
136 ComputePosition(Ptr
, Size
);
138 // Write the data to the underlying stream (which is unbuffered, so
139 // the data will be immediately written out).
140 TheStream
->write(Ptr
, Size
);
142 // Reset the scanning pointer.
146 /// fouts() - This returns a reference to a formatted_raw_ostream for
147 /// standard output. Use it like: fouts() << "foo" << "bar";
148 formatted_raw_ostream
&llvm::fouts() {
149 static formatted_raw_ostream
S(outs());
153 /// ferrs() - This returns a reference to a formatted_raw_ostream for
154 /// standard error. Use it like: ferrs() << "foo" << "bar";
155 formatted_raw_ostream
&llvm::ferrs() {
156 static formatted_raw_ostream
S(errs());
160 /// fdbgs() - This returns a reference to a formatted_raw_ostream for
161 /// the debug stream. Use it like: fdbgs() << "foo" << "bar";
162 formatted_raw_ostream
&llvm::fdbgs() {
163 static formatted_raw_ostream
S(dbgs());