Coverage Report

Created: 2019-07-24 05:18

/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
478
      : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
39
40
128
  Column &set(TrimKind Value) {
41
128
    Trim = Value;
42
128
    return *this;
43
128
  }
44
45
315
  Column &set(AlignmentKind Value) {
46
315
    Alignment = Value;
47
315
    return *this;
48
315
  }
49
50
478
  void render(raw_ostream &OS) const {
51
478
    if (Str.size() <= Width) {
52
478
      if (Alignment == RightAlignment) {
53
315
        OS.indent(Width - Str.size());
54
315
        OS << Str;
55
315
        return;
56
315
      }
57
163
      OS << Str;
58
163
      OS.indent(Width - Str.size());
59
163
      return;
60
163
    }
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
478
raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
77
478
  Value.render(OS);
78
478
  return OS;
79
478
}
80
81
35
Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
82
83
template <typename T>
84
443
Column column(StringRef Str, unsigned Width, const T &Value) {
85
443
  return Column(Str, Width).set(Value);
86
443
}
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
128
Column column(StringRef Str, unsigned Width, const T &Value) {
85
128
  return Column(Str, Width).set(Value);
86
128
}
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
315
Column column(StringRef Str, unsigned Width, const T &Value) {
85
315
  return Column(Str, Width).set(Value);
86
315
}
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
35
                        ArrayRef<StringRef> Functions) {
96
35
  for (StringRef Filename : Files)
97
83
    FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
98
35
  for (StringRef Funcname : Functions)
99
11
    FunctionReportColumns[0] =
100
11
        std::max(FunctionReportColumns[0], Funcname.size());
101
35
}
102
103
/// Prints a horizontal divider long enough to cover the given column
104
/// widths.
105
70
void renderDivider(ArrayRef<size_t> ColumnWidths, raw_ostream &OS) {
106
70
  size_t Length = std::accumulate(ColumnWidths.begin(), ColumnWidths.end(), 0);
107
12.6k
  for (size_t I = 0; I < Length; 
++I12.5k
)
108
12.5k
    OS << '-';
109
70
}
110
111
/// Return the color which correponds to the coverage percentage of a
112
/// certain metric.
113
template <typename T>
114
512
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
115
512
  if (Info.isFullyCovered())
116
334
    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
142
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
115
142
  if (Info.isFullyCovered())
116
73
    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
228
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
115
228
  if (Info.isFullyCovered())
116
180
    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
142
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
115
142
  if (Info.isFullyCovered())
116
81
    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
