llvm/llvm-project

[LLVM-COV] "abort" function leads to incorrect coverage

Closed this issue · 1 comments

Bugzilla Link 49440
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 <limits.h>
#include <stdio.h>
extern void abort(void);

#if INT_MAX == 32767
#define LONG_CHARCONST '!\234a'
#define SHORT_CHARCONST '\234a'
#define POS_CHARCONST '\1'
#elif INT_MAX == 2147483647
#define LONG_CHARCONST '!\234abc'
#define SHORT_CHARCONST '\234abc'
#define POS_CHARCONST '\234a'
#elif INT_MAX == 9223372036854775807
#define LONG_CHARCONST '!\234abcdefg'
#define SHORT_CHARCONST '\234abcdefg'
#define POS_CHARCONST '\234a'
#else

#define LONG_CHARCONST '\234a'
#define SHORT_CHARCONST '\234a'
#define POS_CHARCONST '\1'
#endif

#if POS_CHARCONST < 0
#error Charconst incorrectly sign-extended
#endif

#if LONG_CHARCONST != SHORT_CHARCONST
#error Overly long charconst truncates wrongly for preprocessor
#endif

int main() {
if (LONG_CHARCONST != SHORT_CHARCONST){
abort();
}
if (LONG_CHARCONST != SHORT_CHARCONST)
abort();
if (POS_CHARCONST < 0)
abort();
printf("passed\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
passed
1| |#include <limits.h>
2| |#include <stdio.h>
3| |extern void abort(void);
4| |
5| |#if INT_MAX == 32767
6| |#define LONG_CHARCONST '!\234a'
7| |#define SHORT_CHARCONST '\234a'
8| |#define POS_CHARCONST '\1'
9| |#elif INT_MAX == 2147483647
10| 2|#define LONG_CHARCONST '!\234abc'
11| 2|#define SHORT_CHARCONST '\234abc'
12| 1|#define POS_CHARCONST '\234a'
13| |#elif INT_MAX == 9223372036854775807
14| |#define LONG_CHARCONST '!\234abcdefg'
15| |#define SHORT_CHARCONST '\234abcdefg'
16| |#define POS_CHARCONST '\234a'
17| |#else
18| |
19| |#define LONG_CHARCONST '\234a'
20| |#define SHORT_CHARCONST '\234a'
21| |#define POS_CHARCONST '\1'
22| |#endif
23| |
24| |#if POS_CHARCONST < 0
25| |#error Charconst incorrectly sign-extended
26| |#endif
27| |
28| |#if LONG_CHARCONST != SHORT_CHARCONST
29| |#error Overly long charconst truncates wrongly for preprocessor
30| |#endif
31| |
32| 1|int main() {
33| 1| if (LONG_CHARCONST != SHORT_CHARCONST){
34| 0| abort();
35| 0| }
36| 1| if (LONG_CHARCONST != SHORT_CHARCONST)
37| 1| abort();
38| 1| if (POS_CHARCONST < 0)
39| 0| abort();
40| 1| printf("passed\n");
41| 1| return 0;
42| 1|}

In my opinion, line #​34, #​37 and #​39 should not be executed.

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

    1|       |#include <limits.h>
    2|       |#include <stdio.h>
    3|       |extern void abort(void);
    4|       |
    5|       |#if INT_MAX == 32767
    6|       |#define LONG_CHARCONST '!\234a'
    7|       |#define SHORT_CHARCONST '\234a'
    8|       |#define POS_CHARCONST '\1'
    9|       |#elif INT_MAX == 2147483647
   10|      2|#define LONG_CHARCONST '!\234abc'
   11|      2|#define SHORT_CHARCONST '\234abc'
   12|      1|#define POS_CHARCONST '\234a'
   13|       |#elif INT_MAX == 9223372036854775807
   14|       |#define LONG_CHARCONST '!\234abcdefg'
   15|       |#define SHORT_CHARCONST '\234abcdefg'
   16|       |#define POS_CHARCONST '\234a'
   17|       |#else
   18|       |
   19|       |#define LONG_CHARCONST '\234a'
   20|       |#define SHORT_CHARCONST '\234a'
   21|       |#define POS_CHARCONST '\1'
   22|       |#endif
   23|       |
   24|       |#if POS_CHARCONST < 0
   25|       |#error Charconst incorrectly sign-extended
   26|       |#endif
   27|       |
   28|       |#if LONG_CHARCONST != SHORT_CHARCONST
   29|       |#error Overly long charconst truncates wrongly for preprocessor
   30|       |#endif
   31|       |
   32|      1|int main() {
   33|      1|  if (LONG_CHARCONST != SHORT_CHARCONST){
   34|      0|    abort();
   35|      0|  }
   36|      1|  if (LONG_CHARCONST != SHORT_CHARCONST)
   37|      0|    abort();
   38|      1|  if (POS_CHARCONST < 0)
   39|      0|    abort();
   40|      1|  printf("passed\n");
   41|      1|  return 0;
   42|      1|}