Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More fine-grained behavior of gcc -Wshadow option

Tags:

c

gcc

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?

like image 677
pachanga Avatar asked Aug 27 '09 07:08

pachanga


People also ask

What is the option used for in GCC?

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.

What is GCC optimizations?

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).

Which option should be used with GCC to compile source files to the C99 standard?

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).

What is #pragma GCC optimize o3?

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.


2 Answers

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.

like image 84
Grumbel Avatar answered Sep 22 '22 01:09

Grumbel


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!

like image 33
Peter Avatar answered Sep 25 '22 01:09

Peter