7

Follow up to question: g++ does not show a 'unused' warning.

I fully understand why g++ doesn't warn about these variables, but I would like it to somehow find them anyway. The code I'm working on doesn't have any of those special cases, so a single FloatArray x; is almost definitely left-overs.

Even If i have to mark individual classes (Such as warning for unused FloatArray-objects) it would be very useful. What can I do?

Community
  • 1
  • 1
Mikael Öhman
  • 2,294
  • 15
  • 21
  • Is it really a problem? It's hard to imagine it is, since most compilers will probably inline the objects to nothing and then not use them, right? – Mooing Duck Feb 01 '12 at 20:57
  • But compilers warn about this when they determine that the variable is not used "before optimization", not after. That would be a nightmare! – rodrigo Feb 02 '12 at 12:25

3 Answers3

3

Well, with GCC the following code does warn as you want:

struct Foo
{
};
struct Bar
{
    Foo f;
};
int main()
{
    Bar b; //warning: unused variable 'b' 
}

But if you add a constructor/destructor to the Foo or Bar struct, even a trivial one, it will not warn.

struct Foo
{
    Foo() {}
};
struct Bar
{
    Foo f;
};
int main()
{
    Bar b; //no warning! It calls Foo::Foo() into b.f
}

So the easiest way to regain the warning is to compile all the relevant constructors AND destructors conditionally:

struct Foo
{
#ifndef TEST_UNUSED
    Foo() {}
#endif
};
struct Bar
{
    Foo f;
};
int main()
{
    Bar b; //warning!
}

Now compile with g++ -DTEST_UNUSED to check for extra unused variables.

Not my brightest idea, but it works.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • 2
    This might work for user-created objects, but not for those provided by the language (string, STL containers, etc.). – greg Feb 01 '12 at 00:21
  • This will probably let me find a significant amount of the unused objects in project, so I'll let you have the bounty. – Mikael Öhman Feb 02 '12 at 02:22
1

Well, basically you want to create some sort of simple static analysis tool plugged in GCC ? If that's so, you could start by using MELT to quickly implement an unused variable printer.

http://gcc.gnu.org/wiki/MELT%20tutorial

NewbiZ
  • 2,395
  • 2
  • 26
  • 40
  • Thanks, but I'm not sure about "quickly". I don't fulfill the listed prerequisites (not even close, one might say). I will try to see if I can find someone who have done this in MELT already. – Mikael Öhman Jan 18 '12 at 21:22
-1

I'm not sure if I'm missing something in the question but gcc/g++ has options which allow you to specify which warnings you want and which you don't. So simply just enabled the -Wunused-variable.

See here for more details: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Also, -Wall will turn this and many more useful warning on.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Liam
  • 92
  • 9
  • 2
    The unused-variable warning only works for primitives, not C++ classes, because the constructor/destructor could have a desired effect. – greg Feb 01 '12 at 00:20
  • 1
    Sorry, but -1 since I actually link to the question which explains exactly why this won't work. – Mikael Öhman Feb 02 '12 at 02:23