Skip to content

Commit dc94eb5

Browse files
authored
[clang][CodeCoverage] Fix CoverageMapping for binary conditionals ops (#82141)
Fix an issue that produces a wrong coverage mapping when using binary conditional operators as show in the example below. Before this patch: 1| 1|int binary_cond(int x) { 2| 1| x = x ?: 4; 3| 1| int y = 0; 4| 0| return x; <-- Not covered 5| 1|} After this patch: 1| 1|int binary_cond(int x) { 2| 1| x = x ?: 4; 3| 1| int y = 0; 4| 1| return x; <-- Covered 5| 1|}
1 parent 5e83b26 commit dc94eb5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,8 @@ struct CounterCoverageMappingBuilder
19421942

19431943
extendRegion(E->getTrueExpr());
19441944
OutCount = propagateCounts(TrueCount, E->getTrueExpr());
1945+
} else {
1946+
OutCount = TrueCount;
19451947
}
19461948

19471949
extendRegion(E->getFalseExpr());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only %s | FileCheck %s
2+
3+
// CHECK-LABEL: binary_conditional:
4+
// CHECK-NEXT: File 0, [[@LINE+4]]:31 -> {{[0-9]+}}:2 = #0
5+
// CHECK-NEXT: File 0, [[@LINE+4]]:7 -> [[@LINE+4]]:8 = #0
6+
// CHECK-NEXT: Branch,File 0, [[@LINE+3]]:7 -> [[@LINE+3]]:8 = #1, (#0 - #1)
7+
// CHECK-NEXT: File 0, [[@LINE+2]]:13 -> [[@LINE+2]]:14 = (#0 - #1)
8+
int binary_conditional(int x) {
9+
x = x ? : 4;
10+
int y = x;
11+
return y;
12+
}
13+
14+
// CHECK-LABEL: ternary_conditional:
15+
// CHECK-NEXT: File 0, [[@LINE+6]]:31 -> {{[0-9]+}}:2 = #0
16+
// CHECK-NEXT: File 0, [[@LINE+6]]:7 -> [[@LINE+6]]:8 = #0
17+
// CHECK-NEXT: Branch,File 0, [[@LINE+5]]:7 -> [[@LINE+5]]:8 = #1, (#0 - #1)
18+
// CHECK-NEXT: Gap,File 0, [[@LINE+4]]:10 -> [[@LINE+4]]:11 = #1
19+
// CHECK-NEXT: File 0, [[@LINE+3]]:11 -> [[@LINE+3]]:12 = #1
20+
// CHECK-NEXT: File 0, [[@LINE+2]]:15 -> [[@LINE+2]]:16 = (#0 - #1)
21+
int ternary_conditional(int x) {
22+
x = x ? x : 4;
23+
int y = x;
24+
return y;
25+
}

0 commit comments

Comments
 (0)