Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SourceCoverageViewText.cpp - A text-based code coverage view -------===//
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
/// \file This file implements the text-based coverage renderer.
10
///
11
//===----------------------------------------------------------------------===//
12
13
#include "CoverageReport.h"
14
#include "SourceCoverageViewText.h"
15
#include "llvm/ADT/Optional.h"
16
#include "llvm/ADT/SmallString.h"
17
#include "llvm/ADT/StringExtras.h"
18
19
using namespace llvm;
20
21
Expected<CoveragePrinter::OwnedStream>
22
80
CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) {
23
80
  return createOutputStream(Path, "txt", InToplevel);
24
80
}
25
26
80
void CoveragePrinterText::closeViewFile(OwnedStream OS) {
27
80
  OS->operator<<('\n');
28
80
}
29
30
Error CoveragePrinterText::createIndexFile(
31
    ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
32
14
    const CoverageFiltersMatchAll &Filters) {
33
14
  auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
34
14
  if (Error E = OSOrErr.takeError())
35
1
    return E;
36
13
  auto OS = std::move(OSOrErr.get());
37
13
  raw_ostream &OSRef = *OS.get();
38
13
39
13
  CoverageReport Report(Opts, Coverage);
40
13
  Report.renderFileReports(OSRef, SourceFiles, Filters);
41
13
42
13
  Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
43
13
                                                 << Opts.getLLVMVersionString();
44
13
45
13
  return Error::success();
46
13
}
47
48
namespace {
49
50
static const unsigned LineCoverageColumnWidth = 7;
51
static const unsigned LineNumberColumnWidth = 5;
52
53
/// Get the width of the leading columns.
54
56
unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) {
55
56
  return (Opts.ShowLineStats ? 
LineCoverageColumnWidth + 154
:
02
) +
56
56
         (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 
00
);
57
56
}
58
59
/// The width of the line that is used to divide between the view and
60
/// the subviews.
61
52
unsigned getDividerWidth(const CoverageViewOptions &Opts) {
62
52
  return getCombinedColumnWidth(Opts) + 4;
63
52
}
64
65
} // anonymous namespace
66
67
115
void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
68
69
115
void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
70
71
91
void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) {
72
91
  getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
73
91
                                                      << ":\n";
74
91
}
75
76
void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
77
1.97k
                                              unsigned ViewDepth) {
78
2.24k
  for (unsigned I = 0; I < ViewDepth; 
++I266
)
79
266
    OS << "  |";
80
1.97k
}
81
82
1.89k
void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {}
83
84
void SourceCoverageViewText::renderViewDivider(raw_ostream &OS,
85
52
                                               unsigned ViewDepth) {
86
52
  assert(ViewDepth != 0 && "Cannot render divider at top level");
87
52
  renderLinePrefix(OS, ViewDepth - 1);
88
52
  OS.indent(2);
89
52
  unsigned Length = getDividerWidth(getOptions());
90
988
  for (unsigned I = 0; I < Length; 
++I936
)
91
936
    OS << '-';
92
52
  OS << '\n';
93
52
}
94
95
void SourceCoverageViewText::renderLine(raw_ostream &OS, LineRef L,
96
                                        const LineCoverageStats &LCS,
97
                                        unsigned ExpansionCol,
98
1.89k
                                        unsigned ViewDepth) {
99
1.89k
  StringRef Line = L.Line;
100
1.89k
  unsigned LineNumber = L.LineNo;
101
1.89k
  auto *WrappedSegment = LCS.getWrappedSegment();
102
1.89k
  CoverageSegmentArray Segments = LCS.getLineSegments();
103
1.89k
104
1.89k
  Optional<raw_ostream::Colors> Highlight;
105
1.89k
  SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
106
1.89k
107
1.89k
  // The first segment overlaps from a previous line, so we treat it specially.
108
1.89k
  if (WrappedSegment && 
!WrappedSegment->IsGapRegion1.00k
&&
109
1.89k
      
WrappedSegment->HasCount864
&&
WrappedSegment->Count == 0689
)
110
88
    Highlight = raw_ostream::RED;
111
1.89k
112
1.89k
  // Output each segment of the line, possibly highlighted.
113
1.89k
  unsigned Col = 1;
114
1.89k
  for (const auto *S : Segments) {
115
1.16k
    unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
116
1.16k
    colored_ostream(OS, Highlight ? 
*Highlight115
:
raw_ostream::SAVEDCOLOR1.05k
,
117
1.16k
                    getOptions().Colors && 
Highlight0
, /*Bold=*/false,
118
1.16k
                    /*BG=*/true)
119
1.16k
        << Line.substr(Col - 1, End - Col);
120
1.16k
    if (getOptions().Debug && 
Highlight254
)
121
27
      HighlightedRanges.push_back(std::make_pair(Col, End));
122
1.16k
    Col = End;
123
1.16k
    if ((!S->IsGapRegion || 
(158
Highlight158
&&
*Highlight == raw_ostream::RED27
)) &&
124
1.16k
        
S->HasCount1.03k
&&
S->Count == 0895
)
125
123
      Highlight = raw_ostream::RED;
126
1.04k
    else if (Col == ExpansionCol)
127
0
      Highlight = raw_ostream::CYAN;
128
1.04k
    else
129
1.04k
      Highlight = None;
130
1.16k
  }
131
1.89k
132
1.89k
  // Show the rest of the line.
133
1.89k
  colored_ostream(OS, Highlight ? 
*Highlight96
:
raw_ostream::SAVEDCOLOR1.79k
,
134
1.89k
                  getOptions().Colors && 
Highlight0
, /*Bold=*/false, /*BG=*/true)
135
1.89k
      << Line.substr(Col - 1, Line.size() - Col + 1);
136
1.89k
  OS << '\n';
137
1.89k
138
1.89k
  if (getOptions().Debug) {
139
276
    for (const auto &Range : HighlightedRanges)
140
27
      errs() << "Highlighted line " << LineNumber << ", " << Range.first
141
27
             << " -> " << Range.second << '\n';
142
276
    if (Highlight)
143
16
      errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
144
276
  }
145
1.89k
}
146
147
void SourceCoverageViewText::renderLineCoverageColumn(
148
1.86k
    raw_ostream &OS, const LineCoverageStats &Line) {
149
1.86k
  if (!Line.isMapped()) {
150
914
    OS.indent(LineCoverageColumnWidth) << '|';
151
914
    return;
152
914
  }
153
950
  std::string C = formatCount(Line.getExecutionCount());
154
950
  OS.indent(LineCoverageColumnWidth - C.size());
155
950
  colored_ostream(OS, raw_ostream::MAGENTA,
156
950
                  Line.hasMultipleRegions() && 
getOptions().Colors88
)
157
950
      << C;
158
950
  OS << '|';
159
950
}
160
161
void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
162
1.89k
                                                    unsigned LineNo) {
163
1.89k
  SmallString<32> Buffer;
164
1.89k
  raw_svector_ostream BufferOS(Buffer);
165
1.89k
  BufferOS << LineNo;
166
1.89k
  auto Str = BufferOS.str();
167
1.89k
  // Trim and align to the right.
168
1.89k
  Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
169
1.89k
  OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
170
1.89k
}
171
172
void SourceCoverageViewText::renderRegionMarkers(raw_ostream &OS,
173
                                                 const LineCoverageStats &Line,
174
4
                                                 unsigned ViewDepth) {
175
4
  renderLinePrefix(OS, ViewDepth);
176
4
  OS.indent(getCombinedColumnWidth(getOptions()));
177
4
178
4
  CoverageSegmentArray Segments = Line.getLineSegments();
179
4
180
4
  // Just consider the segments which start *and* end on this line.
181
4
  if (Segments.size() > 1)
182
4
    Segments = Segments.drop_back();
183
4
184
4
  unsigned PrevColumn = 1;
185
15
  for (const auto *S : Segments) {
186
15
    if (!S->IsRegionEntry)
187
6
      continue;
188
9
    if (S->Count == Line.getExecutionCount())
189
4
      continue;
190
5
    // Skip to the new region.
191
5
    if (S->Col > PrevColumn)
192
5
      OS.indent(S->Col - PrevColumn);
193
5
    PrevColumn = S->Col + 1;
194
5
    std::string C = formatCount(S->Count);
195
5
    PrevColumn += C.size();
196
5
    OS << '^' << C;
197
5
198
5
    if (getOptions().Debug)
199
5
      errs() << "Marker at " << S->Line << ":" << S->Col << " = "
200
5
            << formatCount(S->Count) << "\n";
201
5
  }
202
4
  OS << '\n';
203
4
}
204
205
void SourceCoverageViewText::renderExpansionSite(raw_ostream &OS, LineRef L,
206
                                                 const LineCoverageStats &LCS,
207
                                                 unsigned ExpansionCol,
208
0
                                                 unsigned ViewDepth) {
209
0
  renderLinePrefix(OS, ViewDepth);
210
0
  OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1));
