Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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/FileSystem.h"
18
#include "llvm/Support/Format.h"
19
#include "llvm/Support/Path.h"
20
#include <numeric>
21
22
using namespace llvm;
23
24
namespace {
25
26
/// \brief 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
303
      : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
39
40
73
  Column &set(TrimKind Value) {
41
73
    Trim = Value;
42
73
    return *this;
43
73
  }
44
45
207
  Column &set(AlignmentKind Value) {
46
207
    Alignment = Value;
47
207
    return *this;
48
207
  }
49
50
303
  void render(raw_ostream &OS) const {
51
303
    if (
Str.size() <= Width303
) {
52
303
      if (
Alignment == RightAlignment303
) {
53
207
        OS.indent(Width - Str.size());
54
207
        OS << Str;
55
207
        return;
56
207
      }
57
96
      OS << Str;
58
96
      OS.indent(Width - Str.size());
59
96
      return;
60
96
    }
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
303
raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
77
303
  Value.render(OS);
78
303
  return OS;
79
303
}
80
81
23
Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
82
83
template <typename T>
84
280
Column column(StringRef Str, unsigned Width, const T &Value) {
85
280
  return Column(Str, Width).set(Value);
86
280
}
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
73
Column column(StringRef Str, unsigned Width, const T &Value) {
85
73
  return Column(Str, Width).set(Value);
86
73
}
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
207
Column column(StringRef Str, unsigned Width, const T &Value) {
85
207
  return Column(Str, Width).set(Value);
86
207
}
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
/// \brief Adjust column widths to fit long file paths and function names.
94
void adjustColumnWidths(ArrayRef<StringRef> Files,
95
23
                        ArrayRef<StringRef> Functions) {
96
23
  for (StringRef Filename : Files)
97
39
    FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
98
23
  for (StringRef Funcname : Functions)
99
11
    FunctionReportColumns[0] =
100
11
        std::max(FunctionReportColumns[0], Funcname.size());
101
23
}
102
103
/// \brief Prints a horizontal divider long enough to cover the given column
104
/// widths.
105
46
void renderDivider(ArrayRef<size_t> ColumnWidths, raw_ostream &OS) {
106
46
  size_t Length = std::accumulate(ColumnWidths.begin(), ColumnWidths.end(), 0);
107
8.19k
  for (size_t I = 0; 
I < Length8.19k
;
++I8.15k
)
108
8.15k
    OS << '-';
109
46
}
110
111
/// \brief Return the color which correponds to the coverage percentage of a
112
/// certain metric.
113
template <typename T>
114
292
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
115
292
  if (Info.isFullyCovered())
116
164
    return raw_ostream::GREEN;
117
128
  
return Info.getPercentCovered() >= 80.0 ? 128
raw_ostream::YELLOW26
118
102
                                          : raw_ostream::RED;
119
292
}
CoverageReport.cpp:llvm::raw_ostream::Colors (anonymous namespace)::determineCoveragePercentageColor<llvm::RegionCoverageInfo>(llvm::RegionCoverageInfo const&)
Line
Count
Source
114
87
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
115
87
  if (Info.isFullyCovered())
116
36
    return raw_ostream::GREEN;
117
51
  
return Info.getPercentCovered() >= 80.0 ? 51
raw_ostream::YELLOW5
118
46
                                          : raw_ostream::RED;
119
87
}
CoverageReport.cpp:llvm::raw_ostream::Colors (anonymous namespace)::determineCoveragePercentageColor<llvm::FunctionCoverageInfo>(llvm::FunctionCoverageInfo const&)
Line
Count
Source
114
118
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
115
118
  if (Info.isFullyCovered())
116
90
    return raw_ostream::GREEN;
117
28
  
return Info.getPercentCovered() >= 80.0 ? 28
raw_ostream::YELLOW4
118
24
                                          : raw_ostream::RED;
119
118
}
CoverageReport.cpp:llvm::raw_ostream::Colors (anonymous namespace)::determineCoveragePercentageColor<llvm::LineCoverageInfo>(llvm::LineCoverageInfo const&)
Line
Count
Source
114
87
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
115
87
  if (Info.isFullyCovered())
116
38
    return raw_ostream::GREEN;
117
49
  
return Info.getPercentCovered() >= 80.0 ? 49
raw_ostream::YELLOW17
118
32
                                          : raw_ostream::RED;
