Coverage Report

Created: 2018-10-23 15:26

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