LLVM  14.0.0git
FormattedStream.h
Go to the documentation of this file.
1 //===-- llvm/Support/FormattedStream.h - 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 raw_ostream implementations for streams to do
10 // things like pretty-print comments.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_FORMATTEDSTREAM_H
15 #define LLVM_SUPPORT_FORMATTEDSTREAM_H
16 
17 #include "llvm/ADT/SmallString.h"
19 #include <utility>
20 
21 namespace llvm {
22 
23 /// formatted_raw_ostream - A raw_ostream that wraps another one and keeps track
24 /// of line and column position, allowing padding out to specific column
25 /// boundaries and querying the number of lines written to the stream. This
26 /// assumes that the contents of the stream is valid UTF-8 encoded text. This
27 /// doesn't attempt to handle everything Unicode can do (combining characters,
28 /// right-to-left markers, etc), but should cover the cases likely to appear in
29 /// source code or diagnostic messages.
31  /// TheStream - The real stream we output to. We set it to be
32  /// unbuffered, since we're already doing our own buffering.
33  ///
34  raw_ostream *TheStream;
35 
36  /// Position - The current output column and line of the data that's
37  /// been flushed and the portion of the buffer that's been
38  /// scanned. The line and column scheme is zero-based.
39  ///
40  std::pair<unsigned, unsigned> Position;
41 
42  /// Scanned - This points to one past the last character in the
43  /// buffer we've scanned.
44  ///
45  const char *Scanned;
46 
47  /// PartialUTF8Char - Either empty or a prefix of a UTF-8 code unit sequence
48  /// for a Unicode scalar value which should be prepended to the buffer for the
49  /// next call to ComputePosition. This is needed when the buffer is flushed
50  /// when it ends part-way through the UTF-8 encoding of a Unicode scalar
51  /// value, so that we can compute the display width of the character once we
52  /// have the rest of it.
53  SmallString<4> PartialUTF8Char;
54 
55  void write_impl(const char *Ptr, size_t Size) override;
56 
57  /// current_pos - Return the current position within the stream,
58  /// not counting the bytes currently in the buffer.
59  uint64_t current_pos() const override {
60  // Our current position in the stream is all the contents which have been
61  // written to the underlying stream (*not* the current position of the
62  // underlying stream).
63  return TheStream->tell();
64  }
65 
66  /// ComputePosition - Examine the given output buffer and figure out the new
67  /// position after output. This is safe to call multiple times on the same
68  /// buffer, as it records the most recently scanned character and resumes from
69  /// there when the buffer has not been flushed.
70  void ComputePosition(const char *Ptr, size_t size);
71 
72  /// UpdatePosition - scan the characters in [Ptr, Ptr+Size), and update the
73  /// line and column numbers. Unlike ComputePosition, this must be called
74  /// exactly once on each region of the buffer.
75  void UpdatePosition(const char *Ptr, size_t Size);
76 
77  void setStream(raw_ostream &Stream) {
78  releaseStream();
79 
80  TheStream = &Stream;
81 
82  // This formatted_raw_ostream inherits from raw_ostream, so it'll do its
83  // own buffering, and it doesn't need or want TheStream to do another
84  // layer of buffering underneath. Resize the buffer to what TheStream
85  // had been using, and tell TheStream not to do its own buffering.
86  if (size_t BufferSize = TheStream->GetBufferSize())
87  SetBufferSize(BufferSize);
88  else
89  SetUnbuffered();
90  TheStream->SetUnbuffered();
91 
92  Scanned = nullptr;
93  }
94 
95 public:
96  /// formatted_raw_ostream - Open the specified file for
97  /// writing. If an error occurs, information about the error is
98  /// put into ErrorInfo, and the stream should be immediately
99  /// destroyed; the string will be empty if no error occurred.
100  ///
101  /// As a side effect, the given Stream is set to be Unbuffered.
102  /// This is because formatted_raw_ostream does its own buffering,
103  /// so it doesn't want another layer of buffering to be happening
104  /// underneath it.
105  ///
107  : TheStream(nullptr), Position(0, 0) {
108  setStream(Stream);
109  }
110  explicit formatted_raw_ostream() : TheStream(nullptr), Position(0, 0) {
111  Scanned = nullptr;
112  }
113 
115  flush();
116  releaseStream();
117  }
118 
119  /// PadToColumn - Align the output to some column number. If the current
120  /// column is already equal to or more than NewCol, PadToColumn inserts one
121  /// space.
122  ///
123  /// \param NewCol - The column to move to.
124  formatted_raw_ostream &PadToColumn(unsigned NewCol);
125 
126  unsigned getColumn() {
127  // Calculate current position, taking buffer contents into account.
128  ComputePosition(getBufferStart(), GetNumBytesInBuffer());
129  return Position.first;
130  }
131 
132  unsigned getLine() {
133  // Calculate current position, taking buffer contents into account.
134  ComputePosition(getBufferStart(), GetNumBytesInBuffer());
135  return Position.second;
136  }
137 
138  raw_ostream &resetColor() override {
139  TheStream->resetColor();
140  return *this;
141  }
142 
143  raw_ostream &reverseColor() override {
144  TheStream->reverseColor();
145  return *this;
146  }
147 
148  raw_ostream &changeColor(enum Colors Color, bool Bold, bool BG) override {
149  TheStream->changeColor(Color, Bold, BG);
150  return *this;
151  }
152 
153  bool is_displayed() const override {
154  return TheStream->is_displayed();
155  }
156 
157 private:
158  void releaseStream() {
159  // Transfer the buffer settings from this raw_ostream back to the underlying
160  // stream.
161  if (!TheStream)
162  return;
163  if (size_t BufferSize = GetBufferSize())
164  TheStream->SetBufferSize(BufferSize);
165  else
166  TheStream->SetUnbuffered();
167  }
168 };
169 
170 /// fouts() - This returns a reference to a formatted_raw_ostream for
171 /// standard output. Use it like: fouts() << "foo" << "bar";
172 formatted_raw_ostream &fouts();
173 
174 /// ferrs() - This returns a reference to a formatted_raw_ostream for
175 /// standard error. Use it like: ferrs() << "foo" << "bar";
176 formatted_raw_ostream &ferrs();
177 
178 /// fdbgs() - This returns a reference to a formatted_raw_ostream for
179 /// debug output. Use it like: fdbgs() << "foo" << "bar";
180 formatted_raw_ostream &fdbgs();
181 
182 } // end llvm namespace
183 
184 
185 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::raw_ostream::tell
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:135
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::raw_ostream::SetUnbuffered
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:173
llvm::raw_ostream::is_displayed
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:323
llvm::formatted_raw_ostream::formatted_raw_ostream
formatted_raw_ostream()
Definition: FormattedStream.h:110
llvm::formatted_raw_ostream::~formatted_raw_ostream
~formatted_raw_ostream() override
Definition: FormattedStream.h:114
llvm::formatted_raw_ostream::getColumn
unsigned getColumn()
Definition: FormattedStream.h:126
llvm::raw_ostream::changeColor
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward.
Definition: raw_ostream.cpp:522
llvm::formatted_raw_ostream::PadToColumn
formatted_raw_ostream & PadToColumn(unsigned NewCol)
PadToColumn - Align the output to some column number.
Definition: FormattedStream.cpp:115
SmallString.h
llvm::fdbgs
formatted_raw_ostream & fdbgs()
fdbgs() - This returns a reference to a formatted_raw_ostream for debug output.
Definition: FormattedStream.cpp:152
llvm::formatted_raw_ostream::getLine
unsigned getLine()
Definition: FormattedStream.h:132
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::raw_ostream::flush
void flush()
Definition: raw_ostream.h:186
llvm::raw_ostream::GetNumBytesInBuffer
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:178
llvm::ferrs
formatted_raw_ostream & ferrs()
ferrs() - This returns a reference to a formatted_raw_ostream for standard error.
Definition: FormattedStream.cpp:145
llvm::SmallString< 4 >
llvm::formatted_raw_ostream::formatted_raw_ostream
formatted_raw_ostream(raw_ostream &Stream)
formatted_raw_ostream - Open the specified file for writing.
Definition: FormattedStream.h:106
llvm::formatted_raw_ostream
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
Definition: FormattedStream.h:30
llvm::fouts
formatted_raw_ostream & fouts()
fouts() - This returns a reference to a formatted_raw_ostream for standard output.
Definition: FormattedStream.cpp:138
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1528
llvm::raw_ostream::reverseColor
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.cpp:544
llvm::raw_ostream::GetBufferSize
size_t GetBufferSize() const
Definition: raw_ostream.h:160
llvm::formatted_raw_ostream::resetColor
raw_ostream & resetColor() override
Resets the colors to terminal defaults.
Definition: FormattedStream.h:138
llvm::raw_ostream::SetBufferSize
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:155
llvm::raw_ostream::Colors
Colors
Definition: raw_ostream.h:97
llvm::raw_ostream::resetColor
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.cpp:535
llvm::formatted_raw_ostream::is_displayed
bool is_displayed() const override
This function determines if this stream is connected to a "tty" or "console" window.
Definition: FormattedStream.h:153
llvm::formatted_raw_ostream::changeColor
raw_ostream & changeColor(enum Colors Color, bool Bold, bool BG) override
Changes the foreground color of text that will be output from this point forward.
Definition: FormattedStream.h:148
llvm::raw_ostream::getBufferStart
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:374
raw_ostream.h
llvm::formatted_raw_ostream::reverseColor
raw_ostream & reverseColor() override
Reverses the foreground and background colors.
Definition: FormattedStream.h:143