LLVM API Documentation
00001 //===-- llvm/Support/FormattedStream.cpp - 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 the implementation of formatted_raw_ostream. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Support/Debug.h" 00015 #include "llvm/Support/FormattedStream.h" 00016 #include <algorithm> 00017 00018 using namespace llvm; 00019 00020 /// UpdatePosition - Examine the given char sequence and figure out which 00021 /// column we end up in after output, and how many line breaks are contained. 00022 /// 00023 static void UpdatePosition(std::pair<unsigned, unsigned> &Position, const char *Ptr, size_t Size) { 00024 unsigned &Column = Position.first; 00025 unsigned &Line = Position.second; 00026 00027 // Keep track of the current column and line by scanning the string for 00028 // special characters 00029 for (const char *End = Ptr + Size; Ptr != End; ++Ptr) { 00030 ++Column; 00031 switch (*Ptr) { 00032 case '\n': 00033 Line += 1; 00034 case '\r': 00035 Column = 0; 00036 break; 00037 case '\t': 00038 // Assumes tab stop = 8 characters. 00039 Column += (8 - (Column & 0x7)) & 0x7; 00040 break; 00041 } 00042 } 00043 } 00044 00045 /// ComputePosition - Examine the current output and update line and column 00046 /// counts. 00047 void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) { 00048 // If our previous scan pointer is inside the buffer, assume we already 00049 // scanned those bytes. This depends on raw_ostream to not change our buffer 00050 // in unexpected ways. 00051 if (Ptr <= Scanned && Scanned <= Ptr + Size) 00052 // Scan all characters added since our last scan to determine the new 00053 // column. 00054 UpdatePosition(Position, Scanned, Size - (Scanned - Ptr)); 00055 else 00056 UpdatePosition(Position, Ptr, Size); 00057 00058 // Update the scanning pointer. 00059 Scanned = Ptr + Size; 00060 } 00061 00062 /// PadToColumn - Align the output to some column number. 00063 /// 00064 /// \param NewCol - The column to move to. 00065 /// 00066 formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) { 00067 // Figure out what's in the buffer and add it to the column count. 00068 ComputePosition(getBufferStart(), GetNumBytesInBuffer()); 00069 00070 // Output spaces until we reach the desired column. 00071 indent(std::max(int(NewCol - getColumn()), 1)); 00072 return *this; 00073 } 00074 00075 void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) { 00076 // Figure out what's in the buffer and add it to the column count. 00077 ComputePosition(Ptr, Size); 00078 00079 // Write the data to the underlying stream (which is unbuffered, so 00080 // the data will be immediately written out). 00081 TheStream->write(Ptr, Size); 00082 00083 // Reset the scanning pointer. 00084 Scanned = nullptr; 00085 } 00086 00087 /// fouts() - This returns a reference to a formatted_raw_ostream for 00088 /// standard output. Use it like: fouts() << "foo" << "bar"; 00089 formatted_raw_ostream &llvm::fouts() { 00090 static formatted_raw_ostream S(outs()); 00091 return S; 00092 } 00093 00094 /// ferrs() - This returns a reference to a formatted_raw_ostream for 00095 /// standard error. Use it like: ferrs() << "foo" << "bar"; 00096 formatted_raw_ostream &llvm::ferrs() { 00097 static formatted_raw_ostream S(errs()); 00098 return S; 00099 } 00100 00101 /// fdbgs() - This returns a reference to a formatted_raw_ostream for 00102 /// the debug stream. Use it like: fdbgs() << "foo" << "bar"; 00103 formatted_raw_ostream &llvm::fdbgs() { 00104 static formatted_raw_ostream S(dbgs()); 00105 return S; 00106 }