blob: 9febf664f2bef156b2a52dcd04b0a65d0cb3088e [file] [log] [blame]
David Greene20f6ac02009-07-13 16:49:27 +00001//===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
David Greenec97b778b2009-07-10 21:14:44 +00002//
3// The LLVM Compiler Infrastructure
4//
David Greene20f6ac02009-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 Greenec97b778b2009-07-10 21:14:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerca99c342009-07-14 23:14:10 +000010// This file contains the implementation of formatted_raw_ostream.
David Greenec97b778b2009-07-10 21:14:44 +000011//
12//===----------------------------------------------------------------------===//
13
David Greene93a522b2010-01-05 01:28:43 +000014#include "llvm/Support/Debug.h"
David Greenec97b778b2009-07-10 21:14:44 +000015#include "llvm/Support/FormattedStream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000016#include <algorithm>
David Greene8e621f02009-07-23 23:21:10 +000017
David Greene20f6ac02009-07-13 16:49:27 +000018using namespace llvm;
David Greenec97b778b2009-07-10 21:14:44 +000019
Daniel Maleaf83beab2013-05-08 20:29:10 +000020/// UpdatePosition - Examine the given char sequence and figure out which
21/// column we end up in after output, and how many line breaks are contained.
David Greene20f6ac02009-07-13 16:49:27 +000022///
Daniel Maleaf83beab2013-05-08 20:29:10 +000023static void UpdatePosition(std::pair<unsigned, unsigned> &Position, const char *Ptr, size_t Size) {
24 unsigned &Column = Position.first;
25 unsigned &Line = Position.second;
David Greenec97b778b2009-07-10 21:14:44 +000026
Daniel Maleaf83beab2013-05-08 20:29:10 +000027 // Keep track of the current column and line by scanning the string for
28 // special characters
Dan Gohman186b85d2009-08-15 02:02:59 +000029 for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
30 ++Column;
Daniel Maleaf83beab2013-05-08 20:29:10 +000031 switch (*Ptr) {
32 case '\n':
33 Line += 1;
34 case '\r':
Dan Gohman186b85d2009-08-15 02:02:59 +000035 Column = 0;
Daniel Maleaf83beab2013-05-08 20:29:10 +000036 break;
37 case '\t':
Dan Gohman186b85d2009-08-15 02:02:59 +000038 // Assumes tab stop = 8 characters.
39 Column += (8 - (Column & 0x7)) & 0x7;
Daniel Maleaf83beab2013-05-08 20:29:10 +000040 break;
41 }
David Greeneea2f1ce2009-07-29 16:08:27 +000042 }
Dan Gohman186b85d2009-08-15 02:02:59 +000043}
44
Daniel Maleaf83beab2013-05-08 20:29:10 +000045/// ComputePosition - Examine the current output and update line and column
46/// counts.
47void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000048 // If our previous scan pointer is inside the buffer, assume we already
49 // scanned those bytes. This depends on raw_ostream to not change our buffer
50 // in unexpected ways.
Daniel Maleaf83beab2013-05-08 20:29:10 +000051 if (Ptr <= Scanned && Scanned <= Ptr + Size)
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000052 // Scan all characters added since our last scan to determine the new
53 // column.
Daniel Maleaf83beab2013-05-08 20:29:10 +000054 UpdatePosition(Position, Scanned, Size - (Scanned - Ptr));
55 else
56 UpdatePosition(Position, Ptr, Size);
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000057
58 // Update the scanning pointer.
59 Scanned = Ptr + Size;
David Greenec97b778b2009-07-10 21:14:44 +000060}
David Greene20f6ac02009-07-13 16:49:27 +000061
62/// PadToColumn - Align the output to some column number.
63///
64/// \param NewCol - The column to move to.
David Greene20f6ac02009-07-13 16:49:27 +000065///
Chris Lattnerf733d752010-02-15 02:17:50 +000066formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) {
David Greeneea2f1ce2009-07-29 16:08:27 +000067 // Figure out what's in the buffer and add it to the column count.
Daniel Maleaf83beab2013-05-08 20:29:10 +000068 ComputePosition(getBufferStart(), GetNumBytesInBuffer());
David Greene20f6ac02009-07-13 16:49:27 +000069
70 // Output spaces until we reach the desired column.
Daniel Maleaf83beab2013-05-08 20:29:10 +000071 indent(std::max(int(NewCol - getColumn()), 1));
Chris Lattnerf733d752010-02-15 02:17:50 +000072 return *this;
David Greene20f6ac02009-07-13 16:49:27 +000073}
74
Dan Gohman250635e2009-08-15 02:01:04 +000075void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
Dan Gohman186b85d2009-08-15 02:02:59 +000076 // Figure out what's in the buffer and add it to the column count.
Daniel Maleaf83beab2013-05-08 20:29:10 +000077 ComputePosition(Ptr, Size);
Dan Gohman186b85d2009-08-15 02:02:59 +000078
79 // Write the data to the underlying stream (which is unbuffered, so
80 // the data will be immediately written out).
Dan Gohman250635e2009-08-15 02:01:04 +000081 TheStream->write(Ptr, Size);
Dan Gohman186b85d2009-08-15 02:02:59 +000082
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000083 // Reset the scanning pointer.
84 Scanned = 0;
Dan Gohman250635e2009-08-15 02:01:04 +000085}
86
David Greenea31f96c2009-07-14 20:18:05 +000087/// fouts() - This returns a reference to a formatted_raw_ostream for
88/// standard output. Use it like: fouts() << "foo" << "bar";
89formatted_raw_ostream &llvm::fouts() {
90 static formatted_raw_ostream S(outs());
91 return S;
92}
93
94/// ferrs() - This returns a reference to a formatted_raw_ostream for
95/// standard error. Use it like: ferrs() << "foo" << "bar";
96formatted_raw_ostream &llvm::ferrs() {
97 static formatted_raw_ostream S(errs());
98 return S;
99}
David Greene93a522b2010-01-05 01:28:43 +0000100
101/// fdbgs() - This returns a reference to a formatted_raw_ostream for
102/// the debug stream. Use it like: fdbgs() << "foo" << "bar";
103formatted_raw_ostream &llvm::fdbgs() {
104 static formatted_raw_ostream S(dbgs());
105 return S;
106}