Coverage Report

Created: 2018-01-17 21:32

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/llvm-cov/CoverageReport.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CoverageReport.cpp - Code coverage report -------------------------===//
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
// This class implements rendering of a code coverage report.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "CoverageReport.h"
15
#include "RenderingSupport.h"
16
#include "llvm/ADT/DenseMap.h"
17
#include "llvm/Support/Format.h"
18
#include "llvm/Support/Path.h"
19
#include "llvm/Support/ThreadPool.h"
20
#include "llvm/Support/Threading.h"
21
#include <numeric>
22
23
using namespace llvm;
24
25
namespace {
26
27
/// \brief Helper struct which prints trimmed and aligned columns.
28
struct Column {
29
  enum TrimKind { NoTrim, WidthTrim, RightTrim };
30
31
  enum AlignmentKind { LeftAlignment, RightAlignment };
32
33
  StringRef Str;
34
  unsigned Width;
35
  TrimKind Trim;
36
  AlignmentKind Alignment;
37
38
  Column(StringRef Str, unsigned Width)
39
220
      : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
40
41
57
  Column &set(TrimKind Value) {
42
57
    Trim = Value;
43
57
    return *this;
44
57
  }
45
46
147
  Column &set(AlignmentKind Value) {
47
147
    Alignment = Value;
48
147
    return *this;
49
147
  }
50
51
220
  void render(raw_ostream &OS) const {
52
220
    if (Str.size() <= Width) {
53
220
      if (Alignment == RightAlignment) {
54
147
        OS.indent(Width - Str.size());
55
147
        OS << Str;
56
147
        return;
57
147
      }
58
73
      OS << Str;
59
73
      OS.indent(Width - Str.size());
60
73
      return;
61
73
    }
62
0
63
0
    switch (Trim) {
64
0
    case NoTrim:
65
0
      OS << Str;
66
0
      break;
67
0
    case WidthTrim:
68
0
      OS << Str.substr(0, Width);
69
0
      break;
70
0
    case RightTrim:
71
0
      OS << Str.substr(0, Width - 3) << "...";
72
0
      break;
73
0
    }
74
0
  }
75
};
76
77
220
raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
78
220
  Value.render(OS);
79
220
  return OS;
80
220
}
81
82
16
Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
83
84
template <typename T>
85
204
Column column(StringRef Str, unsigned Width, const T &Value) {
86
204
  return Column(Str, Width).set(Value);
87
204
}
CoverageReport.cpp:(anonymous namespace)::Column (anonymous namespace)::column<(anonymous namespace)::Column::TrimKind>(llvm::StringRef, unsigned int, (anonymous namespace)::Column::TrimKind const&)
Line
Count
Source
85
57
Column column(StringRef Str, unsigned Width, const T &Value) {
86
57
  return Column(Str, Width).set(Value);
87
57
}
CoverageReport.cpp:(anonymous namespace)::Column (anonymous namespace)::column<(anonymous namespace)::Column::AlignmentKind>(llvm::StringRef, unsigned int, (anonymous namespace)::Column::AlignmentKind const&)
Line
Count
Source
85
147
Column column(StringRef Str, unsigned Width, const T &Value) {
86
147
  return Column(Str, Width).set(Value);
87
147
}
88
89
// Specify the default column widths.
90
size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10,
91
                              16, 16, 10, 12, 18, 10};
92
size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
93
94
/// \brief Adjust column widths to fit long file paths and function names.
95
void adjustColumnWidths(ArrayRef<StringRef> Files,
96
16
                        ArrayRef<StringRef> Functions) {
97
16
  for (StringRef Filename : Files)
98
35
    FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
99
16
  for (StringRef Funcname : Functions)
100
7
    FunctionReportColumns[0] =
101
7
        std::max(FunctionReportColumns[0], Funcname.size());
102
16
}
103
104
/// \brief Prints a horizontal divider long enough to cover the given column
105
/// widths.
106
32
void renderDivider(ArrayRef<size_t> ColumnWidths, raw_ostream &OS) {
107
32
  size_t Length = std::accumulate(ColumnWidths.begin(), ColumnWidths.end(), 0);
108
5.65k
  for (size_t I = 0; I < Length; 
++I5.62k
)
109
5.62k
    OS << '-';
110
32
}
111
112
/// \brief Return the color which correponds to the coverage percentage of a
113
/// certain metric.
114
template <typename T>
115
228
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
116
228
  if (Info.isFullyCovered())
117
134
    return raw_ostream::GREEN;
