blob: 231ae48759e27fc4de07c10f38a442cba819819f [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"
Nick Lewycky476b2422010-12-19 20:43:38 +000016#include <algorithm>
David Greeneed0e2ad2009-07-23 23:21:10 +000017
David Greene191cf282009-07-13 16:49:27 +000018using namespace llvm;
David Greene62fe47a2009-07-10 21:14:44 +000019
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000020/// CountColumns - Examine the given char sequence and figure out which
David Greene191cf282009-07-13 16:49:27 +000021/// column we end up in after output.
22///
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000023static unsigned CountColumns(unsigned Column, const char *Ptr, size_t Size) {
David Greene191cf282009-07-13 16:49:27 +000024 // Keep track of the current column by scanning the string for
25 // special characters
David Greene62fe47a2009-07-10 21:14:44 +000026
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000027 for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
28 ++Column;
29 if (*Ptr == '\n' || *Ptr == '\r')
30 Column = 0;
31 else if (*Ptr == '\t')
32 // Assumes tab stop = 8 characters.
33 Column += (8 - (Column & 0x7)) & 0x7;
David Greeneeb857282009-07-29 16:08:27 +000034 }
35
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000036 return Column;
37}
38
39/// ComputeColumn - Examine the current output and figure out which
40/// column we end up in after output.
Daniel Dunbarab810272009-08-18 23:36:04 +000041void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) {
42 // If our previous scan pointer is inside the buffer, assume we already
43 // scanned those bytes. This depends on raw_ostream to not change our buffer
44 // in unexpected ways.
45 if (Ptr <= Scanned && Scanned <= Ptr + Size) {
46 // Scan all characters added since our last scan to determine the new
47 // column.
48 ColumnScanned = CountColumns(ColumnScanned, Scanned,
49 Size - (Scanned - Ptr));
50 } else
51 ColumnScanned = CountColumns(ColumnScanned, Ptr, Size);
52
53 // Update the scanning pointer.
54 Scanned = Ptr + Size;
David Greene62fe47a2009-07-10 21:14:44 +000055}
David Greene191cf282009-07-13 16:49:27 +000056
57/// PadToColumn - Align the output to some column number.
58///
59/// \param NewCol - The column to move to.
David Greene191cf282009-07-13 16:49:27 +000060///
Chris Lattnerefbdaa62010-02-15 02:17:50 +000061formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) {
David Greeneeb857282009-07-29 16:08:27 +000062 // Figure out what's in the buffer and add it to the column count.
Daniel Dunbarab810272009-08-18 23:36:04 +000063 ComputeColumn(getBufferStart(), GetNumBytesInBuffer());
David Greene191cf282009-07-13 16:49:27 +000064
65 // Output spaces until we reach the desired column.
Chris Lattnerde51ded2009-08-22 23:16:09 +000066 indent(std::max(int(NewCol - ColumnScanned), 1));
Chris Lattnerefbdaa62010-02-15 02:17:50 +000067 return *this;
David Greene191cf282009-07-13 16:49:27 +000068}
69
Dan Gohmana4a68c12009-08-15 02:01:04 +000070void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000071 // Figure out what's in the buffer and add it to the column count.
Daniel Dunbarab810272009-08-18 23:36:04 +000072 ComputeColumn(Ptr, Size);
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000073
74 // Write the data to the underlying stream (which is unbuffered, so
75 // the data will be immediately written out).
Dan Gohmana4a68c12009-08-15 02:01:04 +000076 TheStream->write(Ptr, Size);
Dan Gohmanfbcb5b62009-08-15 02:02:59 +000077
Daniel Dunbarab810272009-08-18 23:36:04 +000078 // Reset the scanning pointer.
79 Scanned = 0;
Dan Gohmana4a68c12009-08-15 02:01:04 +000080}
81
David Greene71847812009-07-14 20:18:05 +000082/// fouts() - This returns a reference to a formatted_raw_ostream for
83/// standard output. Use it like: fouts() << "foo" << "bar";
84formatted_raw_ostream &llvm::fouts() {
85 static formatted_raw_ostream S(outs());
86 return S;
87}
88
89/// ferrs() - This returns a reference to a formatted_raw_ostream for
90/// standard error. Use it like: ferrs() << "foo" << "bar";
91formatted_raw_ostream &llvm::ferrs() {
92 static formatted_raw_ostream S(errs());
93 return S;
94}
David Greene8f1929b2010-01-05 01:28:43 +000095
96/// fdbgs() - This returns a reference to a formatted_raw_ostream for
97/// the debug stream. Use it like: fdbgs() << "foo" << "bar";
98formatted_raw_ostream &llvm::fdbgs() {
99 static formatted_raw_ostream S(dbgs());
100 return S;
101}