FormattedStream.h revision 001c2b433fd7fc30904abb4eafc78ac996430fe6
1e3ca33aba226c7b5d50703df008ef0ef5f9ea7daHans Wennborg//===-- llvm/CodeGen/FormattedStream.h - Formatted streams ------*- C++ -*-===//
2e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff//
3e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff//                     The LLVM Compiler Infrastructure
4e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff//
5e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff// This file is distributed under the University of Illinois Open Source
6122de3e131a6902d22c97471520ec9005cca6f03Hans Wennborg// License. See LICENSE.TXT for details.
7122de3e131a6902d22c97471520ec9005cca6f03Hans Wennborg//
8e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff//===----------------------------------------------------------------------===//
9e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff//
10e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff// This file contains raw_ostream implementations for streams to do
11e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff// things like pretty-print comments.
12e030358cc06e1cbce3c2e00ca67c946f9164b2a8Chris Lattner//
13e3ca33aba226c7b5d50703df008ef0ef5f9ea7daHans Wennborg//===----------------------------------------------------------------------===//
14e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff
15122de3e131a6902d22c97471520ec9005cca6f03Hans Wennborg#ifndef LLVM_SUPPORT_FORMATTEDSTREAM_H
16e3ca33aba226c7b5d50703df008ef0ef5f9ea7daHans Wennborg#define LLVM_SUPPORT_FORMATTEDSTREAM_H
17122de3e131a6902d22c97471520ec9005cca6f03Hans Wennborg
18122de3e131a6902d22c97471520ec9005cca6f03Hans Wennborg#include "llvm/Support/raw_ostream.h"
1943e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain
20e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroffnamespace llvm
21e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff{
22e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff  /// formatted_raw_ostream - Formatted raw_fd_ostream to handle
23e2ef815de1da36c1ad1494cb58ce37adac1efa26Steve Naroff  /// asm-specific constructs.
2443e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain  ///
2543e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain  class formatted_raw_ostream : public raw_ostream {
2643e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain  public:
2743e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain    /// DELETE_STREAM - Tell the destructor to delete the held stream.
2843e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain    ///
2943e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain    const static bool DELETE_STREAM = true;
3043e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain    /// PRESERVE_STREAM - Tell the destructor to not delete the held
3143e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain    /// stream.
3243e875d2610afcf9e7017b71f46116dc86624fd9Kaelyn Uhrain    ///
33    const static bool PRESERVE_STREAM = false;
34
35  private:
36    /// TheStream - The real stream we output to.
37    ///
38    raw_ostream *TheStream;
39    /// DeleteStream - Do we need to delete TheStream in the
40    /// destructor?
41    ///
42    bool DeleteStream;
43
44    /// Column - The current output column of the stream.  The column
45    /// scheme is zero-based.
46    ///
47    unsigned Column;
48
49    virtual void write_impl(const char *Ptr, unsigned Size) {
50      ComputeColumn(Ptr, Size);
51      TheStream->write(Ptr, Size);
52    }
53
54    /// current_pos - Return the current position within the stream,
55    /// not counting the bytes currently in the buffer.
56    virtual uint64_t current_pos() {
57      // This has the same effect as calling TheStream.current_pos(),
58      // but that interface is private.
59      return TheStream->tell() - TheStream->GetNumBytesInBuffer();
60    }
61
62    /// ComputeColumn - Examine the current output and figure out
63    /// which column we end up in after output.
64    ///
65    void ComputeColumn(const char *Ptr, unsigned Size);
66
67  public:
68    /// formatted_raw_ostream - Open the specified file for
69    /// writing. If an error occurs, information about the error is
70    /// put into ErrorInfo, and the stream should be immediately
71    /// destroyed; the string will be empty if no error occurred.
72    ///
73    /// \param Filename - The file to open. If this is "-" then the
74    /// stream will use stdout instead.
75    /// \param Binary - The file should be opened in binary mode on
76    /// platforms that support this distinction.
77    formatted_raw_ostream(raw_ostream &Stream, bool Delete = false)
78        : raw_ostream(), TheStream(&Stream), DeleteStream(Delete), Column(0) {}
79    explicit formatted_raw_ostream()
80      : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {}
81
82    ~formatted_raw_ostream() {
83      if (DeleteStream)
84        delete TheStream;
85    }
86
87    void setStream(raw_ostream &Stream, bool Delete = false) {
88      TheStream = &Stream;
89      DeleteStream = Delete;
90    }
91
92    /// PadToColumn - Align the output to some column number.
93    ///
94    /// \param NewCol - The column to move to.
95    /// \param MinPad - The minimum space to give after the most
96    /// recent I/O, even if the current column + minpad > newcol.
97    ///
98    void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
99  };
100
101/// fouts() - This returns a reference to a formatted_raw_ostream for
102/// standard output.  Use it like: fouts() << "foo" << "bar";
103formatted_raw_ostream &fouts();
104
105/// ferrs() - This returns a reference to a formatted_raw_ostream for
106/// standard error.  Use it like: ferrs() << "foo" << "bar";
107formatted_raw_ostream &ferrs();
108
109} // end llvm namespace
110
111
112#endif
113