[llvm] [llvm-cov] Fix branch counts of template functions (#111743) (PR #113925)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 21 02:23:51 PST 2024


================
@@ -75,70 +90,120 @@ void renderLineExecutionCounts(raw_ostream &OS,
   }
 }
 
-std::vector<llvm::coverage::CountedRegion>
+std::vector<NestedCountedRegion>
 collectNestedBranches(const coverage::CoverageMapping &Coverage,
                       ArrayRef<llvm::coverage::ExpansionRecord> Expansions,
-                      int ViewDepth = 0, int SrcLine = 0) {
-  std::vector<llvm::coverage::CountedRegion> Branches;
+                      std::vector<LineColPair> &NestedPath) {
+  std::vector<NestedCountedRegion> Branches;
   for (const auto &Expansion : Expansions) {
     auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
 
-    // If we're at the top level, set the corresponding source line.
-    if (ViewDepth == 0)
-      SrcLine = Expansion.Region.LineStart;
+    // Track the path to the nested expansions
+    NestedPath.push_back(Expansion.Region.startLoc());
 
     // Recursively collect branches from nested expansions.
     auto NestedExpansions = ExpansionCoverage.getExpansions();
-    auto NestedExBranches = collectNestedBranches(Coverage, NestedExpansions,
-                                                  ViewDepth + 1, SrcLine);
+    auto NestedExBranches =
+        collectNestedBranches(Coverage, NestedExpansions, NestedPath);
     append_range(Branches, NestedExBranches);
 
     // Add branches from this level of expansion.
     auto ExBranches = ExpansionCoverage.getBranches();
-    for (auto B : ExBranches)
+    for (auto &B : ExBranches)
       if (B.FileID == Expansion.FileID) {
-        B.LineStart = SrcLine;
-        Branches.push_back(B);
+        Branches.push_back(NestedCountedRegion(B, NestedPath));
       }
+
+    NestedPath.pop_back();
   }
 
   return Branches;
 }
 
-bool sortLine(llvm::coverage::CountedRegion I,
-              llvm::coverage::CountedRegion J) {
-  return (I.LineStart < J.LineStart) ||
-         ((I.LineStart == J.LineStart) && (I.ColumnStart < J.ColumnStart));
+void appendNestedCountedRegions(const std::vector<CountedRegion> &Src,
+                                std::vector<NestedCountedRegion> &Dst) {
+  auto Unfolded = make_filter_range(Src, [](auto &Region) {
+    return !Region.TrueFolded || !Region.FalseFolded;
+  });
+  Dst.reserve(Dst.size() + Src.size());
+  std::transform(Unfolded.begin(), Unfolded.end(), std::back_inserter(Dst),
+                 [=](auto &Region) {
+                   return NestedCountedRegion(Region, {Region.startLoc()});
+                 });
+}
+
+void appendNestedCountedRegions(const std::vector<NestedCountedRegion> &Src,
+                                std::vector<NestedCountedRegion> &Dst) {
+  auto Unfolded = make_filter_range(Src, [](auto &NestedRegion) {
+    return !NestedRegion.TrueFolded || !NestedRegion.FalseFolded;
+  });
+  Dst.reserve(Dst.size() + Src.size());
+  std::copy(Unfolded.begin(), Unfolded.end(), std::back_inserter(Dst));
+}
+
+bool sortNested(const NestedCountedRegion &I, const NestedCountedRegion &J) {
+  // This sorts each element by line and column
+  // Implies that all elements are first sorted by getEffectiveLine()
+  return I.NestedPath < J.NestedPath;
+}
+
+void combineInstanceCounts(std::vector<NestedCountedRegion> &Branches) {
+  auto NextBranch = Branches.begin();
+  auto EndBranch = Branches.end();
+
+  while (NextBranch != EndBranch) {
+    auto SumBranch = NextBranch++;
+
+    // Ensure that only branches with the same NestedPath are summed up
+    while (NextBranch != EndBranch &&
+           SumBranch->NestedPath == NextBranch->NestedPath) {
+      SumBranch->ExecutionCount += NextBranch->ExecutionCount;
+      SumBranch->FalseExecutionCount += NextBranch->FalseExecutionCount;
+      NextBranch->TrueFolded = true;
----------------
stma247 wrote:

Okay, I added a new flag `Ignore` to `struct NestedCountedRegion` to make it more clear.

https://github.com/llvm/llvm-project/pull/113925


More information about the llvm-commits mailing list