28
unsigned getNumRedundantPathComponents(ArrayRef<std::string> Paths) {
123
28
  // To start, set the number of redundant path components to the maximum
124
28
  // possible value.
125
28
  SmallVector<StringRef, 8> FirstPathComponents{sys::path::begin(Paths[0]),
126
28
                                                sys::path::end(Paths[0])};
127
28
  unsigned NumRedundant = FirstPathComponents.size();
128
28
129
112
  for (unsigned I = 1, E = Paths.size(); NumRedundant > 0 && I < E; 
++I84
) {
130
84
    StringRef Path = Paths[I];
131
84
    for (const auto &Component :
132
324
         enumerate(make_range(sys::path::begin(Path), sys::path::end(Path)))) {
133
324
      // Do not increase the number of redundant components: that would remove
134
324
      // useful parts of already-visited paths.
135
324
      if (Component.index() >= NumRedundant)
136
55
        break;
137
269
138
269
      // Lower the number of redundant components when there's a mismatch
139
269
      // between the first path, and the path under consideration.
140
269
      if (FirstPathComponents[Component.index()] != Component.value()) {
141
29
        NumRedundant = Component.index();
142
29
        break;
143
29
      }
144
269
    }
145
84
  }
146
28
147
28
  return NumRedundant;
148
28
}
149
150
/// Determine the length of the longest redundant prefix of the paths in
151
/// \p Paths.
152
62
unsigned getRedundantPrefixLen(ArrayRef<std::string> Paths) {
153
62
  // If there's at most one path, no path components are redundant.
154
62
  if (Paths.size() <= 1)
155
34
    return 0;
156
28
157
28
  unsigned PrefixLen = 0;
158
28
  unsigned NumRedundant = getNumRedundantPathComponents(Paths);
159
28
  auto Component = sys::path::begin(Paths[0]);
160
100
  for (unsigned I = 0; I < NumRedundant; 
++I72
) {
161
72
    auto LastComponent = Component;
162
72
    ++Component;
163
72
    PrefixLen += Component - LastComponent;
164
72
  }
165
28
  return PrefixLen;
166
28
}
167
168
} // end anonymous namespace
169
170
namespace llvm {
171
172
void CoverageReport::render(const FileCoverageSummary &File,
173
114
                            raw_ostream &OS) const {
174
114
  auto FileCoverageColor =
175
114
      determineCoveragePercentageColor(File.RegionCoverage);
176
114
  auto FuncCoverageColor =
177
114
      determineCoveragePercentageColor(File.FunctionCoverage);
178
114
  auto InstantiationCoverageColor =
179
114
      determineCoveragePercentageColor(File.InstantiationCoverage);
180
114
  auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
181
114
  SmallString<256> FileName = File.Name;
182
114
  sys::path::remove_dots(FileName, /*remove_dot_dots=*/true);
183
114
  sys::path::native(FileName);
184
114
  OS << column(FileName, FileReportColumns[0], Column::NoTrim);
185
114
186
114
  if (Options.ShowRegionSummary) {
187
114
    OS << format("%*u", FileReportColumns[1],
188
114
                 (unsigned)File.RegionCoverage.getNumRegions());
189
114
    Options.colored_ostream(OS, FileCoverageColor)
190
114
        << format("%*u", FileReportColumns[2],
191
114
                  (unsigned)(File.RegionCoverage.getNumRegions() -
192
114
                             File.RegionCoverage.getCovered()));
193
114
    if (File.RegionCoverage.getNumRegions())
194
112
      Options.colored_ostream(OS, FileCoverageColor)
195
112
          << format("%*.2f", FileReportColumns[3] - 1,
196
112
                    File.RegionCoverage.getPercentCovered())
197
112
          << '%';
198
2
    else
199
2
      OS << column("-", FileReportColumns[3], Column::RightAlignment);
200
114
  }
201
114
202
114
  OS << format("%*u", FileReportColumns[4],
203
114
               (unsigned)File.FunctionCoverage.getNumFunctions());
204
114
  OS << format("%*u", FileReportColumns[5],
205
114
               (unsigned)(File.FunctionCoverage.getNumFunctions() -
206
114
                          File.FunctionCoverage.getExecuted()));
207
114
  if (File.FunctionCoverage.getNumFunctions())
208
112
    Options.colored_ostream(OS, FuncCoverageColor)
209
112
        << format("%*.2f", FileReportColumns[6] - 1,
210
112
                  File.FunctionCoverage.getPercentCovered())
211
112
        << '%';
212
2
  else
213
2
    OS << column("-", FileReportColumns[6], Column::RightAlignment);
214
114
215
114
  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
114
230
114
  OS << format("%*u", FileReportColumns[10],
231
114
               (unsigned)File.LineCoverage.getNumLines());
232
114
  Options.colored_ostream(OS, LineCoverageColor) << format(
233
114
      "%*u", FileReportColumns[11], (unsigned)(File.LineCoverage.getNumLines() -
234
114
                                               File.LineCoverage.getCovered()));
235
114
  if (File.LineCoverage.getNumLines())
236
112
    Options.colored_ostream(OS, LineCoverageColor)
237
112
        << format("%*.2f", FileReportColumns[12] - 1,
238
112
                  File.LineCoverage.getPercentCovered())
239
112
        << '%';
240
2
  else
241
2
    OS << column("-", FileReportColumns[12], Column::RightAlignment);
242
114
  OS << "\n";
243
114
}
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
146
    FileCoverageSummary *FileReport, const CoverageFilter *Filters) {
327
181
  for (const auto &Group : Coverage->getInstantiationGroups(Filename)) {
328
181
    std::vector<FunctionCoverageSummary> InstantiationSummaries;
329
216
    for (const coverage::FunctionRecord *F : Group.getInstantiations()) {
330
216
      if (!Filters->matches(*Coverage, *F))
331
16
        continue;
332
200
      auto InstantiationSummary = FunctionCoverageSummary::get(*Coverage, *F);
333
200
      FileReport->addInstantiation(InstantiationSummary);
334
200
      InstantiationSummaries.push_back(InstantiationSummary);
335
200
    }
336
181
    if (InstantiationSummaries.empty())
337
14
      continue;
338
167
339
167
    auto GroupSummary =
340
167
        FunctionCoverageSummary::get(Group, InstantiationSummaries);
341
167
342
167
    if (Options.Debug)
343
2
      outs() << "InstantiationGroup: " << GroupSummary.Name << " with "
344
2
             << "size = " << Group.size() << "\n";
345
167
346
167
    FileReport->addFunction(GroupSummary);
347
167
  }
348
146
}
349
350
std::vector<FileCoverageSummary> CoverageReport::prepareFileReports(
351
    const coverage::CoverageMapping &Coverage, FileCoverageSummary &Totals,
352
    ArrayRef<std::string> Files, const CoverageViewOptions &Options,
353
62
    const CoverageFilter &Filters) {
354
62
  unsigned LCP = getRedundantPrefixLen(Files);
355
62
  auto NumThreads = Options.NumThreads;
356
62
357
62
  // If NumThreads is not specified, auto-detect a good default.
358
62
  if (NumThreads == 0)
359
51
    NumThreads =
360
51
        std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
361
51
                              unsigned(Files.size())));
