diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h index 81307d7b025d9..39dd47a26f2f2 100644 --- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h +++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h @@ -60,6 +60,12 @@ namespace coverage { class CoverageMappingReader; struct CoverageMappingRecord; +enum class MergeStrategy { + Merge, + Any, + All, +}; + enum class coveragemap_error { success = 0, eof, @@ -384,6 +390,32 @@ struct CountedRegion : public CounterMappingRegion { : CounterMappingRegion(R), ExecutionCount(ExecutionCount), FalseExecutionCount(FalseExecutionCount), TrueFolded(false), FalseFolded(false) {} + + LineColPair viewLoc() const { return startLoc(); } + + bool isMergeable(const CountedRegion &RHS) const { + return (this->viewLoc() == RHS.viewLoc()); + } + + void merge(const CountedRegion &RHS, MergeStrategy Strategy); + + /// Returns comparable rank value in selecting a better Record for merging. + auto getMergeRank(MergeStrategy Strategy) const { + assert(isBranch() && "Dedicated to Branch"); + assert(Strategy == MergeStrategy::Any && "Dedicated to Any"); + unsigned m = 0; + // Prefer both Counts have values. + m = (m << 1) | (ExecutionCount != 0 && FalseExecutionCount != 0); + // Prefer both are unfolded. + m = (m << 1) | (!TrueFolded && !FalseFolded); + // Prefer either Count has value. + m = (m << 1) | (ExecutionCount != 0 || FalseExecutionCount != 0); + // Prefer either is unfolded. + m = (m << 1) | (!TrueFolded || !FalseFolded); + return std::make_pair(m, ExecutionCount + FalseExecutionCount); + } + + void commit() const {} }; /// MCDC Record grouping all information together. @@ -448,6 +480,7 @@ struct MCDCRecord { } }; + using BitmapByCondTy = std::array; using TestVectors = llvm::SmallVector>; using BoolVector = std::array; using TVRowPair = std::pair; @@ -458,6 +491,7 @@ struct MCDCRecord { private: CounterMappingRegion Region; TestVectors TV; + BitmapByCondTy BitmapByCond; std::optional IndependencePairs; BoolVector Folded; CondIDMap PosToID; @@ -465,12 +499,28 @@ struct MCDCRecord { public: MCDCRecord(const CounterMappingRegion &Region, TestVectors &&TV, - BoolVector &&Folded, CondIDMap &&PosToID, LineColPairMap &&CondLoc) - : Region(Region), TV(std::move(TV)), Folded(std::move(Folded)), + BitmapByCondTy &&BitmapByCond, BoolVector &&Folded, + CondIDMap &&PosToID, LineColPairMap &&CondLoc) + : Region(Region), TV(std::move(TV)), + BitmapByCond(std::move(BitmapByCond)), Folded(std::move(Folded)), PosToID(std::move(PosToID)), CondLoc(std::move(CondLoc)) { findIndependencePairs(); } + inline LineColPair viewLoc() const { return Region.endLoc(); } + + bool isMergeable(const MCDCRecord &RHS) const { + return (this->viewLoc() == RHS.viewLoc() && + this->BitmapByCond[false].size() == RHS.BitmapByCond[true].size() && + this->PosToID == RHS.PosToID && this->CondLoc == RHS.CondLoc); + } + + // This may invalidate IndependencePairs + // MCDCRecord &operator+=(const MCDCRecord &RHS); + void merge(MCDCRecord &&RHS, MergeStrategy Strategy); + + void commit() { findIndependencePairs(); } + // Compare executed test vectors against each other to find an independence // pairs for each condition. This processing takes the most time. void findIndependencePairs(); @@ -523,15 +573,43 @@ struct MCDCRecord { return (*IndependencePairs)[PosToID[Condition]]; } - float getPercentCovered() const { - unsigned Folded = 0; + std::pair getCoveredCount() const { unsigned Covered = 0; + unsigned Folded = 0; for (unsigned C = 0; C < getNumConditions(); C++) { if (isCondFolded(C)) Folded++; else if (isConditionIndependencePairCovered(C)) Covered++; } + return {Covered, Folded}; + } + + /// Returns comparable rank value in selecting a better Record for merging. + std::tuple + getMergeRank(MergeStrategy Strategy) const { + auto [Covered, Folded] = getCoveredCount(); + auto NumTVs = getNumTestVectors(); + switch (Strategy) { + default: + llvm_unreachable("Not supported"); + case MergeStrategy::Any: + return { + Covered, // The largest covered number + ~Folded, // Less folded is better + NumTVs, // Show more test vectors + }; + case MergeStrategy::All: + return { + ~Covered, // The smallest covered number + ~Folded, // Less folded is better + NumTVs, // Show more test vectors + }; + } + } + + float getPercentCovered() const { + auto [Covered, Folded] = getCoveredCount(); unsigned Total = getNumConditions() - Folded; if (Total == 0) @@ -900,6 +978,7 @@ class InstantiationGroup { class CoverageData { friend class CoverageMapping; +protected: std::string Filename; std::vector Segments; std::vector Expansions; @@ -914,6 +993,8 @@ class CoverageData { CoverageData(bool Single, StringRef Filename) : Filename(Filename), SingleByteCoverage(Single) {} + CoverageData(CoverageData &&RHS) = default; + /// Get the name of the file this data covers. StringRef getFilename() const { return Filename; } @@ -1020,10 +1101,14 @@ class CoverageMapping { /// The given filename must be the name as recorded in the coverage /// information. That is, only names returned from getUniqueSourceFiles will /// yield a result. - CoverageData getCoverageForFile(StringRef Filename) const; + CoverageData getCoverageForFile( + StringRef Filename, MergeStrategy Strategy = MergeStrategy::Merge, + const DenseSet &FilteredOutFunctions = {}) const; /// Get the coverage for a particular function. - CoverageData getCoverageForFunction(const FunctionRecord &Function) const; + CoverageData + getCoverageForFunction(const FunctionRecord &Function, + MergeStrategy Strategy = MergeStrategy::Merge) const; /// Get the coverage for an expansion within a coverage set. CoverageData getCoverageForExpansion(const ExpansionRecord &Expansion) const; diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp index 6d6678e9e4afe..35fcd0f098a2d 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -246,6 +247,96 @@ Expected CounterMappingContext::evaluate(const Counter &C) const { return LastPoppedValue; } +void CountedRegion::merge(const CountedRegion &RHS, MergeStrategy Strategy) { + assert(this->isBranch() && RHS.isBranch()); + auto MergeCounts = [Strategy](uint64_t &LHSCount, bool &LHSFolded, + uint64_t RHSCount, bool RHSFolded) { + switch (Strategy) { + default: + llvm_unreachable("Don't perform by-parameter merging"); + case MergeStrategy::Merge: + LHSCount += RHSCount; + LHSFolded = (LHSFolded && !LHSCount && RHSFolded); + break; + case MergeStrategy::All: + LHSCount = (LHSFolded ? RHSCount + : RHSFolded ? LHSCount + : std::min(LHSCount, RHSCount)); + LHSFolded = (LHSFolded && RHSFolded); + break; + } + }; + + switch (Strategy) { + case MergeStrategy::Any: + // Take either better (more satisfied) hand side. + // FIXME: Really needed? Better just to merge? + if (this->getMergeRank(Strategy) < RHS.getMergeRank(Strategy)) + *this = RHS; + break; + case MergeStrategy::Merge: + case MergeStrategy::All: + // Able to merge by parameter. + MergeCounts(this->ExecutionCount, this->TrueFolded, RHS.ExecutionCount, + RHS.TrueFolded); + MergeCounts(this->FalseExecutionCount, this->FalseFolded, + RHS.FalseExecutionCount, RHS.FalseFolded); + break; + } +} + +void MCDCRecord::merge(MCDCRecord &&RHS, MergeStrategy Strategy) { + assert(this->TV.size() == + this->BitmapByCond[false].count() + this->BitmapByCond[true].count()); + assert(RHS.TV.size() == + RHS.BitmapByCond[false].count() + RHS.BitmapByCond[true].count()); + assert(this->PosToID == RHS.PosToID); + assert(this->CondLoc == RHS.CondLoc); + + switch (Strategy) { + case MergeStrategy::Merge: + break; + case MergeStrategy::Any: + case MergeStrategy::All: + if (this->getMergeRank(Strategy) < RHS.getMergeRank(Strategy)) + *this = std::move(RHS); + return; + } + + std::array LHSTV; + auto LHSI = this->TV.begin(); + auto RHSI = RHS.TV.begin(); + bool Merged = false; + for (auto MCDCCond : {MCDCRecord::MCDC_False, MCDCRecord::MCDC_True}) { + auto &LHSBitmap = this->BitmapByCond[MCDCCond]; + auto &RHSBitmap = RHS.BitmapByCond[MCDCCond]; + for (unsigned I = 0, E = LHSBitmap.size(); I != E; ++I) { + if (LHSBitmap[I]) { + if (RHSBitmap[I]) + ++RHSI; + LHSTV[LHSI->second].push_back(std::move(*LHSI++)); + } else if (RHSBitmap[I]) { + LHSTV[RHSI->second].push_back(std::move(*RHSI++)); + LHSBitmap[I] = true; + Merged = true; + } + } + + this->Folded[MCDCCond] &= RHS.Folded[MCDCCond]; + } + + if (Merged) + IndependencePairs.reset(); + + assert(LHSI == this->TV.end()); + assert(RHSI == RHS.TV.end()); + this->TV = std::move(LHSTV[false]); + this->TV.append(std::make_move_iterator(LHSTV[true].begin()), + std::make_move_iterator(LHSTV[true].end())); + assert(this->TV.size() == + this->BitmapByCond[false].count() + this->BitmapByCond[true].count()); +} + // Find an independence pair for each condition: // - The condition is true in one test and false in the other. // - The decision outcome is true one test and false in the other. @@ -257,13 +348,7 @@ void MCDCRecord::findIndependencePairs() { IndependencePairs.emplace(); unsigned NumTVs = TV.size(); - // Will be replaced to shorter expr. - unsigned TVTrueIdx = std::distance( - TV.begin(), - std::find_if(TV.begin(), TV.end(), - [&](auto I) { return (I.second == MCDCRecord::MCDC_True); }) - - ); + unsigned TVTrueIdx = BitmapByCond[false].count(); for (unsigned I = TVTrueIdx; I < NumTVs; ++I) { const auto &[A, ACond] = TV[I]; assert(ACond == MCDCRecord::MCDC_True); @@ -408,6 +493,7 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder { /// with a bit value of '1' indicates that the corresponding Test Vector /// identified by that index was executed. const BitVector &Bitmap; + MCDCRecord::BitmapByCondTy BitmapByCond; /// Decision Region to which the ExecutedTestVectorBitmap applies. const CounterMappingRegion &Region; @@ -426,13 +512,27 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder { /// Mapping of calculated MC/DC Independence Pairs for each condition. MCDCRecord::TVPairMap IndependencePairs; - /// Storage for ExecVectors - /// ExecVectors is the alias of its 0th element. - std::array ExecVectorsByCond; + /// Helper for sorting ExecVectors. + struct TVIdxTuple { + MCDCRecord::CondState MCDCCond; /// True/False + unsigned BIdx; /// Bitmap Index + unsigned Ord; /// Last position on ExecVectors + + TVIdxTuple(MCDCRecord::CondState MCDCCond, unsigned BIdx, unsigned Ord) + : MCDCCond(MCDCCond), BIdx(BIdx), Ord(Ord) {} + + bool operator<(const TVIdxTuple &RHS) const { + return (std::tie(this->MCDCCond, this->BIdx, this->Ord) < + std::tie(RHS.MCDCCond, RHS.BIdx, RHS.Ord)); + } + }; + + // Indices for sorted TestVectors; + std::vector ExecVectorIdxs; /// Actual executed Test Vectors for the boolean expression, based on /// ExecutedTestVectorBitmap. - MCDCRecord::TestVectors &ExecVectors; + MCDCRecord::TestVectors ExecVectors; #ifndef NDEBUG DenseSet TVIdxs; @@ -446,11 +546,11 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder { ArrayRef Branches, bool IsVersion11) : NextIDsBuilder(Branches), TVIdxBuilder(this->NextIDs), Bitmap(Bitmap), + BitmapByCond{{BitVector(NumTestVectors), BitVector(NumTestVectors)}}, Region(Region), DecisionParams(Region.getDecisionParams()), Branches(Branches), NumConditions(DecisionParams.NumConditions), Folded{{BitVector(NumConditions), BitVector(NumConditions)}}, - IndependencePairs(NumConditions), ExecVectors(ExecVectorsByCond[false]), - IsVersion11(IsVersion11) {} + IndependencePairs(NumConditions), IsVersion11(IsVersion11) {} private: // Walk the binary decision diagram and try assigning both false and true to @@ -478,10 +578,14 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder { : DecisionParams.BitmapIdx - NumTestVectors + NextTVIdx]) continue; + assert(!BitmapByCond[MCDCCond][NextTVIdx]); + BitmapByCond[MCDCCond][NextTVIdx] = true; + ExecVectorIdxs.emplace_back(MCDCCond, NextTVIdx, ExecVectors.size()); + // Copy the completed test vector to the vector of testvectors. // The final value (T,F) is equal to the last non-dontcare state on the // path (in a short-circuiting system). - ExecVectorsByCond[MCDCCond].push_back({TV, MCDCCond}); + ExecVectors.push_back({TV, MCDCCond}); } // Reset back to DontCare. @@ -500,12 +604,13 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder { assert(TVIdxs.size() == unsigned(NumTestVectors) && "TVIdxs wasn't fulfilled"); - // Fill ExecVectors order by False items and True items. - // ExecVectors is the alias of ExecVectorsByCond[false], so - // Append ExecVectorsByCond[true] on it. - auto &ExecVectorsT = ExecVectorsByCond[true]; - ExecVectors.append(std::make_move_iterator(ExecVectorsT.begin()), - std::make_move_iterator(ExecVectorsT.end())); + llvm::sort(ExecVectorIdxs); + MCDCRecord::TestVectors NewTestVectors; + for (const auto &IdxTuple : ExecVectorIdxs) + NewTestVectors.push_back(std::move(ExecVectors[IdxTuple.Ord])); + ExecVectors = std::move(NewTestVectors); + + assert(!BitmapByCond[false].anyCommon(BitmapByCond[true])); } public: @@ -544,8 +649,9 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder { findExecutedTestVectors(); // Record Test vectors, executed vectors, and independence pairs. - return MCDCRecord(Region, std::move(ExecVectors), std::move(Folded), - std::move(PosToID), std::move(CondLoc)); + return MCDCRecord(Region, std::move(ExecVectors), std::move(BitmapByCond), + std::move(Folded), std::move(PosToID), + std::move(CondLoc)); } }; @@ -1298,7 +1404,8 @@ class SegmentBuilder { /// Combine counts of regions which cover the same area. static ArrayRef - combineRegions(MutableArrayRef Regions) { + combineRegions(MutableArrayRef Regions, + MergeStrategy Strategy) { if (Regions.empty()) return Regions; auto Active = Regions.begin(); @@ -1324,8 +1431,22 @@ class SegmentBuilder { // value for that area. // We add counts of the regions of the same kind as the active region // to handle the both situations. - if (I->Kind == Active->Kind) + if (I->Kind != Active->Kind) + continue; + + switch (Strategy) { + case MergeStrategy::Merge: Active->ExecutionCount += I->ExecutionCount; + break; + case MergeStrategy::Any: + Active->ExecutionCount = + std::max(Active->ExecutionCount, I->ExecutionCount); + break; + case MergeStrategy::All: + Active->ExecutionCount = + std::min(Active->ExecutionCount, I->ExecutionCount); + break; + } } return Regions.drop_back(std::distance(++Active, End)); } @@ -1333,12 +1454,13 @@ class SegmentBuilder { public: /// Build a sorted list of CoverageSegments from a list of Regions. static std::vector - buildSegments(MutableArrayRef Regions) { + buildSegments(MutableArrayRef Regions, + MergeStrategy Strategy) { std::vector Segments; SegmentBuilder Builder(Segments); sortNestedRegions(Regions); - ArrayRef CombinedRegions = combineRegions(Regions); + ArrayRef CombinedRegions = combineRegions(Regions, Strategy); LLVM_DEBUG({ dbgs() << "Combined regions:\n"; @@ -1368,6 +1490,107 @@ class SegmentBuilder { } }; +template +static void mergeRecords(std::vector &Records, MergeStrategy Strategy) { + if (Records.empty()) + return; + + std::vector NewRecords; + + assert(Records.size() <= std::numeric_limits::max()); + + // Build up sorted indices (rather than pointers) of Records. + SmallVector BIdxs(Records.size()); + std::iota(BIdxs.begin(), BIdxs.end(), 0); + llvm::stable_sort(BIdxs, [&](auto A, auto B) { + auto StartA = Records[A].viewLoc(); + auto StartB = Records[B].viewLoc(); + return (std::tie(StartA, A) < std::tie(StartB, B)); + }); + + // 1st element should be stored into SubView. + auto I = BIdxs.begin(), E = BIdxs.end(); + SmallVector ViewRecords{Records[*I++]}; + + auto findMergeableInViewRecords = [&](const RecTy &Branch) { + auto I = ViewRecords.rbegin(), E = ViewRecords.rend(); + for (; I != E; ++I) + if (I->isMergeable(Branch)) + return I; + + // Not mergeable. + return E; + }; + + auto addRecordToSubView = [&] { + assert(!ViewRecords.empty() && "Should have the back"); + for (auto &Acc : ViewRecords) { + Acc.commit(); + NewRecords.push_back(std::move(Acc)); + } + }; + + for (; I != E; ++I) { + assert(!ViewRecords.empty() && "Should have the back in the loop"); + auto &AccB = ViewRecords.back(); + auto &Branch = Records[*I]; + + // Flush current and create the next SubView at the different line. + if (AccB.viewLoc().first != Branch.viewLoc().first) { + addRecordToSubView(); + ViewRecords = {Branch}; + } else if (auto AccI = findMergeableInViewRecords(Branch); + AccI != ViewRecords.rend()) { + // Merge the current Branch into the back of SubView. + AccI->merge(std::move(Branch), Strategy); + } else { + // Not mergeable. + ViewRecords.push_back(Branch); + } + } + + // Flush the last SubView. + addRecordToSubView(); + + // Replace + Records = std::move(NewRecords); +} + +struct MergeableCoverageData : public CoverageData { + std::vector CodeRegions; + MergeStrategy Strategy; + + MergeableCoverageData(MergeStrategy Strategy, bool Single, StringRef Filename) + : CoverageData(Single, Filename), Strategy(Strategy) {} + + void addFunctionRegions( + const FunctionRecord &Function, + std::function shouldProcess, + std::function shouldExpand) { + for (const auto &CR : Function.CountedRegions) + if (shouldProcess(CR)) { + CodeRegions.push_back(CR); + if (shouldExpand(CR)) + Expansions.emplace_back(CR, Function); + } + // Capture branch regions specific to the function (excluding expansions). + for (const auto &CR : Function.CountedBranchRegions) + if (shouldProcess(CR)) + BranchRegions.push_back(CR); + // Capture MCDC records specific to the function. + for (const auto &MR : Function.MCDCRecords) + if (shouldProcess(MR.getDecisionRegion())) + MCDCRecords.push_back(MR); + } + + CoverageData merge(MergeStrategy Strategy) { + mergeRecords(BranchRegions, Strategy); + mergeRecords(MCDCRecords, Strategy); + + Segments = SegmentBuilder::buildSegments(CodeRegions, Strategy); + return CoverageData(std::move(*this)); + } +}; } // end anonymous namespace std::vector CoverageMapping::getUniqueSourceFiles() const { @@ -1417,10 +1640,11 @@ static bool isExpansion(const CountedRegion &R, unsigned FileID) { return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID; } -CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const { +CoverageData CoverageMapping::getCoverageForFile( + StringRef Filename, MergeStrategy Strategy, + const DenseSet &FilteredOutFunctions) const { assert(SingleByteCoverage); - CoverageData FileCoverage(*SingleByteCoverage, Filename); - std::vector Regions; + MergeableCoverageData FileCoverage(Strategy, *SingleByteCoverage, Filename); // Look up the function records in the given file. Due to hash collisions on // the filename, we may get back some records that are not in the file. @@ -1428,28 +1652,18 @@ CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const { getImpreciseRecordIndicesForFilename(Filename); for (unsigned RecordIndex : RecordIndices) { const FunctionRecord &Function = Functions[RecordIndex]; + if (FilteredOutFunctions.count(&Function)) + continue; auto MainFileID = findMainViewFileID(Filename, Function); auto FileIDs = gatherFileIDs(Filename, Function); - for (const auto &CR : Function.CountedRegions) - if (FileIDs.test(CR.FileID)) { - Regions.push_back(CR); - if (MainFileID && isExpansion(CR, *MainFileID)) - FileCoverage.Expansions.emplace_back(CR, Function); - } - // Capture branch regions specific to the function (excluding expansions). - for (const auto &CR : Function.CountedBranchRegions) - if (FileIDs.test(CR.FileID)) - FileCoverage.BranchRegions.push_back(CR); - // Capture MCDC records specific to the function. - for (const auto &MR : Function.MCDCRecords) - if (FileIDs.test(MR.getDecisionRegion().FileID)) - FileCoverage.MCDCRecords.push_back(MR); + FileCoverage.addFunctionRegions( + Function, [&](auto &CR) { return FileIDs.test(CR.FileID); }, + [&](auto &CR) { return (MainFileID && isExpansion(CR, *MainFileID)); }); } LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n"); - FileCoverage.Segments = SegmentBuilder::buildSegments(Regions); - return FileCoverage; + return FileCoverage.merge(Strategy); } std::vector @@ -1478,36 +1692,23 @@ CoverageMapping::getInstantiationGroups(StringRef Filename) const { } CoverageData -CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const { +CoverageMapping::getCoverageForFunction(const FunctionRecord &Function, + MergeStrategy Strategy) const { auto MainFileID = findMainViewFileID(Function); if (!MainFileID) return CoverageData(); assert(SingleByteCoverage); - CoverageData FunctionCoverage(*SingleByteCoverage, - Function.Filenames[*MainFileID]); - std::vector Regions; - for (const auto &CR : Function.CountedRegions) - if (CR.FileID == *MainFileID) { - Regions.push_back(CR); - if (isExpansion(CR, *MainFileID)) - FunctionCoverage.Expansions.emplace_back(CR, Function); - } - // Capture branch regions specific to the function (excluding expansions). - for (const auto &CR : Function.CountedBranchRegions) - if (CR.FileID == *MainFileID) - FunctionCoverage.BranchRegions.push_back(CR); - - // Capture MCDC records specific to the function. - for (const auto &MR : Function.MCDCRecords) - if (MR.getDecisionRegion().FileID == *MainFileID) - FunctionCoverage.MCDCRecords.push_back(MR); + MergeableCoverageData FunctionCoverage(Strategy, *SingleByteCoverage, + Function.Filenames[*MainFileID]); + FunctionCoverage.addFunctionRegions( + Function, [&](auto &CR) { return (CR.FileID == *MainFileID); }, + [&](auto &CR) { return isExpansion(CR, *MainFileID); }); LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n"); - FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions); - return FunctionCoverage; + return FunctionCoverage.merge(Strategy); } CoverageData CoverageMapping::getCoverageForExpansion( @@ -1529,7 +1730,8 @@ CoverageData CoverageMapping::getCoverageForExpansion( LLVM_DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID << "\n"); - ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions); + ExpansionCoverage.Segments = + SegmentBuilder::buildSegments(Regions, MergeStrategy::Merge); return ExpansionCoverage; } diff --git a/llvm/test/tools/llvm-cov/Inputs/branch-templates.cpp b/llvm/test/tools/llvm-cov/Inputs/branch-templates.cpp index 4d932eaf5944a..d8a7c056f3677 100644 --- a/llvm/test/tools/llvm-cov/Inputs/branch-templates.cpp +++ b/llvm/test/tools/llvm-cov/Inputs/branch-templates.cpp @@ -11,9 +11,9 @@ void unused(T x) { template int func(T x) { - if(x) // BRCOV: | Branch ([[@LINE]]:6): [True: 0, False: 1] - return 0; // BRCOV: | Branch ([[@LINE-1]]:6): [True: 1, False: 0] - else // BRCOV: | Branch ([[@LINE-2]]:6): [True: 0, False: 1] + if(x) // BRCOV: | Branch ([[@LINE]]:6): [True: 1, False: {{2|1}}] + return 0; + else return 1; int j = 1; } diff --git a/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.cpp b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.cpp new file mode 100644 index 0000000000000..0f9b5cfbce633 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.cpp @@ -0,0 +1,56 @@ +#include + +template +bool ab(Ty a, Ty b) { + return (a && b); +} +// MERGE: [[@LINE-2]]| 4| return +// ANY: [[@LINE-3]]| 2| return +// ALL: [[@LINE-4]]| 0| return + +// MERGE: MC/DC Coverage for Decision{{[:]}} 100.00% +// ANY: MC/DC Coverage for Decision{{[:]}} 50.00% +// ALL: MC/DC Coverage for Decision{{[:]}} 0.00% +// CHECK-NOT: MC/DC Coverage for Decision{{[:]}} + +// CHECK: _Z2abIbEbT_S0_{{[:]}} +// CHECK: | | MC/DC Coverage for Decision{{[:]}} 50.00% + +// CHECK: _Z2abIxEbT_S0_{{[:]}} +// CHECK: | | MC/DC Coverage for Decision{{[:]}} 50.00% + +// CHECK: Unexecuted instantiation{{[:]}} _Z2abIdEbT_S0_ + +template +bool Cab(bool a, bool b) { + return (a && b && C); +} +// MERGE: [[@LINE-2]]| 4| return +// ANY: [[@LINE-3]]| 2| return +// ALL: [[@LINE-4]]| 2| return + +// MERGE: MC/DC Coverage for Decision{{[:]}} 100.00% +// ANY: MC/DC Coverage for Decision{{[:]}} 50.00% +// ALL: MC/DC Coverage for Decision{{[:]}} 0.00% +// CHECK-NOT: MC/DC Coverage for Decision{{[:]}} + +// CHECK: _Z3CabILb0EEbbb{{[:]}} +// CHECK: | | MC/DC Coverage for Decision{{[:]}} 0.00% + +// CHECK: _Z3CabILb1EEbbb{{[:]}} +// CHECK: | | MC/DC Coverage for Decision{{[:]}} 50.00% + +// CHECK: [[@LINE+1]]| 1|int main +int main(int argc, char **argv) { + printf("%d\n", Cab(false, false)); + printf("%d\n", Cab(true, true)); + printf("%d\n", Cab(true, false)); + printf("%d\n", Cab(true, true)); + printf("%d\n", ab(false, false)); + printf("%d\n", ab(true, true)); + printf("%d\n", ab(1LL, 0LL)); + printf("%d\n", ab(1LL, 1LL)); + if (argc == 2) + printf("%d\n", ab(0.0, 0.0)); + return 0; +} diff --git a/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.proftext b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.proftext new file mode 100644 index 0000000000000..61369462b2fd4 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.proftext @@ -0,0 +1,73 @@ +_Z2abIbEbT_S0_ +# Func Hash: +1550 +# Num Counters: +3 +# Counter Values: +2 +1 +1 +# Num Bitmap Bytes: +$1 +# Bitmap Byte Values: +0x5 + + +_Z2abIxEbT_S0_ +# Func Hash: +1550 +# Num Counters: +3 +# Counter Values: +2 +2 +1 +# Num Bitmap Bytes: +$1 +# Bitmap Byte Values: +0x6 + + +_Z3CabILb0EEbbb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +2 +1 +0 +1 +1 +# Num Bitmap Bytes: +$1 +# Bitmap Byte Values: +0x5 + + +_Z3CabILb1EEbbb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +2 +1 +1 +2 +1 +# Num Bitmap Bytes: +$1 +# Bitmap Byte Values: +0xa + + +main +# Func Hash: +175973464 +# Num Counters: +2 +# Counter Values: +1 +0 + diff --git a/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.yaml b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.yaml new file mode 100644 index 0000000000000..f017f75f24dce --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.yaml @@ -0,0 +1,105 @@ +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + OSABI: ELFOSABI_GNU + Type: ET_REL + Machine: EM_X86_64 + SectionHeaderStringTable: .strtab +Sections: + - Name: __llvm_covfun + Type: SHT_PROGBITS + Flags: [ SHF_GNU_RETAIN ] + AddressAlign: 0x8 + Content: FAD58DE7366495DB2500000058247D0A00000000249EC986A505B62F010101010505012C210C020109070010200502000700100500110185808080080501050021 + - Name: '__llvm_covfun (1)' + Type: SHT_PROGBITS + Flags: [ SHF_GNU_RETAIN ] + AddressAlign: 0x8 + Content: 9CC3B348501DBFE8480000008E83010000000000249EC986A505B62F010103010D0D1105090901191A020201010B000C01000B0011280403000B0016300D02010300000B000C0D0010001130110603020000100011050015001630000A02000000150016 + - Name: '__llvm_covfun (2)' + Type: SHT_PROGBITS + Flags: [ SHF_GNU_RETAIN ] + AddressAlign: 0x8 + Content: 9873627177A03F8E460000008E83010000000000249EC986A505B62F010102010D0D110901191A020201010B000C01000B0011280403000B0016300D02010300000B000C0D0010001130110603020000100011050015001630090002000000150016 + - Name: '__llvm_covfun (3)' + Type: SHT_PROGBITS + Flags: [ SHF_GNU_RETAIN ] + AddressAlign: 0x8 + Content: BF407A207503B266320000000E06000000000000249EC986A505B62F0101020105050906010415020201010B000C280302000B0011300502010200000B000C050010001130090602000000100011 + - Name: '__llvm_covfun (4)' + Type: SHT_PROGBITS + Flags: [ SHF_GNU_RETAIN ] + AddressAlign: 0x8 + Content: 8A05A22CB467C37D320000000E06000000000000249EC986A505B62F0101020105050906010415020201010B000C280302000B0011300502010200000B000C050010001130090602000000100011 + - Name: '__llvm_covfun (5)' + Type: SHT_PROGBITS + Flags: [ SHF_GNU_RETAIN ] + AddressAlign: 0x8 + Content: 1700192CAC8F3F26320000000E06000000000000249EC986A505B62F0101020105050906010415020201010B000C280302000B0011300502010200000B000C050010001130090602000000100011 + - Name: __llvm_covmap + Type: SHT_PROGBITS + Flags: [ SHF_GNU_RETAIN ] + AddressAlign: 0x8 + Content: 000000001D0000000000000006000000021A0000186D6364632D74656D706C617465732D6D657267652E637070000000 + - Name: __llvm_prf_names + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_GNU_RETAIN ] + AddressAlign: 0x1 + Content: 51006D61696E015F5A33436162494C62304545626262015F5A33436162494C62314545626262015F5A32616249624562545F53305F015F5A32616249784562545F53305F015F5A32616249644562545F53305F + - Type: SectionHeaderTable + Sections: + - Name: .strtab + - Name: __llvm_covfun + - Name: '__llvm_covfun (1)' + - Name: '__llvm_covfun (2)' + - Name: '__llvm_covfun (3)' + - Name: '__llvm_covfun (4)' + - Name: '__llvm_covfun (5)' + - Name: __llvm_covmap + - Name: __llvm_prf_names + - Name: .symtab +Symbols: + - Name: __llvm_covmap + Type: STT_SECTION + Section: __llvm_covmap + - Name: __llvm_prf_names + Type: STT_SECTION + Section: __llvm_prf_names + - Name: __covrec_DB956436E78DD5FAu + Type: STT_OBJECT + Section: __llvm_covfun + Binding: STB_WEAK + Size: 0x41 + Other: [ STV_HIDDEN ] + - Name: __covrec_E8BF1D5048B3C39Cu + Type: STT_OBJECT + Section: '__llvm_covfun (1)' + Binding: STB_WEAK + Size: 0x64 + Other: [ STV_HIDDEN ] + - Name: __covrec_8E3FA07771627398u + Type: STT_OBJECT + Section: '__llvm_covfun (2)' + Binding: STB_WEAK + Size: 0x62 + Other: [ STV_HIDDEN ] + - Name: __covrec_66B20375207A40BFu + Type: STT_OBJECT + Section: '__llvm_covfun (3)' + Binding: STB_WEAK + Size: 0x4E + Other: [ STV_HIDDEN ] + - Name: __covrec_7DC367B42CA2058Au + Type: STT_OBJECT + Section: '__llvm_covfun (4)' + Binding: STB_WEAK + Size: 0x4E + Other: [ STV_HIDDEN ] + - Name: __covrec_263F8FAC2C190017u + Type: STT_OBJECT + Section: '__llvm_covfun (5)' + Binding: STB_WEAK + Size: 0x4E + Other: [ STV_HIDDEN ] +... diff --git a/llvm/test/tools/llvm-cov/branch-c-general.test b/llvm/test/tools/llvm-cov/branch-c-general.test index 3c163bf6de45c..9ee15ec75428e 100644 --- a/llvm/test/tools/llvm-cov/branch-c-general.test +++ b/llvm/test/tools/llvm-cov/branch-c-general.test @@ -117,19 +117,19 @@ // REPORT: Name Regions Miss Cover Lines Miss Cover Branches Miss Cover // REPORT-NEXT: --- // REPORT-NEXT: simple_loops 8 0 100.00% 9 0 100.00% 6 0 100.00% -// REPORT-NEXT: conditionals 24 0 100.00% 15 0 100.00% 16 2 87.50% +// REPORT-NEXT: conditionals 22 0 100.00% 15 0 100.00% 16 2 87.50% // REPORT-NEXT: early_exits 20 4 80.00% 25 2 92.00% 16 6 62.50% // REPORT-NEXT: jumps 39 12 69.23% 48 2 95.83% 26 9 65.38% -// REPORT-NEXT: switches 28 5 82.14% 38 4 89.47% 28 7 75.00% +// REPORT-NEXT: switches 27 4 85.19% 38 4 89.47% 28 7 75.00% // REPORT-NEXT: big_switch 25 1 96.00% 32 0 100.00% 30 6 80.00% -// REPORT-NEXT: boolean_operators 16 0 100.00% 13 0 100.00% 22 2 90.91% -// REPORT-NEXT: boolop_loops 19 0 100.00% 14 0 100.00% 16 2 87.50% +// REPORT-NEXT: boolean_operators 14 0 100.00% 13 0 100.00% 22 2 90.91% +// REPORT-NEXT: boolop_loops 15 0 100.00% 14 0 100.00% 16 2 87.50% // REPORT-NEXT: conditional_operator 4 2 50.00% 8 0 100.00% 4 2 50.00% // REPORT-NEXT: do_fallthrough 9 0 100.00% 12 0 100.00% 6 0 100.00% // REPORT-NEXT: main 1 0 100.00% 16 0 100.00% 0 0 0.00% // REPORT-NEXT: c-general.c:static_func 4 0 100.00% 4 0 100.00% 2 0 100.00% // REPORT-NEXT: --- -// REPORT-NEXT: TOTAL 197 24 87.82% 234 8 96.58% 172 36 79.07% +// REPORT-NEXT: TOTAL 188 23 87.77% 234 8 96.58% 172 36 79.07% // Test file-level report. // RUN: llvm-profdata merge %S/Inputs/branch-c-general.proftext -o %t.profdata @@ -159,7 +159,7 @@ // HTML-INDEX: // HTML-INDEX: 96.58% (226/234) // HTML-INDEX: -// HTML-INDEX: 87.82% (173/197) +// HTML-INDEX: 87.77% (165/188) // HTML-INDEX: // HTML-INDEX: 79.07% (136/172) // HTML-INDEX: diff --git a/llvm/test/tools/llvm-cov/branch-export-json.test b/llvm/test/tools/llvm-cov/branch-export-json.test index 7cf4286087982..77e2bab7d9eb3 100644 --- a/llvm/test/tools/llvm-cov/branch-export-json.test +++ b/llvm/test/tools/llvm-cov/branch-export-json.test @@ -18,7 +18,7 @@ // CHECK: 45,5,45,11,0,5,0,0,4 // CHECK: 47,5,47,12,3,2,0,0,4 // CHECK: 53,12,53,20,50,5,0,0,4 -// CHECK: {"count":30,"covered":26,"notcovered":4,"percent":86.666666666666671} +// CHECK: {"count":30,"covered":26,"notcovered":4,"percent":86.6[[#]]} // Check recursive macro-expansions. // RUN: llvm-profdata merge %S/Inputs/branch-macros.proftext -o %t.profdata @@ -46,4 +46,4 @@ // MACROS: 5,15,5,23,1,2,8,0,4 // MACROS: 6,15,6,23,0,1,9,0,4 // MACROS: 8,15,8,38,1,2,2,0,4 -// MACROS: {"count":40,"covered":24,"notcovered":16,"percent":60} +// MACROS: {"count":16,"covered":6,"notcovered":10,"percent":37.5} diff --git a/llvm/test/tools/llvm-cov/branch-export-lcov.test b/llvm/test/tools/llvm-cov/branch-export-lcov.test index fe43dd66de8d0..444277435d60d 100644 --- a/llvm/test/tools/llvm-cov/branch-export-lcov.test +++ b/llvm/test/tools/llvm-cov/branch-export-lcov.test @@ -71,8 +71,8 @@ // MACROS-COUNT-10: BRDA:37 // MACROS-NOT: BRDA:37 // MACROS-NOT: BRDA -// MACROS: BRF:40 -// MACROS: BRH:24 +// MACROS: BRF:16 +// MACROS: BRH:6 // NOBRANCH-NOT: BRDA // NOBRANCH-NOT: BRF diff --git a/llvm/test/tools/llvm-cov/branch-macros.test b/llvm/test/tools/llvm-cov/branch-macros.test index b16ef9d4846d8..4c99cc7b81294 100644 --- a/llvm/test/tools/llvm-cov/branch-macros.test +++ b/llvm/test/tools/llvm-cov/branch-macros.test @@ -1,5 +1,7 @@ // RUN: llvm-profdata merge %S/Inputs/branch-macros.proftext -o %t.profdata // RUN: llvm-cov show --show-expansions --show-branches=count %S/Inputs/branch-macros.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs | FileCheck %S/Inputs/branch-macros.cpp -check-prefixes=CHECK,BRCOV -D#C=999 +// RUN: llvm-cov report --show-branch-summary %S/Inputs/branch-macros.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/branch-macros.cpp | FileCheck %s -check-prefix=FILE + // RUN: llvm-cov show --binary-counters --show-expansions --show-branches=count %S/Inputs/branch-macros.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs | FileCheck %S/Inputs/branch-macros.cpp -check-prefixes=CHECK,BRCOV -D#C=1 // RUN: llvm-cov report --show-branch-summary %S/Inputs/branch-macros.o32l -instr-profile %t.profdata -show-functions -path-equivalence=/tmp,%S/Inputs %S/Inputs/branch-macros.cpp | FileCheck %s -check-prefix=REPORT @@ -9,8 +11,14 @@ // REPORT: Name Regions Miss Cover Lines Miss Cover Branches Miss Cover // REPORT-NEXT: --- -// REPORT-NEXT: _Z4funcii 28 4 85.71% 18 0 100.00% 30 14 53.33% -// REPORT-NEXT: _Z5func2ii 13 1 92.31% 8 0 100.00% 10 2 80.00% +// REPORT-NEXT: _Z4funcii 12 4 66.67% 18 0 100.00% 30 14 53.33% +// REPORT-NEXT: _Z5func2ii 3 0 100.00% 8 0 100.00% 10 2 80.00% // REPORT-NEXT: main 1 0 100.00% 6 0 100.00% 0 0 0.00% // REPORT-NEXT: --- -// REPORT-NEXT: TOTAL 42 5 88.10% 32 0 100.00% 40 16 60.00% +// REPORT-NEXT: TOTAL 16 4 75.00% 32 0 100.00% 40 16 60.00% + +// FILE: Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover +// FILE-NEXT: --- +// FILE-NEXT: branch-macros.cpp 28 5 82.14% 3 0 100.00% 39 0 100.00% 16 10 37.50% +// FILE-NEXT: --- +// FILE-NEXT: TOTAL 28 5 82.14% 3 0 100.00% 39 0 100.00% 16 10 37.50% diff --git a/llvm/test/tools/llvm-cov/branch-noShowBranch.test b/llvm/test/tools/llvm-cov/branch-noShowBranch.test index 9f3cfd55f029b..11a9a5a665b70 100644 --- a/llvm/test/tools/llvm-cov/branch-noShowBranch.test +++ b/llvm/test/tools/llvm-cov/branch-noShowBranch.test @@ -9,16 +9,16 @@ // REPORT-NOT: Name Regions Miss Cover Lines Miss Cover Branches Miss Cover // REPORT: --- // REPORT-NOT: simple_loops 8 0 100.00% 9 0 100.00% 6 0 100.00% -// REPORT-NOT: conditionals 24 0 100.00% 15 0 100.00% 16 2 87.50% +// REPORT-NOT: conditionals 22 0 100.00% 15 0 100.00% 16 2 87.50% // REPORT-NOT: early_exits 20 4 80.00% 25 2 92.00% 16 6 62.50% // REPORT-NOT: jumps 39 12 69.23% 48 2 95.83% 26 9 65.38% -// REPORT-NOT: switches 28 5 82.14% 38 4 89.47% 28 7 75.00% +// REPORT-NOT: switches 27 4 85.19% 38 4 89.47% 28 7 75.00% // REPORT-NOT: big_switch 25 1 96.00% 32 0 100.00% 30 6 80.00% -// REPORT-NOT: boolean_operators 16 0 100.00% 13 0 100.00% 22 2 90.91% -// REPORT-NOT: boolop_loops 19 0 100.00% 14 0 100.00% 16 2 87.50% +// REPORT-NOT: boolean_operators 14 0 100.00% 13 0 100.00% 22 2 90.91% +// REPORT-NOT: boolop_loops 15 0 100.00% 14 0 100.00% 16 2 87.50% // REPORT-NOT: conditional_operator 4 2 50.00% 8 0 100.00% 4 2 50.00% // REPORT-NOT: do_fallthrough 9 0 100.00% 12 0 100.00% 6 0 100.00% // REPORT-NOT: main 1 0 100.00% 16 0 100.00% 0 0 0.00% // REPORT-NOT: c-general.c:static_func 4 0 100.00% 4 0 100.00% 2 0 100.00% -// REPORT: TOTAL 197 24 87.82% 234 8 96.58% -// REPORT-NOT: TOTAL 197 24 87.82% 234 8 96.58% 172 36 79.07% +// REPORT: TOTAL 188 23 87.77% 234 8 96.58% +// REPORT-NOT: TOTAL 188 23 87.77% 234 8 96.58% 172 36 79.07% diff --git a/llvm/test/tools/llvm-cov/branch-templates.test b/llvm/test/tools/llvm-cov/branch-templates.test index d5535022239f5..c4fc9cafbfd80 100644 --- a/llvm/test/tools/llvm-cov/branch-templates.test +++ b/llvm/test/tools/llvm-cov/branch-templates.test @@ -26,6 +26,6 @@ // REPORTFILE: Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover // REPORTFILE-NEXT: --- -// REPORTFILE-NEXT: branch-templates.cpp 12 3 75.00% 2 0 100.00% 17 4 76.47% 8 4 50.00% +// REPORTFILE-NEXT: branch-templates.cpp 12 2 83.33% 2 0 100.00% 17 3 82.35% 8 3 62.50% // REPORTFILE-NEXT: --- -// REPORTFILE-NEXT: TOTAL 12 3 75.00% 2 0 100.00% 17 4 76.47% 8 4 50.00% +// REPORTFILE-NEXT: TOTAL 12 2 83.33% 2 0 100.00% 17 3 82.35% 8 3 62.50% diff --git a/llvm/test/tools/llvm-cov/coverage_watermark.test b/llvm/test/tools/llvm-cov/coverage_watermark.test index 5c48b4f0fb4bf..818baa470bc38 100644 --- a/llvm/test/tools/llvm-cov/coverage_watermark.test +++ b/llvm/test/tools/llvm-cov/coverage_watermark.test @@ -18,15 +18,15 @@ ORIGIN: ORIGIN: 100.00% (2/2) ORIGIN: ORIGIN: 100.00% (3/3) -ORIGIN: -ORIGIN: 75.00% (9/12) -ORIGIN: -ORIGIN: 66.67% (4/6) +ORIGIN: +ORIGIN: 83.33% (10/12) +ORIGIN: +ORIGIN: 83.33% (5/6) ORIGIN: ORIGIN: - (0/0) ORIGIN: -RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -format html -show-region-summary -show-instantiation-summary -o %t.html.dir -path-equivalence=/tmp,%S -coverage-watermark 80,70 %S/showTemplateInstantiations.cpp +RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -format html -show-region-summary -show-instantiation-summary -o %t.html.dir -path-equivalence=/tmp,%S -coverage-watermark 90,70 %S/showTemplateInstantiations.cpp RUN: FileCheck -check-prefix=DOWNGRADE1 %s -input-file %t.html.dir/index.html DOWNGRADE:1 Totals @@ -35,9 +35,9 @@ DOWNGRADE1: 100.00% (2/2) DOWNGRADE1: DOWNGRADE1: 100.00% (3/3) DOWNGRADE1: -DOWNGRADE1: 75.00% (9/12) -DOWNGRADE1: -DOWNGRADE1: 66.67% (4/6) +DOWNGRADE1: 83.33% (10/12) +DOWNGRADE1: +DOWNGRADE1: 83.33% (5/6) DOWNGRADE1: DOWNGRADE1: - (0/0) DOWNGRADE1: @@ -51,9 +51,9 @@ DOWNGRADE2: 100.00% (2/2) DOWNGRADE2: DOWNGRADE2: 100.00% (3/3) DOWNGRADE2: -DOWNGRADE2: 75.00% (9/12) -DOWNGRADE2: -DOWNGRADE2: 66.67% (4/6) +DOWNGRADE2: 83.33% (10/12) +DOWNGRADE2: +DOWNGRADE2: 83.33% (5/6) DOWNGRADE1: DOWNGRADE1: - (0/0) DOWNGRADE1: diff --git a/llvm/test/tools/llvm-cov/mcdc-const.test b/llvm/test/tools/llvm-cov/mcdc-const.test index 5424625cf6a6b..76eb7cf706d73 100644 --- a/llvm/test/tools/llvm-cov/mcdc-const.test +++ b/llvm/test/tools/llvm-cov/mcdc-const.test @@ -61,8 +61,8 @@ // CHECKFULLCASE: | C1-Pair: constant folded // CHECKFULLCASE-NEXT: | C2-Pair: not covered // CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% -// CHECKFULLCASE: | 1 { F, C = T } -// CHECKFULLCASE-NEXT: | 2 { T, C = T } +// CHECKFULLCASE: | 1 { T, C = T } +// CHECKFULLCASE-NEXT: | 2 { F, C = T } // CHECKFULLCASE: | C1-Pair: not covered // CHECKFULLCASE-NEXT: | C2-Pair: constant folded // CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% @@ -106,20 +106,20 @@ // CHECKFULLCASE-NEXT: | C2-Pair: not covered // CHECKFULLCASE-NEXT: | C3-Pair: not covered // CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% -// CHECKFULLCASE: | 1 { F, C, - = T } -// CHECKFULLCASE-NEXT: | 2 { T, C, - = T } +// CHECKFULLCASE: | 1 { T, C, - = T } +// CHECKFULLCASE-NEXT: | 2 { F, C, - = T } // CHECKFULLCASE: | C1-Pair: not covered // CHECKFULLCASE-NEXT: | C2-Pair: constant folded // CHECKFULLCASE-NEXT: | C3-Pair: not covered // CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% -// CHECKFULLCASE: | 1 { C, F, T = T } -// CHECKFULLCASE-NEXT: | 2 { C, T, - = T } +// CHECKFULLCASE: | 1 { C, T, - = T } +// CHECKFULLCASE-NEXT: | 2 { C, F, T = T } // CHECKFULLCASE: | C1-Pair: constant folded // CHECKFULLCASE-NEXT: | C2-Pair: not covered // CHECKFULLCASE-NEXT: | C3-Pair: not covered // CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% -// CHECKFULLCASE: | 1 { F, C, T = T } -// CHECKFULLCASE-NEXT: | 2 { T, C, - = T } +// CHECKFULLCASE: | 1 { T, C, - = T } +// CHECKFULLCASE-NEXT: | 2 { F, C, T = T } // CHECKFULLCASE: | C1-Pair: not covered // CHECKFULLCASE-NEXT: | C2-Pair: constant folded // CHECKFULLCASE-NEXT: | C3-Pair: not covered @@ -151,26 +151,26 @@ // CHECKFULLCASE-NEXT: | C2-Pair: constant folded // CHECKFULLCASE-NEXT: | C3-Pair: covered: (2,3) // CHECKFULLCASE: | MC/DC Coverage for Decision: 100.00% -// CHECKFULLCASE: | 1 { F, T, C = T } -// CHECKFULLCASE-NEXT: | 2 { T, -, C = T } +// CHECKFULLCASE: | 1 { T, -, C = T } +// CHECKFULLCASE-NEXT: | 2 { F, T, C = T } // CHECKFULLCASE: | C1-Pair: not covered // CHECKFULLCASE-NEXT: | C2-Pair: not covered // CHECKFULLCASE-NEXT: | C3-Pair: constant folded // CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% -// CHECKFULLCASE: | 1 { F, C, - = T } -// CHECKFULLCASE-NEXT: | 2 { T, C, - = T } +// CHECKFULLCASE: | 1 { T, C, - = T } +// CHECKFULLCASE-NEXT: | 2 { F, C, - = T } // CHECKFULLCASE: | C1-Pair: not covered // CHECKFULLCASE-NEXT: | C2-Pair: constant folded // CHECKFULLCASE-NEXT: | C3-Pair: not covered // CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% -// CHECKFULLCASE: | 1 { F, T, C = T } -// CHECKFULLCASE-NEXT: | 2 { T, -, C = T } +// CHECKFULLCASE: | 1 { T, -, C = T } +// CHECKFULLCASE-NEXT: | 2 { F, T, C = T } // CHECKFULLCASE: | C1-Pair: not covered // CHECKFULLCASE-NEXT: | C2-Pair: not covered // CHECKFULLCASE-NEXT: | C3-Pair: constant folded // CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% -// CHECKFULLCASE: | 1 { F, C, T = T } -// CHECKFULLCASE-NEXT: | 2 { T, C, - = T } +// CHECKFULLCASE: | 1 { T, C, - = T } +// CHECKFULLCASE-NEXT: | 2 { F, C, T = T } // CHECKFULLCASE: | C1-Pair: not covered // CHECKFULLCASE-NEXT: | C2-Pair: constant folded // CHECKFULLCASE-NEXT: | C3-Pair: not covered diff --git a/llvm/test/tools/llvm-cov/mcdc-general-none.test b/llvm/test/tools/llvm-cov/mcdc-general-none.test index b57b35d49c8c1..db95314ac950a 100644 --- a/llvm/test/tools/llvm-cov/mcdc-general-none.test +++ b/llvm/test/tools/llvm-cov/mcdc-general-none.test @@ -34,10 +34,10 @@ // REPORT: Name Regions Miss Cover Lines Miss Cover Branches Miss Cover MC/DC Conditions Miss Cover // REPORT-NEXT: ------------------------------------------------------------------------------------------------------------------------------------------- -// REPORT-NEXT: _Z4testbbbb 25 0 100.00% 9 0 100.00% 24 2 91.67% 12 12 0.00% +// REPORT-NEXT: _Z4testbbbb 21 0 100.00% 9 0 100.00% 24 2 91.67% 12 12 0.00% // REPORT-NEXT: main 1 0 100.00% 11 0 100.00% 0 0 0.00% 0 0 0.00% // REPORT-NEXT: --- -// REPORT-NEXT: TOTAL 26 0 100.00% 20 0 100.00% 24 2 91.67% 12 12 0.00% +// REPORT-NEXT: TOTAL 22 0 100.00% 20 0 100.00% 24 2 91.67% 12 12 0.00% // Turn off MC/DC summary. // RUN: llvm-cov report %S/Inputs/mcdc-general.o -instr-profile %t.profdata -show-functions -path-equivalence=.,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=REPORT_NOMCDC @@ -67,7 +67,7 @@ // HTML-INDEX: // HTML-INDEX: 100.00% (2/2) // HTML-INDEX: 100.00% (20/20) -// HTML-INDEX: 100.00% (26/26) +// HTML-INDEX: 100.00% (22/22) // HTML-INDEX: 91.67% (22/24) // HTML-INDEX: 0.00% (0/12) // HTML-INDEX: Totals diff --git a/llvm/test/tools/llvm-cov/mcdc-general.test b/llvm/test/tools/llvm-cov/mcdc-general.test index c1e95cb2bd92a..75edcb066e4af 100644 --- a/llvm/test/tools/llvm-cov/mcdc-general.test +++ b/llvm/test/tools/llvm-cov/mcdc-general.test @@ -19,15 +19,15 @@ // CHECK-NEXT: | // CHECK-NEXT: | C1, C2, C3, C4 Result // CHECK-NEXT: | 1 { F, -, F, - = F } -// CHECK-NEXT: | 2 { F, -, T, F = F } -// CHECK-NEXT: | 3 { T, F, F, - = F } +// CHECK-NEXT: | 2 { T, F, F, - = F } +// CHECK-NEXT: | 3 { F, -, T, F = F } // CHECK-NEXT: | 4 { T, F, T, F = F } // CHECK-NEXT: | 5 { T, F, T, T = T } // CHECK-NEXT: | 6 { T, T, -, - = T } // CHECK-NEXT: | // CHECK-NEXT: | C1-Pair: covered: (1,6) -// CHECK-NEXT: | C2-Pair: covered: (3,6) -// CHECK-NEXT: | C3-Pair: covered: (3,5) +// CHECK-NEXT: | C2-Pair: covered: (2,6) +// CHECK-NEXT: | C3-Pair: covered: (2,5) // CHECK-NEXT: | C4-Pair: covered: (4,5) // CHECK-NEXT: | MC/DC Coverage for Decision: 100.00% // CHECK-NEXT: | @@ -100,10 +100,10 @@ // REPORT: Name Regions Miss Cover Lines Miss Cover Branches Miss Cover MC/DC Conditions Miss Cover // REPORT-NEXT: ------------------------------------------------------------------------------------------------------------------------------------------- -// REPORT-NEXT: _Z4testbbbb 25 0 100.00% 9 0 100.00% 24 2 91.67% 12 2 83.33% +// REPORT-NEXT: _Z4testbbbb 21 0 100.00% 9 0 100.00% 24 2 91.67% 12 2 83.33% // REPORT-NEXT: main 1 0 100.00% 11 0 100.00% 0 0 0.00% 0 0 0.00% // REPORT-NEXT: --- -// REPORT-NEXT: TOTAL 26 0 100.00% 20 0 100.00% 24 2 91.67% 12 2 83.33% +// REPORT-NEXT: TOTAL 22 0 100.00% 20 0 100.00% 24 2 91.67% 12 2 83.33% // Turn off MC/DC summary. // RUN: llvm-cov report %S/Inputs/mcdc-general.o -instr-profile %t.profdata -show-functions -path-equivalence=.,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=REPORT_NOMCDC @@ -133,7 +133,7 @@ // HTML-INDEX: // HTML-INDEX: 100.00% (2/2) // HTML-INDEX: 100.00% (20/20) -// HTML-INDEX: 100.00% (26/26) +// HTML-INDEX: 100.00% (22/22) // HTML-INDEX: 91.67% (22/24) // HTML-INDEX: 83.33% (10/12) // HTML-INDEX: Totals diff --git a/llvm/test/tools/llvm-cov/mcdc-templates-merge.test b/llvm/test/tools/llvm-cov/mcdc-templates-merge.test new file mode 100644 index 0000000000000..d4369dc2db293 --- /dev/null +++ b/llvm/test/tools/llvm-cov/mcdc-templates-merge.test @@ -0,0 +1,41 @@ +# Test `merge-instantiations=merge/any/all` + +RUN: yaml2obj %S/Inputs/mcdc-templates-merge.yaml -o %t.o +RUN: llvm-profdata merge %S/Inputs/mcdc-templates-merge.proftext -o %t.profdata + +RUN: llvm-cov show -merge-instantiations=merge --show-mcdc -show-instantiations=true %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %S/Inputs/mcdc-templates-merge.cpp --check-prefixes=CHECK,MERGE +RUN: llvm-cov show -merge-instantiations=any --show-mcdc -show-instantiations=true %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %S/Inputs/mcdc-templates-merge.cpp --check-prefixes=CHECK,ANY +RUN: llvm-cov show -merge-instantiations=all --show-mcdc -show-instantiations=true %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %S/Inputs/mcdc-templates-merge.cpp --check-prefixes=CHECK,ALL + +RUN: llvm-cov report -merge-instantiations=merge --show-mcdc-summary %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %s -check-prefixes=REPORT,MERGE +RUN: llvm-cov report -merge-instantiations=any --show-mcdc-summary %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %s -check-prefixes=REPORT,ANY +RUN: llvm-cov report -merge-instantiations=all --show-mcdc-summary %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %s -check-prefixes=REPORT,ALL + +REPORT: mcdc-templates-merge.cpp + +# Regions +MERGE: 10 1 90.00% +ANY: 10 1 90.00% +ALL: 10 4 60.00% + +# Functions +MERGE: 3 0 100.00% +ANY: 3 0 100.00% +ALL: 3 0 100.00% + +# Lines +MERGE: 19 1 94.74% +ANY: 19 1 94.74% +ALL: 19 4 78.95% + +# Branches +MERGE: 12 1 91.67% +ANY: 11 1 90.91% +ALL: 12 7 41.67% + +# MC/DC Conditions +MERGE: 5 0 100.00% +ANY: 4 2 50.00% +ALL: 4 4 0.00% + +REPORT: TOTAL diff --git a/llvm/test/tools/llvm-cov/zeroFunctionFile.c b/llvm/test/tools/llvm-cov/zeroFunctionFile.c index f463007fe7f60..28caa5ac24165 100644 --- a/llvm/test/tools/llvm-cov/zeroFunctionFile.c +++ b/llvm/test/tools/llvm-cov/zeroFunctionFile.c @@ -16,5 +16,4 @@ int main() { // RUN: llvm-cov show -j 1 %S/Inputs/zeroFunctionFile.covmapping -format html -instr-profile %t.profdata -o %t.dir // RUN: FileCheck %s -input-file=%t.dir/index.html -check-prefix=HTML // HTML-NO: 0.00% (0/0) -// HTML: Files which contain no functions -// HTML: zeroFunctionFile.h +// HTML-NOT: Files which contain no functions diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp index 921f283deedc7..3794ff06f3bf6 100644 --- a/llvm/tools/llvm-cov/CodeCoverage.cpp +++ b/llvm/tools/llvm-cov/CodeCoverage.cpp @@ -395,7 +395,8 @@ CodeCoverageTool::createSourceFileView(StringRef SourceFile, auto SourceBuffer = getSourceFile(SourceFile); if (!SourceBuffer) return nullptr; - auto FileCoverage = Coverage.getCoverageForFile(SourceFile); + auto FileCoverage = + Coverage.getCoverageForFile(SourceFile, ViewOpts.MergeStrategyOpts); if (FileCoverage.empty()) return nullptr; @@ -795,6 +796,14 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { "check-binary-ids", cl::desc("Fail if an object couldn't be found for a " "binary ID in the profile")); + cl::opt MergeStrategyOpts( + "merge-instantiations", cl::desc("Merge instantiations"), + cl::values( + clEnumValN(MergeStrategy::Merge, "merge", "Merge entries by adding"), + clEnumValN(MergeStrategy::Any, "any", "Pick up any better entries"), + clEnumValN(MergeStrategy::All, "all", "Pick up the worst entries")), + cl::init(MergeStrategy::Merge)); + auto commandLineParser = [&, this](int argc, const char **argv) -> int { cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); ViewOpts.Debug = DebugDump; @@ -951,6 +960,7 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { ViewOpts.ExportSummaryOnly = SummaryOnly; ViewOpts.NumThreads = NumThreads; ViewOpts.CompilationDirectory = CompilationDirectory; + ViewOpts.MergeStrategyOpts = MergeStrategyOpts; return 0; }; diff --git a/llvm/tools/llvm-cov/CoverageReport.cpp b/llvm/tools/llvm-cov/CoverageReport.cpp index 00aea4039bfde..9215e2421f514 100644 --- a/llvm/tools/llvm-cov/CoverageReport.cpp +++ b/llvm/tools/llvm-cov/CoverageReport.cpp @@ -446,27 +446,47 @@ void CoverageReport::prepareSingleFileReport(const StringRef Filename, const coverage::CoverageMapping *Coverage, const CoverageViewOptions &Options, const unsigned LCP, FileCoverageSummary *FileReport, const CoverageFilter *Filters) { + DenseSet FilteredOutFunctions; + assert(FileReport->empty()); for (const auto &Group : Coverage->getInstantiationGroups(Filename)) { - std::vector InstantiationSummaries; + bool Updated = false; for (const coverage::FunctionRecord *F : Group.getInstantiations()) { - if (!Filters->matches(*Coverage, *F)) + if (!Filters->matches(*Coverage, *F)) { + FilteredOutFunctions.insert(F); continue; - auto InstantiationSummary = FunctionCoverageSummary::get(*Coverage, *F); - FileReport->addInstantiation(InstantiationSummary); - InstantiationSummaries.push_back(InstantiationSummary); + } + FileReport->InstantiationCoverage.addFunction( + /*Covered=*/F->ExecutionCount > 0); + Updated = true; } - if (InstantiationSummaries.empty()) + if (!Updated) continue; - auto GroupSummary = - FunctionCoverageSummary::get(Group, InstantiationSummaries); + if (Options.Debug) { + std::string Name; + if (Group.hasName()) { + Name = std::string(Group.getName()); + } else { + llvm::raw_string_ostream OS(Name); + OS << "Definition at line " << Group.getLine() << ", column " + << Group.getColumn(); + } - if (Options.Debug) - outs() << "InstantiationGroup: " << GroupSummary.Name << " with " + outs() << "InstantiationGroup: " << Name << " with " << "size = " << Group.size() << "\n"; + } - FileReport->addFunction(GroupSummary); + FileReport->FunctionCoverage.addFunction( + /*Covered=*/Group.getTotalExecutionCount() > 0); } + + auto FileCoverage = Coverage->getCoverageForFile( + Filename, Options.MergeStrategyOpts, FilteredOutFunctions); + if (FileCoverage.empty()) + return; + + *static_cast(FileReport) += + CoverageDataSummary(FileCoverage); } std::vector CoverageReport::prepareFileReports( diff --git a/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp b/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp index 5c002a694f66a..745a92103bccc 100644 --- a/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp +++ b/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp @@ -63,14 +63,15 @@ auto sumMCDCPairs(const ArrayRef &Records) { } static std::pair -sumRegions(ArrayRef CodeRegions, const CoverageData &CD) { +sumRegions(const CoverageData &CD) { // Compute the region coverage. size_t NumCodeRegions = 0, CoveredRegions = 0; - for (auto &CR : CodeRegions) { - if (CR.Kind != CounterMappingRegion::CodeRegion) + for (auto I = CD.begin(), E = CD.end(); I != E; ++I) { + if (!I->IsRegionEntry || !I->HasCount || I->IsGapRegion) continue; + ++NumCodeRegions; - if (CR.ExecutionCount != 0) + if (I->Count) ++CoveredRegions; } @@ -88,9 +89,8 @@ sumRegions(ArrayRef CodeRegions, const CoverageData &CD) { LineCoverageInfo(CoveredLines, NumLines)}; } -CoverageDataSummary::CoverageDataSummary(const CoverageData &CD, - ArrayRef CodeRegions) { - std::tie(RegionCoverage, LineCoverage) = sumRegions(CodeRegions, CD); +CoverageDataSummary::CoverageDataSummary(const CoverageData &CD) { + std::tie(RegionCoverage, LineCoverage) = sumRegions(CD); BranchCoverage = sumBranches(CD.getBranches()); MCDCCoverage = sumMCDCPairs(CD.getMCDCRecords()); } @@ -103,36 +103,10 @@ FunctionCoverageSummary::get(const CoverageMapping &CM, auto Summary = FunctionCoverageSummary(Function.Name, Function.ExecutionCount); - Summary += CoverageDataSummary(CD, Function.CountedRegions); + Summary += CoverageDataSummary(CD); // Compute the branch coverage, including branches from expansions. Summary.BranchCoverage += sumBranchExpansions(CM, CD.getExpansions()); return Summary; } - -FunctionCoverageSummary -FunctionCoverageSummary::get(const InstantiationGroup &Group, - ArrayRef Summaries) { - std::string Name; - if (Group.hasName()) { - Name = std::string(Group.getName()); - } else { - llvm::raw_string_ostream OS(Name); - OS << "Definition at line " << Group.getLine() << ", column " - << Group.getColumn(); - } - - FunctionCoverageSummary Summary(Name, Group.getTotalExecutionCount()); - Summary.RegionCoverage = Summaries[0].RegionCoverage; - Summary.LineCoverage = Summaries[0].LineCoverage; - Summary.BranchCoverage = Summaries[0].BranchCoverage; - Summary.MCDCCoverage = Summaries[0].MCDCCoverage; - for (const auto &FCS : Summaries.drop_front()) { - Summary.RegionCoverage.merge(FCS.RegionCoverage); - Summary.LineCoverage.merge(FCS.LineCoverage); - Summary.BranchCoverage.merge(FCS.BranchCoverage); - Summary.MCDCCoverage.merge(FCS.MCDCCoverage); - } - return Summary; -} diff --git a/llvm/tools/llvm-cov/CoverageSummaryInfo.h b/llvm/tools/llvm-cov/CoverageSummaryInfo.h index d9210676c41bf..09ceba5de7271 100644 --- a/llvm/tools/llvm-cov/CoverageSummaryInfo.h +++ b/llvm/tools/llvm-cov/CoverageSummaryInfo.h @@ -41,11 +41,6 @@ class RegionCoverageInfo { return *this; } - void merge(const RegionCoverageInfo &RHS) { - Covered = std::max(Covered, RHS.Covered); - NumRegions = std::max(NumRegions, RHS.NumRegions); - } - size_t getCovered() const { return Covered; } size_t getNumRegions() const { return NumRegions; } @@ -82,11 +77,6 @@ class LineCoverageInfo { return *this; } - void merge(const LineCoverageInfo &RHS) { - Covered = std::max(Covered, RHS.Covered); - NumLines = std::max(NumLines, RHS.NumLines); - } - size_t getCovered() const { return Covered; } size_t getNumLines() const { return NumLines; } @@ -123,11 +113,6 @@ class BranchCoverageInfo { return *this; } - void merge(const BranchCoverageInfo &RHS) { - Covered = std::max(Covered, RHS.Covered); - NumBranches = std::max(NumBranches, RHS.NumBranches); - } - size_t getCovered() const { return Covered; } size_t getNumBranches() const { return NumBranches; } @@ -164,11 +149,6 @@ class MCDCCoverageInfo { return *this; } - void merge(const MCDCCoverageInfo &RHS) { - CoveredPairs = std::max(CoveredPairs, RHS.CoveredPairs); - NumPairs = std::max(NumPairs, RHS.NumPairs); - } - size_t getCoveredPairs() const { return CoveredPairs; } size_t getNumPairs() const { return NumPairs; } @@ -230,8 +210,14 @@ struct CoverageDataSummary { MCDCCoverageInfo MCDCCoverage; CoverageDataSummary() = default; - CoverageDataSummary(const coverage::CoverageData &CD, - ArrayRef CodeRegions); + CoverageDataSummary(const coverage::CoverageData &CD); + + bool empty() const { + return (RegionCoverage.getNumRegions() == 0 && + LineCoverage.getNumLines() == 0 && + BranchCoverage.getNumBranches() == 0 && + MCDCCoverage.getNumPairs() == 0); + } auto &operator+=(const CoverageDataSummary &RHS) { RegionCoverage += RHS.RegionCoverage; @@ -254,12 +240,6 @@ struct FunctionCoverageSummary : CoverageDataSummary { /// mapping record. static FunctionCoverageSummary get(const coverage::CoverageMapping &CM, const coverage::FunctionRecord &Function); - - /// Compute the code coverage summary for an instantiation group \p Group, - /// given a list of summaries for each instantiation in \p Summaries. - static FunctionCoverageSummary - get(const coverage::InstantiationGroup &Group, - ArrayRef Summaries); }; /// A summary of file's code coverage. @@ -271,24 +251,18 @@ struct FileCoverageSummary : CoverageDataSummary { FileCoverageSummary() = default; FileCoverageSummary(StringRef Name) : Name(Name) {} + bool empty() const { + return (CoverageDataSummary::empty() && + FunctionCoverage.getNumFunctions() == 0 && + InstantiationCoverage.getNumFunctions() == 0); + } + FileCoverageSummary &operator+=(const FileCoverageSummary &RHS) { *static_cast(this) += RHS; FunctionCoverage += RHS.FunctionCoverage; InstantiationCoverage += RHS.InstantiationCoverage; return *this; } - - void addFunction(const FunctionCoverageSummary &Function) { - RegionCoverage += Function.RegionCoverage; - LineCoverage += Function.LineCoverage; - BranchCoverage += Function.BranchCoverage; - MCDCCoverage += Function.MCDCCoverage; - FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0); - } - - void addInstantiation(const FunctionCoverageSummary &Function) { - InstantiationCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0); - } }; /// A cache for demangled symbols. diff --git a/llvm/tools/llvm-cov/CoverageViewOptions.h b/llvm/tools/llvm-cov/CoverageViewOptions.h index 81e69c3814e30..d2135a19e11f6 100644 --- a/llvm/tools/llvm-cov/CoverageViewOptions.h +++ b/llvm/tools/llvm-cov/CoverageViewOptions.h @@ -11,6 +11,7 @@ #include "RenderingSupport.h" #include "llvm/Config/llvm-config.h" +#include "llvm/ProfileData/Coverage/CoverageMapping.h" #include namespace llvm { @@ -48,6 +49,7 @@ struct CoverageViewOptions { bool BinaryCounters; OutputFormat Format; BranchOutputType ShowBranches; + coverage::MergeStrategy MergeStrategyOpts; std::string ShowOutputDirectory; std::vector DemanglerOpts; uint32_t TabSize; diff --git a/llvm/tools/llvm-cov/SourceCoverageView.cpp b/llvm/tools/llvm-cov/SourceCoverageView.cpp index dfecddfaf4143..1ef1952e691c9 100644 --- a/llvm/tools/llvm-cov/SourceCoverageView.cpp +++ b/llvm/tools/llvm-cov/SourceCoverageView.cpp @@ -207,8 +207,8 @@ void SourceCoverageView::print(raw_ostream &OS, bool WholeFile, // through them while we iterate lines. llvm::stable_sort(ExpansionSubViews); llvm::stable_sort(InstantiationSubViews); - llvm::stable_sort(BranchSubViews); - llvm::stable_sort(MCDCSubViews); + // BranchSubViews is sorted. + // MCDCSubViews is sorted. auto NextESV = ExpansionSubViews.begin(); auto EndESV = ExpansionSubViews.end(); auto NextISV = InstantiationSubViews.begin(); diff --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp index c94d3853fc014..e53f7618dc0bf 100644 --- a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp +++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp @@ -23,6 +23,16 @@ using namespace llvm; namespace { +template +bool isSummaryEmpty(const SummaryTy &Report, const CoverageViewOptions &Opts) { + return !(Report.FunctionCoverage.getNumFunctions() || + (Opts.ShowInstantiationSummary && + Report.InstantiationCoverage.getNumFunctions()) || + (Opts.ShowRegionSummary && Report.RegionCoverage.getNumRegions()) || + (Opts.ShowBranchSummary && Report.BranchCoverage.getNumBranches()) || + (Opts.ShowMCDCSummary && Report.MCDCCoverage.getNumPairs())); +} + // Return a string with the special characters in \p Str escaped. std::string escape(StringRef Str, const CoverageViewOptions &Opts) { std::string TabExpandedResult; @@ -666,7 +676,7 @@ Error CoveragePrinterHTML::createIndexFile( Coverage, Totals, SourceFiles, Opts, Filters); bool EmptyFiles = false; for (unsigned I = 0, E = FileReports.size(); I < E; ++I) { - if (FileReports[I].FunctionCoverage.getNumFunctions()) + if (!isSummaryEmpty(FileReports[I], Opts)) emitFileSummary(OSRef, SourceFiles[I], FileReports[I]); else EmptyFiles = true; @@ -734,7 +744,7 @@ struct CoveragePrinterHTMLDirectory::Reporter : public DirectoryCoverageReport { // Make directories at the top of the table. for (auto &&SubDir : SubDirs) { auto &Report = SubDir.second.first; - if (!Report.FunctionCoverage.getNumFunctions()) + if (isSummaryEmpty(Report, Printer.Opts)) EmptyFiles.push_back(&Report); else emitTableRow(OSRef, Options, buildRelLinkToFile(Report.Name), Report, @@ -743,7 +753,7 @@ struct CoveragePrinterHTMLDirectory::Reporter : public DirectoryCoverageReport { for (auto &&SubFile : SubFiles) { auto &Report = SubFile.second; - if (!Report.FunctionCoverage.getNumFunctions()) + if (isSummaryEmpty(Report, Printer.Opts)) EmptyFiles.push_back(&Report); else emitTableRow(OSRef, Options, buildRelLinkToFile(Report.Name), Report,