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/SmallString.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
22using namespace llvm;
23
24namespace {
25
26/// Helper struct which prints trimmed and aligned columns.
27struct 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 : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
39
40 Column &set(TrimKind Value) {
41 Trim = Value;
42 return *this;
43 }
44
45 Column &set(AlignmentKind Value) {
46 Alignment = Value;
47 return *this;
48 }
49
50 void render(raw_ostream &OS) const {
51 if (Str.size() <= Width) {
52 if (Alignment == RightAlignment) {
53 OS.indent(NumSpaces: Width - Str.size());
54 OS << Str;
55 return;
56 }
57 OS << Str;
58 OS.indent(NumSpaces: Width - Str.size());
59 return;
60 }
61
62 switch (Trim) {
63 case NoTrim:
64 OS << Str;
65 break;
66 case WidthTrim:
67 OS << Str.substr(Start: 0, N: Width);
68 break;
69 case RightTrim:
70 OS << Str.substr(Start: 0, N: Width - 3) << "...";
71 break;
72 }
73 }
74};
75
76raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
77 Value.render(OS);
78 return OS;
79}
80
81Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
82
83template <typename T>
84Column column(StringRef Str, unsigned Width, const T &Value) {
85 return Column(Str, Width).set(Value);
86}
87
88// Specify the default column widths.
89size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10, 16, 16, 10,
90 12, 18, 10, 12, 18, 10, 20, 21, 10};
91size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8, 10, 8, 8, 20, 8, 8};
92
93/// Adjust column widths to fit long file paths and function names.
94void adjustColumnWidths(ArrayRef<StringRef> Files,
95 ArrayRef<StringRef> Functions) {
96 for (StringRef Filename : Files)
97 FileReportColumns[0] = std::max(a: FileReportColumns[0], b: Filename.size());
98 for (StringRef Funcname : Functions)
99 FunctionReportColumns[0] =
100 std::max(a: FunctionReportColumns[0], b: Funcname.size());
101}
102
103/// Prints a horizontal divider long enough to cover the given column
104/// widths.
105void renderDivider(raw_ostream &OS, const CoverageViewOptions &Options, bool isFileReport) {
106 size_t Length;
107 if (isFileReport) {
108 Length = std::accumulate(first: std::begin(arr&: FileReportColumns), last: std::end(arr&: FileReportColumns), init: 0);
109 if (!Options.ShowRegionSummary)
110 Length -= (FileReportColumns[1] + FileReportColumns[2] + FileReportColumns[3]);
111 if (!Options.ShowInstantiationSummary)
112 Length -= (FileReportColumns[7] + FileReportColumns[8] + FileReportColumns[9]);
113 if (!Options.ShowBranchSummary)
114 Length -= (FileReportColumns[13] + FileReportColumns[14] + FileReportColumns[15]);
115 if (!Options.ShowMCDCSummary)
116 Length -= (FileReportColumns[16] + FileReportColumns[17] + FileReportColumns[18]);
117 } else {
118 Length = std::accumulate(first: std::begin(arr&: FunctionReportColumns), last: std::end(arr&: FunctionReportColumns), init: 0);
119 if (!Options.ShowBranchSummary)
120 Length -= (FunctionReportColumns[7] + FunctionReportColumns[8] + FunctionReportColumns[9]);
121 if (!Options.ShowMCDCSummary)
122 Length -= (FunctionReportColumns[10] + FunctionReportColumns[11] + FunctionReportColumns[12]);
123 }
124 for (size_t I = 0; I < Length; ++I)
125 OS << '-';
126}
127
128/// Return the color which correponds to the coverage percentage of a
129/// certain metric.
130template <typename T>
131raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
132 if (Info.isFullyCovered())
133 return raw_ostream::GREEN;
134 return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
135 : raw_ostream::RED;
136}
137
138/// Get the number of redundant path components in each path in \p Paths.
139unsigned getNumRedundantPathComponents(ArrayRef<std::string> Paths) {
140 // To start, set the number of redundant path components to the maximum
141 // possible value.
142 SmallVector<StringRef, 8> FirstPathComponents{sys::path::begin(path: Paths[0]),
143 sys::path::end(path: Paths[0])};
144 unsigned NumRedundant = FirstPathComponents.size();
145
146 for (unsigned I = 1, E = Paths.size(); NumRedundant > 0 && I < E; ++I) {
147 StringRef Path = Paths[I];
148 for (const auto &Component :
149 enumerate(First: make_range(x: sys::path::begin(path: Path), y: sys::path::end(path: Path)))) {
150 // Do not increase the number of redundant components: that would remove
151 // useful parts of already-visited paths.
152 if (Component.index() >= NumRedundant)
153 break;
154
155 // Lower the number of redundant components when there's a mismatch
156 // between the first path, and the path under consideration.
157 if (FirstPathComponents[Component.index()] != Component.value()) {
158 NumRedundant = Component.index();
159 break;
160 }
161 }
162 }
163
164 return NumRedundant;
165}
166
167/// Determine the length of the longest redundant prefix of the paths in
168/// \p Paths.
169unsigned getRedundantPrefixLen(ArrayRef<std::string> Paths) {
170 // If there's at most one path, no path components are redundant.
171 if (Paths.size() <= 1)
172 return 0;
173
174 unsigned PrefixLen = 0;
175 unsigned NumRedundant = getNumRedundantPathComponents(Paths);
176 auto Component = sys::path::begin(path: Paths[0]);
177 for (unsigned I = 0; I < NumRedundant; ++I) {
178 auto LastComponent = Component;
179 ++Component;
180 PrefixLen += Component - LastComponent;
181 }
182 return PrefixLen;
183}
184
185/// Determine the length of the longest redundant prefix of the substrs starts
186/// from \p LCP in \p Paths. \p Paths can't be empty. If there's only one
187/// element in \p Paths, the length of the substr is returned. Note this is
188/// differnet from the behavior of the function above.
189unsigned getRedundantPrefixLen(ArrayRef<StringRef> Paths, unsigned LCP) {
190 assert(!Paths.empty() && "Paths must have at least one element");
191
192 auto Iter = Paths.begin();
193 auto IterE = Paths.end();
194 auto Prefix = Iter->substr(Start: LCP);
195 while (++Iter != IterE) {
196 auto Other = Iter->substr(Start: LCP);
197 auto Len = std::min(a: Prefix.size(), b: Other.size());
198 for (std::size_t I = 0; I < Len; ++I) {
199 if (Prefix[I] != Other[I]) {
200 Prefix = Prefix.substr(Start: 0, N: I);
201 break;
202 }
203 }
204 }
205
206 for (auto I = Prefix.size(); --I != SIZE_MAX;) {
207 if (Prefix[I] == '/' || Prefix[I] == '\\')
208 return I + 1;
209 }
210
211 return Prefix.size();
212}
213
214} // end anonymous namespace
215
216namespace llvm {
217
218void CoverageReport::render(const FileCoverageSummary &File,
219 raw_ostream &OS) const {
220 auto FileCoverageColor =
221 determineCoveragePercentageColor(Info: File.RegionCoverage);
222 auto FuncCoverageColor =
223 determineCoveragePercentageColor(Info: File.FunctionCoverage);
224 auto InstantiationCoverageColor =
225 determineCoveragePercentageColor(Info: File.InstantiationCoverage);
226 auto LineCoverageColor = determineCoveragePercentageColor(Info: File.LineCoverage);
227 SmallString<256> FileName = File.Name;
228 sys::path::native(path&: FileName);
229
230 // remove_dots will remove trailing slash, so we need to check before it.
231 auto IsDir = FileName.ends_with(Suffix: sys::path::get_separator());
232 sys::path::remove_dots(path&: FileName, /*remove_dot_dot=*/true);
233 if (IsDir)
234 FileName += sys::path::get_separator();
235
236 OS << column(Str: FileName, Width: FileReportColumns[0], Value: Column::NoTrim);
237
238 if (Options.ShowRegionSummary) {
239 OS << format(Fmt: "%*u", Vals: FileReportColumns[1],
240 Vals: (unsigned)File.RegionCoverage.getNumRegions());
241 Options.colored_ostream(OS, Color: FileCoverageColor)
242 << format(Fmt: "%*u", Vals: FileReportColumns[2],
243 Vals: (unsigned)(File.RegionCoverage.getNumRegions() -
244 File.RegionCoverage.getCovered()));
245 if (File.RegionCoverage.getNumRegions())
246 Options.colored_ostream(OS, Color: FileCoverageColor)
247 << format(Fmt: "%*.2f", Vals: FileReportColumns[3] - 1,
248 Vals: File.RegionCoverage.getPercentCovered())
249 << '%';
250 else
251 OS << column(Str: "-", Width: FileReportColumns[3], Value: Column::RightAlignment);
252 }
253
254 if (Options.ShowFunctionSummary) {
255 OS << format(Fmt: "%*u", Vals: FileReportColumns[4],
256 Vals: (unsigned)File.FunctionCoverage.getNumFunctions());
257 OS << format(Fmt: "%*u", Vals: FileReportColumns[5],
258 Vals: (unsigned)(File.FunctionCoverage.getNumFunctions() -
259 File.FunctionCoverage.getExecuted()));
260 if (File.FunctionCoverage.getNumFunctions())
261 Options.colored_ostream(OS, Color: FuncCoverageColor)
262 << format(Fmt: "%*.2f", Vals: FileReportColumns[6] - 1,
263 Vals: File.FunctionCoverage.getPercentCovered())
264 << '%';
265 else
266 OS << column(Str: "-", Width: FileReportColumns[6], Value: Column::RightAlignment);
267 }
268
269 if (Options.ShowInstantiationSummary) {
270 OS << format(Fmt: "%*u", Vals: FileReportColumns[7],
271 Vals: (unsigned)File.InstantiationCoverage.getNumFunctions());
272 OS << format(Fmt: "%*u", Vals: FileReportColumns[8],
273 Vals: (unsigned)(File.InstantiationCoverage.getNumFunctions() -
274 File.InstantiationCoverage.getExecuted()));
275 if (File.InstantiationCoverage.getNumFunctions())
276 Options.colored_ostream(OS, Color: InstantiationCoverageColor)
277 << format(Fmt: "%*.2f", Vals: FileReportColumns[9] - 1,
278 Vals: File.InstantiationCoverage.getPercentCovered())
279 << '%';
280 else
281 OS << column(Str: "-", Width: FileReportColumns[9], Value: Column::RightAlignment);
282 }
283
284 OS << format(Fmt: "%*u", Vals: FileReportColumns[10],
285 Vals: (unsigned)File.LineCoverage.getNumLines());
286 Options.colored_ostream(OS, Color: LineCoverageColor) << format(
287 Fmt: "%*u", Vals: FileReportColumns[11], Vals: (unsigned)(File.LineCoverage.getNumLines() -
288 File.LineCoverage.getCovered()));
289 if (File.LineCoverage.getNumLines())
290 Options.colored_ostream(OS, Color: LineCoverageColor)
291 << format(Fmt: "%*.2f", Vals: FileReportColumns[12] - 1,
292 Vals: File.LineCoverage.getPercentCovered())
293 << '%';
294 else
295 OS << column(Str: "-", Width: FileReportColumns[12], Value: Column::RightAlignment);
296
297 if (Options.ShowBranchSummary) {
298 OS << format(Fmt: "%*u", Vals: FileReportColumns[13],
299 Vals: (unsigned)File.BranchCoverage.getNumBranches());
300 Options.colored_ostream(OS, Color: LineCoverageColor)
301 << format(Fmt: "%*u", Vals: FileReportColumns[14],
302 Vals: (unsigned)(File.BranchCoverage.getNumBranches() -
303 File.BranchCoverage.getCovered()));
304 if (File.BranchCoverage.getNumBranches())
305 Options.colored_ostream(OS, Color: LineCoverageColor)
306 << format(Fmt: "%*.2f", Vals: FileReportColumns[15] - 1,
307 Vals: File.BranchCoverage.getPercentCovered())
308 << '%';
309 else
310 OS << column(Str: "-", Width: FileReportColumns[15], Value: Column::RightAlignment);
311 }
312
313 if (Options.ShowMCDCSummary) {
314 OS << format(Fmt: "%*u", Vals: FileReportColumns[16],
315 Vals: (unsigned)File.MCDCCoverage.getNumPairs());
316 Options.colored_ostream(OS, Color: LineCoverageColor)
317 << format(Fmt: "%*u", Vals: FileReportColumns[17],
318 Vals: (unsigned)(File.MCDCCoverage.getNumPairs() -
319 File.MCDCCoverage.getCoveredPairs()));
320 if (File.MCDCCoverage.getNumPairs())
321 Options.colored_ostream(OS, Color: LineCoverageColor)
322 << format(Fmt: "%*.2f", Vals: FileReportColumns[18] - 1,
323 Vals: File.MCDCCoverage.getPercentCovered())
324 << '%';
325 else
326 OS << column(Str: "-", Width: FileReportColumns[18], Value: Column::RightAlignment);
327 }
328
329 OS << "\n";
330}
331
332void CoverageReport::render(const FunctionCoverageSummary &Function,
333 const DemangleCache &DC,
334 raw_ostream &OS) const {
335 auto FuncCoverageColor =
336 determineCoveragePercentageColor(Info: Function.RegionCoverage);
337 auto LineCoverageColor =
338 determineCoveragePercentageColor(Info: Function.LineCoverage);
339 OS << column(Str: DC.demangle(Sym: Function.Name), Width: FunctionReportColumns[0],
340 Value: Column::RightTrim)
341 << format(Fmt: "%*u", Vals: FunctionReportColumns[1],
342 Vals: (unsigned)Function.RegionCoverage.getNumRegions());
343 Options.colored_ostream(OS, Color: FuncCoverageColor)
344 << format(Fmt: "%*u", Vals: FunctionReportColumns[2],
345 Vals: (unsigned)(Function.RegionCoverage.getNumRegions() -
346 Function.RegionCoverage.getCovered()));
347 Options.colored_ostream(
348 OS, Color: determineCoveragePercentageColor(Info: Function.RegionCoverage))
349 << format(Fmt: "%*.2f", Vals: FunctionReportColumns[3] - 1,
350 Vals: Function.RegionCoverage.getPercentCovered())
351 << '%';
352 OS << format(Fmt: "%*u", Vals: FunctionReportColumns[4],
353 Vals: (unsigned)Function.LineCoverage.getNumLines());
354 Options.colored_ostream(OS, Color: LineCoverageColor)
355 << format(Fmt: "%*u", Vals: FunctionReportColumns[5],
356 Vals: (unsigned)(Function.LineCoverage.getNumLines() -
357 Function.LineCoverage.getCovered()));
358 Options.colored_ostream(
359 OS, Color: determineCoveragePercentageColor(Info: Function.LineCoverage))
360 << format(Fmt: "%*.2f", Vals: FunctionReportColumns[6] - 1,
361 Vals: Function.LineCoverage.getPercentCovered())
362 << '%';
363 if (Options.ShowBranchSummary) {
364 OS << format(Fmt: "%*u", Vals: FunctionReportColumns[7],
365 Vals: (unsigned)Function.BranchCoverage.getNumBranches());
366 Options.colored_ostream(OS, Color: LineCoverageColor)
367 << format(Fmt: "%*u", Vals: FunctionReportColumns[8],
368 Vals: (unsigned)(Function.BranchCoverage.getNumBranches() -
369 Function.BranchCoverage.getCovered()));
370 Options.colored_ostream(
371 OS, Color: determineCoveragePercentageColor(Info: Function.BranchCoverage))
372 << format(Fmt: "%*.2f", Vals: FunctionReportColumns[9] - 1,
373 Vals: Function.BranchCoverage.getPercentCovered())
374 << '%';
375 }
376 if (Options.ShowMCDCSummary) {
377 OS << format(Fmt: "%*u", Vals: FunctionReportColumns[10],
378 Vals: (unsigned)Function.MCDCCoverage.getNumPairs());
379 Options.colored_ostream(OS, Color: LineCoverageColor)
380 << format(Fmt: "%*u", Vals: FunctionReportColumns[11],
381 Vals: (unsigned)(Function.MCDCCoverage.getNumPairs() -
382 Function.MCDCCoverage.getCoveredPairs()));
383 Options.colored_ostream(
384 OS, Color: determineCoveragePercentageColor(Info: Function.MCDCCoverage))
385 << format(Fmt: "%*.2f", Vals: FunctionReportColumns[12] - 1,
386 Vals: Function.MCDCCoverage.getPercentCovered())
387 << '%';
388 }
389 OS << "\n";
390}
391
392void CoverageReport::renderFunctionReports(ArrayRef<std::string> Files,
393 const DemangleCache &DC,
394 raw_ostream &OS) {
395 bool isFirst = true;
396 for (StringRef Filename : Files) {
397 auto Functions = Coverage.getCoveredFunctions(Filename);
398
399 if (isFirst)
400 isFirst = false;
401 else
402 OS << "\n";
403
404 std::vector<StringRef> Funcnames;
405 for (const auto &F : Functions)
406 Funcnames.emplace_back(args: DC.demangle(Sym: F.Name));
407 adjustColumnWidths(Files: {}, Functions: Funcnames);
408
409 OS << "File '" << Filename << "':\n";
410 OS << column(Str: "Name", Width: FunctionReportColumns[0])
411 << column(Str: "Regions", Width: FunctionReportColumns[1], Value: Column::RightAlignment)
412 << column(Str: "Miss", Width: FunctionReportColumns[2], Value: Column::RightAlignment)
413 << column(Str: "Cover", Width: FunctionReportColumns[3], Value: Column::RightAlignment)
414 << column(Str: "Lines", Width: FunctionReportColumns[4], Value: Column::RightAlignment)
415 << column(Str: "Miss", Width: FunctionReportColumns[5], Value: Column::RightAlignment)
416 << column(Str: "Cover", Width: FunctionReportColumns[6], Value: Column::RightAlignment);
417 if (Options.ShowBranchSummary)
418 OS << column(Str: "Branches", Width: FunctionReportColumns[7], Value: Column::RightAlignment)
419 << column(Str: "Miss", Width: FunctionReportColumns[8], Value: Column::RightAlignment)
420 << column(Str: "Cover", Width: FunctionReportColumns[9], Value: Column::RightAlignment);
421 if (Options.ShowMCDCSummary)
422 OS << column(Str: "MC/DC Conditions", Width: FunctionReportColumns[10],
423 Value: Column::RightAlignment)
424 << column(Str: "Miss", Width: FunctionReportColumns[11], Value: Column::RightAlignment)
425 << column(Str: "Cover", Width: FunctionReportColumns[12], Value: Column::RightAlignment);
426 OS << "\n";
427 renderDivider(OS, Options, isFileReport: false);
428 OS << "\n";
429 FunctionCoverageSummary Totals("TOTAL");
430 for (const auto &F : Functions) {
431 auto Function = FunctionCoverageSummary::get(CM: Coverage, Function: F);
432 ++Totals.ExecutionCount;
433 Totals.RegionCoverage += Function.RegionCoverage;
434 Totals.LineCoverage += Function.LineCoverage;
435 Totals.BranchCoverage += Function.BranchCoverage;
436 Totals.MCDCCoverage += Function.MCDCCoverage;
437 render(Function, DC, OS);
438 }
439 if (Totals.ExecutionCount) {
440 renderDivider(OS, Options, isFileReport: false);
441 OS << "\n";
442 render(Function: Totals, DC, OS);
443 }
444 }
445}
446
447void CoverageReport::prepareSingleFileReport(const StringRef Filename,
448 const coverage::CoverageMapping *Coverage,
449 const CoverageViewOptions &Options, const unsigned LCP,
450 FileCoverageSummary *FileReport, const CoverageFilter *Filters) {
451 for (const auto &Group : Coverage->getInstantiationGroups(Filename)) {
452 std::vector<FunctionCoverageSummary> InstantiationSummaries;
453 for (const coverage::FunctionRecord *F : Group.getInstantiations()) {
454 if (!Filters->matches(CM: *Coverage, Function: *F))
455 continue;
456 auto InstantiationSummary = FunctionCoverageSummary::get(CM: *Coverage, Function: *F);
457 FileReport->addInstantiation(Function: InstantiationSummary);
458 InstantiationSummaries.push_back(x: InstantiationSummary);
459 }
460 if (InstantiationSummaries.empty())
461 continue;
462
463 auto GroupSummary =
464 FunctionCoverageSummary::get(Group, Summaries: InstantiationSummaries);
465
466 if (Options.Debug)
467 outs() << "InstantiationGroup: " << GroupSummary.Name << " with "
468 << "size = " << Group.size() << "\n";
469
470 FileReport->addFunction(Function: GroupSummary);
471 }
472}
473
474std::vector<FileCoverageSummary> CoverageReport::prepareFileReports(
475 const coverage::CoverageMapping &Coverage, FileCoverageSummary &Totals,
476 ArrayRef<std::string> Files, const CoverageViewOptions &Options,
477 const CoverageFilter &Filters) {
478 unsigned LCP = getRedundantPrefixLen(Paths: Files);
479
480 ThreadPoolStrategy S = hardware_concurrency(ThreadCount: Options.NumThreads);
481 if (Options.NumThreads == 0) {
482 // If NumThreads is not specified, create one thread for each input, up to
483 // the number of hardware cores.
484 S = heavyweight_hardware_concurrency(ThreadCount: Files.size());
485 S.Limit = true;
486 }
487 DefaultThreadPool Pool(S);
488
489 std::vector<FileCoverageSummary> FileReports;
490 FileReports.reserve(n: Files.size());
491
492 for (StringRef Filename : Files) {
493 FileReports.emplace_back(args: Filename.drop_front(N: LCP));
494 Pool.async(F: &CoverageReport::prepareSingleFileReport, ArgList&: Filename,
495 ArgList: &Coverage, ArgList: Options, ArgList&: LCP, ArgList: &FileReports.back(), ArgList: &Filters);
496 }
497 Pool.wait();
498
499 for (const auto &FileReport : FileReports)
500 Totals += FileReport;
501
502 return FileReports;
503}
504
505void CoverageReport::renderFileReports(
506 raw_ostream &OS, const CoverageFilters &IgnoreFilenameFilters) const {
507 std::vector<std::string> UniqueSourceFiles;
508 for (StringRef SF : Coverage.getUniqueSourceFiles()) {
509 // Apply ignore source files filters.
510 if (!IgnoreFilenameFilters.matchesFilename(Filename: SF))
511 UniqueSourceFiles.emplace_back(args: SF.str());
512 }
513 renderFileReports(OS, Files: UniqueSourceFiles);
514}
515
516void CoverageReport::renderFileReports(
517 raw_ostream &OS, ArrayRef<std::string> Files) const {
518 renderFileReports(OS, Files, Filters: CoverageFiltersMatchAll());
519}
520
521void CoverageReport::renderFileReports(
522 raw_ostream &OS, ArrayRef<std::string> Files,
523 const CoverageFiltersMatchAll &Filters) const {
524 FileCoverageSummary Totals("TOTAL");
525 auto FileReports =
526 prepareFileReports(Coverage, Totals, Files, Options, Filters);
527 renderFileReports(OS, FileReports, Totals, ShowEmptyFiles: Filters.empty());
528}
529
530void CoverageReport::renderFileReports(
531 raw_ostream &OS, const std::vector<FileCoverageSummary> &FileReports,
532 const FileCoverageSummary &Totals, bool ShowEmptyFiles) const {
533 std::vector<StringRef> Filenames;
534 Filenames.reserve(n: FileReports.size());
535 for (const FileCoverageSummary &FCS : FileReports)
536 Filenames.emplace_back(args: FCS.Name);
537 adjustColumnWidths(Files: Filenames, Functions: {});
538
539 OS << column(Str: "Filename", Width: FileReportColumns[0]);
540 if (Options.ShowRegionSummary)
541 OS << column(Str: "Regions", Width: FileReportColumns[1], Value: Column::RightAlignment)
542 << column(Str: "Missed Regions", Width: FileReportColumns[2], Value: Column::RightAlignment)
543 << column(Str: "Cover", Width: FileReportColumns[3], Value: Column::RightAlignment);
544 if (Options.ShowFunctionSummary)
545 OS << column(Str: "Functions", Width: FileReportColumns[4], Value: Column::RightAlignment)
546 << column(Str: "Missed Functions", Width: FileReportColumns[5],
547 Value: Column::RightAlignment)
548 << column(Str: "Executed", Width: FileReportColumns[6], Value: Column::RightAlignment);
549 if (Options.ShowInstantiationSummary)
550 OS << column(Str: "Instantiations", Width: FileReportColumns[7], Value: Column::RightAlignment)
551 << column(Str: "Missed Insts.", Width: FileReportColumns[8], Value: Column::RightAlignment)
552 << column(Str: "Executed", Width: FileReportColumns[9], Value: Column::RightAlignment);
553 OS << column(Str: "Lines", Width: FileReportColumns[10], Value: Column::RightAlignment)
554 << column(Str: "Missed Lines", Width: FileReportColumns[11], Value: Column::RightAlignment)
555 << column(Str: "Cover", Width: FileReportColumns[12], Value: Column::RightAlignment);
556 if (Options.ShowBranchSummary)
557 OS << column(Str: "Branches", Width: FileReportColumns[13], Value: Column::RightAlignment)
558 << column(Str: "Missed Branches", Width: FileReportColumns[14],
559 Value: Column::RightAlignment)
560 << column(Str: "Cover", Width: FileReportColumns[15], Value: Column::RightAlignment);
561 if (Options.ShowMCDCSummary)
562 OS << column(Str: "MC/DC Conditions", Width: FileReportColumns[16],
563 Value: Column::RightAlignment)
564 << column(Str: "Missed Conditions", Width: FileReportColumns[17],
565 Value: Column::RightAlignment)
566 << column(Str: "Cover", Width: FileReportColumns[18], Value: Column::RightAlignment);
567 OS << "\n";
568 renderDivider(OS, Options, isFileReport: true);
569 OS << "\n";
570
571 std::vector<const FileCoverageSummary *> EmptyFiles;
572 for (const FileCoverageSummary &FCS : FileReports) {
573 if (FCS.FunctionCoverage.getNumFunctions())
574 render(File: FCS, OS);
575 else
576 EmptyFiles.push_back(x: &FCS);
577 }
578
579 if (!EmptyFiles.empty() && ShowEmptyFiles) {
580 OS << "\n"
581 << "Files which contain no functions:\n";
582
583 for (auto FCS : EmptyFiles)
584 render(File: *FCS, OS);
585 }
586
587 renderDivider(OS, Options, isFileReport: true);
588 OS << "\n";
589 render(File: Totals, OS);
590}
591
592Expected<FileCoverageSummary> DirectoryCoverageReport::prepareDirectoryReports(
593 ArrayRef<std::string> SourceFiles) {
594 std::vector<StringRef> Files(SourceFiles.begin(), SourceFiles.end());
595
596 unsigned RootLCP = getRedundantPrefixLen(Paths: Files, LCP: 0);
597 auto LCPath = Files.front().substr(Start: 0, N: RootLCP);
598
599 ThreadPoolStrategy PoolS = hardware_concurrency(ThreadCount: Options.NumThreads);
600 if (Options.NumThreads == 0) {
601 PoolS = heavyweight_hardware_concurrency(ThreadCount: Files.size());
602 PoolS.Limit = true;
603 }
604 DefaultThreadPool Pool(PoolS);
605
606 TPool = &Pool;
607 LCPStack = {RootLCP};
608 FileCoverageSummary RootTotals(LCPath);
609 if (auto E = prepareSubDirectoryReports(Files, Totals: &RootTotals))
610 return {std::move(E)};
611 return {std::move(RootTotals)};
612}
613
614/// Filter out files in LCPStack.back(), group others by subdirectory name
615/// and recurse on them. After returning from all subdirectories, call
616/// generateSubDirectoryReport(). \p Files must be non-empty. The
617/// FileCoverageSummary of this directory will be added to \p Totals.
618Error DirectoryCoverageReport::prepareSubDirectoryReports(
619 const ArrayRef<StringRef> &Files, FileCoverageSummary *Totals) {
620 assert(!Files.empty() && "Files must have at least one element");
621
622 auto LCP = LCPStack.back();
623 auto LCPath = Files.front().substr(Start: 0, N: LCP).str();
624
625 // Use ordered map to keep entries in order.
626 SubFileReports SubFiles;
627 SubDirReports SubDirs;
628 for (auto &&File : Files) {
629 auto SubPath = File.substr(Start: LCPath.size());
630 SmallVector<char, 128> NativeSubPath;
631 sys::path::native(path: SubPath, result&: NativeSubPath);
632 StringRef NativeSubPathRef(NativeSubPath.data(), NativeSubPath.size());
633
634 auto I = sys::path::begin(path: NativeSubPathRef);
635 auto E = sys::path::end(path: NativeSubPathRef);
636 assert(I != E && "Such case should have been filtered out in the caller");
637
638 auto Name = SubPath.substr(Start: 0, N: I->size());
639 if (++I == E) {
640 auto Iter = SubFiles.insert_or_assign(k: Name, obj&: SubPath).first;
641 // Makes files reporting overlap with subdir reporting.
642 TPool->async(F: &CoverageReport::prepareSingleFileReport, ArgList: File, ArgList: &Coverage,
643 ArgList: Options, ArgList&: LCP, ArgList: &Iter->second, ArgList: &Filters);
644 } else {
645 SubDirs[Name].second.push_back(Elt: File);
646 }
647 }
648
649 // Call recursively on subdirectories.
650 for (auto &&KV : SubDirs) {
651 auto &V = KV.second;
652 if (V.second.size() == 1) {
653 // If there's only one file in that subdirectory, we don't bother to
654 // recurse on it further.
655 V.first.Name = V.second.front().substr(Start: LCP);
656 TPool->async(F: &CoverageReport::prepareSingleFileReport, ArgList&: V.second.front(),
657 ArgList: &Coverage, ArgList: Options, ArgList&: LCP, ArgList: &V.first, ArgList: &Filters);
658 } else {
659 auto SubDirLCP = getRedundantPrefixLen(Paths: V.second, LCP);
660 V.first.Name = V.second.front().substr(Start: LCP, N: SubDirLCP);
661 LCPStack.push_back(Elt: LCP + SubDirLCP);
662 if (auto E = prepareSubDirectoryReports(Files: V.second, Totals: &V.first))
663 return E;
664 }
665 }
666
667 TPool->wait();
668
669 FileCoverageSummary CurrentTotals(LCPath);
670 for (auto &&KV : SubFiles)
671 CurrentTotals += KV.second;
672 for (auto &&KV : SubDirs)
673 CurrentTotals += KV.second.first;
674 *Totals += CurrentTotals;
675
676 if (auto E = generateSubDirectoryReport(
677 SubFiles: std::move(SubFiles), SubDirs: std::move(SubDirs), SubTotals: std::move(CurrentTotals)))
678 return E;
679
680 LCPStack.pop_back();
681 return Error::success();
682}
683
684} // end namespace llvm
685