Coverage Report

Created: 2019-05-19 14:56

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