[llvm] [llvm-cov][CoverageMapping] Fix LineCoverageStats incorrectly using gap region count (PR #203740)

Maksim Levental via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 14 10:59:16 PDT 2026


makslevental wrote:

>* Avoid blobs.

Done

> * Could you find and pick up relevant issues? I guess a few issues would be relevant.

The closest related issue I could find was #82356 ("llvm-cov: wrong line coverage of 0 for closing brace after statement after return").

That issue describes a broader family of cases where lines after a return show count=0. However, the example there involves lines that are genuinely unreachable — e.g., a closing `}` where the only path through the block hits return, so control never reaches the brace (the false branch jumps past it, the true branch returns before it). 

```
 1|      2|int foo(int i) {
 2|      2|    if (i) {
 3|      1|        return 0;;
 4|      0|    }
 5|      1|    return 1;
 6|      2|}
 7|       |
 8|      1|int main() {
 9|      1|    foo(0);
10|      1|    foo(1);
11|      1|    return 0;
12|      1|}
```

I'm not sure if reporting 0 for those is arguably correct...

There is another related but broader issue #140893 ("nested macro with unused branch leads to incorrect coverage"). The discussion there traces the problem to the exact code path I touched in `LineCoverageStats`, where the wrapped segment count dominates even when region entries on the line tell a different story. @justincady's analysis in the comments identifies the same root cause: the line execution count takes `max(WrappedSegment->Count, ...)`, so a wrapped segment with `count=1` inflates lines that have a region entry with `count=0`. 

He has draft PR #143910 ("[llvm-cov] Improve line count for wrapped segments") which proposes a broader fix to not use the wrapped segment count at all when there are region entries on the line. That's effectively a superset of my change but with wider blast radius (it changes counts for catch blocks, else branches, etc., and intentionally reduces coverage numbers across the board). **Interestingly** his fix does not fix the tests I checked in here (I checked!) because it doesn't handle the gap region case where non-gap segments on the line prove execution. _Nor does my fix fix his test case_ (I checked that too!).

https://github.com/llvm/llvm-project/pull/203740


More information about the llvm-commits mailing list