为什么GCC不会警告无法访问的代码? [英] Why does GCC not warn for unreachable code?

查看:117
本文介绍了为什么GCC不会警告无法访问的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么 gcc(4.6.3)在此示例中没有警告我无法访问的代码:

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;
}

请注意,错误的status()功能的目的是保持状态.我本来希望通过-Wall收到警告.我还尝试了-Wunreachable-code-Wextra-pedantic-ansi(因为在

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.

看来gcc静默删除了静态变量赋值.

It appears gcc silently removes the static variable assignment.

我认为gcc选项-Wall -Werror应该会引发错误.

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

推荐答案

gcc 4.4会给您警告.在更高版本的gcc中,此功能(-Wunreachable-code)已被删除.

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

请参阅此处: http://gcc.gnu.org/ml/gcc- help/2011-05/msg00360.html

-Wunreachable-code已被删除,因为它不稳定: 依赖优化器,因此gcc的不同版本会发出警告 关于不同的代码.编译器仍然接受并忽略 命令行选项,以便不破坏现有的Makefile.在 在将来的版本中,该选项将被完全删除.

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.

伊恩

这篇关于为什么GCC不会警告无法访问的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