[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
https://github.com/justincady closed https://github.com/llvm/llvm-project/pull/139777 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
hiraditya wrote: LGTM! https://github.com/llvm/llvm-project/pull/139777 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
https://github.com/justincady updated
https://github.com/llvm/llvm-project/pull/139777
>From be83c3207bf195aa7c6df1ce155402fde5911821 Mon Sep 17 00:00:00 2001
From: Justin Cady
Date: Tue, 13 May 2025 15:08:45 -0400
Subject: [PATCH] [Coverage] Fix mapping for do-while loops with terminating
statements
The current region mapping for do-while loops that contain statements
such as break or continue results in inaccurate line coverage reports
for the line following the loop.
This change handles terminating statements the same way that other loop
constructs do, correcting the region mapping for accurate reports. It
also fixes a fragile test relying on exact line numbers.
Fixes #139122
---
clang/lib/CodeGen/CoverageMappingGen.cpp | 5 ++---
.../CoverageMapping/terminate-statements.cpp | 13 +++-
.../test/profile/Linux/coverage-do-while.c| 21 +++
3 files changed, 35 insertions(+), 4 deletions(-)
create mode 100644 compiler-rt/test/profile/Linux/coverage-do-while.c
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 73811d15979d5..6170dc7f59107 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -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) {
diff --git a/clang/test/CoverageMapping/terminate-statements.cpp
b/clang/test/CoverageMapping/terminate-statements.cpp
index ef6b6fd82687d..93d655794ccf5 100644
--- a/clang/test/CoverageMapping/terminate-statements.cpp
+++ b/clang/test/CoverageMapping/terminate-statements.cpp
@@ -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);
@@ -375,5 +385,6 @@ int main() {
abstractcondnoret();
elsecondnoret();
statementexprnoret(false);
+ do_with_break(0);
return 0;
}
diff --git a/compiler-rt/test/profile/Linux/coverage-do-while.c
b/compiler-rt/test/profile/Linux/coverage-do-while.c
new file mode 100644
index 0..9e112cbc4c6a3
--- /dev/null
+++ b/compiler-rt/test/profile/Linux/coverage-do-while.c
@@ -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
+
+// 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
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
https://github.com/ZequanWu approved this pull request. LGTM. Thanks for fixing it. https://github.com/llvm/llvm-project/pull/139777 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
justincady wrote: Ping. :) https://github.com/llvm/llvm-project/pull/139777 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
evodius96 wrote: Thank you for looking at this! https://github.com/llvm/llvm-project/pull/139777 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
llvmbot wrote:
@llvm/pr-subscribers-clang
Author: Justin Cady (justincady)
Changes
The current region mapping for do-while loops that contain statements
such as break or continue results in inaccurate line coverage reports
for the line following the loop.
This change handles terminating statements the same way that other loop
constructs do, correcting the region mapping for accurate reports. It
also fixes a fragile test relying on exact line numbers.
Fixes #139122
---
Full diff: https://github.com/llvm/llvm-project/pull/139777.diff
3 Files Affected:
- (modified) clang/lib/CodeGen/CoverageMappingGen.cpp (+2-3)
- (modified) clang/test/CoverageMapping/terminate-statements.cpp (+12-1)
- (added) compiler-rt/test/profile/Linux/coverage-do-while.c (+21)
``diff
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 73811d15979d5..6170dc7f59107 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -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) {
diff --git a/clang/test/CoverageMapping/terminate-statements.cpp
b/clang/test/CoverageMapping/terminate-statements.cpp
index ef6b6fd82687d..93d655794ccf5 100644
--- a/clang/test/CoverageMapping/terminate-statements.cpp
+++ b/clang/test/CoverageMapping/terminate-statements.cpp
@@ -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);
@@ -375,5 +385,6 @@ int main() {
abstractcondnoret();
elsecondnoret();
statementexprnoret(false);
+ do_with_break(0);
return 0;
}
diff --git a/compiler-rt/test/profile/Linux/coverage-do-while.c
b/compiler-rt/test/profile/Linux/coverage-do-while.c
new file mode 100644
index 0..9e112cbc4c6a3
--- /dev/null
+++ b/compiler-rt/test/profile/Linux/coverage-do-while.c
@@ -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
+
+// 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
``
https://github.com/llvm/llvm-project/pull/139777
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
llvmbot wrote:
@llvm/pr-subscribers-clang-codegen
@llvm/pr-subscribers-pgo
Author: Justin Cady (justincady)
Changes
The current region mapping for do-while loops that contain statements
such as break or continue results in inaccurate line coverage reports
for the line following the loop.
This change handles terminating statements the same way that other loop
constructs do, correcting the region mapping for accurate reports. It
also fixes a fragile test relying on exact line numbers.
Fixes #139122
---
Full diff: https://github.com/llvm/llvm-project/pull/139777.diff
3 Files Affected:
- (modified) clang/lib/CodeGen/CoverageMappingGen.cpp (+2-3)
- (modified) clang/test/CoverageMapping/terminate-statements.cpp (+12-1)
- (added) compiler-rt/test/profile/Linux/coverage-do-while.c (+21)
``diff
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 73811d15979d5..6170dc7f59107 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -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) {
diff --git a/clang/test/CoverageMapping/terminate-statements.cpp
b/clang/test/CoverageMapping/terminate-statements.cpp
index ef6b6fd82687d..93d655794ccf5 100644
--- a/clang/test/CoverageMapping/terminate-statements.cpp
+++ b/clang/test/CoverageMapping/terminate-statements.cpp
@@ -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);
@@ -375,5 +385,6 @@ int main() {
abstractcondnoret();
elsecondnoret();
statementexprnoret(false);
+ do_with_break(0);
return 0;
}
diff --git a/compiler-rt/test/profile/Linux/coverage-do-while.c
b/compiler-rt/test/profile/Linux/coverage-do-while.c
new file mode 100644
index 0..9e112cbc4c6a3
--- /dev/null
+++ b/compiler-rt/test/profile/Linux/coverage-do-while.c
@@ -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
+
+// 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
``
https://github.com/llvm/llvm-project/pull/139777
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
https://github.com/justincady ready_for_review https://github.com/llvm/llvm-project/pull/139777 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [Coverage] Fix mapping for do-while loops with terminating statements (PR #139777)
https://github.com/justincady created
https://github.com/llvm/llvm-project/pull/139777
The current region mapping for do-while loops that contain statements
such as break or continue results in inaccurate line coverage reports
for the line following the loop.
This change handles terminating statements the same way that other loop
constructs do, correcting the region mapping for accurate reports. It
also fixes a fragile test relying on exact line numbers.
Fixes #139122
>From fff85d9cc74e2fbcbf1b2f9c94463070155decd8 Mon Sep 17 00:00:00 2001
From: Justin Cady
Date: Tue, 13 May 2025 15:08:45 -0400
Subject: [PATCH] [Coverage] Fix mapping for do-while loops with terminating
statements
The current region mapping for do-while loops that contain statements
such as break or continue results in inaccurate line coverage reports
for the line following the loop.
This change handles terminating statements the same way that other loop
constructs do, correcting the region mapping for accurate reports. It
also fixes a fragile test relying on exact line numbers.
Fixes #139122
---
clang/lib/CodeGen/CoverageMappingGen.cpp | 5 ++---
.../CoverageMapping/terminate-statements.cpp | 13 +++-
.../test/profile/Linux/coverage-do-while.c| 21 +++
3 files changed, 35 insertions(+), 4 deletions(-)
create mode 100644 compiler-rt/test/profile/Linux/coverage-do-while.c
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 73811d15979d5..6170dc7f59107 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -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) {
diff --git a/clang/test/CoverageMapping/terminate-statements.cpp
b/clang/test/CoverageMapping/terminate-statements.cpp
index ef6b6fd82687d..93d655794ccf5 100644
--- a/clang/test/CoverageMapping/terminate-statements.cpp
+++ b/clang/test/CoverageMapping/terminate-statements.cpp
@@ -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);
@@ -375,5 +385,6 @@ int main() {
abstractcondnoret();
elsecondnoret();
statementexprnoret(false);
+ do_with_break(0);
return 0;
}
diff --git a/compiler-rt/test/profile/Linux/coverage-do-while.c
b/compiler-rt/test/profile/Linux/coverage-do-while.c
new file mode 100644
index 0..9e112cbc4c6a3
--- /dev/null
+++ b/compiler-rt/test/profile/Linux/coverage-do-while.c
@@ -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
+
+// 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
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
