It's common for compilers to provide a switch to warn when code is unreachable. I've also seen macros for some libraries, that provide assertions for unreachable code.
Is there a hint, such as through a pragma, or builtin that I can pass to GCC (or any other compilers for that matter), that will warn or error during compilation if it's determined that a line expected to be unreachable can actually be reached?
Here's an example:
if (!conf->devpath) {
conf->devpath = arg;
return 0;
} // pass other opts into fuse
else {
return 1;
}
UNREACHABLE_LINE();
The value of this is in detecting, after changes in conditions above the expected unreachable line, that the line is in fact reachable.
-Werror= Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors.
You can use a #pragma warning directive to control the level of warning that's reported at compile time in specific source files. Warning pragma directives in source code are unaffected by the /w option.
gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.
gcc 4.5 supports the __builtin_unreachable()
compiler inline, combining this with -Wunreachable-code
might do what you want, but will probably cause spurious warnings
With gcc 4.4.0 Windows cross compiler to PowerPC compiling with -O2 or -O3 the following works for me:
#define unreachable asm("unreachable\n")
The assembler fails with unknown operation if the compiler doesn't optimise it away because it has concluded that it is unreachable.
Yes, it is quite probably `highly unpredictable under different optimization options', and likely to break when I finally update the compiler, but for the moment it's better then nothing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With