<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/119928>119928</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `DiagRuntimeBehavior` (and presumably the CFG in general) incorrectly determines reachability in the presence of `__builtin_is_constant_evaluated`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          zygoloid
      </td>
    </tr>
</table>

<pre>
    [Testcase](https://godbolt.org/z/Wbf8zYfh6):
```c++
void f(int k) {
  // No UB. But warns about UB.
  const int m = __builtin_is_constant_evaluated() ? 0 : *(int*)0;
}

void g(int k) {
  // Has UB. But does not warn about UB.
  const int m = __builtin_is_constant_evaluated() ? k : *(int*)0;
}
```

Presumably we get `f` wrong because the CFG builder evaluates `__builtin_is_constant_evaluated()` to `false` when building a CFG for the conditional expression. But I'm unsure why we get `g` wrong too.

Perhaps the CFG builder could track whether it's within a manifestly constant evaluated context, and pass that information into the constant evaluator. However, with the likely upcoming changes [adding reflection to C++](http://wg21.link/P2996), there'll be other ways in which constant evaluation becomes context-dependent, so this might not be a particularly good solution in the longer term.

Another possible option would be to make the CFG builder entirely skip constant expressions, and instead just use the already-known constant values from them. The C++ language rules already require UB checking within constant expressions, so most additional CFG-based checks are likely to be redundant (though not all of them).
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVUuP2zYQ_jX0ZbCGTK9t6eDDehdOeimCIkHR02IkjiTGFMflkFacX19Qfmza9JFDAWHXkMjvNTMkitjOE23VaqdWLzNMseew_Xru2LE1s5rNOX_7SBIbFFKrF6XLPsajqOWT0nul9x2bml2cc-iU3n9Vev9r3ZZff2v7tdJVXlY8qXVxeRqld_kpnk5sDbRKl9ZHOChdgdrk9wAXWPiZ4dNuDrsUYcTgBbDmFPO7aVXDXiLkzQOo5Qu8vtbJum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