LLVM  9.0.0svn
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 
18 #include <utility>
19 
20 namespace llvm {
21 
22 /// formatted_raw_ostream - A raw_ostream that wraps another one and keeps track
23 /// of line and column position, allowing padding out to specific column
24 /// boundaries and querying the number of lines written to the stream.
25 ///
27  /// TheStream - The real stream we output to. We set it to be
28  /// unbuffered, since we're already doing our own buffering.
29  ///
30  raw_ostream *TheStream;
31 
32  /// Position - The current output column and line of the data that's
33  /// been flushed and the portion of the buffer that's been
34  /// scanned. The line and column scheme is zero-based.
35  ///
36  std::pair<unsigned, unsigned> Position;
37 
38  /// Scanned - This points to one past the last character in the
39  /// buffer we've scanned.
40  ///
41  const char *Scanned;
42 
43  void write_impl(const char *Ptr, size_t Size) override;
44 
45  /// current_pos - Return the current position within the stream,
46  /// not counting the bytes currently in the buffer.
47  uint64_t current_pos() const override {
48  // Our current position in the stream is all the contents which have been
49  // written to the underlying stream (*not* the current position of the
50  // underlying stream).
51  return TheStream->tell();
52  }
53 
54  /// ComputePosition - Examine the given output buffer and figure out the new
55  /// position after output.
56  ///
57  void ComputePosition(const char *Ptr, size_t size);
58 
59  void setStream(raw_ostream &Stream) {
60  releaseStream();
61 
62  TheStream = &Stream;
63 
64  // This formatted_raw_ostream inherits from raw_ostream, so it'll do its
65  // own buffering, and it doesn't need or want TheStream to do another
66  // layer of buffering underneath. Resize the buffer to what TheStream
67  // had been using, and tell TheStream not to do its own buffering.
68  if (size_t BufferSize = TheStream->GetBufferSize())
69  SetBufferSize(BufferSize);
70  else
71  SetUnbuffered();
72  TheStream->SetUnbuffered();
73 
74  Scanned = nullptr;
75  }
76 
77 public:
78  /// formatted_raw_ostream - Open the specified file for
79  /// writing. If an error occurs, information about the error is
80  /// put into ErrorInfo, and the stream should be immediately
81  /// destroyed; the string will be empty if no error occurred.
82  ///
83  /// As a side effect, the given Stream is set to be Unbuffered.
84  /// This is because formatted_raw_ostream does its own buffering,
85  /// so it doesn't want another layer of buffering to be happening
86  /// underneath it.
87  ///
89  : TheStream(nullptr), Position(0, 0) {
90  setStream(Stream);
91  }
92  explicit formatted_raw_ostream() : TheStream(nullptr), Position(0, 0) {
93  Scanned = nullptr;
94  }
95 
97  flush();
98  releaseStream();
99  }
100 
101  /// PadToColumn - Align the output to some column number. If the current
102  /// column is already equal to or more than NewCol, PadToColumn inserts one
103  /// space.
104  ///
105  /// \param NewCol - The column to move to.
106  formatted_raw_ostream &PadToColumn(unsigned NewCol);
107 
108  /// getColumn - Return the column number
109  unsigned getColumn() { return Position.first; }
110 
111  /// getLine - Return the line number
112  unsigned getLine() { return Position.second; }
113 
114  raw_ostream &resetColor() override {
115  TheStream->resetColor();
116  return *this;
117  }
118 
119  raw_ostream &reverseColor() override {
120  TheStream->reverseColor();
121  return *this;
122  }
123 
124  raw_ostream &changeColor(enum Colors Color, bool Bold, bool BG) override {
125  TheStream->changeColor(Color, Bold, BG);
126  return *this;
127  }
128 
129  bool is_displayed() const override {
130  return TheStream->is_displayed();
131  }
132 
133 private:
134  void releaseStream() {
135  // Transfer the buffer settings from this raw_ostream back to the underlying
136  // stream.
137  if (!TheStream)
138  return;
139  if (size_t BufferSize = GetBufferSize())
140  TheStream->SetBufferSize(BufferSize);
141  else
142  TheStream->SetUnbuffered();
143  }
144 };
145 
146 /// fouts() - This returns a reference to a formatted_raw_ostream for
147 /// standard output. Use it like: fouts() << "foo" << "bar";
149 
150 /// ferrs() - This returns a reference to a formatted_raw_ostream for
151 /// standard error. Use it like: ferrs() << "foo" << "bar";
153 
154 /// fdbgs() - This returns a reference to a formatted_raw_ostream for
155 /// debug output. Use it like: fdbgs() << "foo" << "bar";
157 
158 } // end llvm namespace
159 
160 
161 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
formatted_raw_ostream(raw_ostream &Stream)
formatted_raw_ostream - Open the specified file for writing.
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
size_t GetBufferSize() const
Definition: raw_ostream.h:115
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.h:256
bool is_displayed() const override
This function determines if this stream is connected to a "tty" or "console" window.
raw_ostream & resetColor() override
Resets the colors to terminal defaults.
Position
Position to insert a new instruction relative to an existing instruction.
formatted_raw_ostream & PadToColumn(unsigned NewCol)
PadToColumn - Align the output to some column number.
unsigned getLine()
getLine - Return the line number
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. ...
unsigned getColumn()
getColumn - Return the column number
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:128
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.h:270
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:110
Color
A "color", which is either even or odd.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1173
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:275
formatted_raw_ostream & fdbgs()
fdbgs() - This returns a reference to a formatted_raw_ostream for debug output.
uint32_t Size
Definition: Profile.cpp:46
formatted_raw_ostream & ferrs()
ferrs() - This returns a reference to a formatted_raw_ostream for standard error. ...
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.h:267
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:99
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
formatted_raw_ostream & fouts()
fouts() - This returns a reference to a formatted_raw_ostream for standard output.
raw_ostream & reverseColor() override
Reverses the foreground and background colors.