1 //===-- llvm/Support/FormattedStream.h - Formatted streams ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains raw_ostream implementations for streams to do
11 // things like pretty-print comments.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_FORMATTEDSTREAM_H
16 #define LLVM_SUPPORT_FORMATTEDSTREAM_H
18 #include "llvm/Support/raw_ostream.h"
22 class formatted_tool_output_file;
24 /// formatted_raw_ostream - Formatted raw_fd_ostream to handle
25 /// asm-specific constructs.
27 class formatted_raw_ostream : public raw_ostream {
28 friend class formatted_tool_output_file;
31 /// DELETE_STREAM - Tell the destructor to delete the held stream.
33 static const bool DELETE_STREAM = true;
35 /// PRESERVE_STREAM - Tell the destructor to not delete the held
38 static const bool PRESERVE_STREAM = false;
41 /// TheStream - The real stream we output to. We set it to be
42 /// unbuffered, since we're already doing our own buffering.
44 raw_ostream *TheStream;
46 /// DeleteStream - Do we need to delete TheStream in the
51 /// ColumnScanned - The current output column of the data that's
52 /// been flushed and the portion of the buffer that's been
53 /// scanned. The column scheme is zero-based.
55 unsigned ColumnScanned;
57 /// Scanned - This points to one past the last character in the
58 /// buffer we've scanned.
62 virtual void write_impl(const char *Ptr, size_t Size);
64 /// current_pos - Return the current position within the stream,
65 /// not counting the bytes currently in the buffer.
66 virtual uint64_t current_pos() const {
67 // This has the same effect as calling TheStream.current_pos(),
68 // but that interface is private.
69 return TheStream->tell() - TheStream->GetNumBytesInBuffer();
72 /// ComputeColumn - Examine the given output buffer and figure out which
73 /// column we end up in after output.
75 void ComputeColumn(const char *Ptr, size_t size);
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.
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
88 formatted_raw_ostream(raw_ostream &Stream, bool Delete = false)
89 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
90 setStream(Stream, Delete);
92 explicit formatted_raw_ostream()
93 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
97 ~formatted_raw_ostream() {
102 void setStream(raw_ostream &Stream, bool Delete = false) {
106 DeleteStream = Delete;
108 // This formatted_raw_ostream inherits from raw_ostream, so it'll do its
109 // own buffering, and it doesn't need or want TheStream to do another
110 // layer of buffering underneath. Resize the buffer to what TheStream
111 // had been using, and tell TheStream not to do its own buffering.
112 if (size_t BufferSize = TheStream->GetBufferSize())
113 SetBufferSize(BufferSize);
116 TheStream->SetUnbuffered();
121 /// PadToColumn - Align the output to some column number. If the current
122 /// column is already equal to or more than NewCol, PadToColumn inserts one
125 /// \param NewCol - The column to move to.
126 formatted_raw_ostream &PadToColumn(unsigned NewCol);
129 void releaseStream() {
130 // Delete the stream if needed. Otherwise, transfer the buffer
131 // settings from this raw_ostream back to the underlying stream.
136 else if (size_t BufferSize = GetBufferSize())
137 TheStream->SetBufferSize(BufferSize);
139 TheStream->SetUnbuffered();
143 /// formatted_tool_output_file - This is a subclass of formatted_raw_ostream
144 /// for use when the underlying stream is a tool_output_file. It exposes
145 /// keep() and several other member functions.
146 class formatted_tool_output_file : public formatted_raw_ostream {
148 tool_output_file &get_tool_output_file() const {
149 return *static_cast<tool_output_file *>(TheStream);
153 formatted_tool_output_file(tool_output_file &Stream, bool Delete = false)
154 : formatted_raw_ostream(Stream, Delete) {}
156 formatted_tool_output_file() {}
158 ~formatted_tool_output_file();
160 void setStream(tool_output_file &Stream, bool Delete = false) {
161 return formatted_raw_ostream::setStream(Stream, Delete);
164 void keep() { return get_tool_output_file().keep(); }
165 bool has_error() const { return get_tool_output_file().has_error(); }
166 void clear_error() { return get_tool_output_file().clear_error(); }
168 // The inner stream is unbuffered; flush the outer stream's buffer.
170 // The inner stream can close its file descriptor now.
171 return get_tool_output_file().close();
175 /// fouts() - This returns a reference to a formatted_raw_ostream for
176 /// standard output. Use it like: fouts() << "foo" << "bar";
177 formatted_raw_ostream &fouts();
179 /// ferrs() - This returns a reference to a formatted_raw_ostream for
180 /// standard error. Use it like: ferrs() << "foo" << "bar";
181 formatted_raw_ostream &ferrs();
183 /// fdbgs() - This returns a reference to a formatted_raw_ostream for
184 /// debug output. Use it like: fdbgs() << "foo" << "bar";
185 formatted_raw_ostream &fdbgs();
187 } // end llvm namespace