[llvm] a21f13b - llvm-cov: Refactor CoverageSummaryInfo. NFC. (#121189)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 28 00:49:13 PST 2024
Author: NAKAMURA Takumi
Date: 2024-12-28T17:49:10+09:00
New Revision: a21f13bde2564a691a2da49adb773816f6c4e06b
URL: https://github.com/llvm/llvm-project/commit/a21f13bde2564a691a2da49adb773816f6c4e06b
DIFF: https://github.com/llvm/llvm-project/commit/a21f13bde2564a691a2da49adb773816f6c4e06b.diff
LOG: llvm-cov: Refactor CoverageSummaryInfo. NFC. (#121189)
- Let subfunctions return `CoverageInfo` objects w/o accumulating
reference values.
- Introduce `CoverageDataSummary` for handling `CoverageData`-oriented
metrics.
Added:
Modified:
llvm/tools/llvm-cov/CoverageSummaryInfo.cpp
llvm/tools/llvm-cov/CoverageSummaryInfo.h
Removed:
################################################################################
diff --git a/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp b/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp
index ad7561d3dc62c0..5c002a694f66ae 100644
--- a/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp
+++ b/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp
@@ -16,8 +16,9 @@
using namespace llvm;
using namespace coverage;
-static void sumBranches(size_t &NumBranches, size_t &CoveredBranches,
- const ArrayRef<CountedRegion> &Branches) {
+static auto sumBranches(const ArrayRef<CountedRegion> &Branches) {
+ size_t NumBranches = 0;
+ size_t CoveredBranches = 0;
for (const auto &BR : Branches) {
if (!BR.TrueFolded) {
// "True" Condition Branches.
@@ -32,20 +33,22 @@ static void sumBranches(size_t &NumBranches, size_t &CoveredBranches,
++CoveredBranches;
}
}
+ return BranchCoverageInfo(CoveredBranches, NumBranches);
}
-static void sumBranchExpansions(size_t &NumBranches, size_t &CoveredBranches,
- const CoverageMapping &CM,
- ArrayRef<ExpansionRecord> Expansions) {
+static BranchCoverageInfo
+sumBranchExpansions(const CoverageMapping &CM,
+ ArrayRef<ExpansionRecord> Expansions) {
+ BranchCoverageInfo BranchCoverage;
for (const auto &Expansion : Expansions) {
auto CE = CM.getCoverageForExpansion(Expansion);
- sumBranches(NumBranches, CoveredBranches, CE.getBranches());
- sumBranchExpansions(NumBranches, CoveredBranches, CM, CE.getExpansions());
+ BranchCoverage += sumBranches(CE.getBranches());
+ BranchCoverage += sumBranchExpansions(CM, CE.getExpansions());
}
+ return BranchCoverage;
}
-static std::pair<size_t, size_t>
-sumMCDCPairs(const ArrayRef<MCDCRecord> &Records) {
+auto sumMCDCPairs(const ArrayRef<MCDCRecord> &Records) {
size_t NumPairs = 0, CoveredPairs = 0;
for (const auto &Record : Records) {
const auto NumConditions = Record.getNumConditions();
@@ -56,7 +59,7 @@ sumMCDCPairs(const ArrayRef<MCDCRecord> &Records) {
++CoveredPairs;
}
}
- return {NumPairs, CoveredPairs};
+ return MCDCCoverageInfo(CoveredPairs, NumPairs);
}
static std::pair<RegionCoverageInfo, LineCoverageInfo>
@@ -85,24 +88,27 @@ sumRegions(ArrayRef<CountedRegion> CodeRegions, const CoverageData &CD) {
LineCoverageInfo(CoveredLines, NumLines)};
}
+CoverageDataSummary::CoverageDataSummary(const CoverageData &CD,
+ ArrayRef<CountedRegion> CodeRegions) {
+ std::tie(RegionCoverage, LineCoverage) = sumRegions(CodeRegions, CD);
+ BranchCoverage = sumBranches(CD.getBranches());
+ MCDCCoverage = sumMCDCPairs(CD.getMCDCRecords());
+}
+
FunctionCoverageSummary
FunctionCoverageSummary::get(const CoverageMapping &CM,
const coverage::FunctionRecord &Function) {
CoverageData CD = CM.getCoverageForFunction(Function);
- auto [RegionCoverage, LineCoverage] = sumRegions(Function.CountedRegions, CD);
- // Compute the branch coverage, including branches from expansions.
- size_t NumBranches = 0, CoveredBranches = 0;
- sumBranches(NumBranches, CoveredBranches, CD.getBranches());
- sumBranchExpansions(NumBranches, CoveredBranches, CM, CD.getExpansions());
+ auto Summary =
+ FunctionCoverageSummary(Function.Name, Function.ExecutionCount);
- size_t NumPairs = 0, CoveredPairs = 0;
- std::tie(NumPairs, CoveredPairs) = sumMCDCPairs(CD.getMCDCRecords());
+ Summary += CoverageDataSummary(CD, Function.CountedRegions);
- return FunctionCoverageSummary(
- Function.Name, Function.ExecutionCount, RegionCoverage, LineCoverage,
- BranchCoverageInfo(CoveredBranches, NumBranches),
- MCDCCoverageInfo(CoveredPairs, NumPairs));
+ // Compute the branch coverage, including branches from expansions.
+ Summary.BranchCoverage += sumBranchExpansions(CM, CD.getExpansions());
+
+ return Summary;
}
FunctionCoverageSummary
@@ -117,8 +123,7 @@ FunctionCoverageSummary::get(const InstantiationGroup &Group,
<< Group.getColumn();
}
- FunctionCoverageSummary Summary(Name);
- Summary.ExecutionCount = Group.getTotalExecutionCount();
+ FunctionCoverageSummary Summary(Name, Group.getTotalExecutionCount());
Summary.RegionCoverage = Summaries[0].RegionCoverage;
Summary.LineCoverage = Summaries[0].LineCoverage;
Summary.BranchCoverage = Summaries[0].BranchCoverage;
diff --git a/llvm/tools/llvm-cov/CoverageSummaryInfo.h b/llvm/tools/llvm-cov/CoverageSummaryInfo.h
index 64c2c8406cf3eb..d9210676c41bf3 100644
--- a/llvm/tools/llvm-cov/CoverageSummaryInfo.h
+++ b/llvm/tools/llvm-cov/CoverageSummaryInfo.h
@@ -223,26 +223,32 @@ class FunctionCoverageInfo {
}
};
-/// A summary of function's code coverage.
-struct FunctionCoverageSummary {
- std::string Name;
- uint64_t ExecutionCount;
+struct CoverageDataSummary {
RegionCoverageInfo RegionCoverage;
LineCoverageInfo LineCoverage;
BranchCoverageInfo BranchCoverage;
MCDCCoverageInfo MCDCCoverage;
- FunctionCoverageSummary(const std::string &Name)
- : Name(Name), ExecutionCount(0) {}
+ CoverageDataSummary() = default;
+ CoverageDataSummary(const coverage::CoverageData &CD,
+ ArrayRef<coverage::CountedRegion> CodeRegions);
- FunctionCoverageSummary(const std::string &Name, uint64_t ExecutionCount,
- const RegionCoverageInfo &RegionCoverage,
- const LineCoverageInfo &LineCoverage,
- const BranchCoverageInfo &BranchCoverage,
- const MCDCCoverageInfo &MCDCCoverage)
- : Name(Name), ExecutionCount(ExecutionCount),
- RegionCoverage(RegionCoverage), LineCoverage(LineCoverage),
- BranchCoverage(BranchCoverage), MCDCCoverage(MCDCCoverage) {}
+ auto &operator+=(const CoverageDataSummary &RHS) {
+ RegionCoverage += RHS.RegionCoverage;
+ LineCoverage += RHS.LineCoverage;
+ BranchCoverage += RHS.BranchCoverage;
+ MCDCCoverage += RHS.MCDCCoverage;
+ return *this;
+ }
+};
+
+/// A summary of function's code coverage.
+struct FunctionCoverageSummary : CoverageDataSummary {
+ std::string Name;
+ uint64_t ExecutionCount;
+
+ FunctionCoverageSummary(const std::string &Name, uint64_t ExecutionCount = 0)
+ : Name(Name), ExecutionCount(ExecutionCount) {}
/// Compute the code coverage summary for the given function coverage
/// mapping record.
@@ -257,12 +263,8 @@ struct FunctionCoverageSummary {
};
/// A summary of file's code coverage.
-struct FileCoverageSummary {
+struct FileCoverageSummary : CoverageDataSummary {
StringRef Name;
- RegionCoverageInfo RegionCoverage;
- LineCoverageInfo LineCoverage;
- BranchCoverageInfo BranchCoverage;
- MCDCCoverageInfo MCDCCoverage;
FunctionCoverageInfo FunctionCoverage;
FunctionCoverageInfo InstantiationCoverage;
@@ -270,11 +272,8 @@ struct FileCoverageSummary {
FileCoverageSummary(StringRef Name) : Name(Name) {}
FileCoverageSummary &operator+=(const FileCoverageSummary &RHS) {
- RegionCoverage += RHS.RegionCoverage;
- LineCoverage += RHS.LineCoverage;
+ *static_cast<CoverageDataSummary *>(this) += RHS;
FunctionCoverage += RHS.FunctionCoverage;
- BranchCoverage += RHS.BranchCoverage;
- MCDCCoverage += RHS.MCDCCoverage;
InstantiationCoverage += RHS.InstantiationCoverage;
return *this;
}
More information about the llvm-commits
mailing list