Why does GCC not warn for unreachable code?
Asked Answered
M

3

23

Why doesn't GCC (4.6.3) give me any warning for the unreachable code in the below example?

#include <stdio.h>

int status(void)
{
    static int first_time = 1;

    if (first_time) {
        return 1;
        first_time = 0; /* Never reached */
    } else {
        return 0;
    }
}

int main(int argc, const char *argv[])
{
    printf("first call %d\n", status());
    printf("second call %d\n", status());
    return 0;
}

Note, the purpose of the faulty status() function was to maintain a status. I had expected to get a warning for this with -Wall. I tried also -Wunreachable-code, -Wextra, -pedantic, and -ansi (as it was discussed here). Yet, none of those give me a warning.

It appears GCC silently removes the static variable assignment.

In my opinion, GCC options -Wall -Werror should throw an error.

Majesty answered 22/6, 2013 at 10:54 Comment(0)
N
29

GCC 4.4 will give you a warning. In the later versions of GCC, this feature (-Wunreachable-code) has been removed.

See Re: gcc -Wunreachable-code option

The -Wunreachable-code has been removed, because it was unstable: it relied on the optimizer, and so different versions of gcc would warn about different code. The compiler still accepts and ignores the command line option so that existing Makefiles are not broken. In some future release the option will be removed entirely.

Nealneala answered 20/1, 2014 at 17:24 Comment(2)
I hate it when they remove features based on reasons like that. And doing it covertly... "The compiler still accepts and ignores the command line option" So after a compiler update we had a bug, that could have been avoided if the compiler had warned...Gynophore
Commit bc3c12a29c67753c473b465767b2ba6b2f7e72a6 if anyone is curious.Spicule
I
2

A smaller set of non-broken unreachable code warnings making it back to GCC?

The following bug report

  • Bug 46476 - Missing Warning about unreachable code after return [-Wunreachable-code-return]

Has been tracking ongoing work (albeit slow) of bringing back a smaller set of unreachable code warnings in GCC implemented at GIMPLE lowering time, focusing on avoiding the aggressiveness of the original feature ("-Wunreachable-code is seriously broken, I suggest to remove its implementation completely."):

Richard Biener 2021-11-29 14:29:49 UTC

-Wunreachable-code-ctrl at GIMPLE lowering time

This is the -Wunreachable-code-ctrl (not enabled by -Wextra) patch diagnosing unreachable stmts after a break, continue, goto (or loops without exit via the backedge goto).

Note that unlike clang which seems to model the option names after what kind of stmt is detected as unreachable these patches model the option names after what kind of stmt makes other stmts unreachable. Not sure what is more useful in practice [to avoid coding-style issues].

However, it been one year since the last update of the ticket.

Irksome answered 31/10, 2022 at 9:4 Comment(0)
L
1

gcc has dozens of passes -- to see them try compiling with switches like

-da -dAp -Wa,-a -fdump-ipa-all-all -fdump-tree-all-all -fdump-rtl-all-all

My guess is that some pass has done dead-code elimination before the pass designated to issue the warning in question. Which could reasonably be considered a bug, but likely the gcc team regard the warning more as a convenience than a moral commitment, and aren't motivated to do a lot of work to make it precise and complete. If you want to contribute, you could disable optimization passes one-by-one until you find the one preventing the warning, then file a bug report documenting the problem. If that isn't worth your time, maybe fixing it isn't worth their time. :-)

Leatherjacket answered 14/8, 2013 at 22:31 Comment(3)
Re "-fdump-rtl-all-all": Not just "-fdump-rtl-all"? (same as "-da"). For what version of GCC?Dunkin
Does "-dAp" work? Should it be the separate "-dA -dp"? For what version of GCC?Dunkin
E.g., the OP has left the building: "Last seen more than 9 years ago". Perhaps somebody else can chime in?Dunkin

© 2022 - 2025 — McMap. All rights reserved.