118
94
  return Info.getPercentCovered() >= 80.0 ? 
raw_ostream::YELLOW22
119
94
                                          : 
raw_ostream::RED72
;
120
94
}
CoverageReport.cpp:llvm::raw_ostream::Colors (anonymous namespace)::determineCoveragePercentageColor<llvm::RegionCoverageInfo>(llvm::RegionCoverageInfo const&)
Line
Count
Source
115
66
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
116
66
  if (Info.isFullyCovered())
117
30
    return raw_ostream::GREEN;
118
36
  return Info.getPercentCovered() >= 80.0 ? 
raw_ostream::YELLOW5
119
36
                                          : 
raw_ostream::RED31
;
120
36
}
CoverageReport.cpp:llvm::raw_ostream::Colors (anonymous namespace)::determineCoveragePercentageColor<llvm::FunctionCoverageInfo>(llvm::FunctionCoverageInfo const&)
Line
Count
Source
115
96
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
116
96
  if (Info.isFullyCovered())
117
72
    return raw_ostream::GREEN;
118
24
  return Info.getPercentCovered() >= 80.0 ? 
raw_ostream::YELLOW6
119
24
                                          : 
raw_ostream::RED18
;
120
24
}
CoverageReport.cpp:llvm::raw_ostream::Colors (anonymous namespace)::determineCoveragePercentageColor<llvm::LineCoverageInfo>(llvm::LineCoverageInfo const&)
Line
Count
Source
115
66
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
116
66
  if (Info.isFullyCovered())
117
32
    return raw_ostream::GREEN;
118
34
  return Info.getPercentCovered() >= 80.0 ? 
raw_ostream::YELLOW11
119
34
                                          : 
