blob: d149ba1a4c87423834885dc0fc5cc04b4ac21667 [file] [log] [blame]
Max Morozb2091c92018-11-09 16:10:44 +00001//===- CoverageExporterLcov.cpp - Code coverage export --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements export of code coverage data to lcov trace file format.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15//
16// The trace file code coverage export follows the following format (see also
17// https://linux.die.net/man/1/geninfo). Each quoted string appears on its own
18// line; the indentation shown here is only for documentation purposes.
19//
20// - for each source file:
21// - "SF:<absolute path to source file>"
22// - for each function:
23// - "FN:<line number of function start>,<function name>"
24// - for each function:
25// - "FNDA:<execution count>,<function name>"
26// - "FNF:<number of functions found>"
27// - "FNH:<number of functions hit>"
28// - for each instrumented line:
29// - "DA:<line number>,<execution count>[,<checksum>]
30// - "LH:<number of lines with non-zero execution count>"
31// - "LF:<nubmer of instrumented lines>"
32// - "end_of_record"
33//
34// If the user is exporting summary information only, then the FN, FNDA, and DA
35// lines will not be present.
36//
37//===----------------------------------------------------------------------===//
38
39#include "CoverageExporterLcov.h"
40#include "CoverageReport.h"
41
42using namespace llvm;
43
44namespace {
45
46void renderFunctionSummary(raw_ostream &OS,
47 const FileCoverageSummary &Summary) {
48 OS << "FNF:" << Summary.FunctionCoverage.getNumFunctions() << '\n'
49 << "FNH:" << Summary.FunctionCoverage.getExecuted() << '\n';
50}
51
52void renderFunctions(
53 raw_ostream &OS,
54 const iterator_range<coverage::FunctionRecordIterator> &Functions) {
55 for (const auto &F : Functions) {
56 auto StartLine = F.CountedRegions.front().LineStart;
57 OS << "FN:" << StartLine << ',' << F.Name << '\n';
58 }
59 for (const auto &F : Functions)
60 OS << "FNDA:" << F.ExecutionCount << ',' << F.Name << '\n';
61}
62
63void renderLineExecutionCounts(raw_ostream &OS,
64 const coverage::CoverageData &FileCoverage) {
65 coverage::LineCoverageIterator LCI{FileCoverage, 1};
66 coverage::LineCoverageIterator LCIEnd = LCI.getEnd();
67 for (; LCI != LCIEnd; ++LCI) {
68 const coverage::LineCoverageStats &LCS = *LCI;
69 if (LCS.isMapped()) {
70 OS << "DA:" << LCS.getLine() << ',' << LCS.getExecutionCount() << '\n';
71 }
72 }
73}
74
75void renderLineSummary(raw_ostream &OS, const FileCoverageSummary &Summary) {
76 OS << "LF:" << Summary.LineCoverage.getNumLines() << '\n'
77 << "LH:" << Summary.LineCoverage.getCovered() << '\n';
78}
79
80void renderFile(raw_ostream &OS, const coverage::CoverageMapping &Coverage,
81 const std::string &Filename,
82 const FileCoverageSummary &FileReport, bool ExportSummaryOnly) {
83 OS << "SF:" << Filename << '\n';
84
85 if (!ExportSummaryOnly) {
86 renderFunctions(OS, Coverage.getCoveredFunctions());
87 }
88 renderFunctionSummary(OS, FileReport);
89
90 if (!ExportSummaryOnly) {
91 // Calculate and render detailed coverage information for given file.
92 auto FileCoverage = Coverage.getCoverageForFile(Filename);
93 renderLineExecutionCounts(OS, FileCoverage);
94 }
95 renderLineSummary(OS, FileReport);
96
97 OS << "end_of_record\n";
98}
99
100void renderFiles(raw_ostream &OS, const coverage::CoverageMapping &Coverage,
101 ArrayRef<std::string> SourceFiles,
102 ArrayRef<FileCoverageSummary> FileReports,
103 bool ExportSummaryOnly) {
104 for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I)
105 renderFile(OS, Coverage, SourceFiles[I], FileReports[I], ExportSummaryOnly);
106}
107
108} // end anonymous namespace
109
110void CoverageExporterLcov::renderRoot(const CoverageFilters &IgnoreFilters) {
111 std::vector<std::string> SourceFiles;
112 for (StringRef SF : Coverage.getUniqueSourceFiles()) {
113 if (!IgnoreFilters.matchesFilename(SF))
114 SourceFiles.emplace_back(SF);
115 }
116 renderRoot(SourceFiles);
117}
118
119void CoverageExporterLcov::renderRoot(ArrayRef<std::string> SourceFiles) {
120 FileCoverageSummary Totals = FileCoverageSummary("Totals");
121 auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
122 SourceFiles, Options);
123 renderFiles(OS, Coverage, SourceFiles, FileReports,
124 Options.ExportSummaryOnly);
125}