FormattedStream.h revision 7aaad71722aeb26f14c8523ee55df91e38837a02
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 /// ColumnFlushed - The current output column of the data that's 53 /// been flushed. The column scheme is zero-based. 54 /// 55 unsigned ColumnFlushed; 56 57 virtual void write_impl(const char *Ptr, size_t Size) { 58 ComputeColumn(ColumnFlushed); 59 TheStream->write(Ptr, Size); 60 } 61 62 /// current_pos - Return the current position within the stream, 63 /// not counting the bytes currently in the buffer. 64 virtual uint64_t current_pos() { 65 // This has the same effect as calling TheStream.current_pos(), 66 // but that interface is private. 67 return TheStream->tell() - TheStream->GetNumBytesInBuffer(); 68 } 69 70 /// ComputeColumn - Examine the current buffer and figure out 71 /// which column we're in. 72 /// 73 void ComputeColumn(unsigned &Column); 74 75 public: 76 /// formatted_raw_ostream - Open the specified file for 77 /// writing. If an error occurs, information about the error is 78 /// put into ErrorInfo, and the stream should be immediately 79 /// destroyed; the string will be empty if no error occurred. 80 /// 81 /// As a side effect, the given Stream is set to be Unbuffered. 82 /// This is because formatted_raw_ostream does its own buffering, 83 /// so it doesn't want another layer of buffering to be happening 84 /// underneath it. 85 /// 86 formatted_raw_ostream(raw_ostream &Stream, bool Delete = false) 87 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnFlushed(0) { 88 setStream(Stream, Delete); 89 } 90 explicit formatted_raw_ostream() 91 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnFlushed(0) {} 92 93 ~formatted_raw_ostream() { 94 if (DeleteStream) 95 delete TheStream; 96 } 97 98 void setStream(raw_ostream &Stream, bool Delete = false) { 99 if (DeleteStream) 100 delete TheStream; 101 102 TheStream = &Stream; 103 DeleteStream = Delete; 104 105 // This formatted_raw_ostream inherits from raw_ostream, so it'll do its 106 // own buffering, and it doesn't need or want TheStream to do another 107 // layer of buffering underneath. Resize the buffer to what TheStream 108 // had been using, and tell TheStream not to do its own buffering. 109 TheStream->flush(); 110 if (size_t BufferSize = TheStream->GetNumBytesInBuffer()) 111 SetBufferSize(BufferSize); 112 TheStream->SetUnbuffered(); 113 } 114 115 /// PadToColumn - Align the output to some column number. 116 /// 117 /// \param NewCol - The column to move to. 118 /// \param MinPad - The minimum space to give after the most 119 /// recent I/O, even if the current column + minpad > newcol. 120 /// 121 void PadToColumn(unsigned NewCol, unsigned MinPad = 0); 122 }; 123 124/// fouts() - This returns a reference to a formatted_raw_ostream for 125/// standard output. Use it like: fouts() << "foo" << "bar"; 126formatted_raw_ostream &fouts(); 127 128/// ferrs() - This returns a reference to a formatted_raw_ostream for 129/// standard error. Use it like: ferrs() << "foo" << "bar"; 130formatted_raw_ostream &ferrs(); 131 132} // end llvm namespace 133 134 135#endif 136