Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

putting #warning inside #define's body

In a header that should be able to compile in C and C++ files, in Visual Studio (2010) and MinGW (32-bit - v3.4.5, 64-bit - v4.5.0) I am trying to minimize the size by changing each one of such line (there are many of them):

// for symbol A
#ifdef __GNUC__
# warning Symbol A is deprecated. Use predefined const cnA instead.
#else
# pragma message("Symbol A is deprecated. Use predefined const cnA instead.")
#endif

// Same for B
// Same for C
// . . . 

to

// define this once:
#ifdef __GNUC__
# define X_Warning(x) #warning "Symbol " x " is deprecated. Use cn" x  // (1)
#else
# define X_Warning(x) __pragma(message("Symbol " x " is deprecated. Use cn" x "))
#endif

// and use like this:
X_Warning("A")
X_Warning("B")
X_Warning("C")

or, at least to this:

// define this once:
#ifdef __GNUC__
# define Y_Warning(x) #warning x   // (2)
#else
# define Y_Warning(x) __pragma(message(x))
#endif

// and use like this:
Y_Warning("Symbol A is deprecated. Use predefined const cnA instead.")
Y_Warning("Symbol B is deprecated. Use predefined const cnB instead.")
Y_Warning("Symbol C is deprecated. Use predefined const cnC instead.")
. . .

But the line marked with (1) doesn't work.

__pragma is Microsoft's equivalent for #pragma to use in this kind of situations.

  1. What is the right way to do this?
  2. Is it even possible for MinGW/gcc?
  3. is __GNU__ right symbol to use for such things?

P.S. I forgot to mention that A, B, C.. are #define-ed symbols. In that case it is not possible to do with my old MinGW v3.4.5 (at least in my case with this particular configuration). And @Edwin's answer is correct.

But _Pragma is supported by newer versions of MingW and, thanks to @Christoph for an answer, it is possible to do as follows:

// define this once:
#ifdef __GNUC__
# define DO_PRAGMA(x) _Pragma (#x)
# define X_Warning(x) DO_PRAGMA( message "Symbol " #x " is deprecated. Use cn" x )
#else
# define X_Warning(x) __pragma(message("Symbol " x " is depricated. Use cn" x ))
#endif

// and use like this:
#ifdef A
  X_Warning("A")
#endif
#ifdef C
  X_Warning("B")
#endif
#ifdef B
  X_Warning("C")
#endif

Marking as deprecated seems to work in some cases, but not for me. It requires you to define deprecated symbols before their usage which is not my case and beyond my control.

like image 474
sny Avatar asked Jan 21 '11 03:01

sny


1 Answers

In msvs 2010 there is this :

 #pragma deprecated( identifier1 [,identifier2, ...] )

don't know about other compilers

and microsoft specific:

__declspec(deprecated) void func1(int) {}
__declspec(deprecated("** this is a deprecated function **")) void func2(int) {}
__declspec(deprecated(MY_TEXT)) void func3(int) {}
like image 62
engf-010 Avatar answered Sep 28 '22 18:09

engf-010