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:
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.
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?
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker. Then the output consists of object files output by the assembler.
GCC has a range of optimization levels, plus individual options to enable or disable particular optimizations. The overall compiler optimization level is controlled by the command line option -On, where n is the required optimization level, as follows: -O0 . (default).
The option -fno-gnu89-inline explicitly tells GCC to use the C99 semantics for inline when in C99 or gnu99 mode (i.e., it specifies the default behavior).
It turns on certain optimization flags for GCC. The syntax is #pragma GCC optimize (option, ...) From the official source on GCC pragmas, this pragma allows you to set global optimization flags (specified by option ) for functions that come after it.
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.
When the warning only pops up in a few limited cases you can work around it with:
#pragma GCC diagnostic ignored "-Wshadow"
For the rest of the cases refactoring the code or doing the grep
trick Peter mentioned seem to be the only options.
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.
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!
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With