211
0
  renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
212
0
}
213
214
void SourceCoverageViewText::renderExpansionView(raw_ostream &OS,
215
                                                 ExpansionView &ESV,
216
3
                                                 unsigned ViewDepth) {
217
3
  // Render the child subview.
218
3
  if (getOptions().Debug)
219
3
    errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol()
220
3
           << " -> " << ESV.getEndCol() << '\n';
221
3
  ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
222
3
                  /*ShowTitle=*/false, ViewDepth + 1);
223
3
}
224
225
void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
226
                                                     InstantiationView &ISV,
227
32
                                                     unsigned ViewDepth) {
228
32
  renderLinePrefix(OS, ViewDepth);
229
32
  OS << ' ';
230
32
  if (!ISV.View)
231
6
    getOptions().colored_ostream(OS, raw_ostream::RED)
232
6
        << "Unexecuted instantiation: " << ISV.FunctionName << "\n";
233
26
  else
234
26
    ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
235
26
                    /*ShowTitle=*/false, ViewDepth);
236
32
}
237
238
25
void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) {
239
25
  if (getOptions().hasProjectTitle())
240
0
    getOptions().colored_ostream(OS, raw_ostream::CYAN)
241
0
        << getOptions().ProjectTitle << "\n";
242
25
243
25
  getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n";
244
25
245
25
  if (getOptions().hasCreatedTime())
246
25
    getOptions().colored_ostream(OS, raw_ostream::CYAN)
247
25
        << getOptions().CreatedTimeStr << "\n";
248
25
}
249
250
void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned,
251
115
                                               unsigned) {}