diff --git a/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp b/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp index ad7561d3dc62c..5c002a694f66a 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 &Branches) { +static auto sumBranches(const ArrayRef &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 Expansions) { +static BranchCoverageInfo +sumBranchExpansions(const CoverageMapping &CM, + ArrayRef 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 -sumMCDCPairs(const ArrayRef &Records) { +auto sumMCDCPairs(const ArrayRef &Records) { size_t NumPairs = 0, CoveredPairs = 0; for (const auto &Record : Records) { const auto NumConditions = Record.getNumConditions(); @@ -56,7 +59,7 @@ sumMCDCPairs(const ArrayRef &Records) { ++CoveredPairs; } } - return {NumPairs, CoveredPairs}; + return MCDCCoverageInfo(CoveredPairs, NumPairs); } static std::pair @@ -85,24 +88,27 @@ sumRegions(ArrayRef CodeRegions, const CoverageData &CD) { LineCoverageInfo(CoveredLines, NumLines)}; } +CoverageDataSummary::CoverageDataSummary(const CoverageData &CD, + ArrayRef 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 64c2c8406cf3e..d9210676c41bf 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 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(this) += RHS; FunctionCoverage += RHS.FunctionCoverage; - BranchCoverage += RHS.BranchCoverage; - MCDCCoverage += RHS.MCDCCoverage; InstantiationCoverage += RHS.InstantiationCoverage; return *this; }