FormattedStream.h revision a4a68c1b439af1bacf8b9c3c06cdb97f56be4d94
1//===-- llvm/CodeGen/FormattedStream.h - Formatted streams ------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains raw_ostream implementations for streams to do
11// things like pretty-print comments.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_SUPPORT_FORMATTEDSTREAM_H
16#define LLVM_SUPPORT_FORMATTEDSTREAM_H
17
18#include "llvm/Support/raw_ostream.h"
19
20namespace llvm
21{
22  /// formatted_raw_ostream - Formatted raw_fd_ostream to handle
23  /// asm-specific constructs.
24  ///
25  class formatted_raw_ostream : public raw_ostream {
26  public:
27    /// DELETE_STREAM - Tell the destructor to delete the held stream.
28    ///
29    const static bool DELETE_STREAM = true;
30
31    /// PRESERVE_STREAM - Tell the destructor to not delete the held
32    /// stream.
33    ///
34    const static bool PRESERVE_STREAM = false;
35
36    /// MAX_COLUMN_PAD - This is the maximum column padding we ever
37    /// expect to see.
38    ///
39    const static unsigned MAX_COLUMN_PAD = 100;
40
41  private:
42    /// TheStream - The real stream we output to. We set it to be
43    /// unbuffered, since we're already doing our own buffering.
44    ///
45    raw_ostream *TheStream;
46
47    /// DeleteStream - Do we need to delete TheStream in the
48    /// destructor?
49    ///
50    bool DeleteStream;
51
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.
55    ///
56    unsigned ColumnScanned;
57
58    /// Scanned - This points to one past the last character in the
59    /// buffer we've scanned.
60    ///
61    iterator Scanned;
62
63    virtual void write_impl(const char *Ptr, size_t Size);
64
65    /// current_pos - Return the current position within the stream,
66    /// not counting the bytes currently in the buffer.
67    virtual uint64_t current_pos() {
68      // This has the same effect as calling TheStream.current_pos(),
69      // but that interface is private.
70      return TheStream->tell() - TheStream->GetNumBytesInBuffer();
71    }
72
73    /// ComputeColumn - Examine the current output and figure out
74    /// which column we end up in after output.
75    ///
76    void ComputeColumn();
77
78  public:
79    /// formatted_raw_ostream - Open the specified file for
80    /// writing. If an error occurs, information about the error is
81    /// put into ErrorInfo, and the stream should be immediately
82    /// destroyed; the string will be empty if no error occurred.
83    ///
84    /// As a side effect, the given Stream is set to be Unbuffered.
85    /// This is because formatted_raw_ostream does its own buffering,
86    /// so it doesn't want another layer of buffering to be happening
87    /// underneath it.
88    ///
89    formatted_raw_ostream(raw_ostream &Stream, bool Delete = false)
90      : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
91      setStream(Stream, Delete);
92    }
93    explicit formatted_raw_ostream()
94      : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
95      Scanned = begin();
96    }
97
98    ~formatted_raw_ostream() {
99      flush();
100      releaseStream();
101    }
102
103    void setStream(raw_ostream &Stream, bool Delete = false) {
104      releaseStream();
105
106      TheStream = &Stream;
107      DeleteStream = Delete;
108
109      // This formatted_raw_ostream inherits from raw_ostream, so it'll do its
110      // own buffering, and it doesn't need or want TheStream to do another
111      // layer of buffering underneath. Resize the buffer to what TheStream
112      // had been using, and tell TheStream not to do its own buffering.
113      if (size_t BufferSize = TheStream->GetBufferSize())
114        SetBufferSize(BufferSize);
115      else
116        SetUnbuffered();
117      TheStream->SetUnbuffered();
118
119      Scanned = begin();
120    }
121
122    /// PadToColumn - Align the output to some column number.
123    ///
124    /// \param NewCol - The column to move to.
125    /// \param MinPad - The minimum space to give after the most
126    /// recent I/O, even if the current column + minpad > newcol.
127    ///
128    void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
129
130  private:
131    void releaseStream() {
132      // Delete the stream if needed. Otherwise, transfer the buffer
133      // settings from this raw_ostream back to the underlying stream.
134      if (!TheStream)
135        return;
136      if (DeleteStream)
137        delete TheStream;
138      else if (size_t BufferSize = GetBufferSize())
139        TheStream->SetBufferSize(BufferSize);
140      else
141        TheStream->SetUnbuffered();
142    }
143  };
144
145/// fouts() - This returns a reference to a formatted_raw_ostream for
146/// standard output.  Use it like: fouts() << "foo" << "bar";
147formatted_raw_ostream &fouts();
148
149/// ferrs() - This returns a reference to a formatted_raw_ostream for
150/// standard error.  Use it like: ferrs() << "foo" << "bar";
151formatted_raw_ostream &ferrs();
152
153} // end llvm namespace
154
155
156#endif
157