[英]How can I get gcc to warn me about “int i = i;”
A simple program: 一个简单的程序:
int main()
{
long i = i;
return 0;
}
Compiling as C gives no errors and no warnings. 编译为C不会出现错误,也不会出现警告。
$ gcc -Wall -Wextra -pedantic 1.c
Compiling as C++ gives a warning: 编译为C ++会发出警告:
$ c++ -Wall -Wextra -pedantic 1.c
1.c: In function ‘int main()’:
1.c:3:7: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
long i = i;
In both cases variable i seems to be 0, although in c++ it could be uninitialized. 在两种情况下,变量i似乎都是0,尽管在c ++中它可能是未初始化的。 I actually made such a typo in one of my functions and it was quite hard to find it. 我实际上在我的一个功能中犯了这样的错误,很难找到它。 What can I do to avoid this? 我该怎么做才能避免这种情况? I'd expect at least a warning. 我希望至少有一个警告。 Moreover, Clang doesn't give any warning in either case (c or c++). 此外,Clang在任何一种情况下都不给出任何警告(c或c ++)。 Is there a specific part of the standard that says anything about this behavior? 是否有标准的特定部分说明了这种行为?
Edit: Having tried something similar: 编辑:尝试了类似的东西:
$ cat 1.c
int main(void)
{
int k = k + 0;
int i = i + 1;
return 0;
}
The warning (in C) is generated only for "i". 警告(在C中)仅针对“i”生成。
$ gcc -Wall -Wextra 1.c
1.c: In function ‘main’:
1.c:4:6: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
int i = i + 1;
For GCC compiling C programs, you need to add the compiler flag -Winit-self
. 对于GCC编译C程序,需要添加编译器标志-Winit-self
。 (You also need -Wall
or -Wuninitialized
, see below.) For GCC compiling C++ programs, this flag is implied by -Wall
but for C it needs to specified explicitly; (你还需要-Wall
或-Wuninitialized
,见下文。)对于GCC编译C ++程序, -Wall
暗示了这个标志,但对于C,它需要明确指定; it is not part of -Wextra
either. 它也不是-Wextra
一部分。
For Clang, the situation is slightly more interesting. 对于Clang来说,情况稍微有点儿了。 In the snippet in the OP, Clang does not produce any diagnostic. 在OP的片段中,Clang不会产生任何诊断。 However, with the slightly different snippet supplied in the GCC manual below, a diagnostic is provided: 但是,使用下面GCC手册中提供的略有不同的片段,提供了诊断:
int f() {
int i = i;
return i;
}
The difference is that in the above snippet, the (uninitialized) value of i
is actually used. 不同之处在于,在上面的片段中,实际使用了i
的(未初始化的)值。 Apparently, in the original code Clang detected that the variable was useless and eliminated it as dead code before applying the diagnostic. 显然,在原始代码中,Clang检测到该变量是无用的,并在应用诊断之前将其作为死代码消除。
In Clang, the diagnostic is triggered by -Wuninitialized
, which is enabled by -Wall
as in GCC. 在Clang中,诊断由-Wuninitialized
触发, -Wall
在GCC中启用。
Here's an excerpt from the GCC manual: 以下是GCC手册的摘录:
-Winit-self
(C, C++, Objective-C and Objective-C++ only)-Winit-self
(-Winit-self
C,C ++,Objective-C和Objective-C ++)Warn about uninitialized variables that are initialized with themselves. 警告用自己初始化的未初始化变量。 Note this option can only be used with the
-Wuninitialized
option. 请注意,此选项只能与-Wuninitialized
选项一起使用。For example, GCC warns about
i
being uninitialized in the following snippet only when-Winit-self
has been specified: 例如,GCC仅在指定-Winit-self
时警告i
在以下代码段中未初始化:int f() { int i = i; return i; }
This warning is enabled by
-Wall
in C++. 此警告由-Wall
在C ++中启用。
As the excerpt indicates, -Wuninitialized
is also required. 如摘录所示,也需要-Wuninitialized
。 In both C and C++, -Wall
implies -Wuninitialized
. 在C和C ++中, -Wall
意味着-Wuninitialized
。 However, note that many uninitialized uses will not be detected unless some optimization level is also requested. 但请注意,除非还要求某些优化级别,否则将无法检测到许多未初始化的用途。 (That doesn't apply to -Winit-self
, as far as I know. It can be detected without optimization.) (据我所知,这不适用于-Winit-self
。可以在没有优化的情况下检测到它。)
Irritatingly, when you unmark a question as a duplicate, the previously-marked duplicates disappear. 令人恼火的是,当您将问题标记为重复时,先前标记的重复项将消失。 I unmarked it because none of the duplicates actually answered the question in the body; 我没有标记它,因为没有一个重复实际上回答了正文中的问题; I also edited the title. 我还编辑了标题。
For reference, here are the original duplicates, which may be of interest: 作为参考,这里是原始的重复,可能是有意义的:
Why does the compiler allow initializing a variable with itself? 为什么编译器允许自己初始化变量?
gcc failing to warn of uninitialized variable gcc没有警告未初始化的变量
Why is this initialization accepted by the c++ compiler? 为什么c ++编译器接受这个初始化? static int x = x; static int x = x;
Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++14? 在C ++ 14中,C ++标准是否在使用不确定值和未定义行为方面发生了变化?
It is basically: 基本上是:
int i;
i = i;
in which i
is an uninitialized value. 其中i
是未初始化的值。
The combination of -Wall -Winit-self
seems to add this diagnostic: -Wall -Winit-self
的组合似乎添加了这个诊断:
$ gcc -Wall -Winit-self t.c
t.c: In function ‘main’:
t.c:3:10: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
long i = i;
^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.