views:

236

answers:

3

I'm using the Borland (AKA "Embarcodegearland") C++Builder 2007 compiler which has a minor bug that certain static const items from system header files can cause spurious "xyzzy is declared but never used" warnings.

I'm trying to get my code 100% warning free, so want a way of masking these particular warnings (note - but not by simply turning off the warning!)

Also, I can't modify the header files. I need a way of 'faking' the use of the items, preferably without even knowing their type.

As an example, adding this function to my .cpp modules fixes warnings for these four items, but it seems a bit 'ad-hoc'. Is there a better and preferably self-documenting way of doing this?

static int fakeUse()
{
  return OneHour + OneMinute + OneSecond + OneMillisecond;
}

EDIT: Alex suggested something like this:

#pragma option push
#pragma warn -8080
#include "dateutils.hpp"
#pragma option pop

...which sadly doesn't work because the warning status isn't managed cleverly by the compiler, so messages are still shown.

EDIT #2: AshleysBrain has a good suggestion. I've implemented it by building a "dateutils_fix.hpp" header file like this:

#ifndef DATEUTILS_FIXH
#define DATEUTILS_FIXH

#include <dateutils.hpp>

static void FIX_DATEUTIL_WARNINGS()
{
    UNREFERENCED(OneHour);
    UNREFERENCED(OneMinute);
    UNREFERENCED(OneSecond);
    UNREFERENCED(OneMillisecond);
}

#endif

... and then #including this header instead of dateutils.hpp in my own code.

+5  A: 

Does C++ Builder support a #pragma warning option? If so, you can disable that warning around the #include lines.

In pseudo-code

#pragma warning(push)
#pragma warning(disable: 1234)
#include <someheader.h>
#pragma warning(pop)

Something like that...

This might help for C++ Builder.

Alex
+1 for a nice try :-) Sadly the warning disable only has effect at the point where the messages are output (ie, end of compilation) - so if you 'pop', the messages still show. If you don't pop, then all warnings for 'unused' variables are hidden.
Roddy
+7  A: 

A common way to reference variables is something like this:

#define UNREFERENCED(x)  ((void)x)

// ...

void MyFunc()
{
    const int x = 5; // never used for whatever reason
    UNREFERENCED(x); // stops compiler warning
}

The 'cast to void' effectively means 'do nothing with this expression' so should be equivalent to a no-op. It also counts as a reference to the variable though, so the warning is silenced. You need to place it in a function code though, so maybe a class constructor or startup method will do for you. I don't have C++ Builder to try it on, but this works for other compilers.

AshleysBrain
But isn't the problem that it's arising from system headers, not his code?
Alex
@Alex, true, but I can still fix it this way. The compiler can only deduce that the static isn't used after all the code is compiled.
Roddy
+1  A: 

Another option is to use something like:

template <class T>
inline void unused(const T&) {}

Boost and Qt have those as ignore_unused_variable_warning and Q_UNUSED respectively.

And here's a little article about it @ Sutter's Mill: Shutting up compiler warnings

Dmitry
Nice link, thanks. I have a feeling this template approach may be 'bloatier' if optimizations are disabled, bot otherwise it works well.
Roddy