119
87
}
120
121
/// \brief Get the number of redundant path components in each path in \p Paths.
122
11
unsigned getNumRedundantPathComponents(ArrayRef<std::string> Paths) {
123
11
  // To start, set the number of redundant path components to the maximum
124
11
  // possible value.
125
11
  SmallVector<StringRef, 8> FirstPathComponents{sys::path::begin(Paths[0]),
126
11
                                                sys::path::end(Paths[0])};
127
11
  unsigned NumRedundant = FirstPathComponents.size();
128
11
129
32
  for (unsigned I = 1, E = Paths.size(); 
NumRedundant > 0 && 32
I < E32
;
++I21
) {
130
21
    StringRef Path = Paths[I];
131
21
    for (const auto &Component :
132
68
         enumerate(make_range(sys::path::begin(Path), sys::path::end(Path)))) {
133
68
      // Do not increase the number of redundant components: that would remove
134
68
      // useful parts of already-visited paths.
135
68
      if (Component.index() >= NumRedundant)
136
10
        break;
137
58
138
58
      // Lower the number of redundant components when there's a mismatch
139
58
      // between the first path, and the path under consideration.
140
58
      
if (58
FirstPathComponents[Component.index()] != Component.value()58
) {
141
11
        NumRedundant = Component.index();
142
11
        break;
143
11
      }
144
68
    }
145
21
  }
146
11
147
11
  return NumRedundant;
148
11
}
149
150
/// \brief Determine the length of the longest redundant prefix of the paths in
151
/// \p Paths.
152
42
unsigned getRedundantPrefixLen(ArrayRef<std::string> Paths) {
153
42
  // If there's at most one path, no path components are redundant.
154
42
  if (Paths.size() <= 1)
155
31
    return 0;
156
11
157
11
  unsigned PrefixLen = 0;
158
11
  unsigned NumRedundant = getNumRedundantPathComponents(Paths);
159
11
  auto Component = sys::path::begin(Paths[0]);
160
36
  for (unsigned I = 0; 
I < NumRedundant36
;
++I25
) {
161
25
    auto LastComponent = Component;
162
25
    ++Component;
163
25
    PrefixLen += Component - LastComponent;
164
25
  }
165
42
  return PrefixLen;
166
42
}
167
168
} // end anonymous namespace
169
170
namespace llvm {
171
172
void CoverageReport::render(const FileCoverageSummary &File,
173
59
                            raw_ostream &OS) const {
174
59
  auto FileCoverageColor =
175
59
      determineCoveragePercentageColor(File.RegionCoverage);
176
59
  auto FuncCoverageColor =
177
59
      determineCoveragePercentageColor(File.FunctionCoverage);
178
59
  auto InstantiationCoverageColor =
179
59
      determineCoveragePercentageColor(File.InstantiationCoverage);
180
59
  auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
181
59
  SmallString<256> FileName = File.Name;
182
59
  sys::path::remove_dots(FileName, /*remove_dot_dots=*/true);
183
59
  sys::path::native(FileName);
184
59
  OS << column(FileName, FileReportColumns[0], Column::NoTrim);
185
59
186
59
  if (
Options.ShowRegionSummary59
) {
187
59
    OS << format("%*u", FileReportColumns[1],
188
59
                 (unsigned)File.RegionCoverage.getNumRegions());
189
59
    Options.colored_ostream(OS, FileCoverageColor)
190
59
        << format("%*u", FileReportColumns[2],
191
59
                  (unsigned)(File.RegionCoverage.getNumRegions() -
192
59
                             File.RegionCoverage.getCovered()));
193
59
    if (File.RegionCoverage.getNumRegions())
194
57
      Options.colored_ostream(OS, FileCoverageColor)
195
57
          << format("%*.2f", FileReportColumns[3] - 1,
196
57
                    File.RegionCoverage.getPercentCovered())
197
57
          << '%';
198
59
    else
199
2
      OS << column("-", FileReportColumns[3], Column::RightAlignment);
200
59
  }
201
59
202
59
  OS << format("%*u", FileReportColumns[4],
203
59
               (unsigned)File.FunctionCoverage.getNumFunctions());
204
59
  OS << format("%*u", FileReportColumns[5],
205
59
               (unsigned)(File.FunctionCoverage.getNumFunctions() -
206
59
                          File.FunctionCoverage.getExecuted()));
207
59
  if (File.FunctionCoverage.getNumFunctions())
208
57
    Options.colored_ostream(OS, FuncCoverageColor)
209
57
        << format("%*.2f", FileReportColumns[6] - 1,
210
57
                  File.FunctionCoverage.getPercentCovered())
211
57
        << '%';
212
59
  else
213
2
    OS << column("-", FileReportColumns[6], Column::RightAlignment);
214
59
215
59
  if (
Options.ShowInstantiationSummary59
) {
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
2
    else
227
0
      OS << column("-", FileReportColumns[9], Column::RightAlignment);
228
2
  }
229
59
230
59
  OS << format("%*u", FileReportColumns[10],
231
59
               (unsigned)File.LineCoverage.getNumLines());
232
59
  Options.colored_ostream(OS, LineCoverageColor) << format(
233
59
      "%*u", FileReportColumns[11], (unsigned)(File.LineCoverage.getNumLines() -
234
59
                                               File.LineCoverage.getCovered()));
235
59
  if (File.LineCoverage.getNumLines())
236
57
    Options.colored_ostream(OS, LineCoverageColor)
237
57
        << format("%*.2f", FileReportColumns[12] - 1,
238
57
                  File.LineCoverage.getPercentCovered())
239
57
        << '%';
240
59
  else
241
2
    OS << column("-", FileReportColumns[12], Column::RightAlignment);
242
59
  OS << "\n";
243
59
}
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
3
    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.ExecutionCount3
) {
316
3
      renderDivider(FunctionReportColumns, OS);
317
3
      OS << "\n";
318
3
      render(Totals, DC, OS);
319
3
    }
