LLVM 20.0.0git
FormattedStream.cpp
Go to the documentation of this file.
1//===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation of formatted_raw_ostream.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/Support/Debug.h"
18#include <algorithm>
19
20using namespace llvm;
21
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.
26void formatted_raw_ostream::UpdatePosition(const char *Ptr, size_t Size) {
27 unsigned &Column = Position.first;
28 unsigned &Line = Position.second;
29
30 auto ProcessUTF8CodePoint = [&Line, &Column](StringRef CP) {
31 int Width = sys::unicode::columnWidthUTF8(CP);
33 Column += Width;
34
35 // The only special whitespace characters we care about are single-byte.
36 if (CP.size() > 1)
37 return;
38
39 switch (CP[0]) {
40 case '\n':
41 Line += 1;
42 [[fallthrough]];
43 case '\r':
44 Column = 0;
45 break;
46 case '\t':
47 // Assumes tab stop = 8 characters.
48 Column += (8 - (Column & 0x7)) & 0x7;
49 break;
50 }
51 };
52
53 // If we have a partial UTF-8 sequence from the previous buffer, check that
54 // first.
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));
62 return;
63 } else {
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
66 // numbers.
67 PartialUTF8Char.append(StringRef(Ptr, BytesFromBuffer));
68 ProcessUTF8CodePoint(PartialUTF8Char);
69 PartialUTF8Char.clear();
70 Ptr += BytesFromBuffer;
71 Size -= BytesFromBuffer;
72 }
73 }
74
75 // Now scan the rest of the buffer.
76 unsigned NumBytes;
77 for (const char *End = Ptr + Size; Ptr < End; Ptr += NumBytes) {
78 NumBytes = getNumBytesForUTF8(*Ptr);
79
80 // The buffer might end part way through a UTF-8 code unit sequence for a
81 // Unicode scalar value if it got flushed. If this happens, we can't know
82 // the display width until we see the rest of the code point. Stash the
83 // bytes we do have, so that we can reconstruct the whole code point later,
84 // even if the buffer is being flushed.
85 if ((unsigned)(End - Ptr) < NumBytes) {
86 PartialUTF8Char = StringRef(Ptr, End - Ptr);
87 return;
88 }
89
90 ProcessUTF8CodePoint(StringRef(Ptr, NumBytes));
91 }
92}
93
94/// ComputePosition - Examine the current output and update line and column
95/// counts.
96void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
97 if (DisableScan)
98 return;
99
100 // If our previous scan pointer is inside the buffer, assume we already
101 // scanned those bytes. This depends on raw_ostream to not change our buffer
102 // in unexpected ways.
103 if (Ptr <= Scanned && Scanned <= Ptr + Size)
104 // Scan all characters added since our last scan to determine the new
105 // column.
106 UpdatePosition(Scanned, Size - (Scanned - Ptr));
107 else
108 UpdatePosition(Ptr, Size);
109
110 // Update the scanning pointer.
111 Scanned = Ptr + Size;
112}
113
114/// PadToColumn - Align the output to some column number.
115///
116/// \param NewCol - The column to move to.
117///
119 // Figure out what's in the buffer and add it to the column count.
120 ComputePosition(getBufferStart(), GetNumBytesInBuffer());
121
122 // Output spaces until we reach the desired column.
123 indent(std::max(int(NewCol - getColumn()), 1));
124 return *this;
125}
126
127void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
128 // Figure out what's in the buffer and add it to the column count.
129 ComputePosition(Ptr, Size);
130
131 // Write the data to the underlying stream (which is unbuffered, so
132 // the data will be immediately written out).
133 TheStream->write(Ptr, Size);
134
135 // Reset the scanning pointer.
136 Scanned = nullptr;
137}
138
139/// fouts() - This returns a reference to a formatted_raw_ostream for
140/// standard output. Use it like: fouts() << "foo" << "bar";
142 static formatted_raw_ostream S(outs());
143 return S;
144}
145
146/// ferrs() - This returns a reference to a formatted_raw_ostream for
147/// standard error. Use it like: ferrs() << "foo" << "bar";
149 static formatted_raw_ostream S(errs());
150 return S;
151}
152
153/// fdbgs() - This returns a reference to a formatted_raw_ostream for
154/// the debug stream. Use it like: fdbgs() << "foo" << "bar";
156 static formatted_raw_ostream S(dbgs());
157 return S;
158}
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
size_t size() const
Definition: SmallVector.h:92
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
formatted_raw_ostream & PadToColumn(unsigned NewCol)
PadToColumn - Align the output to some column number.
raw_ostream & write(unsigned char C)
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:396
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:190
@ ErrorNonPrintableCharacter
Definition: Unicode.h:29
int columnWidthUTF8(StringRef Text)
Gets the number of positions the UTF8-encoded Text is likely to occupy when output on a terminal ("ch...
Definition: Unicode.cpp:481
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
formatted_raw_ostream & fdbgs()
fdbgs() - This returns a reference to a formatted_raw_ostream for debug output.
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
unsigned getNumBytesForUTF8(UTF8 firstByte)
Definition: ConvertUTF.cpp:545
formatted_raw_ostream & fouts()
fouts() - This returns a reference to a formatted_raw_ostream for standard output.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
formatted_raw_ostream & ferrs()
ferrs() - This returns a reference to a formatted_raw_ostream for standard error.