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