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

[LLVM-COV] Else statement inside an if statement lead to incorrect coverage information in llvm-cov

Open llvmbot opened this issue 9 years ago • 9 comments

Bugzilla Link 35404
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @def-,@rnk,@vedantk,@ZequanWu

Extended Description

$ clang -v
clang version 6.0.0-svn318792-1~exp1 (trunk)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.1
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.2.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64

$ cat small.c

int main()
{
    int i=1, j=1;
    if (i > 0) {
        if (j > 0)
            return 0;
        else
            j++;
    }
    return 1;
}
$ clang -w -O0 -g -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.gcov

$ cat small.gcov
    1|       |int main()
    2|      1|{
    3|      1|    int i=1, j=1;
    4|      1|    if (i > 0) {
    5|      1|        if (j > 0)
    6|      1|            return 0;
    7|      0|        else
    8|      0|            j++;
    9|      1|    }
   10|      1|    return 1;
   11|      1|}

Line #​10 is wrongly marked as executed. While removing Line #​7 and Line #​8, the result is correct.

llvmbot avatar Nov 24 '17 03:11 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#36986

llvmbot avatar Nov 27 '21 00:11 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#35437

vedantk avatar Nov 27 '21 00:11 vedantk

The right way to solve is to emit a gap area between the end of each statement which may terminate its region, and the start of the next statement. However, there's a chance this could blow up the size of coverage mappings. I think we could add a gap region between two statements only if the first statement contains a terminating statement like return, continue, break etc.

ZequanWu avatar Feb 12 '21 20:02 ZequanWu

if (true && true) { return 0; }

The second true is shown as uncovered. Same bug I assume.

def- avatar Jan 25 '18 11:01 def-

I went back as far as AppleClang-703 and found that this has never worked properly.

The right way to solve is to emit a gap area between the end of each statement which may terminate its region, and the start of the next statement. However, there's a chance this could blow up the size of coverage mappings.

I don't know when I'll be able to get to this. If anyone is interested in tackling this I'd be happy to help out.

vedantk avatar Nov 30 '17 02:11 vedantk

The problem is that the area from 9:6 -> 10:5 isn't mapped separately. I.e it's active region comes from 2:1.

Simpler reproducer:

int main() {
  if (1)
    return 0;
  else
    ;
  return 1;
}

vedantk avatar Nov 30 '17 01:11 vedantk

*** Bug llvm/llvm-project#34751 has been marked as a duplicate of this bug. ***

vedantk avatar Nov 30 '17 01:11 vedantk

*** Bug llvm/llvm-bugzilla-archive#35437 has been marked as a duplicate of this bug. ***

vedantk avatar Nov 30 '17 01:11 vedantk

Fixed since 9783e2098800

maflcko avatar Jul 20 '26 15:07 maflcko