简体   繁体   English

我怎样才能让gcc警告我“int i = i;”

[英]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: 作为参考,这里是原始的重复,可能是有意义的:

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.

相关问题 如何使GCC / Clang警告未初始化成员的使用? - How can I make GCC/Clang warn about the use of uninitialized members? 我如何告诉gcc警告我字符串被赋予整数值? - How can I tell gcc to warn me of strings being assigned an integer value? 如何告诉gcc在没有中断的情况下对switch / case语句发出警告(或失败)? - How can I tell gcc to warn (or fail) on switch/case statements without a break? 如何让GCC警告标识符名称冲突? - How to get GCC to warn about identifier name collisions? 我可以让GCC警告将过多的类型传递给函数吗? - Can I make GCC warn on passing too-wide types to functions? 如果我将可选参数保留为默认值,我可以让编译器警告我吗? - Can I make a compiler warn me if I leave optional argument as default value? 如何在GCC中弃用已弃用的函数中删除已弃用的警告? - How can I get rid of deprecated warnings in deprecated functions in GCC? 如何让gcc为所有符号名称添加前缀 - How can I get gcc to add a prefix to all symbol names 如何让GCC在ROM中放置C ++ constexpr? - How can I get GCC to place a C++ constexpr in ROM? 为什么编译器在我定义_CRT_SECURE_NO_WARNINGS之后仍然警告我不安全的strtok? - Why does the compiler still warn me about unsafe strtok even after I define _CRT_SECURE_NO_WARNINGS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM