[llvm-objdump] - Import the test/Object/X86/no-start-symbol.test test case and rewrit...
[llvm-complete.git] / lib / Support / FormattedStream.cpp
blob4eb747038bb9ee8a81753cfd9b891f6616839f7b
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/FormattedStream.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <algorithm>
18 using namespace llvm;
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;
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;
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);
59 // Update the scanning pointer.
60 Scanned = Ptr + Size;
63 /// PadToColumn - Align the output to some column number.
64 ///
65 /// \param NewCol - The column to move to.
66 ///
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());
71 // Output spaces until we reach the desired column.
72 indent(std::max(int(NewCol - getColumn()), 1));
73 return *this;
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);
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);
84 // Reset the scanning pointer.
85 Scanned = nullptr;
88 /// fouts() - This returns a reference to a formatted_raw_ostream for
89 /// standard output. Use it like: fouts() << "foo" << "bar";
90 formatted_raw_ostream &llvm::fouts() {
91 static formatted_raw_ostream S(outs());
92 return S;
95 /// ferrs() - This returns a reference to a formatted_raw_ostream for
96 /// standard error. Use it like: ferrs() << "foo" << "bar";
97 formatted_raw_ostream &llvm::ferrs() {
98 static formatted_raw_ostream S(errs());
99 return S;
102 /// fdbgs() - This returns a reference to a formatted_raw_ostream for
103 /// the debug stream. Use it like: fdbgs() << "foo" << "bar";
104 formatted_raw_ostream &llvm::fdbgs() {
105 static formatted_raw_ostream S(dbgs());
106 return S;