Coverage Report

Created: 2017-09-21 03:39

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