2018-11-10 00:10:44 +08:00
|
|
|
//===- CoverageExporterLcov.cpp - Code coverage export --------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-11-10 00:10:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements export of code coverage data to lcov trace file format.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The trace file code coverage export follows the following format (see also
|
|
|
|
// https://linux.die.net/man/1/geninfo). Each quoted string appears on its own
|
|
|
|
// line; the indentation shown here is only for documentation purposes.
|
|
|
|
//
|
|
|
|
// - for each source file:
|
|
|
|
// - "SF:<absolute path to source file>"
|
|
|
|
// - for each function:
|
|
|
|
// - "FN:<line number of function start>,<function name>"
|
|
|
|
// - for each function:
|
|
|
|
// - "FNDA:<execution count>,<function name>"
|
|
|
|
// - "FNF:<number of functions found>"
|
|
|
|
// - "FNH:<number of functions hit>"
|
|
|
|
// - for each instrumented line:
|
|
|
|
// - "DA:<line number>,<execution count>[,<checksum>]
|
|
|
|
// - "LH:<number of lines with non-zero execution count>"
|
|
|
|
// - "LF:<nubmer of instrumented lines>"
|
|
|
|
// - "end_of_record"
|
|
|
|
//
|
|
|
|
// If the user is exporting summary information only, then the FN, FNDA, and DA
|
|
|
|
// lines will not be present.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CoverageExporterLcov.h"
|
|
|
|
#include "CoverageReport.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void renderFunctionSummary(raw_ostream &OS,
|
|
|
|
const FileCoverageSummary &Summary) {
|
|
|
|
OS << "FNF:" << Summary.FunctionCoverage.getNumFunctions() << '\n'
|
|
|
|
<< "FNH:" << Summary.FunctionCoverage.getExecuted() << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderFunctions(
|
|
|
|
raw_ostream &OS,
|
|
|
|
const iterator_range<coverage::FunctionRecordIterator> &Functions) {
|
|
|
|
for (const auto &F : Functions) {
|
|
|
|
auto StartLine = F.CountedRegions.front().LineStart;
|
|
|
|
OS << "FN:" << StartLine << ',' << F.Name << '\n';
|
|
|
|
}
|
|
|
|
for (const auto &F : Functions)
|
|
|
|
OS << "FNDA:" << F.ExecutionCount << ',' << F.Name << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderLineExecutionCounts(raw_ostream &OS,
|
|
|
|
const coverage::CoverageData &FileCoverage) {
|
|
|
|
coverage::LineCoverageIterator LCI{FileCoverage, 1};
|
|
|
|
coverage::LineCoverageIterator LCIEnd = LCI.getEnd();
|
|
|
|
for (; LCI != LCIEnd; ++LCI) {
|
|
|
|
const coverage::LineCoverageStats &LCS = *LCI;
|
|
|
|
if (LCS.isMapped()) {
|
|
|
|
OS << "DA:" << LCS.getLine() << ',' << LCS.getExecutionCount() << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderLineSummary(raw_ostream &OS, const FileCoverageSummary &Summary) {
|
|
|
|
OS << "LF:" << Summary.LineCoverage.getNumLines() << '\n'
|
|
|
|
<< "LH:" << Summary.LineCoverage.getCovered() << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderFile(raw_ostream &OS, const coverage::CoverageMapping &Coverage,
|
|
|
|
const std::string &Filename,
|
2020-01-23 04:48:25 +08:00
|
|
|
const FileCoverageSummary &FileReport, bool ExportSummaryOnly,
|
|
|
|
bool SkipFunctions) {
|
2018-11-10 00:10:44 +08:00
|
|
|
OS << "SF:" << Filename << '\n';
|
|
|
|
|
2020-01-23 04:48:25 +08:00
|
|
|
if (!ExportSummaryOnly && !SkipFunctions) {
|
2019-06-28 23:38:25 +08:00
|
|
|
renderFunctions(OS, Coverage.getCoveredFunctions(Filename));
|
2018-11-10 00:10:44 +08:00
|
|
|
}
|
|
|
|
renderFunctionSummary(OS, FileReport);
|
|
|
|
|
|
|
|
if (!ExportSummaryOnly) {
|
|
|
|
// Calculate and render detailed coverage information for given file.
|
|
|
|
auto FileCoverage = Coverage.getCoverageForFile(Filename);
|
|
|
|
renderLineExecutionCounts(OS, FileCoverage);
|
|
|
|
}
|
|
|
|
renderLineSummary(OS, FileReport);
|
|
|
|
|
|
|
|
OS << "end_of_record\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderFiles(raw_ostream &OS, const coverage::CoverageMapping &Coverage,
|
|
|
|
ArrayRef<std::string> SourceFiles,
|
|
|
|
ArrayRef<FileCoverageSummary> FileReports,
|
2020-01-23 04:48:25 +08:00
|
|
|
bool ExportSummaryOnly, bool SkipFunctions) {
|
2018-11-10 00:10:44 +08:00
|
|
|
for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I)
|
2020-01-23 04:48:25 +08:00
|
|
|
renderFile(OS, Coverage, SourceFiles[I], FileReports[I], ExportSummaryOnly,
|
|
|
|
SkipFunctions);
|
2018-11-10 00:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2019-10-30 13:38:38 +08:00
|
|
|
void CoverageExporterLcov::renderRoot(const CoverageFilters &IgnoreFilters) {
|
2018-11-10 00:10:44 +08:00
|
|
|
std::vector<std::string> SourceFiles;
|
|
|
|
for (StringRef SF : Coverage.getUniqueSourceFiles()) {
|
2019-10-30 13:38:38 +08:00
|
|
|
if (!IgnoreFilters.matchesFilename(SF))
|
2018-11-10 00:10:44 +08:00
|
|
|
SourceFiles.emplace_back(SF);
|
|
|
|
}
|
|
|
|
renderRoot(SourceFiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageExporterLcov::renderRoot(ArrayRef<std::string> SourceFiles) {
|
|
|
|
FileCoverageSummary Totals = FileCoverageSummary("Totals");
|
|
|
|
auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
|
|
|
|
SourceFiles, Options);
|
2020-01-23 04:48:25 +08:00
|
|
|
renderFiles(OS, Coverage, SourceFiles, FileReports, Options.ExportSummaryOnly,
|
|
|
|
Options.SkipFunctions);
|
2018-11-10 00:10:44 +08:00
|
|
|
}
|