320
3
  }
321
3
}
322
323
std::vector<FileCoverageSummary> CoverageReport::prepareFileReports(
324
    const coverage::CoverageMapping &Coverage, FileCoverageSummary &Totals,
325
    ArrayRef<std::string> Files, const CoverageViewOptions &Options,
326
42
    const CoverageFilter &Filters) {
327
42
  std::vector<FileCoverageSummary> FileReports;
328
42
  unsigned LCP = getRedundantPrefixLen(Files);
329
42
330
63
  for (StringRef Filename : Files) {
331
63
    FileCoverageSummary Summary(Filename.drop_front(LCP));
332
63
333
90
    for (const auto &Group : Coverage.getInstantiationGroups(Filename)) {
334
90
      std::vector<FunctionCoverageSummary> InstantiationSummaries;
335
102
      for (const coverage::FunctionRecord *F : Group.getInstantiations()) {
336
102
        if (!Filters.matches(Coverage, *F))
337
10
          continue;
338
92
        auto InstantiationSummary = FunctionCoverageSummary::get(Coverage, *F);
339
92
        Summary.addInstantiation(InstantiationSummary);
340
92
        Totals.addInstantiation(InstantiationSummary);
341
92
        InstantiationSummaries.push_back(InstantiationSummary);
342
92
      }
343
90
      if (InstantiationSummaries.empty())
344
8
        continue;
345
82
346
82
      auto GroupSummary =
347
82
          FunctionCoverageSummary::get(Group, InstantiationSummaries);
348
82
349
82
      if (Options.Debug)
350
2
        outs() << "InstantiationGroup: " << GroupSummary.Name << " with "
351
2
               << "size = " << Group.size() << "\n";
352
90
353
90
      Summary.addFunction(GroupSummary);
354
90
      Totals.addFunction(GroupSummary);
355
90
    }
356
63
357
63
    FileReports.push_back(Summary);
358
63
  }
359
42
360
42
  return FileReports;
361
42
}
362
363
10
void CoverageReport::renderFileReports(raw_ostream &OS) const {
364
10
  std::vector<std::string> UniqueSourceFiles;
365
10
  for (StringRef SF : Coverage.getUniqueSourceFiles())
366
28
    UniqueSourceFiles.emplace_back(SF.str());
367
10
  renderFileReports(OS, UniqueSourceFiles, CoverageFiltersMatchAll());
368
10
}
369
370
void CoverageReport::renderFileReports(raw_ostream &OS,
371
                                       ArrayRef<std::string> Files,
372
20
                                       const CoverageFilter &Filters) const {
373
20
  FileCoverageSummary Totals("TOTAL");
374
20
  auto FileReports =
375
20
      prepareFileReports(Coverage, Totals, Files, Options, Filters);
376
20
377
20
  std::vector<StringRef> Filenames;
378
20
  for (const FileCoverageSummary &FCS : FileReports)
379
39
    Filenames.emplace_back(FCS.Name);
380
20
  adjustColumnWidths(Filenames, {});
381
20
382
20
  OS << column("Filename", FileReportColumns[0]);
383
20
  if (Options.ShowRegionSummary)
384
20
    OS << column("Regions", FileReportColumns[1], Column::RightAlignment)
385
20
       << column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
386
20
       << column("Cover", FileReportColumns[3], Column::RightAlignment);
387
20
  OS << column("Functions", FileReportColumns[4], Column::RightAlignment)
388
20
     << column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
389
20
     << column("Executed", FileReportColumns[6], Column::RightAlignment);
390
20
  if (Options.ShowInstantiationSummary)
391
1
    OS << column("Instantiations", FileReportColumns[7], Column::RightAlignment)
392
1
       << column("Missed Insts.", FileReportColumns[8], Column::RightAlignment)
393
1
       << column("Executed", FileReportColumns[9], Column::RightAlignment);
394
20
  OS << column("Lines", FileReportColumns[10], Column::RightAlignment)
395
20
     << column("Missed Lines", FileReportColumns[11], Column::RightAlignment)
396
20
     << column("Cover", FileReportColumns[12], Column::RightAlignment) << "\n";
397
20
  renderDivider(FileReportColumns, OS);
398
20
  OS << "\n";
399
20
400
20
  bool EmptyFiles = false;
401
39
  for (const FileCoverageSummary &FCS : FileReports) {
402
39
    if (FCS.FunctionCoverage.getNumFunctions())
403
37
      render(FCS, OS);
404
39
    else
405
2
      EmptyFiles = true;
406
39
  }
407
20
408
20
  if (
EmptyFiles20
) {
409
2
    OS << "\n"
410
2
       << "Files which contain no functions:\n";
411
2
412
2
    for (const FileCoverageSummary &FCS : FileReports)
413
4
      
if (4
!FCS.FunctionCoverage.getNumFunctions()4
)
414
2
        render(FCS, OS);
415
2
  }
416
20
417
20
  renderDivider(FileReportColumns, OS);
418
20
  OS << "\n";
419
20
  render(Totals, OS);
420
20
}
421
422
} // end namespace llvm