blob: c72b5a1751b4097bf6c74a3411b3b01bf9cc3f6d [file] [log] [blame]
David Greene191cf282009-07-13 16:49:27 +00001//===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
David Greene62fe47a2009-07-10 21:14:44 +00002//
3// The LLVM Compiler Infrastructure
4//
David Greene191cf282009-07-13 16:49:27 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
David Greene62fe47a2009-07-10 21:14:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner1f318e02009-07-14 23:14:10 +000010// This file contains the implementation of formatted_raw_ostream.
David Greene62fe47a2009-07-10 21:14:44 +000011//
12//===----------------------------------------------------------------------===//
13
David Greene8f1929b2010-01-05 01:28:43 +000014#include "llvm/Support/Debug.h"
David Greene62fe47a2009-07-10 21:14:44 +000015#include "llvm/Support/FormattedStream.h"
David Greeneed0e2ad2009-07-23 23:21:10 +000016
David Greene191cf282009-07-13 16:49:27 +000017using namespace llvm;
David Greene62fe47a2009-07-10 21:14:44 +000018
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000019/// CountColumns - Examine the given char sequence and figure out which
David Greene191cf282009-07-13 16:49:27 +000020/// column we end up in after output.
21///
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000022static unsigned CountColumns(unsigned Column, const char *Ptr, size_t Size) {
David Greene191cf282009-07-13 16:49:27 +000023 // Keep track of the current column by scanning the string for
24 // special characters
David Greene62fe47a2009-07-10 21:14:44 +000025
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000026 for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
27 ++Column;
28 if (*Ptr == '\n' || *Ptr == '\r')
29 Column = 0;
30 else if (*Ptr == '\t')
31 // Assumes tab stop = 8 characters.
32 Column += (8 - (Column & 0x7)) & 0x7;
David Greeneeb857282009-07-29 16:08:27 +000033 }
34
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000035 return Column;
36}
37
38/// ComputeColumn - Examine the current output and figure out which
39/// column we end up in after output.
Daniel Dunbarab810272009-08-18 23:36:04 +000040void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) {
41 // If our previous scan pointer is inside the buffer, assume we already
42 // scanned those bytes. This depends on raw_ostream to not change our buffer
43 // in unexpected ways.
44 if (Ptr <= Scanned && Scanned <= Ptr + Size) {
45 // Scan all characters added since our last scan to determine the new
46 // column.
47 ColumnScanned = CountColumns(ColumnScanned, Scanned,
48 Size - (Scanned - Ptr));
49 } else
50 ColumnScanned = CountColumns(ColumnScanned, Ptr, Size);
51
52 // Update the scanning pointer.
53 Scanned = Ptr + Size;
David Greene62fe47a2009-07-10 21:14:44 +000054}
David Greene191cf282009-07-13 16:49:27 +000055
56/// PadToColumn - Align the output to some column number.
57///
58/// \param NewCol - The column to move to.
David Greene191cf282009-07-13 16:49:27 +000059///
Chris Lattnerefbdaa62010-02-15 02:17:50 +000060formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) {
David Greeneeb857282009-07-29 16:08:27 +000061 // Figure out what's in the buffer and add it to the column count.
Daniel Dunbarab810272009-08-18 23:36:04 +000062 ComputeColumn(getBufferStart(), GetNumBytesInBuffer());
David Greene191cf282009-07-13 16:49:27 +000063
64 // Output spaces until we reach the desired column.
Chris Lattnerde51ded2009-08-22 23:16:09 +000065 indent(std::max(int(NewCol - ColumnScanned), 1));
Chris Lattnerefbdaa62010-02-15 02:17:50 +000066 return *this;
David Greene191cf282009-07-13 16:49:27 +000067}
68
Dan Gohmana4a68c12009-08-15 02:01:04 +000069void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000070 // Figure out what's in the buffer and add it to the column count.
Daniel Dunbarab810272009-08-18 23:36:04 +000071 ComputeColumn(Ptr, Size);
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000072
73 // Write the data to the underlying stream (which is unbuffered, so
74 // the data will be immediately written out).
Dan Gohmana4a68c12009-08-15 02:01:04 +000075 TheStream->write(Ptr, Size);
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000076
Daniel Dunbarab810272009-08-18 23:36:04 +000077 // Reset the scanning pointer.
78 Scanned = 0;
Dan Gohmana4a68c12009-08-15 02:01:04 +000079}
80
David Greene71847812009-07-14 20:18:05 +000081/// fouts() - This returns a reference to a formatted_raw_ostream for
82/// standard output. Use it like: fouts() << "foo" << "bar";
83formatted_raw_ostream &llvm::fouts() {
84 static formatted_raw_ostream S(outs());
85 return S;
86}
87
88/// ferrs() - This returns a reference to a formatted_raw_ostream for
89/// standard error. Use it like: ferrs() << "foo" << "bar";
90formatted_raw_ostream &llvm::ferrs() {
91 static formatted_raw_ostream S(errs());
92 return S;
93}
David Greene8f1929b2010-01-05 01:28:43 +000094
95/// fdbgs() - This returns a reference to a formatted_raw_ostream for
96/// the debug stream. Use it like: fdbgs() << "foo" << "bar";
97formatted_raw_ostream &llvm::fdbgs() {
98 static formatted_raw_ostream S(dbgs());
99 return S;
100}