llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

llvm-cov: wrong coverage for statement with nested conditional operator

Open llvmbot opened this issue 5 years ago • 1 comments

Bugzilla Link 48795
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @dwblaikie,@vedantk,@ZequanWu

Extended Description

Consider this case: ... $ cat small.c int main() { int r = 0, z = 1; for (int i = 0; i < 5; i++) { r = (z * z > 4) ? (i < r ? i : r) : r; z++; } return r; } ...

... $ clang -O0 -w -fcoverage-mapping -fprofile-instr-generate=small.profraw small.c; ./a.out; llvm-profdata merge small.profraw -o small.profdata; llvm-cov show a.out -instr-profile=small.profdata small.c > small.c.lcov; cat small.c.lcov 1| |int main() 2| 1|{ 3| 1| int r = 0, z = 1; 4| 6| for (int i = 0; i < 5; i++) { 5| 3| r = (z * z > 4) ? (i < r ? i : r) : r; 6| 5| z++; 7| 5| } 8| 1| return r; 9| 1|} ...

We can found that Line 5 is executed 3 times. The expected output should be 5.

... $ clang --version Ubuntu clang version 12.0.0-++20210117052627+ed396212da41-1~exp1~20210117163329.287 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin

llvmbot avatar Jan 19 '21 03:01 llvmbot

$ clang --version Ubuntu clang version 16.0.0-++20220905063833+2417618d5ca4-1~exp1~20220905183932.719

$ cat small.c int main() { int r = 0, z = 1; for (int i = 0; i < 5; i++) { r = (z * z > 4) ? (i < r ? i : r) : r; z++; } return r; }

$ clang -w -O0 -g -Wimplicit-function-declaration -fcoverage-mapping -fprofle-instr-generate=small.profraw small.c; ./a.out; llvm-profdata merge small.profraw -o small.profdata; llvm-cov show a.out -instr-profile=small.profdata small.c > small.c.lcov; cat small.c.lcov 1| |int main() 2| 1|{ 3| 1| int r = 0, z = 1; 4| 6| for (int i = 0; i < 5; i++) { 5| 5| r = (z * z > 4) ? (i < r ? i : r) : r; 6| 5| z++; 7| 5| } 8| 1| return r; 9| 1|}

In the trunk version, the reported code coverage is correct. I was wondering that this bug should be also closed as fixed.

iamanonymouscs avatar Sep 08 '22 14:09 iamanonymouscs