GCC -Wuninitialized / -Wmaybe,未初始化的问题 [英] GCC -Wuninitialized / -Wmaybe-uninitialized issues

查看:4317
本文介绍了GCC -Wuninitialized / -Wmaybe,未初始化的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 GCC-4.7(Ubuntu的/ Linaro的4.7.2-11 precise2)经历了一个很奇怪的问题4.7.2 。我无法编译下面的有效code而不发出警告:

 的extern无效dostuff(无效);INT测试(INT ARG1,ARG2 INT)
{
    INT RET;    如果(ARG1)RET = ARG2? 1:2;    做东西();    如果(Arg1)将返回RET;    返回0;
}

编译选项和输出:

  $ GCC-4.7 -o test.o -c -Os test.c的-Wall
test.c的:在功能测试:
test.c的:5:6:警告:RET可能在这个函数中使用未初始化[-Wmaybe-未初始化]

但是,下面的code没有警告编译(虽然略微低效率的组装):

 的extern无效dostuff(无效);INT测试(INT ARG1,ARG2 INT)
{
    INT RET;    如果(ARG1和放大器;&安培; ARG2)RET = 1;
    如果(ARG1和放大器;&安培;!ARG2)RET = 2;    做东西();    如果(Arg1)将返回RET;    返回0;
}

我有点卡住,正在考虑这个编译器错误。有什么想法?


解决方案

事实上,这是海湾合作委员会中的一个已知的问题。结果
GCC是臭名昭著的报告 不正确初始化的变量 。结果
缺点已经充分注意到并有克服缺点举措:结果
更好的未初始化警告的:


  

GNU编译器警告有关使用未初始化变量的选项 -Wuninitialized 。不过,目前的实现具有一定的明显不足。一方面,一些用户想更详细的和一致的警告。而另一方面,一些用户想获得尽可能少的警告越好。该项目的目标是实现这两种可能性,而在同一时间改善目前的能力


该倡议旨在提供更好的警告和它引用作为你的情况类似为例情况。相关部分是:


  

一个用户理解什么作为假阳性可以是用于该特定用户的不同。一些用户感兴趣的是那些因为与优化当前环境相结合的行动隐蔽案件。然而,许多用户都没有,因为这种情况下是隐藏的​​,因为它不能在编译code出现。典型的例子就是


  INT X;
如果(F())
     X = 3;
返回X;


  

,其中f的总是适合当前环境返回为非零,并且因此,它可以被优化了。在这里,一组用户想获得,因为'F'未初始化的警告时,其他地方可能编译回零。然而,其他组的用户会考虑杂散关于不能在可执行文件被编译出现的情况发出警告。


I am experiencing a very strange issue using gcc-4.7 (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2. I am unable to compile the following valid code without a warning:

extern void dostuff(void);

int test(int arg1, int arg2)
{
    int ret;

    if (arg1) ret = arg2 ? 1 : 2;

    dostuff();

    if (arg1) return ret;

    return 0;
}

Compile options and output:

$ gcc-4.7 -o test.o -c -Os test.c -Wall
test.c: In function ‘test’:
test.c:5:6: warning: ‘ret’ may be used uninitialized in this function [-Wmaybe-uninitialized]

However, the following code compiles with no warning (albeit to slightly less efficient assembly):

extern void dostuff(void);

int test(int arg1, int arg2)
{
    int ret;

    if (arg1 && arg2) ret = 1;
    if (arg1 && !arg2) ret = 2;

    dostuff();

    if (arg1) return ret;

    return 0;
}

I am somewhat stuck and am considering this a compiler bug. Any thoughts?

解决方案

Indeed this is a known problem in gcc.
gcc is notorious for reporting incorrect uninitialized variables.
The shortcomings have been duly noted and there is a initiative to overcome the shortcomings:
Better Uninitialized Warnings:

The GNU Compiler Collection warns about the use of uninitialized variables with the option -Wuninitialized. However, the current implementation has some perceived shortcomings. On one hand, some users would like more verbose and consistent warnings. On the other hand, some users would like to get as few warnings as possible. The goal of this project is to implement both possibilities while at the same time improving the current capabilities.

The initiative aims at providing better warnings and it quotes a example case similar as your case. The relevant portion being:

What an user understands as a false positive may be different for the particular user. Some users are interested in cases that are hidden because of actions of the optimizers combined with the current environment. However, many users aren't, since that case is hidden because it cannot arise in the compiled code. The canonical example is

int x;
if (f ())
     x = 3;
return x;

where 'f' always return non-zero for the current environment, and thus, it may be optimized away. Here, a group of users would like to get an uninitialized warning since 'f' may return zero when compiled elsewhere. Yet, other group of users would consider spurious a warning about a situation that cannot arise in the executable being compiled.

这篇关于GCC -Wuninitialized / -Wmaybe,未初始化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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