Coverage Report

Created: 2017-10-03 07:32

/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
40
CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) {
24
40
  return createOutputStream(Path, "txt", InToplevel);
25
40
}
26
27
40
void CoveragePrinterText::closeViewFile(OwnedStream OS) {
28
40
  OS->operator<<('\n');
29
40
}
30
31
Error CoveragePrinterText::createIndexFile(
32
    ArrayRef<std::string> SourceFiles,
33
11
    const coverage::CoverageMapping &Coverage, const CoverageFilter &Filters) {
34
11
  auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
35
11
  if (Error E = OSOrErr.takeError())
36
1
    return E;
37
10
  auto OS = std::move(OSOrErr.get());
38
10
  raw_ostream &OSRef = *OS.get();
39
10
40
10
  CoverageReport Report(Opts, Coverage);
41
10
  Report.renderFileReports(OSRef, SourceFiles, Filters);
42
10
43
10
  Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
44
10
                                                 << Opts.getLLVMVersionString();
45
10
46
10
  return Error::success();
47
10
}
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
55
void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
69
70
55
void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
71
72
31
void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) {
73
31
  getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
74
31
                                                      << ":\n";
75
31
}
76
77
void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
78
1.65k
                                              unsigned ViewDepth) {
79
1.74k
  for (unsigned I = 0; 
I < ViewDepth1.74k
;
++I94
)
80
94
    OS << "  |";
81
1.65k
}
82
83
1.60k
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.60k
    CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) {
100
1.60k
  StringRef Line = L.Line;
101
1.60k
  unsigned LineNumber = L.LineNo;
102
1.60k
103
1.60k
  Optional<raw_ostream::Colors> Highlight;
104
1.60k
  SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
105
1.60k
106
1.60k
  // The first segment overlaps from a previous line, so we treat it specially.
107
1.60k
  if (
WrappedSegment && 1.60k
WrappedSegment->HasCount1.42k
&&
WrappedSegment->Count == 0470
)
108
59
    Highlight = raw_ostream::RED;
109
1.60k
110
1.60k
  // Output each segment of the line, possibly highlighted.
111
1.60k
  unsigned Col = 1;
112
620
  for (const auto *S : Segments) {
113
620
    unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
114
620
    colored_ostream(OS, Highlight ? 
*Highlight90
:
raw_ostream::SAVEDCOLOR530
,
115
0
                    getOptions().Colors && Highlight, /*Bold=*/false,
116
620
                    /*BG=*/true)
117
620
        << Line.substr(Col - 1, End - Col);
118
620
    if (
getOptions().Debug && 620
Highlight226
)
119
28
      HighlightedRanges.push_back(std::make_pair(Col, End));
120
620
    Col = End;
121
620
    if (Col == ExpansionCol)
122
0
      Highlight = raw_ostream::CYAN;
123
620
    else 
if (620
!S->IsGapRegion && 620
S->HasCount610
&&
S->Count == 0532
)
124
88
      Highlight = raw_ostream::RED;
125
620
    else
126
532
      Highlight = None;
127
620
  }
128
1.60k
129
1.60k
  // Show the rest of the line.
130
1.60k
  colored_ostream(OS, Highlight ? 
*Highlight57
:
raw_ostream::SAVEDCOLOR1.55k
,
131
0
                  getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true)
132
1.60k
      << Line.substr(Col - 1, Line.size() - Col + 1);
133
1.60k
  OS << '\n';
134
1.60k
135
1.60k
  if (
getOptions().Debug1.60k
) {
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.60k
}
143
144
void SourceCoverageViewText::renderLineCoverageColumn(
145
1.57k
    raw_ostream &OS, const LineCoverageStats &Line) {
146
1.57k
  if (
!Line.isMapped()1.57k
) {
147
1.05k
    OS.indent(LineCoverageColumnWidth) << '|';
148
1.05k
    return;
149
1.05k
  }
150
525
  std::string C = formatCount(Line.ExecutionCount);
151
525
  OS.indent(LineCoverageColumnWidth - C.size());
152
525
  colored_ostream(OS, raw_ostream::MAGENTA,
153
47
                  Line.hasMultipleRegions() && getOptions().Colors)
154
1.57k
      << C;
155
1.57k
  OS << '|';
156
1.57k
}
157
158
void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
159
1.60k
                                                    unsigned LineNo) {
160
1.60k
  SmallString<32> Buffer;
161
1.60k
  raw_svector_ostream BufferOS(Buffer);
162
1.60k
  BufferOS << LineNo;
163
1.60k
  auto Str = BufferOS.str();
164
1.60k
  // Trim and align to the right.
165
1.60k
  Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
166
1.60k
  OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
167
1.60k
}
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
                  /*ShowTitle=*/false, 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
                    /*ShowTitle=*/false, ViewDepth);
227
12
}
228
229
15
void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) {
230
15
  if (getOptions().hasProjectTitle())
231
1
    getOptions().colored_ostream(OS, raw_ostream::CYAN)
232
1
        << getOptions().ProjectTitle << "\n";
233
15
234
15
  getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n";
235
15
236
15
  if (getOptions().hasCreatedTime())
237
15
    getOptions().colored_ostream(OS, raw_ostream::CYAN)
238
15
        << getOptions().CreatedTimeStr << "\n";
239
15
}
240
241
void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned,
242
55
                                               unsigned) {}