Coverage Report

Created: 2018-01-17 21:32

/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
39
CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) {
24
39
  return createOutputStream(Path, "txt", InToplevel);
25
39
}
26
27
39
void CoveragePrinterText::closeViewFile(OwnedStream OS) {
28
39
  OS->operator<<('\n');
29
39
}
30
31
Error CoveragePrinterText::createIndexFile(
32
    ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
33
9
    const CoverageFiltersMatchAll &Filters) {
34
9
  auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
35
9
  if (Error E = OSOrErr.takeError())
36
1
    return E;
37
8
  auto OS = std::move(OSOrErr.get());
38
8
  raw_ostream &OSRef = *OS.get();
39
8
40
8
  CoverageReport Report(Opts, Coverage);
41
8
  Report.renderFileReports(OSRef, SourceFiles, Filters);
42
8
43
8
  Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
44
8
                                                 << Opts.getLLVMVersionString();
45
8
46
8
  return Error::success();
47
8
}
48
49
namespace {
50
51
static const unsigned LineCoverageColumnWidth = 7;
52
static const unsigned LineNumberColumnWidth = 5;
53
54
/// \brief Get the width of the leading columns.
55
31
unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) {
56
31
  return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 
00
) +
57
31
         (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 
00
);
58
31
}
59
60
/// \brief The width of the line that is used to divide between the view and
61
/// the subviews.
62
29
unsigned getDividerWidth(const CoverageViewOptions &Opts) {
63
29
  return getCombinedColumnWidth(Opts) + 4;
64
29
}
65
66
} // anonymous namespace
67
68
56
void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
69
70
56
void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
71
72
45
void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) {
73
45
  getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
74
45
                                                      << ":\n";
75
45
}
76
77
void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
78
1.04k
                                              unsigned ViewDepth) {
79
1.20k
  for (unsigned I = 0; I < ViewDepth; 
++I160
)
80
160
    OS << "  |";
81
1.04k
}
82
83
995
void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {}
84
85
void SourceCoverageViewText::renderViewDivider(raw_ostream &OS,
86
29
                                               unsigned ViewDepth) {
87
29
  assert(ViewDepth != 0 && "Cannot render divider at top level");
88
29
  renderLinePrefix(OS, ViewDepth - 1);
89
29
  OS.indent(2);
90
29
  unsigned Length = getDividerWidth(getOptions());
91
551
  for (unsigned I = 0; I < Length; 
++I522
)
92
522
    OS << '-';
93
29
  OS << '\n';
94
29
}
95
96
void SourceCoverageViewText::renderLine(raw_ostream &OS, LineRef L,
97
                                        const LineCoverageStats &LCS,
98
                                        unsigned ExpansionCol,
99
995
                                        unsigned ViewDepth) {
100
995
  StringRef Line = L.Line;
101
995
  unsigned LineNumber = L.LineNo;
102
995
  auto *WrappedSegment = LCS.getWrappedSegment();
103
995
  CoverageSegmentArray Segments = LCS.getLineSegments();
104
995
105
995
  Optional<raw_ostream::Colors> Highlight;
106
995
  SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
107
995
108
995
  // The first segment overlaps from a previous line, so we treat it specially.
109
995
  if (WrappedSegment && 
!WrappedSegment->IsGapRegion532
&&
110
995
      
WrappedSegment->HasCount450
&&
WrappedSegment->Count == 0388
)
111
40
    Highlight = raw_ostream::RED;
112
995
113
995
  // Output each segment of the line, possibly highlighted.
114
995
  unsigned Col = 1;
115
995
  for (const auto *S : Segments) {
116
667
    unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
117
667
    colored_ostream(OS, Highlight ? 
*Highlight58
:
raw_ostream::SAVEDCOLOR609
,
118
667
                    getOptions().Colors && 
Highlight0
, /*Bold=*/false,
119
667
                    /*BG=*/true)
120
667
        << Line.substr(Col - 1, End - Col);
121
667
    if (getOptions().Debug && 
Highlight190
)
122
16
      HighlightedRanges.push_back(std::make_pair(Col, End));
123
667
    Col = End;
124
667
    if ((!S->IsGapRegion || 
(88
Highlight88
&&
*Highlight == raw_ostream::RED14
)) &&
125
667
        
S->HasCount593
&&
S->Count == 0524
)
126
60
      Highlight = raw_ostream::RED;
127
607
    else if (Col == ExpansionCol)
128
0
      Highlight = raw_ostream::CYAN;
129
607
    else
130
607
      Highlight = None;
131
667
  }
132
995
133
995
  // Show the rest of the line.
134
995
  colored_ostream(OS, Highlight ? 
*Highlight42
:
raw_ostream::SAVEDCOLOR953
,
135
995
                  getOptions().Colors && 
Highlight0
, /*Bold=*/false, /*BG=*/true)
136
995
      << Line.substr(Col - 1, Line.size() - Col + 1);
137
995
  OS << '\n';
138
995
139
995
  if (getOptions().Debug) {
140
200
    for (const auto &Range : HighlightedRanges)
141
16
      errs() << "Highlighted line " << LineNumber << ", " << Range.first
142
16
             << " -> " << Range.second << '\n';
143
200
    if (Highlight)
144
6
      errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
145
200
  }
146
995
}
147
148
void SourceCoverageViewText::renderLineCoverageColumn(
149
995
    raw_ostream &OS, const LineCoverageStats &Line) {
150
995
  if (!Line.isMapped()) {
151
458
    OS.indent(LineCoverageColumnWidth) << '|';
152
458
    return;
153
458
  }
154
537
  std::string C = formatCount(Line.getExecutionCount());
155
537
  OS.indent(LineCoverageColumnWidth - C.size());
156
537
  colored_ostream(OS, raw_ostream::MAGENTA,
157
537
                  Line.hasMultipleRegions() && 
getOptions().Colors59
)
158
537
      << C;
159
537
  OS << '|';
160
537
}
161
162
void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
163
995
                                                    unsigned LineNo) {
164
995
  SmallString<32> Buffer;
165
995
  raw_svector_ostream BufferOS(Buffer);
166
995
  BufferOS << LineNo;
167
995
  auto Str = BufferOS.str();
168
995
  // Trim and align to the right.
169
995
  Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
170
995
  OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
171
995
}
172
173
void SourceCoverageViewText::renderRegionMarkers(raw_ostream &OS,
174
                                                 const LineCoverageStats &Line,
175
2
                                                 unsigned ViewDepth) {
176
2
  renderLinePrefix(OS, ViewDepth);
177
2
  OS.indent(getCombinedColumnWidth(getOptions()));
178
2
179
2
  CoverageSegmentArray Segments = Line.getLineSegments();
180
2
181
2
  // Just consider the segments which start *and* end on this line.
182
2
  if (Segments.size() > 1)
183
2
    Segments = Segments.drop_back();
184
2
185
2
  unsigned PrevColumn = 1;
186
8
  for (const auto *S : Segments) {
187
8
    if (!S->IsRegionEntry)
188
3
      continue;
189
5
    if (S->Count == Line.getExecutionCount())
190
2
      continue;
191
3
    // Skip to the new region.
192
3
    if (S->Col > PrevColumn)
193
3
      OS.indent(S->Col - PrevColumn);
194
3
    PrevColumn = S->Col + 1;
195
3
    std::string C = formatCount(S->Count);
196
3
    PrevColumn += C.size();
197
3
    OS << '^' << C;
198
3
199
3
    if (getOptions().Debug)
200
3
      errs() << "Marker at " << S->Line << ":" << S->Col << " = "
201
3
            << formatCount(S->Count) << "\n";
202
3
  }
203
2
  OS << '\n';
204
2
}
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
16
                                                     unsigned ViewDepth) {
229
16
  renderLinePrefix(OS, ViewDepth);
230
16
  OS << ' ';
231
16
  if (!ISV.View)
232
2
    getOptions().colored_ostream(OS, raw_ostream::RED)
233
2
        << "Unexecuted instantiation: " << ISV.FunctionName << "\n";
234
14
  else
235
14
    ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
236
14
                    /*ShowTitle=*/false, ViewDepth);
237
16
}
238
239
14
void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) {
240
14
  if (getOptions().hasProjectTitle())
241
0
    getOptions().colored_ostream(OS, raw_ostream::CYAN)
242
0
        << getOptions().ProjectTitle << "\n";
243
14
244
14
  getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n";
245
14
246
14
  if (getOptions().hasCreatedTime())
247
14
    getOptions().colored_ostream(OS, raw_ostream::CYAN)
248
14
        << getOptions().CreatedTimeStr << "\n";
249
14
}
250
251
void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned,
252
56
                                               unsigned) {}