Coverage Report

Created: 2018-08-19 14:04

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