CoverageExporterLcov.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //===- CoverageExporterLcov.cpp - Code coverage export --------------------===//
  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 file implements export of code coverage data to lcov trace file format.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // The trace file code coverage export follows the following format (see also
  15. // https://linux.die.net/man/1/geninfo). Each quoted string appears on its own
  16. // line; the indentation shown here is only for documentation purposes.
  17. //
  18. // - for each source file:
  19. // - "SF:<absolute path to source file>"
  20. // - for each function:
  21. // - "FN:<line number of function start>,<function name>"
  22. // - for each function:
  23. // - "FNDA:<execution count>,<function name>"
  24. // - "FNF:<number of functions found>"
  25. // - "FNH:<number of functions hit>"
  26. // - for each instrumented line:
  27. // - "DA:<line number>,<execution count>[,<checksum>]
  28. // - for each branch:
  29. // - "BRDA:<line number>,<branch pair id>,<branch id>,<count>"
  30. // - "BRF:<number of branches found>"
  31. // - "BRH:<number of branches hit>"
  32. // - "LH:<number of lines with non-zero execution count>"
  33. // - "LF:<number of instrumented lines>"
  34. // - "end_of_record"
  35. //
  36. // If the user is exporting summary information only, then the FN, FNDA, and DA
  37. // lines will not be present.
  38. //
  39. //===----------------------------------------------------------------------===//
  40. #include "CoverageExporterLcov.h"
  41. #include "CoverageReport.h"
  42. using namespace llvm;
  43. namespace {
  44. void renderFunctionSummary(raw_ostream &OS,
  45. const FileCoverageSummary &Summary) {
  46. OS << "FNF:" << Summary.FunctionCoverage.getNumFunctions() << '\n'
  47. << "FNH:" << Summary.FunctionCoverage.getExecuted() << '\n';
  48. }
  49. void renderFunctions(
  50. raw_ostream &OS,
  51. const iterator_range<coverage::FunctionRecordIterator> &Functions) {
  52. for (const auto &F : Functions) {
  53. auto StartLine = F.CountedRegions.front().LineStart;
  54. OS << "FN:" << StartLine << ',' << F.Name << '\n';
  55. }
  56. for (const auto &F : Functions)
  57. OS << "FNDA:" << F.ExecutionCount << ',' << F.Name << '\n';
  58. }
  59. void renderLineExecutionCounts(raw_ostream &OS,
  60. const coverage::CoverageData &FileCoverage) {
  61. coverage::LineCoverageIterator LCI{FileCoverage, 1};
  62. coverage::LineCoverageIterator LCIEnd = LCI.getEnd();
  63. for (; LCI != LCIEnd; ++LCI) {
  64. const coverage::LineCoverageStats &LCS = *LCI;
  65. if (LCS.isMapped()) {
  66. OS << "DA:" << LCS.getLine() << ',' << LCS.getExecutionCount() << '\n';
  67. }
  68. }
  69. }
  70. std::vector<llvm::coverage::CountedRegion>
  71. collectNestedBranches(const coverage::CoverageMapping &Coverage,
  72. ArrayRef<llvm::coverage::ExpansionRecord> Expansions,
  73. int ViewDepth = 0, int SrcLine = 0) {
  74. std::vector<llvm::coverage::CountedRegion> Branches;
  75. for (const auto &Expansion : Expansions) {
  76. auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
  77. // If we're at the top level, set the corresponding source line.
  78. if (ViewDepth == 0)
  79. SrcLine = Expansion.Region.LineStart;
  80. // Recursively collect branches from nested expansions.
  81. auto NestedExpansions = ExpansionCoverage.getExpansions();
  82. auto NestedExBranches = collectNestedBranches(Coverage, NestedExpansions,
  83. ViewDepth + 1, SrcLine);
  84. append_range(Branches, NestedExBranches);
  85. // Add branches from this level of expansion.
  86. auto ExBranches = ExpansionCoverage.getBranches();
  87. for (auto B : ExBranches)
  88. if (B.FileID == Expansion.FileID) {
  89. B.LineStart = SrcLine;
  90. Branches.push_back(B);
  91. }
  92. }
  93. return Branches;
  94. }
  95. bool sortLine(llvm::coverage::CountedRegion I,
  96. llvm::coverage::CountedRegion J) {
  97. return (I.LineStart < J.LineStart) ||
  98. ((I.LineStart == J.LineStart) && (I.ColumnStart < J.ColumnStart));
  99. }
  100. void renderBranchExecutionCounts(raw_ostream &OS,
  101. const coverage::CoverageMapping &Coverage,
  102. const coverage::CoverageData &FileCoverage) {
  103. std::vector<llvm::coverage::CountedRegion> Branches =
  104. FileCoverage.getBranches();
  105. // Recursively collect branches for all file expansions.
  106. std::vector<llvm::coverage::CountedRegion> ExBranches =
  107. collectNestedBranches(Coverage, FileCoverage.getExpansions());
  108. // Append Expansion Branches to Source Branches.
  109. append_range(Branches, ExBranches);
  110. // Sort branches based on line number to ensure branches corresponding to the
  111. // same source line are counted together.
  112. llvm::sort(Branches, sortLine);
  113. auto NextBranch = Branches.begin();
  114. auto EndBranch = Branches.end();
  115. // Branches with the same source line are enumerated individually
  116. // (BranchIndex) as well as based on True/False pairs (PairIndex).
  117. while (NextBranch != EndBranch) {
  118. unsigned CurrentLine = NextBranch->LineStart;
  119. unsigned PairIndex = 0;
  120. unsigned BranchIndex = 0;
  121. while (NextBranch != EndBranch && CurrentLine == NextBranch->LineStart) {
  122. if (!NextBranch->Folded) {
  123. unsigned BC1 = NextBranch->ExecutionCount;
  124. unsigned BC2 = NextBranch->FalseExecutionCount;
  125. bool BranchNotExecuted = (BC1 == 0 && BC2 == 0);
  126. for (int I = 0; I < 2; I++, BranchIndex++) {
  127. OS << "BRDA:" << CurrentLine << ',' << PairIndex << ','
  128. << BranchIndex;
  129. if (BranchNotExecuted)
  130. OS << ',' << '-' << '\n';
  131. else
  132. OS << ',' << (I == 0 ? BC1 : BC2) << '\n';
  133. }
  134. PairIndex++;
  135. }
  136. NextBranch++;
  137. }
  138. }
  139. }
  140. void renderLineSummary(raw_ostream &OS, const FileCoverageSummary &Summary) {
  141. OS << "LF:" << Summary.LineCoverage.getNumLines() << '\n'
  142. << "LH:" << Summary.LineCoverage.getCovered() << '\n';
  143. }
  144. void renderBranchSummary(raw_ostream &OS, const FileCoverageSummary &Summary) {
  145. OS << "BRF:" << Summary.BranchCoverage.getNumBranches() << '\n'
  146. << "BRH:" << Summary.BranchCoverage.getCovered() << '\n';
  147. }
  148. void renderFile(raw_ostream &OS, const coverage::CoverageMapping &Coverage,
  149. const std::string &Filename,
  150. const FileCoverageSummary &FileReport, bool ExportSummaryOnly,
  151. bool SkipFunctions, bool SkipBranches) {
  152. OS << "SF:" << Filename << '\n';
  153. if (!ExportSummaryOnly && !SkipFunctions) {
  154. renderFunctions(OS, Coverage.getCoveredFunctions(Filename));
  155. }
  156. renderFunctionSummary(OS, FileReport);
  157. if (!ExportSummaryOnly) {
  158. // Calculate and render detailed coverage information for given file.
  159. auto FileCoverage = Coverage.getCoverageForFile(Filename);
  160. renderLineExecutionCounts(OS, FileCoverage);
  161. if (!SkipBranches)
  162. renderBranchExecutionCounts(OS, Coverage, FileCoverage);
  163. }
  164. if (!SkipBranches)
  165. renderBranchSummary(OS, FileReport);
  166. renderLineSummary(OS, FileReport);
  167. OS << "end_of_record\n";
  168. }
  169. void renderFiles(raw_ostream &OS, const coverage::CoverageMapping &Coverage,
  170. ArrayRef<std::string> SourceFiles,
  171. ArrayRef<FileCoverageSummary> FileReports,
  172. bool ExportSummaryOnly, bool SkipFunctions,
  173. bool SkipBranches) {
  174. for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I)
  175. renderFile(OS, Coverage, SourceFiles[I], FileReports[I], ExportSummaryOnly,
  176. SkipFunctions, SkipBranches);
  177. }
  178. } // end anonymous namespace
  179. void CoverageExporterLcov::renderRoot(const CoverageFilters &IgnoreFilters) {
  180. std::vector<std::string> SourceFiles;
  181. for (StringRef SF : Coverage.getUniqueSourceFiles()) {
  182. if (!IgnoreFilters.matchesFilename(SF))
  183. SourceFiles.emplace_back(SF);
  184. }
  185. renderRoot(SourceFiles);
  186. }
  187. void CoverageExporterLcov::renderRoot(ArrayRef<std::string> SourceFiles) {
  188. FileCoverageSummary Totals = FileCoverageSummary("Totals");
  189. auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
  190. SourceFiles, Options);
  191. renderFiles(OS, Coverage, SourceFiles, FileReports, Options.ExportSummaryOnly,
  192. Options.SkipFunctions, Options.SkipBranches);
  193. }