raw_ostream::RED23
;
120
34
}
121
122
/// \brief Get the number of redundant path components in each path in \p Paths.
123
14
unsigned getNumRedundantPathComponents(ArrayRef<std::string> Paths) {
124
14
  // To start, set the number of redundant path components to the maximum
125
14
  // possible value.
126
14
  SmallVector<StringRef, 8> FirstPathComponents{sys::path::begin(Paths[0]),
127
14
                                                sys::path::end(Paths[0])};
128
14
  unsigned NumRedundant = FirstPathComponents.size();
129
14
130
62
  for (unsigned I = 1, E = Paths.size(); NumRedundant > 0 && I < E; 
++I48
) {
131
48
    StringRef Path = Paths[I];
132
48
    for (const auto &Component :
133
187
         enumerate(make_range(sys::path::begin(Path), sys::path::end(Path)))) {
134
187
      // Do not increase the number of redundant components: that would remove
135
187
      // useful parts of already-visited paths.
136
187
      if (Component.index() >= NumRedundant)
137
34
        break;
138
153
139
153
      // Lower the number of redundant components when there's a mismatch
140
153
      // between the first path, and the path under consideration.
141
153
      if (FirstPathComponents[Component.index()] != Component.value()) {
142
14
        NumRedundant = Component.index();
143
14
        break;
144
14
      }
145
153
    }
146
48
  }
147
14
148
14
  return NumRedundant;
149
14
}
150
151
/// \brief Determine the length of the longest redundant prefix of the paths in
152
/// \p Paths.
153
32
unsigned getRedundantPrefixLen(ArrayRef<std::string> Paths) {
154
32
  // If there's at most one path, no path components are redundant.
155
32
  if (Paths.size() <= 1)
156
18
    return 0;
157
14
158
14
  unsigned PrefixLen = 0;
159
14
  unsigned NumRedundant = getNumRedundantPathComponents(Paths);
160
14
  auto Component = sys::path::begin(Paths[0]);
161
51
  for (unsigned I = 0; I < NumRedundant; 
++I37
) {
162
37
    auto LastComponent = Component;
163
37
    ++Component;
164
37
    PrefixLen += Component - LastComponent;
165
37
  }
166
14
  return PrefixLen;
167
14
}
168
169
} // end anonymous namespace
170
171
namespace llvm {
172
173
void CoverageReport::render(const FileCoverageSummary &File,
174
48
                            raw_ostream &OS) const {
175
48
  auto FileCoverageColor =
176
48
      determineCoveragePercentageColor(File.RegionCoverage);
177
48
  auto FuncCoverageColor =
178
48
      determineCoveragePercentageColor(File.FunctionCoverage);
179
48
  auto InstantiationCoverageColor =
180
48
      determineCoveragePercentageColor(File.InstantiationCoverage);
181
48
  auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
182
48
  SmallString<256> FileName = File.Name;
183
48
  sys::path::remove_dots(FileName, /*remove_dot_dots=*/true);
184
48
  sys::path::native(FileName);
185
48
  OS << column(FileName, FileReportColumns[0], Column::NoTrim);
186
48
187
48
  if (Options.ShowRegionSummary) {
188
48
    OS << format("%*u", FileReportColumns[1],
189
48
                 (unsigned)File.RegionCoverage.getNumRegions());
190
48
    Options.colored_ostream(OS, FileCoverageColor)
191
48
        << format("%*u", FileReportColumns[2],
192
48
                  (unsigned)(File.RegionCoverage.getNumRegions() -
193
48
                             File.RegionCoverage.getCovered()));
194
48
    if (File.RegionCoverage.getNumRegions())
195
46
      Options.colored_ostream(OS, FileCoverageColor)
196
46
          << format("%*.2f", FileReportColumns[3] - 1,
197
46
                    File.RegionCoverage.getPercentCovered())
198
46
          << '%';
199
2
    else
200
2
      OS << column("-", FileReportColumns[3], Column::RightAlignment);
201
48
  }
202
48
203
48
  OS << format("%*u", FileReportColumns[4],
204
48
               (unsigned)File.FunctionCoverage.getNumFunctions());
205
48
  OS << format("%*u", FileReportColumns[5],
206
48
               (unsigned)(File.FunctionCoverage.getNumFunctions() -
207
48
                          File.FunctionCoverage.getExecuted()));
208
48
  if (File.FunctionCoverage.getNumFunctions())
209
46
    Options.colored_ostream(OS, FuncCoverageColor)
210
46
        << format("%*.2f", FileReportColumns[6] - 1,
211
46
                  File.FunctionCoverage.getPercentCovered())
212
46
        << '%';
213
2
  else
214
2
    OS << column("-", FileReportColumns[6], Column::RightAlignment);
215
48
216
48
  if (Options.ShowInstantiationSummary) {
217
2
    OS << format("%*u", FileReportColumns[7],
218
2
                 (unsigned)File.InstantiationCoverage.getNumFunctions());
219
2
    OS << format("%*u", FileReportColumns[8],
220
2
                 (unsigned)(File.InstantiationCoverage.getNumFunctions() -
221
2
                            File.InstantiationCoverage.getExecuted()));
222
2
    if (File.InstantiationCoverage.getNumFunctions())
223
2
      Options.colored_ostream(OS, InstantiationCoverageColor)
224
2
          << format("%*.2f", FileReportColumns[9] - 1,
225
2
                    File.InstantiationCoverage.getPercentCovered())
226
2
          << '%';
227
0
    else
228
0
      OS << column("-", FileReportColumns[9], Column::RightAlignment);
229
2
  }
230
48
231
48
  OS << format("%*u", FileReportColumns[10],
232
48
               (unsigned)File.LineCoverage.getNumLines());
233
48
  Options.colored_ostream(OS, LineCoverageColor) << format(
234
48
      "%*u", FileReportColumns[11], (unsigned)(File.LineCoverage.getNumLines() -
235
48
                                               File.LineCoverage.getCovered()));
236
48
  if (File.LineCoverage.getNumLines())
237
46
    Options.colored_ostream(OS, LineCoverageColor)
238
46
        << format("%*.2f", FileReportColumns[12] - 1,
239
46
                  File.LineCoverage.getPercentCovered())
240
46
        << '%';
241
2
  else
242
2
    OS << column("-", FileReportColumns[12], Column::RightAlignment);
243
48
  OS << "\n";
244
48
}
245
246
void CoverageReport::render(const FunctionCoverageSummary &Function,
247
                            const DemangleCache &DC,
248
9
                            raw_ostream &OS) const {
249
9
  auto FuncCoverageColor =
250
9
      determineCoveragePercentageColor(Function.RegionCoverage);
251
9
  auto LineCoverageColor =
252
9
      determineCoveragePercentageColor(Function.LineCoverage);
253
9
  OS << column(DC.demangle(Function.Name), FunctionReportColumns[0],
254
9
               Column::RightTrim)
255
9
     << format("%*u", FunctionReportColumns[1],
256
9
               (unsigned)Function.RegionCoverage.getNumRegions());
257
9
  Options.colored_ostream(OS, FuncCoverageColor)
258
9
      << format("%*u", FunctionReportColumns[2],
259
9
                (unsigned)(Function.RegionCoverage.getNumRegions() -
260
9
                           Function.RegionCoverage.getCovered()));
261
9
  Options.colored_ostream(
262
9
      OS, determineCoveragePercentageColor(Function.RegionCoverage))
263
9
      << format("%*.2f", FunctionReportColumns[3] - 1,
264
9
                Function.RegionCoverage.getPercentCovered())
265
9
      << '%';
266
9
  OS << format("%*u", FunctionReportColumns[4],
267
9
               (unsigned)Function.LineCoverage.getNumLines());
268
9
  Options.colored_ostream(OS, LineCoverageColor)
269
9
      << format("%*u", FunctionReportColumns[5],
270
9
                (unsigned)(Function.LineCoverage.getNumLines() -
271
9
                           Function.LineCoverage.getCovered()));
272
9
  Options.colored_ostream(
273
9
      OS, determineCoveragePercentageColor(Function.LineCoverage))
274
9
      << format("%*.2f", FunctionReportColumns[6] - 1,
275
9
                Function.LineCoverage.getPercentCovered())
276
9
      << '%';
277
9
  OS << "\n";
278
9
}
279
280
void CoverageReport::renderFunctionReports(ArrayRef<std::string> Files,
281
                                           const DemangleCache &DC,
282
2
                                           raw_ostream &OS) {
283
2
  bool isFirst = true;
284
2
  for (StringRef Filename : Files) {
285
2
    auto Functions = Coverage.getCoveredFunctions(Filename);
286
2
287
2
    if (isFirst)
288
2
      isFirst = false;
289
0
    else
290
0
      OS << "\n";
291
2
292
2
    std::vector<StringRef> Funcnames;
293
2
    for (const auto &F : Functions)
294
7
      Funcnames.emplace_back(DC.demangle(F.Name));
295
2
    adjustColumnWidths({}, Funcnames);
296
2
297
2
    OS << "File '" << Filename << "':\n";
298
2
    OS << column("Name", FunctionReportColumns[0])
299
2
       << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
300
2
       << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
301
2
       << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
302
2
       << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
303
2
       << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
304
2
       << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
305
2
    OS << "\n";
306
2
    renderDivider(FunctionReportColumns, OS);
307
2
    OS << "\n";
308
2
    FunctionCoverageSummary Totals("TOTAL");
309
7
    for (const auto &F : Functions) {
310
7
      auto Function = FunctionCoverageSummary::get(Coverage, F);
311
7
      ++Totals.ExecutionCount;
312
7
      Totals.RegionCoverage += Function.RegionCoverage;
313
7
      Totals.LineCoverage += Function.LineCoverage;
314
7
      render(Function, DC, OS);
315
7
    }
316
2
    if (Totals.ExecutionCount) {
317
2
      renderDivider(FunctionReportColumns, OS);
318
2
      OS << "\n";
319
2
      render(Totals, DC, OS);
320
2
    }
321
2
  }
322
2
}
323
324
void CoverageReport::prepareSingleFileReport(const StringRef Filename,
325
    const coverage::CoverageMapping *Coverage,
326
    const CoverageViewOptions &Options, const unsigned LCP,
327
80
    FileCoverageSummary *FileReport, const CoverageFilter *Filters) {
328
91
  for (const auto &Group : Coverage->getInstantiationGroups(Filename)) {
329
91
    std::vector<FunctionCoverageSummary> InstantiationSummaries;
330
112
    for (const coverage::FunctionRecord *F : Group.getInstantiations()) {
331
112
      if (!Filters->matches(*Coverage, *F))
332
10
        continue;
333
102
      auto InstantiationSummary = FunctionCoverageSummary::get(*Coverage, *F);
334
102
      FileReport->addInstantiation(InstantiationSummary);
335
102
      InstantiationSummaries.push_back(InstantiationSummary);
336
102
    }
337
91
    if (InstantiationSummaries.empty())
338
8
      continue;
339
83
340
83
    auto GroupSummary =
341
83
        FunctionCoverageSummary::get(Group, InstantiationSummaries);
342
83
343
83
    if (Options.Debug)
344
0
      outs() << "InstantiationGroup: " << GroupSummary.Name << " with "
345
0
             << "size = " << Group.size() << "\n";
346
83
347
83
    FileReport->addFunction(GroupSummary);
348
83
  }
349
80
}
350
351
std::vector<FileCoverageSummary> CoverageReport::prepareFileReports(
352
    const coverage::CoverageMapping &Coverage, FileCoverageSummary &Totals,
353
    ArrayRef<std::string> Files, const CoverageViewOptions &Options,
354
32
    const CoverageFilter &Filters) {
355
32
  unsigned LCP = getRedundantPrefixLen(Files);
356
32
  auto NumThreads = Options.NumThreads;
357
32
358
32
  // If NumThreads is not specified, auto-detect a good default.
359
32
  if (NumThreads == 0)
360
23
    NumThreads =
361
23
        std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
362
23
                              unsigned(Files.size())));
