Folks, I really like the -Wshadow option since it helps to spot some possible problematic pieces of code. I want to use it in a really large project but I can't since it's too strict. For example it throws a warning for the following case:
伙计们,我真的很喜欢-Wshadow选项,因为它有助于发现一些可能存在问题的代码片段。我想在一个非常大的项目中使用它,但是我不能,因为它太严格了。例如,它对以下情况发出警告:
struct Foo
{
Foo(int info) : info_(info) {} //shadow warning is here
void info(){ ... }
int info_;
};
gcc
throws a warning about "int info" variable shadowing "void info" method in constructor which is... well, not really a useful warning for me.
gcc在构造函数中对“int info”变量shadowing“void info”方法发出警告,即…对我来说,这并不是一个有用的警告。
What I really care about is cases such as the following:
我真正关心的是以下情况:
int i = 0;
for(int j=0;j<10;++j)
{
int i = j; //local scope variable "int i" shadows outer scope variable
++i;
}
Is it possible to make gcc
warn about these cases only?
是否可能只让海湾合作委员会对这些案件提出警告?
2 个解决方案
#1
10
For external libraries you can try to specify their include path with -isystem
instead of -I
, this will cause gcc to stop reporting warnings in them most of the time.
对于外部库,您可以尝试使用-isystem而不是-I来指定它们的包含路径,这将导致gcc在大多数情况下停止报告警告。
When the warning only pops up in a few limited cases you can work around it with:
当警告只在一些有限的情况下出现时,您可以使用以下方法:
#pragma GCC diagnostic ignored "-Wshadow"
被忽视的“-Wshadow”
For the rest of the cases refactoring the code or doing the grep
trick Peter mentioned seem to be the only options.
在其他情况下,重构代码或使用Peter提到的grep技巧似乎是唯一的选择。
#2
3
Sadly gcc has no way to disable warnings for specific lines. Anyway, if you were doing that, you should just rename your constructor's info parameter to my_info, and the shadow will go away.
遗憾的是,gcc无法禁用特定行的警告。无论如何,如果您这样做,您应该将构造函数的info参数重命名为my_info,那么阴影就会消失。
The best advice I can give you is to work on removing all those warnings, even though you don't care about them. In general it is better to not use parameter names that hide member functions.
我能给你的最好的建议是,尽管你并不在意这些警告,但要努力消除它们。通常,最好不要使用隐藏成员函数的参数名称。
This is the approach we have taken in enabling new warnings, and introducing other static checkers. In my opinion the pain is worth the gain overall.
这是我们在启用新警告和引入其他静态检查器时采用的方法。在我看来,总的来说,痛苦是值得的。
For some cases we were able to run a script over the code to make the changes for us. I'm not sure how easy that would be. Alternatively a good editor (such as emacs or VIM) should help you make that type of "mechanical" change semi-automatically and fairly quickly, if you can drive the editor well!
对于某些情况,我们可以在代码上运行一个脚本,以便为我们进行更改。我不知道这有多简单。或者,一个好的编辑器(如emacs或VIM)应该可以帮助您半自动和相当快速地进行这种类型的“机械”更改,如果您能够很好地驱动编辑器的话!
One other really hacky option is to create a list of known "okay" exceptions, and grep them out of the compiler output. You could create a wrapper for gcc, which would just extract warnings you don't want to see. I don't recommend it though, it's probably be easier to fix your code base!
另一个真正的hacky选项是创建一个已知的“ok”异常列表,并将它们从编译器输出中删除。您可以为gcc创建一个包装器,它只提取您不想看到的警告。我并不推荐它,它可能更容易修复你的代码库!
#1
10
For external libraries you can try to specify their include path with -isystem
instead of -I
, this will cause gcc to stop reporting warnings in them most of the time.
对于外部库,您可以尝试使用-isystem而不是-I来指定它们的包含路径,这将导致gcc在大多数情况下停止报告警告。
When the warning only pops up in a few limited cases you can work around it with:
当警告只在一些有限的情况下出现时,您可以使用以下方法:
#pragma GCC diagnostic ignored "-Wshadow"
被忽视的“-Wshadow”
For the rest of the cases refactoring the code or doing the grep
trick Peter mentioned seem to be the only options.
在其他情况下,重构代码或使用Peter提到的grep技巧似乎是唯一的选择。
#2
3
Sadly gcc has no way to disable warnings for specific lines. Anyway, if you were doing that, you should just rename your constructor's info parameter to my_info, and the shadow will go away.
遗憾的是,gcc无法禁用特定行的警告。无论如何,如果您这样做,您应该将构造函数的info参数重命名为my_info,那么阴影就会消失。
The best advice I can give you is to work on removing all those warnings, even though you don't care about them. In general it is better to not use parameter names that hide member functions.
我能给你的最好的建议是,尽管你并不在意这些警告,但要努力消除它们。通常,最好不要使用隐藏成员函数的参数名称。
This is the approach we have taken in enabling new warnings, and introducing other static checkers. In my opinion the pain is worth the gain overall.
这是我们在启用新警告和引入其他静态检查器时采用的方法。在我看来,总的来说,痛苦是值得的。
For some cases we were able to run a script over the code to make the changes for us. I'm not sure how easy that would be. Alternatively a good editor (such as emacs or VIM) should help you make that type of "mechanical" change semi-automatically and fairly quickly, if you can drive the editor well!
对于某些情况,我们可以在代码上运行一个脚本,以便为我们进行更改。我不知道这有多简单。或者,一个好的编辑器(如emacs或VIM)应该可以帮助您半自动和相当快速地进行这种类型的“机械”更改,如果您能够很好地驱动编辑器的话!
One other really hacky option is to create a list of known "okay" exceptions, and grep them out of the compiler output. You could create a wrapper for gcc, which would just extract warnings you don't want to see. I don't recommend it though, it's probably be easier to fix your code base!
另一个真正的hacky选项是创建一个已知的“ok”异常列表,并将它们从编译器输出中删除。您可以为gcc创建一个包装器,它只提取您不想看到的警告。我并不推荐它,它可能更容易修复你的代码库!