Coverage Report

Created: 2019-07-24 05:18

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