Coverage Report

Created: 2017-09-21 03:39

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