Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'hide' spurious "declared but never used" warnings?

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.

like image 391
Roddy Avatar asked Apr 26 '10 09:04

Roddy


3 Answers

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.

like image 198
AshleysBrain Avatar answered Oct 18 '22 23:10

AshleysBrain


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.

like image 31
Alex Budovski Avatar answered Oct 19 '22 00:10

Alex Budovski


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

like image 2
Dmitry Avatar answered Oct 18 '22 23:10

Dmitry