blob: 2ed71c7e43119ea6d35543455aa7cf039282debe [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"
Benjamin Kramer799003b2015-03-23 19:32:43 +000016#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000017#include <algorithm>
David Greene8e621f02009-07-23 23:21:10 +000018
David Greene20f6ac02009-07-13 16:49:27 +000019using namespace llvm;
David Greenec97b778b2009-07-10 21:14:44 +000020
Daniel Maleaf83beab2013-05-08 20:29:10 +000021/// UpdatePosition - Examine the given char sequence and figure out which
22/// column we end up in after output, and how many line breaks are contained.
David Greene20f6ac02009-07-13 16:49:27 +000023///
Daniel Maleaf83beab2013-05-08 20:29:10 +000024static void UpdatePosition(std::pair<unsigned, unsigned> &Position, const char *Ptr, size_t Size) {
25 unsigned &Column = Position.first;
26 unsigned &Line = Position.second;
David Greenec97b778b2009-07-10 21:14:44 +000027
Daniel Maleaf83beab2013-05-08 20:29:10 +000028 // Keep track of the current column and line by scanning the string for
29 // special characters
Dan Gohman186b85d2009-08-15 02:02:59 +000030 for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
31 ++Column;
Daniel Maleaf83beab2013-05-08 20:29:10 +000032 switch (*Ptr) {
33 case '\n':
34 Line += 1;
35 case '\r':
Dan Gohman186b85d2009-08-15 02:02:59 +000036 Column = 0;
Daniel Maleaf83beab2013-05-08 20:29:10 +000037 break;
38 case '\t':
Dan Gohman186b85d2009-08-15 02:02:59 +000039 // Assumes tab stop = 8 characters.
40 Column += (8 - (Column & 0x7)) & 0x7;
Daniel Maleaf83beab2013-05-08 20:29:10 +000041 break;
42 }
David Greeneea2f1ce2009-07-29 16:08:27 +000043 }
Dan Gohman186b85d2009-08-15 02:02:59 +000044}
45
Daniel Maleaf83beab2013-05-08 20:29:10 +000046/// ComputePosition - Examine the current output and update line and column
47/// counts.
48void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000049 // If our previous scan pointer is inside the buffer, assume we already
50 // scanned those bytes. This depends on raw_ostream to not change our buffer
51 // in unexpected ways.
Daniel Maleaf83beab2013-05-08 20:29:10 +000052 if (Ptr <= Scanned && Scanned <= Ptr + Size)
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000053 // Scan all characters added since our last scan to determine the new
54 // column.
Daniel Maleaf83beab2013-05-08 20:29:10 +000055 UpdatePosition(Position, Scanned, Size - (Scanned - Ptr));
56 else
57 UpdatePosition(Position, Ptr, Size);
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000058
59 // Update the scanning pointer.
60 Scanned = Ptr + Size;
David Greenec97b778b2009-07-10 21:14:44 +000061}
David Greene20f6ac02009-07-13 16:49:27 +000062
63/// PadToColumn - Align the output to some column number.
64///
65/// \param NewCol - The column to move to.
David Greene20f6ac02009-07-13 16:49:27 +000066///
Chris Lattnerf733d752010-02-15 02:17:50 +000067formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) {
David Greeneea2f1ce2009-07-29 16:08:27 +000068 // Figure out what's in the buffer and add it to the column count.
Daniel Maleaf83beab2013-05-08 20:29:10 +000069 ComputePosition(getBufferStart(), GetNumBytesInBuffer());
David Greene20f6ac02009-07-13 16:49:27 +000070
71 // Output spaces until we reach the desired column.
Daniel Maleaf83beab2013-05-08 20:29:10 +000072 indent(std::max(int(NewCol - getColumn()), 1));
Chris Lattnerf733d752010-02-15 02:17:50 +000073 return *this;
David Greene20f6ac02009-07-13 16:49:27 +000074}
75
Dan Gohman250635e2009-08-15 02:01:04 +000076void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
Dan Gohman186b85d2009-08-15 02:02:59 +000077 // Figure out what's in the buffer and add it to the column count.
Daniel Maleaf83beab2013-05-08 20:29:10 +000078 ComputePosition(Ptr, Size);
Dan Gohman186b85d2009-08-15 02:02:59 +000079
80 // Write the data to the underlying stream (which is unbuffered, so
81 // the data will be immediately written out).
Dan Gohman250635e2009-08-15 02:01:04 +000082 TheStream->write(Ptr, Size);
Dan Gohman186b85d2009-08-15 02:02:59 +000083
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000084 // Reset the scanning pointer.
Craig Topperc10719f2014-04-07 04:17:22 +000085 Scanned = nullptr;
Dan Gohman250635e2009-08-15 02:01:04 +000086}
87
David Greenea31f96c2009-07-14 20:18:05 +000088/// fouts() - This returns a reference to a formatted_raw_ostream for
89/// standard output. Use it like: fouts() << "foo" << "bar";
90formatted_raw_ostream &llvm::fouts() {
91 static formatted_raw_ostream S(outs());
92 return S;
93}
94
95/// ferrs() - This returns a reference to a formatted_raw_ostream for
96/// standard error. Use it like: ferrs() << "foo" << "bar";
97formatted_raw_ostream &llvm::ferrs() {
98 static formatted_raw_ostream S(errs());
99 return S;
100}
David Greene93a522b2010-01-05 01:28:43 +0000101
102/// fdbgs() - This returns a reference to a formatted_raw_ostream for
103/// the debug stream. Use it like: fdbgs() << "foo" << "bar";
104formatted_raw_ostream &llvm::fdbgs() {
105 static formatted_raw_ostream S(dbgs());
106 return S;
107}