Path: blob/main/contrib/llvm-project/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp
35231 views
//===- CoverageSummaryInfo.cpp - Coverage summary for function/file -------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// These structures are used to represent code coverage metrics9// for functions/files.10//11//===----------------------------------------------------------------------===//1213#include "CoverageSummaryInfo.h"1415using namespace llvm;16using namespace coverage;1718static void sumBranches(size_t &NumBranches, size_t &CoveredBranches,19const ArrayRef<CountedRegion> &Branches) {20for (const auto &BR : Branches) {21// Skip folded branches.22if (BR.Folded)23continue;2425// "True" Condition Branches.26++NumBranches;27if (BR.ExecutionCount > 0)28++CoveredBranches;29// "False" Condition Branches.30++NumBranches;31if (BR.FalseExecutionCount > 0)32++CoveredBranches;33}34}3536static void sumBranchExpansions(size_t &NumBranches, size_t &CoveredBranches,37const CoverageMapping &CM,38ArrayRef<ExpansionRecord> Expansions) {39for (const auto &Expansion : Expansions) {40auto CE = CM.getCoverageForExpansion(Expansion);41sumBranches(NumBranches, CoveredBranches, CE.getBranches());42sumBranchExpansions(NumBranches, CoveredBranches, CM, CE.getExpansions());43}44}4546static std::pair<size_t, size_t>47sumMCDCPairs(const ArrayRef<MCDCRecord> &Records) {48size_t NumPairs = 0, CoveredPairs = 0;49for (const auto &Record : Records) {50const auto NumConditions = Record.getNumConditions();51for (unsigned C = 0; C < NumConditions; C++) {52if (!Record.isCondFolded(C))53++NumPairs;54if (Record.isConditionIndependencePairCovered(C))55++CoveredPairs;56}57}58return {NumPairs, CoveredPairs};59}6061FunctionCoverageSummary62FunctionCoverageSummary::get(const CoverageMapping &CM,63const coverage::FunctionRecord &Function) {64// Compute the region coverage.65size_t NumCodeRegions = 0, CoveredRegions = 0;66for (auto &CR : Function.CountedRegions) {67if (CR.Kind != CounterMappingRegion::CodeRegion)68continue;69++NumCodeRegions;70if (CR.ExecutionCount != 0)71++CoveredRegions;72}7374// Compute the line coverage75size_t NumLines = 0, CoveredLines = 0;76CoverageData CD = CM.getCoverageForFunction(Function);77for (const auto &LCS : getLineCoverageStats(CD)) {78if (!LCS.isMapped())79continue;80++NumLines;81if (LCS.getExecutionCount())82++CoveredLines;83}8485// Compute the branch coverage, including branches from expansions.86size_t NumBranches = 0, CoveredBranches = 0;87sumBranches(NumBranches, CoveredBranches, CD.getBranches());88sumBranchExpansions(NumBranches, CoveredBranches, CM, CD.getExpansions());8990size_t NumPairs = 0, CoveredPairs = 0;91std::tie(NumPairs, CoveredPairs) = sumMCDCPairs(CD.getMCDCRecords());9293return FunctionCoverageSummary(94Function.Name, Function.ExecutionCount,95RegionCoverageInfo(CoveredRegions, NumCodeRegions),96LineCoverageInfo(CoveredLines, NumLines),97BranchCoverageInfo(CoveredBranches, NumBranches),98MCDCCoverageInfo(CoveredPairs, NumPairs));99}100101FunctionCoverageSummary102FunctionCoverageSummary::get(const InstantiationGroup &Group,103ArrayRef<FunctionCoverageSummary> Summaries) {104std::string Name;105if (Group.hasName()) {106Name = std::string(Group.getName());107} else {108llvm::raw_string_ostream OS(Name);109OS << "Definition at line " << Group.getLine() << ", column "110<< Group.getColumn();111}112113FunctionCoverageSummary Summary(Name);114Summary.ExecutionCount = Group.getTotalExecutionCount();115Summary.RegionCoverage = Summaries[0].RegionCoverage;116Summary.LineCoverage = Summaries[0].LineCoverage;117Summary.BranchCoverage = Summaries[0].BranchCoverage;118Summary.MCDCCoverage = Summaries[0].MCDCCoverage;119for (const auto &FCS : Summaries.drop_front()) {120Summary.RegionCoverage.merge(FCS.RegionCoverage);121Summary.LineCoverage.merge(FCS.LineCoverage);122Summary.BranchCoverage.merge(FCS.BranchCoverage);123Summary.MCDCCoverage.merge(FCS.MCDCCoverage);124}125return Summary;126}127128129