user654789384
user654789384

Reputation: 395

Forward-define a pragma for GCC

I am looking for a solution to forward-declare/define a pragma for GCC.

I use message pragmas as todo list (#pragma message "do this and that"). However, i would like the option to enable/disable the messages completely by a construct as follows:

Warning, this is pseudo-code:

// Definition
#if 1 // generate todo list
#define ADD_TODO(msg) #pragma message "[todo]" msg
#else
#define ADD_TODO(msg) /*empty*/
#endif
// Usage
ADD_TODO("this may result in unitialized variables, fix this")

Has someone experience with such constructs?

Upvotes: 3

Views: 101

Answers (1)

Petr Skocik
Petr Skocik

Reputation: 60097

You want the _Pragma preprocessing operator (introduced in C99):

// Definition
#define PRAGMA(...) _Pragma(#__VA_ARGS__)

#if 1 // generate todo list
#define ADD_TODO(msg) PRAGMA( message "[todo]" msg)
#else
#define ADD_TODO(msg) /*empty*/
#endif
// Usage
ADD_TODO("this may result in unitialized variables, fix this")

The operator solves the problem of not being able to use preprocessor directives (such as #pragma) inside a #define). It takes a string-literal argument, which is quite impractical to construct by hand, and that is why you'll pretty much always see it wrapped in macro that constructs the string using the # (stringification) operator as shown in the above snippet.

Upvotes: 5

Related Questions