363
32
364
32
  ThreadPool Pool(NumThreads);
365
32
366
32
  std::vector<FileCoverageSummary> FileReports;
367
32
  FileReports.reserve(Files.size());
368
32
369
80
  for (StringRef Filename : Files) {
370
80
    FileReports.emplace_back(Filename.drop_front(LCP));
371
80
    Pool.async(&CoverageReport::prepareSingleFileReport, Filename,
372
80
               &Coverage, Options, LCP, &FileReports.back(), &Filters);
373
80
  }
374
32
  Pool.wait();
375
32
376
32
  for (const auto &FileReport : FileReports)
377
80
    Totals += FileReport;
378
32
379
32
  return FileReports;
380
32
}
381
382
6
void CoverageReport::renderFileReports(raw_ostream &OS) const {
383
6
  std::vector<std::string> UniqueSourceFiles;
384
6
  for (StringRef SF : Coverage.getUniqueSourceFiles())
385
19
    UniqueSourceFiles.emplace_back(SF.str());
386
6
  renderFileReports(OS, UniqueSourceFiles);
387
6
}
388
389
void CoverageReport::renderFileReports(
390
6
    raw_ostream &OS, ArrayRef<std::string> Files) const {
391
6
  renderFileReports(OS, Files, CoverageFiltersMatchAll());
392
6
}
393
394
void CoverageReport::renderFileReports(
395
    raw_ostream &OS, ArrayRef<std::string> Files,
396
14
    const CoverageFiltersMatchAll &Filters) const {
397
14
  FileCoverageSummary Totals("TOTAL");
398
14
  auto FileReports =
399
14
      prepareFileReports(Coverage, Totals, Files, Options, Filters);
400
14
401
14
  std::vector<StringRef> Filenames;
402
14
  for (const FileCoverageSummary &FCS : FileReports)
403
35
    Filenames.emplace_back(FCS.Name);
404
14
  adjustColumnWidths(Filenames, {});
405
14
406
14
  OS << column("Filename", FileReportColumns[0]);
407
14
  if (Options.ShowRegionSummary)
408
14
    OS << column("Regions", FileReportColumns[1], Column::RightAlignment)
409
14
       << column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
410
14
       << column("Cover", FileReportColumns[3], Column::RightAlignment);
411
14
  OS << column("Functions", FileReportColumns[4], Column::RightAlignment)
412
14
     << column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
413
14
     << column("Executed", FileReportColumns[6], Column::RightAlignment);
414
14
  if (Options.ShowInstantiationSummary)
415
1
    OS << column("Instantiations", FileReportColumns[7], Column::RightAlignment)
416
1
       << column("Missed Insts.", FileReportColumns[8], Column::RightAlignment)
417
1
       << column("Executed", FileReportColumns[9], Column::RightAlignment);
418
14
  OS << column("Lines", FileReportColumns[10], Column::RightAlignment)
419
14
     << column("Missed Lines", FileReportColumns[11], Column::RightAlignment)
420
14
     << column("Cover", FileReportColumns[12], Column::RightAlignment) << "\n";
421
14
  renderDivider(FileReportColumns, OS);
422
14
  OS << "\n";
423
14
424
14
  bool EmptyFiles = false;
425
35
  for (const FileCoverageSummary &FCS : FileReports) {
426
35
    if (FCS.FunctionCoverage.getNumFunctions())
427
32
      render(FCS, OS);
428
3
    else
429
3
      EmptyFiles = true;
430
35
  }
431
14
432
14
  if (EmptyFiles && 
Filters.empty()3
) {
433
2
    OS << "\n"
434
2
       << "Files which contain no functions:\n";
435
2
436
2
    for (const FileCoverageSummary &FCS : FileReports)
437
4
      if (!FCS.FunctionCoverage.getNumFunctions())
438
2
        render(FCS, OS);
439
2
  }
440
14
441
14
  renderDivider(FileReportColumns, OS);
442
14
  OS << "\n";
443
14
  render(Totals, OS);
444
14
}
445
446
} // end namespace llvm