CoverageReport.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #include "CoverageReport.h"
  13. #include "RenderingSupport.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/Support/Format.h"
  16. #include "llvm/Support/Path.h"
  17. #include "llvm/Support/ThreadPool.h"
  18. #include "llvm/Support/Threading.h"
  19. #include <numeric>
  20. using namespace llvm;
  21. namespace {
  22. /// Helper struct which prints trimmed and aligned columns.
  23. struct Column {
  24. enum TrimKind { NoTrim, WidthTrim, RightTrim };
  25. enum AlignmentKind { LeftAlignment, RightAlignment };
  26. StringRef Str;
  27. unsigned Width;
  28. TrimKind Trim;
  29. AlignmentKind Alignment;
  30. Column(StringRef Str, unsigned Width)
  31. : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
  32. Column &set(TrimKind Value) {
  33. Trim = Value;
  34. return *this;
  35. }
  36. Column &set(AlignmentKind Value) {
  37. Alignment = Value;
  38. return *this;
  39. }
  40. void render(raw_ostream &OS) const {
  41. if (Str.size() <= Width) {
  42. if (Alignment == RightAlignment) {
  43. OS.indent(Width - Str.size());
  44. OS << Str;
  45. return;
  46. }
  47. OS << Str;
  48. OS.indent(Width - Str.size());
  49. return;
  50. }
  51. switch (Trim) {
  52. case NoTrim:
  53. OS << Str;
  54. break;
  55. case WidthTrim:
  56. OS << Str.substr(0, Width);
  57. break;
  58. case RightTrim:
  59. OS << Str.substr(0, Width - 3) << "...";
  60. break;
  61. }
  62. }
  63. };
  64. raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
  65. Value.render(OS);
  66. return OS;
  67. }
  68. Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
  69. template <typename T>
  70. Column column(StringRef Str, unsigned Width, const T &Value) {
  71. return Column(Str, Width).set(Value);
  72. }
  73. // Specify the default column widths.
  74. size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10, 16,
  75. 16, 10, 12, 18, 10, 12, 18, 10};
  76. size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8, 10, 8, 8};
  77. /// Adjust column widths to fit long file paths and function names.
  78. void adjustColumnWidths(ArrayRef<StringRef> Files,
  79. ArrayRef<StringRef> Functions) {
  80. for (StringRef Filename : Files)
  81. FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
  82. for (StringRef Funcname : Functions)
  83. FunctionReportColumns[0] =
  84. std::max(FunctionReportColumns[0], Funcname.size());
  85. }
  86. /// Prints a horizontal divider long enough to cover the given column
  87. /// widths.
  88. void renderDivider(ArrayRef<size_t> ColumnWidths, raw_ostream &OS) {
  89. size_t Length = std::accumulate(ColumnWidths.begin(), ColumnWidths.end(), 0);
  90. for (size_t I = 0; I < Length; ++I)
  91. OS << '-';
  92. }
  93. /// Return the color which correponds to the coverage percentage of a
  94. /// certain metric.
  95. template <typename T>
  96. raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
  97. if (Info.isFullyCovered())
  98. return raw_ostream::GREEN;
  99. return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
  100. : raw_ostream::RED;
  101. }
  102. /// Get the number of redundant path components in each path in \p Paths.
  103. unsigned getNumRedundantPathComponents(ArrayRef<std::string> Paths) {
  104. // To start, set the number of redundant path components to the maximum
  105. // possible value.
  106. SmallVector<StringRef, 8> FirstPathComponents{sys::path::begin(Paths[0]),
  107. sys::path::end(Paths[0])};
  108. unsigned NumRedundant = FirstPathComponents.size();
  109. for (unsigned I = 1, E = Paths.size(); NumRedundant > 0 && I < E; ++I) {
  110. StringRef Path = Paths[I];
  111. for (const auto &Component :
  112. enumerate(make_range(sys::path::begin(Path), sys::path::end(Path)))) {
  113. // Do not increase the number of redundant components: that would remove
  114. // useful parts of already-visited paths.
  115. if (Component.index() >= NumRedundant)
  116. break;
  117. // Lower the number of redundant components when there's a mismatch
  118. // between the first path, and the path under consideration.
  119. if (FirstPathComponents[Component.index()] != Component.value()) {
  120. NumRedundant = Component.index();
  121. break;
  122. }
  123. }
  124. }
  125. return NumRedundant;
  126. }
  127. /// Determine the length of the longest redundant prefix of the paths in
  128. /// \p Paths.
  129. unsigned getRedundantPrefixLen(ArrayRef<std::string> Paths) {
  130. // If there's at most one path, no path components are redundant.
  131. if (Paths.size() <= 1)
  132. return 0;
  133. unsigned PrefixLen = 0;
  134. unsigned NumRedundant = getNumRedundantPathComponents(Paths);
  135. auto Component = sys::path::begin(Paths[0]);
  136. for (unsigned I = 0; I < NumRedundant; ++I) {
  137. auto LastComponent = Component;
  138. ++Component;
  139. PrefixLen += Component - LastComponent;
  140. }
  141. return PrefixLen;
  142. }
  143. } // end anonymous namespace
  144. namespace llvm {
  145. void CoverageReport::render(const FileCoverageSummary &File,
  146. raw_ostream &OS) const {
  147. auto FileCoverageColor =
  148. determineCoveragePercentageColor(File.RegionCoverage);
  149. auto FuncCoverageColor =
  150. determineCoveragePercentageColor(File.FunctionCoverage);
  151. auto InstantiationCoverageColor =
  152. determineCoveragePercentageColor(File.InstantiationCoverage);
  153. auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
  154. SmallString<256> FileName = File.Name;
  155. sys::path::remove_dots(FileName, /*remove_dot_dot=*/true);
  156. sys::path::native(FileName);
  157. OS << column(FileName, FileReportColumns[0], Column::NoTrim);
  158. if (Options.ShowRegionSummary) {
  159. OS << format("%*u", FileReportColumns[1],
  160. (unsigned)File.RegionCoverage.getNumRegions());
  161. Options.colored_ostream(OS, FileCoverageColor)
  162. << format("%*u", FileReportColumns[2],
  163. (unsigned)(File.RegionCoverage.getNumRegions() -
  164. File.RegionCoverage.getCovered()));
  165. if (File.RegionCoverage.getNumRegions())
  166. Options.colored_ostream(OS, FileCoverageColor)
  167. << format("%*.2f", FileReportColumns[3] - 1,
  168. File.RegionCoverage.getPercentCovered())
  169. << '%';
  170. else
  171. OS << column("-", FileReportColumns[3], Column::RightAlignment);
  172. }
  173. OS << format("%*u", FileReportColumns[4],
  174. (unsigned)File.FunctionCoverage.getNumFunctions());
  175. OS << format("%*u", FileReportColumns[5],
  176. (unsigned)(File.FunctionCoverage.getNumFunctions() -
  177. File.FunctionCoverage.getExecuted()));
  178. if (File.FunctionCoverage.getNumFunctions())
  179. Options.colored_ostream(OS, FuncCoverageColor)
  180. << format("%*.2f", FileReportColumns[6] - 1,
  181. File.FunctionCoverage.getPercentCovered())
  182. << '%';
  183. else
  184. OS << column("-", FileReportColumns[6], Column::RightAlignment);
  185. if (Options.ShowInstantiationSummary) {
  186. OS << format("%*u", FileReportColumns[7],
  187. (unsigned)File.InstantiationCoverage.getNumFunctions());
  188. OS << format("%*u", FileReportColumns[8],
  189. (unsigned)(File.InstantiationCoverage.getNumFunctions() -
  190. File.InstantiationCoverage.getExecuted()));
  191. if (File.InstantiationCoverage.getNumFunctions())
  192. Options.colored_ostream(OS, InstantiationCoverageColor)
  193. << format("%*.2f", FileReportColumns[9] - 1,
  194. File.InstantiationCoverage.getPercentCovered())
  195. << '%';
  196. else
  197. OS << column("-", FileReportColumns[9], Column::RightAlignment);
  198. }
  199. OS << format("%*u", FileReportColumns[10],
  200. (unsigned)File.LineCoverage.getNumLines());
  201. Options.colored_ostream(OS, LineCoverageColor) << format(
  202. "%*u", FileReportColumns[11], (unsigned)(File.LineCoverage.getNumLines() -
  203. File.LineCoverage.getCovered()));
  204. if (File.LineCoverage.getNumLines())
  205. Options.colored_ostream(OS, LineCoverageColor)
  206. << format("%*.2f", FileReportColumns[12] - 1,
  207. File.LineCoverage.getPercentCovered())
  208. << '%';
  209. else
  210. OS << column("-", FileReportColumns[12], Column::RightAlignment);
  211. if (Options.ShowBranchSummary) {
  212. OS << format("%*u", FileReportColumns[13],
  213. (unsigned)File.BranchCoverage.getNumBranches());
  214. Options.colored_ostream(OS, LineCoverageColor)
  215. << format("%*u", FileReportColumns[14],
  216. (unsigned)(File.BranchCoverage.getNumBranches() -
  217. File.BranchCoverage.getCovered()));
  218. if (File.BranchCoverage.getNumBranches())
  219. Options.colored_ostream(OS, LineCoverageColor)
  220. << format("%*.2f", FileReportColumns[15] - 1,
  221. File.BranchCoverage.getPercentCovered())
  222. << '%';
  223. else
  224. OS << column("-", FileReportColumns[15], Column::RightAlignment);
  225. }
  226. OS << "\n";
  227. }
  228. void CoverageReport::render(const FunctionCoverageSummary &Function,
  229. const DemangleCache &DC,
  230. raw_ostream &OS) const {
  231. auto FuncCoverageColor =
  232. determineCoveragePercentageColor(Function.RegionCoverage);
  233. auto LineCoverageColor =
  234. determineCoveragePercentageColor(Function.LineCoverage);
  235. OS << column(DC.demangle(Function.Name), FunctionReportColumns[0],
  236. Column::RightTrim)
  237. << format("%*u", FunctionReportColumns[1],
  238. (unsigned)Function.RegionCoverage.getNumRegions());
  239. Options.colored_ostream(OS, FuncCoverageColor)
  240. << format("%*u", FunctionReportColumns[2],
  241. (unsigned)(Function.RegionCoverage.getNumRegions() -
  242. Function.RegionCoverage.getCovered()));
  243. Options.colored_ostream(
  244. OS, determineCoveragePercentageColor(Function.RegionCoverage))
  245. << format("%*.2f", FunctionReportColumns[3] - 1,
  246. Function.RegionCoverage.getPercentCovered())
  247. << '%';
  248. OS << format("%*u", FunctionReportColumns[4],
  249. (unsigned)Function.LineCoverage.getNumLines());
  250. Options.colored_ostream(OS, LineCoverageColor)
  251. << format("%*u", FunctionReportColumns[5],
  252. (unsigned)(Function.LineCoverage.getNumLines() -
  253. Function.LineCoverage.getCovered()));
  254. Options.colored_ostream(
  255. OS, determineCoveragePercentageColor(Function.LineCoverage))
  256. << format("%*.2f", FunctionReportColumns[6] - 1,
  257. Function.LineCoverage.getPercentCovered())
  258. << '%';
  259. if (Options.ShowBranchSummary) {
  260. OS << format("%*u", FunctionReportColumns[7],
  261. (unsigned)Function.BranchCoverage.getNumBranches());
  262. Options.colored_ostream(OS, LineCoverageColor)
  263. << format("%*u", FunctionReportColumns[8],
  264. (unsigned)(Function.BranchCoverage.getNumBranches() -
  265. Function.BranchCoverage.getCovered()));
  266. Options.colored_ostream(
  267. OS, determineCoveragePercentageColor(Function.BranchCoverage))
  268. << format("%*.2f", FunctionReportColumns[9] - 1,
  269. Function.BranchCoverage.getPercentCovered())
  270. << '%';
  271. }
  272. OS << "\n";
  273. }
  274. void CoverageReport::renderFunctionReports(ArrayRef<std::string> Files,
  275. const DemangleCache &DC,
  276. raw_ostream &OS) {
  277. bool isFirst = true;
  278. for (StringRef Filename : Files) {
  279. auto Functions = Coverage.getCoveredFunctions(Filename);
  280. if (isFirst)
  281. isFirst = false;
  282. else
  283. OS << "\n";
  284. std::vector<StringRef> Funcnames;
  285. for (const auto &F : Functions)
  286. Funcnames.emplace_back(DC.demangle(F.Name));
  287. adjustColumnWidths({}, Funcnames);
  288. OS << "File '" << Filename << "':\n";
  289. OS << column("Name", FunctionReportColumns[0])
  290. << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
  291. << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
  292. << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
  293. << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
  294. << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
  295. << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
  296. if (Options.ShowBranchSummary)
  297. OS << column("Branches", FunctionReportColumns[7], Column::RightAlignment)
  298. << column("Miss", FunctionReportColumns[8], Column::RightAlignment)
  299. << column("Cover", FunctionReportColumns[9], Column::RightAlignment);
  300. OS << "\n";
  301. renderDivider(FunctionReportColumns, OS);
  302. OS << "\n";
  303. FunctionCoverageSummary Totals("TOTAL");
  304. for (const auto &F : Functions) {
  305. auto Function = FunctionCoverageSummary::get(Coverage, F);
  306. ++Totals.ExecutionCount;
  307. Totals.RegionCoverage += Function.RegionCoverage;
  308. Totals.LineCoverage += Function.LineCoverage;
  309. Totals.BranchCoverage += Function.BranchCoverage;
  310. render(Function, DC, OS);
  311. }
  312. if (Totals.ExecutionCount) {
  313. renderDivider(FunctionReportColumns, OS);
  314. OS << "\n";
  315. render(Totals, DC, OS);
  316. }
  317. }
  318. }
  319. void CoverageReport::prepareSingleFileReport(const StringRef Filename,
  320. const coverage::CoverageMapping *Coverage,
  321. const CoverageViewOptions &Options, const unsigned LCP,
  322. FileCoverageSummary *FileReport, const CoverageFilter *Filters) {
  323. for (const auto &Group : Coverage->getInstantiationGroups(Filename)) {
  324. std::vector<FunctionCoverageSummary> InstantiationSummaries;
  325. for (const coverage::FunctionRecord *F : Group.getInstantiations()) {
  326. if (!Filters->matches(*Coverage, *F))
  327. continue;
  328. auto InstantiationSummary = FunctionCoverageSummary::get(*Coverage, *F);
  329. FileReport->addInstantiation(InstantiationSummary);
  330. InstantiationSummaries.push_back(InstantiationSummary);
  331. }
  332. if (InstantiationSummaries.empty())
  333. continue;
  334. auto GroupSummary =
  335. FunctionCoverageSummary::get(Group, InstantiationSummaries);
  336. if (Options.Debug)
  337. outs() << "InstantiationGroup: " << GroupSummary.Name << " with "
  338. << "size = " << Group.size() << "\n";
  339. FileReport->addFunction(GroupSummary);
  340. }
  341. }
  342. std::vector<FileCoverageSummary> CoverageReport::prepareFileReports(
  343. const coverage::CoverageMapping &Coverage, FileCoverageSummary &Totals,
  344. ArrayRef<std::string> Files, const CoverageViewOptions &Options,
  345. const CoverageFilter &Filters) {
  346. unsigned LCP = getRedundantPrefixLen(Files);
  347. ThreadPoolStrategy S = hardware_concurrency(Options.NumThreads);
  348. if (Options.NumThreads == 0) {
  349. // If NumThreads is not specified, create one thread for each input, up to
  350. // the number of hardware cores.
  351. S = heavyweight_hardware_concurrency(Files.size());
  352. S.Limit = true;
  353. }
  354. ThreadPool Pool(S);
  355. std::vector<FileCoverageSummary> FileReports;
  356. FileReports.reserve(Files.size());
  357. for (StringRef Filename : Files) {
  358. FileReports.emplace_back(Filename.drop_front(LCP));
  359. Pool.async(&CoverageReport::prepareSingleFileReport, Filename,
  360. &Coverage, Options, LCP, &FileReports.back(), &Filters);
  361. }
  362. Pool.wait();
  363. for (const auto &FileReport : FileReports)
  364. Totals += FileReport;
  365. return FileReports;
  366. }
  367. void CoverageReport::renderFileReports(
  368. raw_ostream &OS, const CoverageFilters &IgnoreFilenameFilters) const {
  369. std::vector<std::string> UniqueSourceFiles;
  370. for (StringRef SF : Coverage.getUniqueSourceFiles()) {
  371. // Apply ignore source files filters.
  372. if (!IgnoreFilenameFilters.matchesFilename(SF))
  373. UniqueSourceFiles.emplace_back(SF.str());
  374. }
  375. renderFileReports(OS, UniqueSourceFiles);
  376. }
  377. void CoverageReport::renderFileReports(
  378. raw_ostream &OS, ArrayRef<std::string> Files) const {
  379. renderFileReports(OS, Files, CoverageFiltersMatchAll());
  380. }
  381. void CoverageReport::renderFileReports(
  382. raw_ostream &OS, ArrayRef<std::string> Files,
  383. const CoverageFiltersMatchAll &Filters) const {
  384. FileCoverageSummary Totals("TOTAL");
  385. auto FileReports =
  386. prepareFileReports(Coverage, Totals, Files, Options, Filters);
  387. std::vector<StringRef> Filenames;
  388. for (const FileCoverageSummary &FCS : FileReports)
  389. Filenames.emplace_back(FCS.Name);
  390. adjustColumnWidths(Filenames, {});
  391. OS << column("Filename", FileReportColumns[0]);
  392. if (Options.ShowRegionSummary)
  393. OS << column("Regions", FileReportColumns[1], Column::RightAlignment)
  394. << column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
  395. << column("Cover", FileReportColumns[3], Column::RightAlignment);
  396. OS << column("Functions", FileReportColumns[4], Column::RightAlignment)
  397. << column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
  398. << column("Executed", FileReportColumns[6], Column::RightAlignment);
  399. if (Options.ShowInstantiationSummary)
  400. OS << column("Instantiations", FileReportColumns[7], Column::RightAlignment)
  401. << column("Missed Insts.", FileReportColumns[8], Column::RightAlignment)
  402. << column("Executed", FileReportColumns[9], Column::RightAlignment);
  403. OS << column("Lines", FileReportColumns[10], Column::RightAlignment)
  404. << column("Missed Lines", FileReportColumns[11], Column::RightAlignment)
  405. << column("Cover", FileReportColumns[12], Column::RightAlignment);
  406. if (Options.ShowBranchSummary)
  407. OS << column("Branches", FileReportColumns[13], Column::RightAlignment)
  408. << column("Missed Branches", FileReportColumns[14],
  409. Column::RightAlignment)
  410. << column("Cover", FileReportColumns[15], Column::RightAlignment);
  411. OS << "\n";
  412. renderDivider(FileReportColumns, OS);
  413. OS << "\n";
  414. bool EmptyFiles = false;
  415. for (const FileCoverageSummary &FCS : FileReports) {
  416. if (FCS.FunctionCoverage.getNumFunctions())
  417. render(FCS, OS);
  418. else
  419. EmptyFiles = true;
  420. }
  421. if (EmptyFiles && Filters.empty()) {
  422. OS << "\n"
  423. << "Files which contain no functions:\n";
  424. for (const FileCoverageSummary &FCS : FileReports)
  425. if (!FCS.FunctionCoverage.getNumFunctions())
  426. render(FCS, OS);
  427. }
  428. renderDivider(FileReportColumns, OS);
  429. OS << "\n";
  430. render(Totals, OS);
  431. }
  432. } // end namespace llvm