llvm/llvm-project

[LLVM-COV] Label in switch statement and dead code were wrongly marked as executed

Closed this issue · 2 comments

Bugzilla Link 49423
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>
int *ptr;
struct a {
int c;
};
int main(int argc, char **argv) {
struct a e;
e.c = 2;
int x = 0;
for (;;)
switch (e.c)
case 3: {
printf("1");
int resxxx;
case 2:
ptr = &resxxx;
*ptr = 123;
if (x)
return 0;
else
x = 1;
}
printf("2");
return 1;
}

$ 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
1| |#include<stdio.h>
2| |int *ptr;
3| |struct a {
4| | int c;
5| |};
6| 1|int main(int argc, char **argv) {
7| 1| struct a e;
8| 1| e.c = 2;
9| 1| int x = 0;
10| 1| for (;;)
11| 2| switch (e.c)
12| 2| case 3: {
13| 0| printf("1");
14| 0| int resxxx;
15| 2| case 2:
16| 2| ptr = &resxxx;
17| 2| *ptr = 123;
18| 2| if (x)
19| 1| return 0;
20| 1| else
21| 1| x = 1;
22| 2| }
23| 1| printf("2");
24| 0| return 1;
25| 1|}
26| |

Line 12 and 23 were actually not executed.

line 10 was executed 2 times actually

In 14, with the same command, the coverage of line 23 is correct.

    1|       |#include<stdio.h>
    2|       |int *ptr;
    3|       |struct a {
    4|       |  int c;
    5|       |};
    6|      1|int main(int argc, char **argv) {
    7|      1|  struct a e;
    8|      1|  e.c = 2;
    9|      1|  int x = 0;
   10|      1|  for (;;)
   11|      2|    switch (e.c)
   12|      2|    case 3: {
   13|      0|      printf("1");
   14|      0|      int resxxx;
   15|      2|    case 2:
   16|      2|      ptr = &resxxx;
   17|      2|      *ptr = 123;
   18|      2|      if (x)
   19|      1|        return 0;
   20|      1|      else
   21|      1|        x = 1;
   22|      2|    }
   23|      0|      printf("2");
   24|      0|      return 1;
   25|      1|}