LLVM  9.0.0svn
FormattedStream.cpp
Go to the documentation of this file.
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 
14 #include "llvm/Support/Debug.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 ///
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;
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.
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 ///
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 
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";
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";
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";
105  static formatted_raw_ostream S(dbgs());
106  return S;
107 }
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
Position
Position to insert a new instruction relative to an existing instruction.
formatted_raw_ostream & PadToColumn(unsigned NewCol)
PadToColumn - Align the output to some column number.
unsigned getColumn()
getColumn - Return the column number
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:317
raw_ostream & write(unsigned char C)
formatted_raw_ostream & fdbgs()
fdbgs() - This returns a reference to a formatted_raw_ostream for debug output.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:133
static void UpdatePosition(std::pair< unsigned, unsigned > &Position, const char *Ptr, size_t Size)
UpdatePosition - Examine the given char sequence and figure out which column we end up in after outpu...
uint32_t Size
Definition: Profile.cpp:46
formatted_raw_ostream & ferrs()
ferrs() - This returns a reference to a formatted_raw_ostream for standard error. ...
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
formatted_raw_ostream & fouts()
fouts() - This returns a reference to a formatted_raw_ostream for standard output.