llvm/llvm-project

[LLVM-COV] "return" statement and "exit" statement lead to incorrect coverage

Closed this issue · 1 comments

Bugzilla Link 49439
Version 11.0
OS Linux
Reporter LLVM Bugzilla Contributor

Extended Description

$ clang -v
clang version 11.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/wangyang/llvm-project/build/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Candidate multilib: .;@m64
Selected multilib: .;@m64

$ cat test.c
#include<stdio.h>
unsigned test(unsigned one, unsigned bit) {
unsigned val = bit & 1;
unsigned zero = one >> 1;

val++;
return zero + (val >> 1);
}

int main() {
if (test(1, 0) != 0)
abort();
if (test(1, 1) != 1)
abort();
if (test(1, 65535) != 1)
abort();
printf("0\n");
exit(0);
printf("1\n");
return 0;
printf("2\n");
return 0;
}

$ clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=test.profraw test.c; ./a.out; llvm-profdata merge test.profraw -o test.profdata; llvm-cov show a.out -instr-profile=test.profdata test.c > test.lcov; cat test.lcov
0
1| |#include<stdio.h>
2| 3|unsigned test(unsigned one, unsigned bit) {
3| 3| unsigned val = bit & 1;
4| 3| unsigned zero = one >> 1;
5| 3|
6| 3| val++;
7| 3| return zero + (val >> 1);
8| 3|}
9| |
10| 1|int main() {
11| 1| if (test(1, 0) != 0)
12| 0| abort();
13| 1| if (test(1, 1) != 1)
14| 0| abort();
15| 1| if (test(1, 65535) != 1)
16| 0| abort();
17| 1| printf("0\n");
18| 1| exit(0);
19| 1| printf("1\n");
20| 0| return 0;
21| 1| printf("2\n");
22| 0| return 0;
23| 1|}

Only line 17 was executed among the three "printf" statements. Line #​19 and #​21 were wrongly marked as executed.

In 14, the coverage is correct with the same commands.

    1|       |#include<stdio.h>
    2|      3|unsigned test(unsigned one, unsigned bit) {
    3|      3|  unsigned val = bit & 1;
    4|      3|  unsigned zero = one >> 1;
    5|       |
    6|      3|  val++;
    7|      3|  return zero + (val >> 1);
    8|      3|}
    9|       |
   10|      1|int main() {
   11|      1|  if (test(1, 0) != 0)
   12|      0|    abort();
   13|      1|  if (test(1, 1) != 1)
   14|      0|    abort();
   15|      1|  if (test(1, 65535) != 1)
   16|      0|    abort();
   17|      1|  printf("0\n");
   18|      1|  exit(0);
   19|      0|  printf("1\n");
   20|      0|  return 0;
   21|      0|  printf("2\n");
   22|      0|  return 0;
   23|      1|}