blob: b0cb06c1daa2f1dfc390412ce68f3b00d884da1e [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
14#include "llvm/Support/FormattedStream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/Support/Debug.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;
Galina Kistanova78706a32017-05-19 21:08:28 +000035 LLVM_FALLTHROUGH;
Daniel Maleaf83beab2013-05-08 20:29:10 +000036 case '\r':
Dan Gohman186b85d2009-08-15 02:02:59 +000037 Column = 0;
Daniel Maleaf83beab2013-05-08 20:29:10 +000038 break;
39 case '\t':
Dan Gohman186b85d2009-08-15 02:02:59 +000040 // Assumes tab stop = 8 characters.
41 Column += (8 - (Column & 0x7)) & 0x7;
Daniel Maleaf83beab2013-05-08 20:29:10 +000042 break;
43 }
David Greeneea2f1ce2009-07-29 16:08:27 +000044 }
Dan Gohman186b85d2009-08-15 02:02:59 +000045}
46
Daniel Maleaf83beab2013-05-08 20:29:10 +000047/// ComputePosition - Examine the current output and update line and column
48/// counts.
49void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000050 // If our previous scan pointer is inside the buffer, assume we already
51 // scanned those bytes. This depends on raw_ostream to not change our buffer
52 // in unexpected ways.
Daniel Maleaf83beab2013-05-08 20:29:10 +000053 if (Ptr <= Scanned && Scanned <= Ptr + Size)
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000054 // Scan all characters added since our last scan to determine the new
55 // column.
Daniel Maleaf83beab2013-05-08 20:29:10 +000056 UpdatePosition(Position, Scanned, Size - (Scanned - Ptr));
57 else
58 UpdatePosition(Position, Ptr, Size);
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000059
60 // Update the scanning pointer.
61 Scanned = Ptr + Size;
David Greenec97b778b2009-07-10 21:14:44 +000062}
David Greene20f6ac02009-07-13 16:49:27 +000063
64/// PadToColumn - Align the output to some column number.
65///
66/// \param NewCol - The column to move to.
David Greene20f6ac02009-07-13 16:49:27 +000067///
Fangrui Songf78650a2018-07-30 19:41:25 +000068formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) {
David Greeneea2f1ce2009-07-29 16:08:27 +000069 // Figure out what's in the buffer and add it to the column count.
Daniel Maleaf83beab2013-05-08 20:29:10 +000070 ComputePosition(getBufferStart(), GetNumBytesInBuffer());
David Greene20f6ac02009-07-13 16:49:27 +000071
72 // Output spaces until we reach the desired column.
Daniel Maleaf83beab2013-05-08 20:29:10 +000073 indent(std::max(int(NewCol - getColumn()), 1));
Chris Lattnerf733d752010-02-15 02:17:50 +000074 return *this;
David Greene20f6ac02009-07-13 16:49:27 +000075}
76
Dan Gohman250635e2009-08-15 02:01:04 +000077void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
Dan Gohman186b85d2009-08-15 02:02:59 +000078 // Figure out what's in the buffer and add it to the column count.
Daniel Maleaf83beab2013-05-08 20:29:10 +000079 ComputePosition(Ptr, Size);
Dan Gohman186b85d2009-08-15 02:02:59 +000080
81 // Write the data to the underlying stream (which is unbuffered, so
82 // the data will be immediately written out).
Dan Gohman250635e2009-08-15 02:01:04 +000083 TheStream->write(Ptr, Size);
Dan Gohman186b85d2009-08-15 02:02:59 +000084
Daniel Dunbar17a6fd22009-08-18 23:36:04 +000085 // Reset the scanning pointer.
Craig Topperc10719f2014-04-07 04:17:22 +000086 Scanned = nullptr;
Dan Gohman250635e2009-08-15 02:01:04 +000087}
88
David Greenea31f96c2009-07-14 20:18:05 +000089/// fouts() - This returns a reference to a formatted_raw_ostream for
90/// standard output. Use it like: fouts() << "foo" << "bar";
91formatted_raw_ostream &llvm::fouts() {
92 static formatted_raw_ostream S(outs());
93 return S;
94}
95
96/// ferrs() - This returns a reference to a formatted_raw_ostream for
97/// standard error. Use it like: ferrs() << "foo" << "bar";
98formatted_raw_ostream &llvm::ferrs() {
99 static formatted_raw_ostream S(errs());
100 return S;
101}
David Greene93a522b2010-01-05 01:28:43 +0000102
103/// fdbgs() - This returns a reference to a formatted_raw_ostream for
104/// the debug stream. Use it like: fdbgs() << "foo" << "bar";
105formatted_raw_ostream &llvm::fdbgs() {
106 static formatted_raw_ostream S(dbgs());
107 return S;
108}