How to hint to GCC that a line should be unreachable at compile time?

Matt Joiner picture Matt Joiner · Aug 1, 2010 · Viewed 9.9k times · Source

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.

Answer

Hasturkun picture Hasturkun · Aug 1, 2010

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