海湾合作委员会未能发出警告未初始化的变量的 [英] gcc failing to warn of uninitialized variable

查看:1048
本文介绍了海湾合作委员会未能发出警告未初始化的变量的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code的可被未初始化的变量。看来,GCC应该产生一个警告而不是:

The following code has a variable that may be uninitialized. It seems that gcc should be generating a warning but isn't:

$ cat a.c
int foo(int b) {
  int a;
  if (b)
    a = 1;
  return a;
}

$ gcc-4.7 -c -Wall -Wmaybe-uninitialized -o a.o ./a.c
$ gcc-4.7 -v
Using built-in specs.
COLLECT_GCC=gcc-4.7
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-2ubuntu1~12.04' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04)

如何让GCC来报告未初始化的变量任何线索?

Any clues on how to get gcc to report the uninitialized variable?

推荐答案

看起来你不能 - 看的这个bug报告。 (和这个,该标记为一个的欺骗 - 它有一个相同的测试情况下你的。)因为它看起来像根源bug是近10岁,似乎这不是容易解决的问题。事实上,第二个错误我联系到了那句永远不会成为固定的讨论,这样不好看。

It looks like you can't - see this bug report. (And this one, which is marked as a dupe of that one - it has an identical test case to yours.) Since it looks like the root-cause bug is almost 10 years old, it would seem it's not an easy problem to solve. In fact, the second bug I linked to has the phrase "Never going to be fixed" in the discussion, so that doesn't look good.

如果这对你真的很重要,确实的赶上这一次与 -Wsometimes-未初始化,它包含在 -Wall

If it's really important to you, clang does catch this one with -Wsometimes-uninitialized, which is included with -Wall:

a.c:3:7: warning: variable 'a' is used uninitialized whenever 'if' condition is
      false [-Wsometimes-uninitialized]
  if (b)
      ^
a.c:5:10: note: uninitialized use occurs here
  return a;
         ^
a.c:3:3: note: remove the 'if' if its condition is always true
  if (b)
  ^~~~~~
a.c:2:8: note: initialize the variable 'a' to silence this warning
  int a;
       ^
        = 0
1 warning generated.

这篇关于海湾合作委员会未能发出警告未初始化的变量的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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