CoverageExporterLcov.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. Branches.insert(Branches.end(), NestedExBranches.begin(),
  85. NestedExBranches.end());
  86. // Add branches from this level of expansion.
  87. auto ExBranches = ExpansionCoverage.getBranches();
  88. for (auto B : ExBranches)
  89. if (B.FileID == Expansion.FileID) {
  90. B.LineStart = SrcLine;
  91. Branches.push_back(B);
  92. }
  93. }
  94. return Branches;
  95. }
  96. bool sortLine(llvm::coverage::CountedRegion I,
  97. llvm::coverage::CountedRegion J) {
  98. return (I.LineStart < J.LineStart) ||
  99. ((I.LineStart == J.LineStart) && (I.ColumnStart < J.ColumnStart));
  100. }
  101. void renderBranchExecutionCounts(raw_ostream &OS,
  102. const coverage::CoverageMapping &Coverage,
  103. const coverage::CoverageData &FileCoverage) {
  104. std::vector<llvm::coverage::CountedRegion> Branches =
  105. FileCoverage.getBranches();
  106. // Recursively collect branches for all file expansions.
  107. std::vector<llvm::coverage::CountedRegion> ExBranches =
  108. collectNestedBranches(Coverage, FileCoverage.getExpansions());
  109. // Append Expansion Branches to Source Branches.
  110. Branches.insert(Branches.end(), ExBranches.begin(), ExBranches.end());
  111. // Sort branches based on line number to ensure branches corresponding to the
  112. // same source line are counted together.
  113. llvm::sort(Branches, sortLine);
  114. auto NextBranch = Branches.begin();
  115. auto EndBranch = Branches.end();
  116. // Branches with the same source line are enumerated individually
  117. // (BranchIndex) as well as based on True/False pairs (PairIndex).
  118. while (NextBranch != EndBranch) {
  119. unsigned CurrentLine = NextBranch->LineStart;
  120. unsigned PairIndex = 0;
  121. unsigned BranchIndex = 0;
  122. while (NextBranch != EndBranch && CurrentLine == NextBranch->LineStart) {
  123. if (!NextBranch->Folded) {
  124. unsigned BC1 = NextBranch->ExecutionCount;
  125. unsigned BC2 = NextBranch->FalseExecutionCount;
  126. bool BranchNotExecuted = (BC1 == 0 && BC2 == 0);
  127. for (int I = 0; I < 2; I++, BranchIndex++) {
  128. OS << "BRDA:" << CurrentLine << ',' << PairIndex << ','
  129. << BranchIndex;
  130. if (BranchNotExecuted)
  131. OS << ',' << '-' << '\n';
  132. else
  133. OS << ',' << (I == 0 ? BC1 : BC2) << '\n';
  134. }
  135. PairIndex++;
  136. }
  137. NextBranch++;
  138. }
  139. }
  140. }
  141. void renderLineSummary(raw_ostream &OS, const FileCoverageSummary &Summary) {
  142. OS << "LF:" << Summary.LineCoverage.getNumLines() << '\n'
  143. << "LH:" << Summary.LineCoverage.getCovered() << '\n';
  144. }
  145. void renderBranchSummary(raw_ostream &OS, const FileCoverageSummary &Summary) {
  146. OS << "BRF:" << Summary.BranchCoverage.getNumBranches() << '\n'
  147. << "BFH:" << Summary.BranchCoverage.getCovered() << '\n';
  148. }
  149. void renderFile(raw_ostream &OS, const coverage::CoverageMapping &Coverage,
  150. const std::string &Filename,
  151. const FileCoverageSummary &FileReport, bool ExportSummaryOnly,
  152. bool SkipFunctions) {
  153. OS << "SF:" << Filename << '\n';
  154. if (!ExportSummaryOnly && !SkipFunctions) {
  155. renderFunctions(OS, Coverage.getCoveredFunctions(Filename));
  156. }
  157. renderFunctionSummary(OS, FileReport);
  158. if (!ExportSummaryOnly) {
  159. // Calculate and render detailed coverage information for given file.
  160. auto FileCoverage = Coverage.getCoverageForFile(Filename);
  161. renderLineExecutionCounts(OS, FileCoverage);
  162. renderBranchExecutionCounts(OS, Coverage, FileCoverage);
  163. }
  164. renderBranchSummary(OS, FileReport);
  165. renderLineSummary(OS, FileReport);
  166. OS << "end_of_record\n";
  167. }
  168. void renderFiles(raw_ostream &OS, const coverage::CoverageMapping &Coverage,
  169. ArrayRef<std::string> SourceFiles,
  170. ArrayRef<FileCoverageSummary> FileReports,
  171. bool ExportSummaryOnly, bool SkipFunctions) {
  172. for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I)
  173. renderFile(OS, Coverage, SourceFiles[I], FileReports[I], ExportSummaryOnly,
  174. SkipFunctions);
  175. }
  176. } // end anonymous namespace
  177. void CoverageExporterLcov::renderRoot(const CoverageFilters &IgnoreFilters) {
  178. std::vector<std::string> SourceFiles;
  179. for (StringRef SF : Coverage.getUniqueSourceFiles()) {
  180. if (!IgnoreFilters.matchesFilename(SF))
  181. SourceFiles.emplace_back(SF);
  182. }
  183. renderRoot(SourceFiles);
  184. }
  185. void CoverageExporterLcov::renderRoot(ArrayRef<std::string> SourceFiles) {
  186. FileCoverageSummary Totals = FileCoverageSummary("Totals");
  187. auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
  188. SourceFiles, Options);
  189. renderFiles(OS, Coverage, SourceFiles, FileReports, Options.ExportSummaryOnly,
  190. Options.SkipFunctions);
  191. }