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 
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"
18 #include <algorithm>
19 
20 using 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.
UpdatePosition(const char * Ptr,size_t Size)26 void 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);
32     if (Width != sys::unicode::ErrorNonPrintableCharacter)
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.
ComputePosition(const char * Ptr,size_t Size)96 void 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 ///
PadToColumn(unsigned NewCol)118 formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) {
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 
write_impl(const char * Ptr,size_t Size)127 void 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";
fouts()141 formatted_raw_ostream &llvm::fouts() {
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";
ferrs()148 formatted_raw_ostream &llvm::ferrs() {
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";
fdbgs()155 formatted_raw_ostream &llvm::fdbgs() {
156   static formatted_raw_ostream S(dbgs());
157   return S;
158 }
159