InstrProf: Don't combine expansion regions with code regions
authorJustin Bogner <mail@justinbogner.com>
Wed, 18 Feb 2015 19:01:06 +0000 (19:01 +0000)
committerJustin Bogner <mail@justinbogner.com>
Wed, 18 Feb 2015 19:01:06 +0000 (19:01 +0000)
This was leading to duplicate counts when a code region happened to
overlap exactly with an expansion. The combining behaviour only makes
sense for code regions.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229723 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ProfileData/CoverageMapping.cpp
unittests/ProfileData/CoverageMappingTest.cpp

index bef8605..a124d79 100644 (file)
@@ -314,7 +314,7 @@ public:
         popRegion();
       if (PrevRegion && PrevRegion->startLoc() == Region.startLoc() &&
           PrevRegion->endLoc() == Region.endLoc()) {
-        if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion)
+        if (Region.Kind == coverage::CounterMappingRegion::CodeRegion)
           Segments.back().addCount(Region.ExecutionCount);
       } else {
         // Add this region to the stack.
index 5e64843..9fceacb 100644 (file)
@@ -222,4 +222,41 @@ TEST_F(CoverageMappingTest, uncovered_function) {
   ASSERT_EQ(CoverageSegment(3, 4, false),   Segments[1]);
 }
 
+TEST_F(CoverageMappingTest, combine_regions) {
+  ProfileWriter.addFunctionCounts("func", 0x1234, {10, 20, 30});
+  readProfCounts();
+
+  addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
+  addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
+  addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
+  loadCoverageMapping("func", 0x1234);
+
+  CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
+  std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
+  ASSERT_EQ(4U, Segments.size());
+  ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
+  ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
+  ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
+  ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
+}
+
+TEST_F(CoverageMappingTest, dont_combine_expansions) {
+  ProfileWriter.addFunctionCounts("func", 0x1234, {10, 20});
+  readProfCounts();
+
+  addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
+  addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
+  addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
+  addExpansionCMR("file1", "include1", 3, 3, 4, 4);
+  loadCoverageMapping("func", 0x1234);
+
+  CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
+  std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
+  ASSERT_EQ(4U, Segments.size());
+  ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
+  ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
+  ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
+  ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
+}
+
 } // end anonymous namespace