On 2/20/23 12:28, Segher Boessenkool wrote:
On Mon, Feb 20, 2023 at 12:18:36PM +0100, Marc Glisse via Gcc-help wrote:
On Mon, 20 Feb 2023, Gabriel Ravier via Gcc-help wrote:
This is the kind of thing that makes me wonder why there isn't some kind
of `__builtin_unreachable_do_not_optimize()` builtin that allows one to
mark places in code that should never be reached and should thus be warned
about if such a thing happens while at the same time never doing any
optimization on the basis of the presence of the call.
-fsanitize=unreachable -fsanitize=null and others prevent the kind of
optimization you are worried about.
Or even just __builtin_trap(), or abort(), or similar. Just a printf()
thing if you really want to just warn.
"Never doing any optimisation" based on <anything> is of course not a
reasonable expectation; but you *can* ask for reachable code not to be
optimised away. This is the default, just don't mark reachable code as
unreachable :-)
Segher
What I mean is that it would be nice to have a builtin that has the same
effect as a `*(char *)0 = 0` in that the compiler will warn you if it
thinks code execution will reach it, except that it should do do nothing
else. `*(char *)0 = 0` is a really bad solution in that regard because
while it will result in the compiler emitting a warning, it will also
result in actual undefined behavior that can crash the program or get
optimized out. `__builtin_trap` and `abort` are also equally bad in that
they result in visible behavior at runtime, which is equivalently bad if
you solely want a warning and nothing else (also, as far as I know, GCC
will not emit a warning on the basis that they are reached either, so
they're completely useless if one's goal is to get a build-time warning).
Something that seems instead quite useful would be some kind of builtin
that would result in a warning if GCC thinks code execution will reach
it, while having absolutely no other effect on code generation or
execution (i.e. no optimization, no trap, no abort, no nothing), which
neither deliberate UB nor calls to functions like `abort` or
`__builtin_trap` will result in.