Issue 157981
Summary [Coverage] Incorrect line coverage reported for multi-line for-loop header
Labels new issue
Assignees
Reporter erkaii
    How to reproduce it (Compiler explorer [link](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:2,endLineNumber:9,positionColumn:1,positionLineNumber:1,selectionStartColumn:2,selectionStartLineNumber:9,startColumn:1,startLineNumber:1),source:'int+main()+%7B%0A++++for+(int+j+%3D+0%3B+j+%3C+7%3B+j%2B%2B)+%7B%0A++++++++for+(int+i+%3D+j%3B+i+%3C+2%3B+%0A+++++++++++++i%2B%2B)+%7B%0A++++++++++++int+h%3B%0A++++++++%7D%0A++++%7D%0A++++return+0%3B%0A%7D'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:clang_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'1',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-fprofile-instr-generate+-fcoverage-mapping+',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(trunk)+(Editor+%231)',t:'0')),k:50,l:'4',m:50,n:'0',o:'',s:0,t:'0'),(g:!((h:tool,i:(args:'',argsPanelShown:'0',compilerName:'x86-64+clang+(trunk)',editorid:1,fontScale:12,fontUsePx:'0',j:1,monacoEditorHasBeenAutoOpened:'1',monacoEditorOpen:'1',monacoStdin:'1',stdin:'',stdinPanelShown:'1',toolId:llvm-covtrunk,treeid:0,wrap:'1'),l:'5',n:'0',o:'llvm-cov+(clang-only)+x86-64+clang+(trunk)+(Editor+%231,+Compiler+%231)',t:'0')),header:(),l:'4',m:50,n:'0',o:'',s:0,t:'0')),k:50,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)):

Hit the bug when measuring coverage for code [https://sources.debian.org/src/psmisc/23.6-1/src/fuser.c#L2067](https://sources.debian.org/src/psmisc/23.6-1/src/fuser.c#L2067).

```bash
cat > test.c << EOF
int main() {
    for (int j = 0; j < 7; j++) {
        for (int i = j; i < 2; 
             i++) {
            int h;
        }
 }
    return 0;
}
EOF
clang -fprofile-instr-generate -fcoverage-mapping test.c -o test
./test
llvm-profdata merge default.profraw -o default.profdata
llvm-cov show -instr-profile default.profdata test
```

Coverage report where line 4 is incorrectly marked as covered 7 times (should be 3 times):

```
    1|      1|int main() {
    2|      8| for (int j = 0; j < 7; j++) {
    3|     10|        for (int i = j; i < 2; 
    4|      7|             i++) {
    5|      3|            int h;
 6|      3|        }
    7|      7|    }
    8|      1|    return 0;
    9| 1|}
```

The necessary condition to trigger this bug:
1. A ```for``` loop with an updating statement on a separate line. 
2. The ```for``` loop doesn't get all the iterations completed.

Tests done with

```
clang --version
Ubuntu clang version 20.1.8 (++20250708082409+6fb913d3e2ec-1~exp1~20250708202428.132)
Target: x86_64-pc-linux-gnu
Thread model: posix
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to