blob: 4eb747038bb9ee8a81753cfd9b891f6616839f7b [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
David Greenec97b778b2009-07-10 21:14:44 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattnerca99c342009-07-14 23:14:10 +00009// This file contains the implementation of formatted_raw_ostream.
David Greenec97b778b2009-07-10 21:14:44 +000010//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/FormattedStream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000015#include "llvm/Support/raw_ostream.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;
Galina Kistanova78706a32017-05-19 21:08:28 +000034 LLVM_FALLTHROUGH;
Daniel Maleaf83beab2013-05-08 20:29:10 +000035 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///
Fangrui Songf78650a2018-07-30 19:41:25 +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}