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

[LLVM-COV] a statement after calling 'assert()' function is wrongly marked as 'not executed'

Open MaggieYingYi opened this issue 4 years ago • 6 comments

$ clang -v
clang version 16.0.0 (https://github.com/llvm/llvm-project.git 4a77d96903c996076c9df0f696a7ba7ca2de966a)
Target: x86_64-unknown-linux-gnu

$ clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=main.profraw main.cpp
$ ./a.out
$ llvm-profdata merge main.profraw -o main.profdata
$ llvm-cov show a.out -instr-profile=main.profdata main.cpp > main.gcov
$ cat main.gcov
    1|       |#include <assert.h>
    2|      1|int do_add(int a, int b) { return a + b; }
    3|      1|int main(void) {
    4|      1|  assert(true);
    5|      0|  int res = do_add(10, 20);
    6|      1|  return res;
    7|      1|}

Line 5 is wrongly marked as 'not executed'.

MaggieYingYi avatar Nov 16 '22 15:11 MaggieYingYi

I found that the issue is caused by the following commit 9783e2.

Author: Zequan Wu <[email protected]>
Date:   Thu Mar 4 11:50:31 2021 -0800

    Revert "Revert "[Coverage] Emit gap region between statements if first statements contains terminate statements.""

    Reland with update on test case ContinuousSyncmode/basic.c.

    This reverts commit fe5c2c3ca682b140dd5e640e75948363b6b25ef9.

MaggieYingYi avatar Nov 16 '22 15:11 MaggieYingYi

The generated code coverage is correct in the commit 0d4dad (just before commit 9783e2).

$ clang -v
clang version 13.0.0 (https://github.com/llvm/llvm-project.git 0d4dadc64c4f3e072bf0410dc522e5e1f2731c01)
Target: x86_64-unknown-linux-gnu

$ clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=main.profraw main.cpp
$ ./a.out
$ llvm-profdata merge main.profraw -o main.profdata
$ llvm-cov show a.out -instr-profile=main.profdata main.cpp > main.gcov
$ cat main.gcov
    1|       |#include <assert.h>
    2|      1|int do_add(int a, int b) { return a + b; }
    3|      1|int main(void) {
    4|      1|  assert(true);
    5|      1|  int res = do_add(10, 20);
    6|      1|  return res;
    7|      1|}

The issue started from commit 9783e2.

$ clang -v
clang version 13.0.0 (https://github.com/llvm/llvm-project.git 9783e2098800b954c55ae598a1ce5c4b93444fc0)
Target: x86_64-unknown-linux-gnu

$ /home/maggie/github/llvm-project/build/bin/clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=main.profraw main.cpp
$ ./a.out
$ /home/maggie/github/llvm-project/build/bin/llvm-profdata merge main.profraw -o main.profdata
$ /home/maggie/github/llvm-project/build/bin/llvm-cov show a.out -instr-profile=main.profdata main.cpp
$ cat main.gcov
    1|       |#include <assert.h>
    2|      1|int do_add(int a, int b) { return a + b; }
    3|      1|int main(void) {
    4|      1|  assert(true);
    5|      0|  int res = do_add(10, 20);
    6|      1|  return res;
    7|      1|}

MaggieYingYi avatar Nov 16 '22 15:11 MaggieYingYi

If we remove line 4, line 5 is correctly masked as executed.

It looks like the issue is related with calling 'assert()'.

MaggieYingYi avatar Nov 16 '22 15:11 MaggieYingYi

In the current coverage mapping implementation, we terminate the current region and start a zero region when we hit a nonreturn function. However, for logical OR, the second operand is not executed if the first operand evaluates to true. If the nonreturn function is called in the right side of logical OR and the left side of logical OR is TRUE, we should not start a zero GapRegionCounter. This will also apply to VisitAbstractConditionalOperator.

MaggieYingYi avatar Feb 20 '23 10:02 MaggieYingYi

I have created a D144371 to fix the issue.

Issues of 59030, 57388, 57481 and 60701 are fixed by D144371.

MaggieYingYi avatar Feb 20 '23 10:02 MaggieYingYi

Thanks @ZequanWu for reviewing the patch.

The fix has been committed into main branch at commit 94dd4766a61b.

MaggieYingYi avatar Mar 02 '23 09:03 MaggieYingYi