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;
30 /// PRESERVE_STREAM - Tell the destructor to not delete the held
33 const static bool PRESERVE_STREAM = false;
36 /// TheStream - The real stream we output to.
38 raw_ostream *TheStream;
39 /// DeleteStream - Do we need to delete TheStream in the
44 /// Column - The current output column of the stream. The column
45 /// scheme is zero-based.
49 virtual void write_impl(const char *Ptr, unsigned Size) {
50 ComputeColumn(Ptr, Size);
51 TheStream->write(Ptr, Size);
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();
62 /// ComputeColumn - Examine the current output and figure out
63 /// which column we end up in after output.
65 void ComputeColumn(const char *Ptr, unsigned Size);
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.
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) {}
82 ~formatted_raw_ostream() {
87 void setStream(raw_ostream &Stream, bool Delete = false) {
89 DeleteStream = Delete;
92 /// PadToColumn - Align the output to some column number.
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.
98 void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
101 /// fouts() - This returns a reference to a formatted_raw_ostream for
102 /// standard output. Use it like: fouts() << "foo" << "bar";
103 formatted_raw_ostream &fouts();
105 /// ferrs() - This returns a reference to a formatted_raw_ostream for
106 /// standard error. Use it like: ferrs() << "foo" << "bar";
107 formatted_raw_ostream &ferrs();
109 } // end llvm namespace