LLVM API Documentation
00001 //===-- llvm/CodeGen/FormattedStream.h - Formatted streams ------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains raw_ostream implementations for streams to do 00011 // things like pretty-print comments. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_SUPPORT_FORMATTEDSTREAM_H 00016 #define LLVM_SUPPORT_FORMATTEDSTREAM_H 00017 00018 #include "llvm/Support/raw_ostream.h" 00019 00020 namespace llvm 00021 { 00022 /// formatted_raw_ostream - Formatted raw_fd_ostream to handle 00023 /// asm-specific constructs. 00024 /// 00025 class formatted_raw_ostream : public raw_ostream { 00026 public: 00027 /// DELETE_STREAM - Tell the destructor to delete the held stream. 00028 /// 00029 const static bool DELETE_STREAM = true; 00030 00031 /// PRESERVE_STREAM - Tell the destructor to not delete the held 00032 /// stream. 00033 /// 00034 const static bool PRESERVE_STREAM = false; 00035 00036 /// MAX_COLUMN_PAD - This is the maximum column padding we ever 00037 /// expect to see. 00038 /// 00039 const static unsigned MAX_COLUMN_PAD = 100; 00040 00041 private: 00042 /// TheStream - The real stream we output to. We set it to be 00043 /// unbuffered, since we're already doing our own buffering. 00044 /// 00045 raw_ostream *TheStream; 00046 00047 /// DeleteStream - Do we need to delete TheStream in the 00048 /// destructor? 00049 /// 00050 bool DeleteStream; 00051 00052 /// ColumnScanned - The current output column of the data that's 00053 /// been flushed and the portion of the buffer that's been 00054 /// scanned. The column scheme is zero-based. 00055 /// 00056 unsigned ColumnScanned; 00057 00058 /// Scanned - This points to one past the last character in the 00059 /// buffer we've scanned. 00060 /// 00061 const char *Scanned; 00062 00063 virtual void write_impl(const char *Ptr, size_t Size); 00064 00065 /// current_pos - Return the current position within the stream, 00066 /// not counting the bytes currently in the buffer. 00067 virtual uint64_t current_pos() { 00068 // This has the same effect as calling TheStream.current_pos(), 00069 // but that interface is private. 00070 return TheStream->tell() - TheStream->GetNumBytesInBuffer(); 00071 } 00072 00073 /// ComputeColumn - Examine the given output buffer and figure out which 00074 /// column we end up in after output. 00075 /// 00076 void ComputeColumn(const char *Ptr, size_t size); 00077 00078 public: 00079 /// formatted_raw_ostream - Open the specified file for 00080 /// writing. If an error occurs, information about the error is 00081 /// put into ErrorInfo, and the stream should be immediately 00082 /// destroyed; the string will be empty if no error occurred. 00083 /// 00084 /// As a side effect, the given Stream is set to be Unbuffered. 00085 /// This is because formatted_raw_ostream does its own buffering, 00086 /// so it doesn't want another layer of buffering to be happening 00087 /// underneath it. 00088 /// 00089 formatted_raw_ostream(raw_ostream &Stream, bool Delete = false) 00090 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) { 00091 setStream(Stream, Delete); 00092 } 00093 explicit formatted_raw_ostream() 00094 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) { 00095 Scanned = 0; 00096 } 00097 00098 ~formatted_raw_ostream() { 00099 flush(); 00100 releaseStream(); 00101 } 00102 00103 void setStream(raw_ostream &Stream, bool Delete = false) { 00104 releaseStream(); 00105 00106 TheStream = &Stream; 00107 DeleteStream = Delete; 00108 00109 // This formatted_raw_ostream inherits from raw_ostream, so it'll do its 00110 // own buffering, and it doesn't need or want TheStream to do another 00111 // layer of buffering underneath. Resize the buffer to what TheStream 00112 // had been using, and tell TheStream not to do its own buffering. 00113 if (size_t BufferSize = TheStream->GetBufferSize()) 00114 SetBufferSize(BufferSize); 00115 else 00116 SetUnbuffered(); 00117 TheStream->SetUnbuffered(); 00118 00119 Scanned = 0; 00120 } 00121 00122 /// PadToColumn - Align the output to some column number. If the current 00123 /// column is already equal to or more than NewCol, PadToColumn inserts one 00124 /// space. 00125 /// 00126 /// \param NewCol - The column to move to. 00127 void PadToColumn(unsigned NewCol); 00128 00129 private: 00130 void releaseStream() { 00131 // Delete the stream if needed. Otherwise, transfer the buffer 00132 // settings from this raw_ostream back to the underlying stream. 00133 if (!TheStream) 00134 return; 00135 if (DeleteStream) 00136 delete TheStream; 00137 else if (size_t BufferSize = GetBufferSize()) 00138 TheStream->SetBufferSize(BufferSize); 00139 else 00140 TheStream->SetUnbuffered(); 00141 } 00142 }; 00143 00144 /// fouts() - This returns a reference to a formatted_raw_ostream for 00145 /// standard output. Use it like: fouts() << "foo" << "bar"; 00146 formatted_raw_ostream &fouts(); 00147 00148 /// ferrs() - This returns a reference to a formatted_raw_ostream for 00149 /// standard error. Use it like: ferrs() << "foo" << "bar"; 00150 formatted_raw_ostream &ferrs(); 00151 00152 } // end llvm namespace 00153 00154 00155 #endif