• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the implementation of formatted_raw_ostream.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/FormattedStream.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <algorithm>
17 
18 using namespace llvm;
19 
20 /// 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.
22 ///
UpdatePosition(std::pair<unsigned,unsigned> & Position,const char * Ptr,size_t Size)23 static void UpdatePosition(std::pair<unsigned, unsigned> &Position, const char *Ptr, size_t Size) {
24   unsigned &Column = Position.first;
25   unsigned &Line = Position.second;
26 
27   // Keep track of the current column and line by scanning the string for
28   // special characters
29   for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
30     ++Column;
31     switch (*Ptr) {
32     case '\n':
33       Line += 1;
34       LLVM_FALLTHROUGH;
35     case '\r':
36       Column = 0;
37       break;
38     case '\t':
39       // Assumes tab stop = 8 characters.
40       Column += (8 - (Column & 0x7)) & 0x7;
41       break;
42     }
43   }
44 }
45 
46 /// ComputePosition - Examine the current output and update line and column
47 /// counts.
ComputePosition(const char * Ptr,size_t Size)48 void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
49   // 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.
52   if (Ptr <= Scanned && Scanned <= Ptr + Size)
53     // Scan all characters added since our last scan to determine the new
54     // column.
55     UpdatePosition(Position, Scanned, Size - (Scanned - Ptr));
56   else
57     UpdatePosition(Position, Ptr, Size);
58 
59   // Update the scanning pointer.
60   Scanned = Ptr + Size;
61 }
62 
63 /// PadToColumn - Align the output to some column number.
64 ///
65 /// \param NewCol - The column to move to.
66 ///
PadToColumn(unsigned NewCol)67 formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) {
68   // Figure out what's in the buffer and add it to the column count.
69   ComputePosition(getBufferStart(), GetNumBytesInBuffer());
70 
71   // Output spaces until we reach the desired column.
72   indent(std::max(int(NewCol - getColumn()), 1));
73   return *this;
74 }
75 
write_impl(const char * Ptr,size_t Size)76 void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
77   // Figure out what's in the buffer and add it to the column count.
78   ComputePosition(Ptr, Size);
79 
80   // Write the data to the underlying stream (which is unbuffered, so
81   // the data will be immediately written out).
82   TheStream->write(Ptr, Size);
83 
84   // Reset the scanning pointer.
85   Scanned = nullptr;
86 }
87 
88 /// fouts() - This returns a reference to a formatted_raw_ostream for
89 /// standard output.  Use it like: fouts() << "foo" << "bar";
fouts()90 formatted_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";
ferrs()97 formatted_raw_ostream &llvm::ferrs() {
98   static formatted_raw_ostream S(errs());
99   return S;
100 }
101 
102 /// fdbgs() - This returns a reference to a formatted_raw_ostream for
103 /// the debug stream.  Use it like: fdbgs() << "foo" << "bar";
fdbgs()104 formatted_raw_ostream &llvm::fdbgs() {
105   static formatted_raw_ostream S(dbgs());
106   return S;
107 }
108