Skip to content

[Coverage] Fix mapping for do-while loops with terminating statements #139777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions clang/lib/CodeGen/CoverageMappingGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1703,14 +1703,13 @@ struct CounterCoverageMappingBuilder
if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
if (BodyHasTerminateStmt)
HasTerminateStmt = true;
}

// Create Branch Region around condition.
if (!llvm::EnableSingleByteCoverage)
createBranchRegion(S->getCond(), BodyCount, BranchCount.Skipped);

if (BodyHasTerminateStmt)
HasTerminateStmt = true;
}

void VisitForStmt(const ForStmt *S) {
Expand Down
13 changes: 12 additions & 1 deletion clang/test/CoverageMapping/terminate-statements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,20 @@ int elsecondnoret(void) {

// CHECK-LABEL: _Z18statementexprnoretb:
int statementexprnoret(bool crash) {
int rc = ({ if (crash) abort(); 0; }); // CHECK: File 0, 351:35 -> 352:12 = (#0 - #1)
int rc = ({ if (crash) abort(); 0; }); // CHECK: File 0, [[@LINE]]:35 -> [[@LINE+1]]:12 = (#0 - #1)
return rc; // CHECK-NOT: Gap
}

// CHECK-LABEL: _Z13do_with_breaki:
int do_with_break(int n) {
do {
if (n == 87) {
break;
} // CHECK: File 0, [[@LINE-2]]:18 -> [[@LINE]]:6 = #2
} while (0); // CHECK: File 0, [[@LINE]]:12 -> [[@LINE]]:13 = ((#0 + #1) - #2)
return 0; // CHECK-NOT: Gap,File 0, [[@LINE-1]]:15
}

int main() {
foo(0);
foo(1);
Expand All @@ -375,5 +385,6 @@ int main() {
abstractcondnoret();
elsecondnoret();
statementexprnoret(false);
do_with_break(0);
return 0;
}
21 changes: 21 additions & 0 deletions compiler-rt/test/profile/Linux/coverage-do-while.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// REQUIRES: lld-available
// XFAIL: powerpc64-target-arch

// RUN: %clangxx_profgen -fuse-ld=lld -fcoverage-mapping -o %t %s
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t
// RUN: llvm-profdata merge -o %t.profdata %t.profraw
// RUN: llvm-cov show %t -instr-profile=%t.profdata 2>&1 | FileCheck %s

#include <stdio.h>

// clang-format off
int main(int argc, char **argv) { // CHECK: [[@LINE]]| 1|int main(
do { // CHECK: [[@LINE]]| 1| do {
if (argc == 87) { // CHECK: [[@LINE]]| 1| if (argc
break; // CHECK: [[@LINE]]| 0| break
} // CHECK: [[@LINE]]| 0| }
} while (0); // CHECK: [[@LINE]]| 1| } while
printf("coverage after do is present\n"); // CHECK: [[@LINE]]| 1| printf(
return 0; // CHECK: [[@LINE]]| 1| return
} // CHECK: [[@LINE]]| 1|}
// clang-format on