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.
GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.
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.
-w is the GCC-wide option to disable warning messages.
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 ...
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.
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.
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.
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.
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
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