362
62
363
62
  ThreadPool Pool(NumThreads);
364
62
365
62
  std::vector<FileCoverageSummary> FileReports;
366
62
  FileReports.reserve(Files.size());
367
62
368
146
  for (StringRef Filename : Files) {
369
146
    FileReports.emplace_back(Filename.drop_front(LCP));
370
146
    Pool.async(&CoverageReport::prepareSingleFileReport, Filename,
371
146
               &Coverage, Options, LCP, &FileReports.back(), &Filters);
372
146
  }
373
62
  Pool.wait();
374
62
375
62
  for (const auto &FileReport : FileReports)
376
146
    Totals += FileReport;
377
62
378
62
  return FileReports;
379
62
}
380
381
void CoverageReport::renderFileReports(
382
16
    raw_ostream &OS, const CoverageFilters &IgnoreFilenameFilters) const {
383
16
  std::vector<std::string> UniqueSourceFiles;
384
56
  for (StringRef SF : Coverage.getUniqueSourceFiles()) {
385
56
    // Apply ignore source files filters.
386
56
    if (!IgnoreFilenameFilters.matchesFilename(SF))
387
51
      UniqueSourceFiles.emplace_back(SF.str());
388
56
  }
389
16
  renderFileReports(OS, UniqueSourceFiles);
390
16
}
391
392
void CoverageReport::renderFileReports(
393
19
    raw_ostream &OS, ArrayRef<std::string> Files) const {
394
19
  renderFileReports(OS, Files, CoverageFiltersMatchAll());
395
19
}
396
397
void CoverageReport::renderFileReports(
398
    raw_ostream &OS, ArrayRef<std::string> Files,
399
32
    const CoverageFiltersMatchAll &Filters) const {
400
32
  FileCoverageSummary Totals("TOTAL");
401
32
  auto FileReports =
402
32
      prepareFileReports(Coverage, Totals, Files, Options, Filters);
403
32
404
32
  std::vector<StringRef> Filenames;
405
32
  for (const FileCoverageSummary &FCS : FileReports)
406
83
    Filenames.emplace_back(FCS.Name);
407
32
  adjustColumnWidths(Filenames, {});
408
32
409
32
  OS << column("Filename", FileReportColumns[0]);
410
32
  if (Options.ShowRegionSummary)
411
32
    OS << column("Regions", FileReportColumns[1], Column::RightAlignment)
412
32
       << column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
413
32
       << column("Cover", FileReportColumns[3], Column::RightAlignment);
414
32
  OS << column("Functions", FileReportColumns[4], Column::RightAlignment)
415
32
     << column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
416
32
     << column("Executed", FileReportColumns[6], Column::RightAlignment);
417
32
  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
32
  OS << column("Lines", FileReportColumns[10], Column::RightAlignment)
422
32
     << column("Missed Lines", FileReportColumns[11], Column::RightAlignment)
423
32
     << column("Cover", FileReportColumns[12], Column::RightAlignment) << "\n";
424
32
  renderDivider(FileReportColumns, OS);
425
32
  OS << "\n";
426
32
427
32
  bool EmptyFiles = false;
428
83
  for (const FileCoverageSummary &FCS : FileReports) {
429
83
    if (FCS.FunctionCoverage.getNumFunctions())
430
80
      render(FCS, OS);
431
3
    else
432
3
      EmptyFiles = true;
433
83
  }
434
32
435
32
  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
32
444
32
  renderDivider(FileReportColumns, OS);
445
32
  OS << "\n";
446
32
  render(Totals, OS);
447
32
}
448
449
} // end namespace llvm