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

    <tr>
        <th>Summary</th>
        <td>
            clang++ should always emit `@llvm.trap()` when flowing off the end of a non-void function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    Currently, when optimizations are enabled, flowing off the end of a function in C++ mode emits `unreachable`. This means that a function such as:
```cpp
int get(int x) {
    if (x == 0) { }
    else std::abort();
}
```
... is equivalent to simply calling `abort()`. This behavior does not reflect developer intent, and it's possible to fall through into other code sections as a result of simply treating this as optimizable UB.

Even with no warning flags enabled, clang emits
> ```none
> <source>:6:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]
>     6 | }
>       | ^
> ```

Flowing off the end of a function is a common developer mistake and basically never intentional. It should not be hidden by the optimizer because it may result in security vulnerabilities such as [CVE-2014-9296](https://access.redhat.com/security/cve/cve-2014-9296).

If a developer actually wants the current behavior, they can simply write:
```cpp
int get(int x) {
    if (x == 0) {
 __builtin_unreachable(); // or, since C++23
        std::unreachable(); // or, alternatively
        [[assume(false)]];
    }
    else std::abort();
}
```
There is zero motivation for violating programmer intent and emitting security vulnerabilities. This offers no optimization opportunities that could not be trivially obtained with more explicit code.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVc2yozgPfRpno0oKTH4uiyxubpKq3vf3zbJLgAiaMTZjC9Lpp58ykJ9bM129aYoixMiydHR0hCHwxRLt1eagNscF9tI4vz9xIHvFgRaFq277j957smJuSn_AtSELrhNu-QcKOxsAPQFZLAxV0aI27sr2Aq6uQZr4qQJXA0Ld2zLuALbwofRB6QO0riKgliWA2ia99YRlE12pbbKCrw0HaAltAGlQXn2EvmwAg8reVXJUybvaJtNddt20wlbgQq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