Reputation: 64477
I use this throughout my code:
#pragma message "FIXME: this needs to be fixed"
I want these warning messages to only show up for users who enabled them. I thought I'll just write a macro for it, like:
#ifdef SHOW_DEVELOPER_MESSAGES
#define DEVELOPER_MESSAGE(msg) _Pragma("message " ## msg)
#endif
And use it like this:
DEVELOPER_MESSAGE("TEST: -- xxxx --")
But this results in the following error:
Pasting formed '"message ""TEST: -- xxxxx --"', an invalid preprocessing token
I have read C/C++ Macro string concatenation and tried to use it but I couldn't get it to work. I'm thinking this has to do that the message needs to be in escaped quotes like so (this one works I just don't know how to create an escaped string like this that works with _Pragma):
_Pragma("message \"THIS is the actual message!\"")
How do I create a macro that prints out a pragma message (or comparable solution)?
The solution should be a one-liner, enclosing each pragma message with #ifdef SHOW_DEVELOPER_MESSAGES
would be defeating the purpose of this exercise.
Bonus question: How would I need to change the macro to create different variants for different message types, for example:
DEVELOPER_FIXME("bug")
Which always prefixes the string with FIXME to form: "FIXME: bug"
.
Upvotes: 0
Views: 2013
Reputation: 539745
(With help from http://gcc.gnu.org/ml/gcc-help/2010-10/msg00194.html):
#ifdef SHOW_DEVELOPER_MESSAGES
#define PRAGMA_MESSAGE(x) _Pragma(#x)
#define DEVELOPER_MESSAGE(msg) PRAGMA_MESSAGE(message msg)
#endif
Answer to bonus question:
#define DEVELOPER_FIXME(msg) PRAGMA_MESSAGE(message "FIXME:" msg)
Upvotes: 7