[Coverage] Add gap region between binary operator '&& and ||' and RHS (#149085)
## Issue Summary
We identified an inaccuracy in line coverage reporting when short-circuit evaluation occurs in multi-line conditional expressions.
Specifically:
1. Un-executed conditions following line breaks may be incorrectly marked as covered
(e.g., conditionB in a non-executed && chain shows coverage)
```
1| |#include <iostream>
2| |
3| 1|int main() {
4| 1| bool conditionA = false;
5| 1| bool conditionB = true;
6| 1| if (conditionA &&
7| 1| conditionB) {
8| 0| std::cout << "IF-THEN" << std::endl;
9| 0| }
10| 1| return 0;
11| 1|}
```
2. Inconsistent coverage reporting across un-executed conditions
*(adjacent un-executed conditions may show 1 vs 0 line coverage)*
```
1| |#include <iostream>
2| |
3| 1|int main() {
4| 1| bool conditionA = false;
5| 1| bool conditionB = true;
6| 1| bool conditionC = true;
7| 1| if (conditionA &&
8| 1| (conditionB ||
9| 0| conditionC)) {
10| 0| std::cout << "IF-THEN" << std::endl;
11| 0| }
12| 1| return 0;
13| 1|}
```
This is resolved by inserting a GapRegion when mapping logical operators to isolate coverage contexts around short-circuit evaluation.
GitOrigin-RevId: f4cf610159189ec877142313a18698434386afb0
diff --git a/test/profile/Linux/coverage_short_circuit.cpp b/test/profile/Linux/coverage_short_circuit.cpp
new file mode 100644
index 0000000..54f0c4c
--- /dev/null
+++ b/test/profile/Linux/coverage_short_circuit.cpp
@@ -0,0 +1,36 @@
+// RUN: %clangxx_profgen -std=c++17 -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
+
+void foo() { // CHECK: [[@LINE]]| 1|void foo() {
+ bool cond1 = false; // CHECK-NEXT: [[@LINE]]| 1| bool cond1 = false;
+ bool cond2 = true; // CHECK-NEXT: [[@LINE]]| 1| bool cond2 = true;
+ if (cond1 && // CHECK-NEXT: [[@LINE]]| 1| if (cond1 &&
+ cond2) { // CHECK-NEXT: [[@LINE]]| 0| cond2) {
+ } // CHECK-NEXT: [[@LINE]]| 0| }
+} // CHECK-NEXT: [[@LINE]]| 1|}
+
+void bar() { // CHECK: [[@LINE]]| 1|void bar() {
+ bool cond1 = true; // CHECK-NEXT: [[@LINE]]| 1| bool cond1 = true;
+ bool cond2 = false; // CHECK-NEXT: [[@LINE]]| 1| bool cond2 = false;
+ if (cond1 && // CHECK-NEXT: [[@LINE]]| 1| if (cond1 &&
+ cond2) { // CHECK-NEXT: [[@LINE]]| 1| cond2) {
+ } // CHECK-NEXT: [[@LINE]]| 0| }
+} // CHECK-NEXT: [[@LINE]]| 1|}
+
+void baz() { // CHECK: [[@LINE]]| 1|void baz() {
+ bool cond1 = false; // CHECK-NEXT: [[@LINE]]| 1| bool cond1 = false;
+ bool cond2 = true; // CHECK-NEXT: [[@LINE]]| 1| bool cond2 = true;
+ if (cond1 // CHECK-NEXT: [[@LINE]]| 1| if (cond1
+ && // CHECK-NEXT: [[@LINE]]| 0| &&
+ cond2) { // CHECK-NEXT: [[@LINE]]| 0| cond2) {
+ } // CHECK-NEXT: [[@LINE]]| 0| }
+} // CHECK-NEXT: [[@LINE]]| 1|}
+
+int main() {
+ foo();
+ bar();
+ baz();
+ return 0;
+}