Skip to content

Commit

Permalink
Fix branch coverage merging in FunctionCoverageSummary::get() for ins…
Browse files Browse the repository at this point in the history
…tantiation

groups.

This change corrects the implementation for the branch coverage
summary to do the same thing for branches that is done for lines and regions.
That is, across function instantiations in an instantiation group, the maximum
branch coverage found in any of those instantiations is returned, with the
total number of branches being the same across instantiations.

Differential Revision: https://reviews.llvm.org/D102193
  • Loading branch information
evodius96 committed May 11, 2021
1 parent 2c1f9f3 commit 6400905
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
16 changes: 15 additions & 1 deletion llvm/test/tools/llvm-cov/branch-templates.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// RUN: llvm-profdata merge %S/Inputs/branch-templates.proftext -o %t.profdata
// RUN: llvm-cov show --show-expansions --show-branches=count %S/Inputs/branch-templates.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S %s | FileCheck %s
// RUN: llvm-cov report --show-branch-summary %S/Inputs/branch-templates.o32l -instr-profile %t.profdata -show-functions -path-equivalence=/tmp,%S %s | FileCheck %s -check-prefix=REPORT
// RUN: llvm-cov report --show-branch-summary %S/Inputs/branch-templates.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S %s | FileCheck %s -check-prefix=REPORTFILE

#include <stdio.h>

template<typename T>
void unused(T x) {
return;
Expand Down Expand Up @@ -45,3 +45,17 @@ int main() {
// REPORT-NEXT: _Z4funcIfEiT_ 5 2 60.00% 7 3 57.14% 2 1 50.00%
// REPORT-NEXT: ---
// REPORT-NEXT: TOTAL 22 7 68.18% 31 11 64.52% 12 6 50.00%

// Make sure the covered branch tally for the function instantiation group is
// merged to reflect maximum branch coverage of a single instantiation, just
// like what is done for lines and regions. Also, the total branch tally
// summary for an instantiation group should agree with the total number of
// branches in the definition (In this case, 2 and 6 for func<>() and main(),
// respectively). This is returned by: FunctionCoverageSummary::get(const
// InstantiationGroup &Group, ...)

// REPORTFILE: Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
// REPORTFILE-NEXT: ---
// REPORTFILE-NEXT: /tmp/branch-templates.cpp 12 3 75.00% 2 0 100.00% 17 4 76.47% 8 4 50.00%
// REPORTFILE-NEXT: ---
// REPORTFILE-NEXT: TOTAL 12 3 75.00% 2 0 100.00% 17 4 76.47% 8 4 50.00%
6 changes: 1 addition & 5 deletions llvm/tools/llvm-cov/CoverageSummaryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ FunctionCoverageSummary::get(const InstantiationGroup &Group,
for (const auto &FCS : Summaries.drop_front()) {
Summary.RegionCoverage.merge(FCS.RegionCoverage);
Summary.LineCoverage.merge(FCS.LineCoverage);

// Sum branch coverage across instantiation groups for the summary rather
// than "merge" the maximum count. This is a clearer view into whether all
// created branches are covered.
Summary.BranchCoverage += FCS.BranchCoverage;
Summary.BranchCoverage.merge(FCS.BranchCoverage);
}
return Summary;
}
5 changes: 5 additions & 0 deletions llvm/tools/llvm-cov/CoverageSummaryInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ 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; }
Expand Down

0 comments on commit 6400905

Please sign in to comment.