Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/FormattedStream.cpp
Line
Count
Source (jump to first uncovered line)
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
///
23
7.76M
static void UpdatePosition(std::pair<unsigned, unsigned> &Position, const char *Ptr, size_t Size) {
24
7.76M
  unsigned &Column = Position.first;
25
7.76M
  unsigned &Line = Position.second;
26
7.76M
27
7.76M
  // Keep track of the current column and line by scanning the string for
28
7.76M
  // special characters
29
762M
  for (const char *End = Ptr + Size; Ptr != End; 
++Ptr754M
) {
30
754M
    ++Column;
31
754M
    switch (*Ptr) {
32
754M
    case '\n':
33
21.7M
      Line += 1;
34
21.7M
      LLVM_FALLTHROUGH;
35
21.7M
    case '\r':
36
21.7M
      Column = 0;
37
21.7M
      break;
38
21.7M
    case '\t':
39
9.13M
      // Assumes tab stop = 8 characters.
40
9.13M
      Column += (8 - (Column & 0x7)) & 0x7;
41
9.13M
      break;
42
754M
    }
43
754M
  }
44
7.76M
}
45
46
/// ComputePosition - Examine the current output and update line and column
47
/// counts.
48
7.76M
void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
49
7.76M
  // If our previous scan pointer is inside the buffer, assume we already
50
7.76M
  // scanned those bytes. This depends on raw_ostream to not change our buffer
51
7.76M
  // in unexpected ways.
52
7.76M
  if (Ptr <= Scanned && 
Scanned <= Ptr + Size4.51M
)
53
4.51M
    // Scan all characters added since our last scan to determine the new
54
4.51M
    // column.
55
4.51M
    UpdatePosition(Position, Scanned, Size - (Scanned - Ptr));
56
3.24M
  else
57
3.24M
    UpdatePosition(Position, Ptr, Size);
58
7.76M
59
7.76M
  // Update the scanning pointer.
60
7.76M
  Scanned = Ptr + Size;
61
7.76M
}
62
63
/// PadToColumn - Align the output to some column number.
64
///
65
/// \param NewCol - The column to move to.
66
///
67
4.51M
formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) {
68
4.51M
  // Figure out what's in the buffer and add it to the column count.
69
4.51M
  ComputePosition(getBufferStart(), GetNumBytesInBuffer());
70
4.51M
71
4.51M
  // Output spaces until we reach the desired column.
72
4.51M
  indent(std::max(int(NewCol - getColumn()), 1));
73
4.51M
  return *this;
74
4.51M
}
75
76
3.24M
void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
77
3.24M
  // Figure out what's in the buffer and add it to the column count.
78
3.24M
  ComputePosition(Ptr, Size);
79
3.24M
80
3.24M
  // Write the data to the underlying stream (which is unbuffered, so
81
3.24M
  // the data will be immediately written out).
82
3.24M
  TheStream->write(Ptr, Size);
83
3.24M
84
3.24M
  // Reset the scanning pointer.
85
3.24M
  Scanned = nullptr;
86
3.24M
}
87
88
/// fouts() - This returns a reference to a formatted_raw_ostream for
89
/// standard output.  Use it like: fouts() << "foo" << "bar";
90
4.12k
formatted_raw_ostream &llvm::fouts() {
91
4.12k
  static formatted_raw_ostream S(outs());
92
4.12k
  return S;
93
4.12k
}
94
95
/// ferrs() - This returns a reference to a formatted_raw_ostream for
96
/// standard error.  Use it like: ferrs() << "foo" << "bar";
97
0
formatted_raw_ostream &llvm::ferrs() {
98
0
  static formatted_raw_ostream S(errs());
99
0
  return S;
100
0
}
101
102
/// fdbgs() - This returns a reference to a formatted_raw_ostream for
103
/// the debug stream.  Use it like: fdbgs() << "foo" << "bar";
104
0
formatted_raw_ostream &llvm::fdbgs() {
105
0
  static formatted_raw_ostream S(dbgs());
106
0
  return S;
107
0
}