Issue 243402
Summary [Coverage] Ternary with constant non-zero arms as &&/|| operand isn't reported as Folded
Labels new issue
Assignees
Reporter Durgesh-Shelke
    ### Summary

When a `ConditionalOperator` (`?:`) whose arms are both non-zero integer constants is used as an operand to a logical operator (`&&` or `||`), branch coverage emits an active branch region for the ternary _expression_ that can never evaluate to `False`. 

Unlike simple constants (e.g. `x && 1`), which Clang marks as `[Folded - Ignored]`, this ternary operand is reported as an active condition with `[True: N, False: 0]`, presenting an uncoverable branch target in branch coverage reports.

### Reproducer

    int evaluate(int x, int y) {
        return (x > 0) && (y > 0 ? 10 : 20);
    }

    int main(void) {
        evaluate(1, 5);
        evaluate(1, -1);
        return 0;
    }

  Commands:

    clang -O0 -fprofile-instr-generate -fcoverage-mapping test.c -o test
    ./test
    llvm-profdata merge default.profraw -o test.profdata
    llvm-cov show ./test -instr-profile="" -show-branches=count test.c

### Observed Output

        1|      2|int evaluate(int x, int y) {
        2|      2|    return (x > 0) && (y > 0 ? 10 : 20);
      ------------------
      |  Branch (2:12): [True: 2, False: 0]
      |  Branch (2:23): [True: 2, False: 0]
      |  Branch (2:24): [True: 1, False: 1]
      ------------------

  Here:

  • Branch (2:12) is (x > 0)
  • Branch (2:24) is the condition (y > 0) inside the ternary ([True: 1, False: 1])
  • Branch (2:23) is the ternary (y > 0 ? 10 : 20) acting as the RHS operand to &&

### Context & Distinction (Sema vs Constant Evaluation)

Clang's Sema already recognizes that this conversion is tautological and emits -Wtautological-constant-compare:

    warning: converting the result of '?:' with integer constants to a boolean always evaluates to 'true' [-Wtautological-constant-compare]

  (Verified verbatim on Clang 14.0.0, Clang 16.0.6, and trunk).

However, in clang/lib/CodeGen/CoverageMappingGen.cpp, branch folding relies on constant evaluation via ConditionFoldsToBool (which calls Cond->EvaluateAsInt(...)). Because y > 0 is dynamic, EvaluateAsInt fails on the ternary _expression_, so it is not treated as folded even though its boolean conversion value is statically fixed.

  By contrast, an explicit constant operand such as return (x > 0) && 1; correctly produces:

      |  Branch (2:12): [True: 2, False: 0]
      |  Branch (2:23): [Folded - Ignored]

### Proposed Enhancement

  Where both arms of a ConditionalOperator evaluate to non-zero constant expressions, coverage mapping could fold the branch region corresponding to the ternary operand (reporting it as [Folded - Ignored]), matching the check Sema performs.

Note: For ternaries where an arm can evaluate to zero (e.g. x && (c ? a : 0)), or non-constant arms (x && (c ? a : b)), preserving the branch record is correct. The suggestion applies strictly when both arms are non-zero constants.

### Environment

  • Clang versions verified:
      • Clang 14.0.0 (Ubuntu 14.0.0-1ubuntu1.1, x86_64)
      • Clang 16.0.6 (Alpine / AFL++ 4.21c, x86_64)
      • Clang trunk (24.0.0git abcbd442e332, verified on Compiler Explorer)
  • OS: Linux x86_64
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to