Issue 127004
Summary Misreported number of branches in conditional when ternary is introduced
Labels new issue
Assignees
Reporter mitchgrout
    With the following source file:

```c
/* test.c */

int ex1(int x, int y)
{
  if (x || y) { return 1; }
  return 0;
}

#define M(x) (x? 1 : 0)
int ex2(int x, int y)
{
  if (x || M(y)) { return 1; }
  return 0;
}

int main(void)
{
  ex1(0, 0); ex1(0, 1); ex1(1, 0); ex1(1, 1);
  ex2(0, 0); ex2(0, 1); ex2(1, 0); ex2(1, 1);
 return 0;
}
```

Compile with `clang --coverage -o test test.c`, execute it, and generate the coverage report via `llvm-cov gcov test-test.gcda --branch-counts --branch-probabilities`; the resulting `test.c.gcov` file will contain:

```text
...
        -:    1:/* test.c */
        -: 2:
function ex1 called 4 returned 100% blocks executed 100%
        4: 3:int ex1(int x, int y)
        -:    4:{
        4:    5:  if (x || y) { return 1; }
branch  0 taken 2
branch  1 taken 2
branch  2 taken 1
branch 3 taken 1
        1:    6:  return 0;
        4:    7:}
        -:    8:
 -:    9:#define M(x) (x? 1 : 0)
function ex2 called 4 returned 100% blocks executed 87%
        4:   10:int ex2(int x, int y)
        -: 11:{
        4:   12:  if (x || M(y)) { return 1; }
branch  0 taken 2
branch  1 taken 2
branch  2 taken 1
branch  3 taken 1
branch  4 taken 0
branch  5 taken 1
        1:   13:  return 0;
        4:   14:}
 -:   15:
...
```

`ex1` has 4 branches with 100% coverage, as expected. `ex2`, which should be equivalent to `ex1`, instead has 6 branches, with only 5/6 being taken. It's not clear why clang has opted to introduce a new branch, and it is similarly not clear how this branch would be executed.

---

System:

```text
Ubuntu clang version 19.1.7 (++20250114103238+cd708029e0b2-1~exp1~20250114103342.77)
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