2

We need to get our project compiling with no warnings from maximum level gcc. Currently we have a lot of warnings about the following:

struct Bob
{
    int a;
    int b;

    Bob()
        : b(0)
        , a(0)
    {
    }
};

The warning says that the order of the initialization list does not match the order the members are declared in the class. I understand this warning because the real order of initialization comes from the order of members in the class, and if some depend on others, arranging the initialization list in the same order can help spot bugs.

However, we have TONS (and tons) of code which have initialization lists not matching the member order and the members don't depend on each other, so there's no problem. But we are Required to remove all warnings from our code.

Is there some automatic way we can solve this? Did I mention we have tons of code?

Thanks!!

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • 1
    So why didn't you compile your code with these warnings enabled EARLIER? [And `-Werror` which makes the compiler refuse to produce an object file if you have warnings - so nobody ever gets past the initial "test that it compiles" phase without fixing the arnings]. – Mats Petersson Apr 08 '13 at 15:39
  • 4
    Because we don't live in a perfect world and now I need to solve this. Could you offer any help? – Neil Kirk Apr 08 '13 at 15:40
  • It can be done, but it would be quite tricky. In the time it would take you to get an automatic solution working, you could probably fix several hundred constructors by hand. I'd just bite the bullet. – Beta Apr 08 '13 at 15:42
  • I would suggest that, for the future, you add those flags to your build system! ;) – Mats Petersson Apr 08 '13 at 15:45
  • For simple class definitions like the one above, you could write a parser that remembers the member order and reads and reorders the initialisation lists. Just ignore typedefs, functions and class definitions that are too complicated (e.g. inner classes etc.). It should be possible to write a parser like that in some scripting language in a matter of hours for your specific project, just don't get too fancy. It will not erase *all* the warnings, but the majority, so you can do the rest by hand. Just remember to run your unit tests after the conversion ;) – Arne Mertz Apr 08 '13 at 15:46
  • @Beta while that's true for a fully automated solution, there can be some quick solution that does much, but not everything. – Arne Mertz Apr 08 '13 at 15:48

2 Answers2

2

Assuming that you have someone who is sensible that is requesting that you have "no warnings", then you can request an exception and "disabling this warning". It should probably be done on a file-by-file basis, ideally with a written "plan to fix in future" solution, rather than a wholesale "let's disable this warning for all files".

The other option is to find all the places where this is causing a warning, and reordering the initializers.

Note that the warning is just saying "the initialization happens in a different order than you wrote it", so the re-ordering will not CHANGE the behaviour of your code at all - the compiler already does the re-ordering for you!

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

You can use

-Wno-reorder

to just disable those warnings.

  • 1
    … which is quite dangerous. Only use it **locally** [via `#pragma GCC diagnostic ignored`](http://stackoverflow.com/a/3394305/1968), *not on the command line*! (Oh, and push and pop this diagnostic so that it’s only valid for a limited scope.) – Konrad Rudolph Apr 08 '13 at 15:40
  • Thanks, but I cannot disable the warnings. The code must be changed. – Neil Kirk Apr 08 '13 at 15:41
  • @KonradRudolph Op has "tons" of code. He doesn't want to modify all files. It is at his own risk. –  Apr 08 '13 at 15:41
  • @stardust_ Granted, but you can (and should) stuff those into a “legacy” wrapping header and put the pragma around those includes only. – Konrad Rudolph Apr 08 '13 at 15:42
  • @NeilKirk Then you are looking for some scripting features not C++ specific. –  Apr 08 '13 at 15:42
  • @NeilKirk I think it will be quite complicated to do some kind of search and replace. Because of the infinite possibilities of original code arrangement. –  Apr 08 '13 at 15:43
  • @stardust_ that kind of tool would just have to parse the Header and .cpp for each class, gathering the variable declaration order and reordering the constructor initializer list. everything else (includes, namespaces, inner classes, any functions except constructors, constructor signatures...) can just be ignored. If the tool encounters code it cannot handle, just let it do nothing - the goal should be to get most of the problems, not all of them. – Arne Mertz Apr 08 '13 at 15:52