Skip to content

Commit 310bfcc

Browse files
committed
[Coverage] Use the most-recent completed region count (PR35437)
This is a fix for the coverage segment builder. If multiple regions must be popped off the active stack at once, and more than one of them end at the same location, emit a segment using the count from the most-recent completed region. Fixes PR35437, rdar://35760630 Testing: invoked llvm-cov on a stage2 build of clang, additional unit tests, check-profile git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319391 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b45fa1a commit 310bfcc

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,12 @@ class SegmentBuilder {
388388
if (CompletedSegmentLoc == CompletedRegion->endLoc())
389389
continue;
390390

391+
// Use the count from the next completed region if it ends at the same
392+
// location.
393+
if (I + 1 < E &&
394+
CompletedRegion->endLoc() == ActiveRegions[I + 1]->endLoc())
395+
CompletedRegion = ActiveRegions[I + 1];
396+
391397
startSegment(*CompletedRegion, CompletedSegmentLoc, false);
392398
}
393399

test/tools/llvm-cov/deferred-region.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void while_loop() {
4545
break; // CHECK: [[@LINE]]|{{ +}}1|
4646
// CHECK: [[@LINE]]|{{ +}}0|
4747
while (++x < 5) {} // CHECK: [[@LINE]]|{{ +}}0|
48-
} // CHECK: [[@LINE]]|{{ +}}1|
48+
} // CHECK: [[@LINE]]|{{ +}}0|
4949

5050
if (x == 0) // CHECK: [[@LINE]]|{{ +}}1|
5151
throw Error(); // CHECK: [[@LINE]]|{{ +}}0|
@@ -97,6 +97,8 @@ int main() {
9797
// MARKER-NEXT: Highlighted line 47, 14 -> 21
9898
// MARKER-NEXT: Highlighted line 47, 21 -> 23
9999
// MARKER-NEXT: Highlighted line 47, 23 -> 25
100+
// MARKER-NEXT: Highlighted line 47, 25 -> ?
101+
// MARKER-NEXT: Highlighted line 48, 1 -> 6
100102
// MARKER-NEXT: Highlighted line 51, 7 -> 20
101103
// MARKER-NEXT: Marker at 53:5 = 1
102104
// MARKER-NEXT: Highlighted line 55, 9 -> 14

unittests/ProfileData/CoverageMappingTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,32 @@ TEST_P(CoverageMappingTest, multiple_regions_end_after_parent_ends) {
466466
EXPECT_EQ(CoverageSegment(9, 9, false), Segments[7]);
467467
}
468468

469+
TEST_P(CoverageMappingTest, multiple_completed_segments_at_same_loc) {
470+
ProfileWriter.addRecord({"func1", 0x1234, {0, 1, 2}}, Err);
471+
startFunction("func1", 0x1234);
472+
473+
// PR35437
474+
addCMR(Counter::getCounter(1), "file1", 2, 1, 18, 2);
475+
addCMR(Counter::getCounter(0), "file1", 8, 12, 14, 6);
476+
addCMR(Counter::getCounter(1), "file1", 9, 1, 14, 6);
477+
addCMR(Counter::getCounter(2), "file1", 11, 13, 11, 14);
478+
479+
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
480+
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
481+
const auto &FunctionRecord = *FunctionRecords.begin();
482+
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
483+
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
484+
485+
ASSERT_EQ(6U, Segments.size());
486+
EXPECT_EQ(CoverageSegment(2, 1, 1, true), Segments[0]);
487+
EXPECT_EQ(CoverageSegment(8, 12, 0, true), Segments[1]);
488+
EXPECT_EQ(CoverageSegment(9, 1, 1, true), Segments[2]);
489+
EXPECT_EQ(CoverageSegment(11, 13, 2, true), Segments[3]);
490+
// Use count=1 (from 9:1 -> 14:6), not count=0 (from 8:12 -> 14:6).
491+
EXPECT_EQ(CoverageSegment(11, 14, 1, false), Segments[4]);
492+
EXPECT_EQ(CoverageSegment(18, 2, false), Segments[5]);
493+
}
494+
469495
TEST_P(CoverageMappingTest, dont_emit_redundant_segments) {
470496
ProfileWriter.addRecord({"func1", 0x1234, {1, 1}}, Err);
471497
startFunction("func1", 0x1234);

0 commit comments

Comments
 (0)