1 //===-- llvm/CodeGen/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 /// formatted_raw_ostream - Formatted raw_fd_ostream to handle
23 /// asm-specific constructs.
25 class formatted_raw_ostream : public raw_ostream {
27 /// DELETE_STREAM - Tell the destructor to delete the held stream.
29 const static bool DELETE_STREAM = true;
31 /// PRESERVE_STREAM - Tell the destructor to not delete the held
34 const static bool PRESERVE_STREAM = false;
36 /// MAX_COLUMN_PAD - This is the maximum column padding we ever
39 const static unsigned MAX_COLUMN_PAD = 100;
42 /// TheStream - The real stream we output to. We set it to be
43 /// unbuffered, since we're already doing our own buffering.
45 raw_ostream *TheStream;
47 /// DeleteStream - Do we need to delete TheStream in the
52 /// ColumnScanned - The current output column of the data that's
53 /// been flushed and the portion of the buffer that's been
54 /// scanned. The column scheme is zero-based.
56 unsigned ColumnScanned;
58 /// Scanned - This points to one past the last character in the
59 /// buffer we've scanned.
63 virtual void write_impl(const char *Ptr, size_t Size) {
65 TheStream->write(Ptr, Size);
69 /// current_pos - Return the current position within the stream,
70 /// not counting the bytes currently in the buffer.
71 virtual uint64_t current_pos() {
72 // This has the same effect as calling TheStream.current_pos(),
73 // but that interface is private.
74 return TheStream->tell() - TheStream->GetNumBytesInBuffer();
77 /// ComputeColumn - Examine the current output and figure out
78 /// which column we end up in after output.
83 /// formatted_raw_ostream - Open the specified file for
84 /// writing. If an error occurs, information about the error is
85 /// put into ErrorInfo, and the stream should be immediately
86 /// destroyed; the string will be empty if no error occurred.
88 /// As a side effect, the given Stream is set to be Unbuffered.
89 /// This is because formatted_raw_ostream does its own buffering,
90 /// so it doesn't want another layer of buffering to be happening
93 formatted_raw_ostream(raw_ostream &Stream, bool Delete = false)
94 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
95 setStream(Stream, Delete);
97 explicit formatted_raw_ostream()
98 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
102 ~formatted_raw_ostream() {
108 void setStream(raw_ostream &Stream, bool Delete = false) {
113 DeleteStream = Delete;
115 // This formatted_raw_ostream inherits from raw_ostream, so it'll do its
116 // own buffering, and it doesn't need or want TheStream to do another
117 // layer of buffering underneath. Resize the buffer to what TheStream
118 // had been using, and tell TheStream not to do its own buffering.
120 if (size_t BufferSize = TheStream->GetNumBytesInBuffer())
121 SetBufferSize(BufferSize);
122 TheStream->SetUnbuffered();
127 /// PadToColumn - Align the output to some column number.
129 /// \param NewCol - The column to move to.
130 /// \param MinPad - The minimum space to give after the most
131 /// recent I/O, even if the current column + minpad > newcol.
133 void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
136 /// fouts() - This returns a reference to a formatted_raw_ostream for
137 /// standard output. Use it like: fouts() << "foo" << "bar";
138 formatted_raw_ostream &fouts();
140 /// ferrs() - This returns a reference to a formatted_raw_ostream for
141 /// standard error. Use it like: ferrs() << "foo" << "bar";
142 formatted_raw_ostream &ferrs();
144 } // end llvm namespace