1 //===- CoverageSummaryInfo.cpp - Coverage summary for function/file -------===//
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
7 //===----------------------------------------------------------------------===//
9 // These structures are used to represent code coverage metrics
10 // for functions/files.
12 //===----------------------------------------------------------------------===//
14 #include "CoverageSummaryInfo.h"
17 using namespace coverage
;
19 static void sumBranches(size_t &NumBranches
, size_t &CoveredBranches
,
20 const ArrayRef
<CountedRegion
> &Branches
) {
21 for (const auto &BR
: Branches
) {
22 // Skip folded branches.
26 // "True" Condition Branches.
28 if (BR
.ExecutionCount
> 0)
30 // "False" Condition Branches.
32 if (BR
.FalseExecutionCount
> 0)
37 static void sumBranchExpansions(size_t &NumBranches
, size_t &CoveredBranches
,
38 const CoverageMapping
&CM
,
39 ArrayRef
<ExpansionRecord
> Expansions
) {
40 for (const auto &Expansion
: Expansions
) {
41 auto CE
= CM
.getCoverageForExpansion(Expansion
);
42 sumBranches(NumBranches
, CoveredBranches
, CE
.getBranches());
43 sumBranchExpansions(NumBranches
, CoveredBranches
, CM
, CE
.getExpansions());
47 FunctionCoverageSummary
48 FunctionCoverageSummary::get(const CoverageMapping
&CM
,
49 const coverage::FunctionRecord
&Function
) {
50 // Compute the region coverage.
51 size_t NumCodeRegions
= 0, CoveredRegions
= 0;
52 for (auto &CR
: Function
.CountedRegions
) {
53 if (CR
.Kind
!= CounterMappingRegion::CodeRegion
)
56 if (CR
.ExecutionCount
!= 0)
60 // Compute the line coverage
61 size_t NumLines
= 0, CoveredLines
= 0;
62 CoverageData CD
= CM
.getCoverageForFunction(Function
);
63 for (const auto &LCS
: getLineCoverageStats(CD
)) {
67 if (LCS
.getExecutionCount())
71 // Compute the branch coverage, including branches from expansions.
72 size_t NumBranches
= 0, CoveredBranches
= 0;
73 sumBranches(NumBranches
, CoveredBranches
, CD
.getBranches());
74 sumBranchExpansions(NumBranches
, CoveredBranches
, CM
, CD
.getExpansions());
76 return FunctionCoverageSummary(
77 Function
.Name
, Function
.ExecutionCount
,
78 RegionCoverageInfo(CoveredRegions
, NumCodeRegions
),
79 LineCoverageInfo(CoveredLines
, NumLines
),
80 BranchCoverageInfo(CoveredBranches
, NumBranches
));
83 FunctionCoverageSummary
84 FunctionCoverageSummary::get(const InstantiationGroup
&Group
,
85 ArrayRef
<FunctionCoverageSummary
> Summaries
) {
87 if (Group
.hasName()) {
88 Name
= std::string(Group
.getName());
90 llvm::raw_string_ostream
OS(Name
);
91 OS
<< "Definition at line " << Group
.getLine() << ", column "
95 FunctionCoverageSummary
Summary(Name
);
96 Summary
.ExecutionCount
= Group
.getTotalExecutionCount();
97 Summary
.RegionCoverage
= Summaries
[0].RegionCoverage
;
98 Summary
.LineCoverage
= Summaries
[0].LineCoverage
;
99 Summary
.BranchCoverage
= Summaries
[0].BranchCoverage
;
100 for (const auto &FCS
: Summaries
.drop_front()) {
101 Summary
.RegionCoverage
.merge(FCS
.RegionCoverage
);
102 Summary
.LineCoverage
.merge(FCS
.LineCoverage
);
103 Summary
.BranchCoverage
.merge(FCS
.BranchCoverage
);