Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GCC not warn for unreachable code?

Tags:

I wonder why gcc (4.6.3) gives me no warning for the unreachable code in this 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.

like image 260
Andreas Haas Avatar asked Jun 22 '13 10:06

Andreas Haas


People also ask

How do I enable warnings in GCC?

GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.

How does GCC treat warning errors?

The warning is emitted only with --coverage enabled. By default, this warning is enabled and is treated as an error. -Wno-coverage-invalid-line-number can be used to disable the warning or -Wno-error=coverage-invalid-line-number can be used to disable the error. Suppress warning messages emitted by #warning directives.

Which option of GCC inhibit all warning messages?

-w is the GCC-wide option to disable warning messages.

How do you find an unreachable code?

While some simple cases of unreachable code can be detected by static analysis (typically if a condition in an if statement can be determined to be always true or false), most cases of unreachable code can only be detected by performing coverage analysis in testing, with the caveat that code reported as not being ...

What happened to the -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.

How to fix ‘GCC is not recognized as an internal command’?

If you have installed MinGW tools on your system and are trying to run gcc from command prompt, you might get this error – 'gcc' is not recognized as an internal or external command. This post explains how we can fix this problem once for all. The first step is to check what all you have in your PATH environment variable.

How to compile a C program using GCC in Linux?

GCC is not recognized as internal or external command. In system variables, find the ‘PATH’ environment variable and then edit it to add MinGW path. You need to add ‘;’ at the end of the existing value and then enter MinGW path. After doing the above steps, open a new command prompt and run gcc to compile a C program.

What are lexical rules in GCC front end?

A broad class of warnings implemented in GCC front ends are those that are based on lexical rules. These can be thought of as sophisticated regular expressions. In static analysis terminology, these rules are sometimes referred to as pattern rules.


1 Answers

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

See here: http://gcc.gnu.org/ml/gcc-help/2011-05/msg00360.html

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.

Ian

like image 112
A. K. Avatar answered Sep 22 '22 22:09

A. K.