xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 //===- CoverageSummaryInfo.cpp - Coverage summary for function/file -------===//
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 // These structures are used to represent code coverage metrics
10 // for functions/files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CoverageSummaryInfo.h"
15 
16 using namespace llvm;
17 using namespace coverage;
18 
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.
23     if (BR.Folded)
24       continue;
25 
26     // "True" Condition Branches.
27     ++NumBranches;
28     if (BR.ExecutionCount > 0)
29       ++CoveredBranches;
30     // "False" Condition Branches.
31     ++NumBranches;
32     if (BR.FalseExecutionCount > 0)
33       ++CoveredBranches;
34   }
35 }
36 
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());
44   }
45 }
46 
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)
54       continue;
55     ++NumCodeRegions;
56     if (CR.ExecutionCount != 0)
57       ++CoveredRegions;
58   }
59 
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)) {
64     if (!LCS.isMapped())
65       continue;
66     ++NumLines;
67     if (LCS.getExecutionCount())
68       ++CoveredLines;
69   }
70 
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());
75 
76   return FunctionCoverageSummary(
77       Function.Name, Function.ExecutionCount,
78       RegionCoverageInfo(CoveredRegions, NumCodeRegions),
79       LineCoverageInfo(CoveredLines, NumLines),
80       BranchCoverageInfo(CoveredBranches, NumBranches));
81 }
82 
83 FunctionCoverageSummary
84 FunctionCoverageSummary::get(const InstantiationGroup &Group,
85                              ArrayRef<FunctionCoverageSummary> Summaries) {
86   std::string Name;
87   if (Group.hasName()) {
88     Name = std::string(Group.getName());
89   } else {
90     llvm::raw_string_ostream OS(Name);
91     OS << "Definition at line " << Group.getLine() << ", column "
92        << Group.getColumn();
93   }
94 
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);
104   }
105   return Summary;
106 }
107