Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate strings in the arguments of _Pragma

The argument of _Pragma is a string so I would think that when you paste strings together in the normal c-preprocessor way (ie putting them right next to eachother) that you could form a new string for the argument of _Pragma. However

_Pragma("GCC Poison " "puts")

fails with the error

error: _Pragma takes a parenthesized string literal

How can this be circumvented?

This particular example isn't very useful and has a trivial solution of making them all one string to begin with, but the end goal is to stringize a macro into it

like image 601
rtpax Avatar asked Aug 18 '17 17:08

rtpax


1 Answers

The DO_PRAGMA macro in the GNU docs is defined like this

#define DO_PRAGMA(x) _Pragma (#x)

Using this, if you put two seperate token unstringized next to eachother, they will become stringized. To expand macros within the definition, it must go through one level of indirection, so define as

#define DO_PRAGMA_(x) _Pragma (#x)
#define DO_PRAGMA(x) DO_PRAGMA_(x)

Using this you can create shorthands for various pragmas like this

#define POISON(name) DO_PRAGMA(GCC poison name)

POISON(puts) // becomes _Pragma("GCC poison puts")

Thanks to Eugene Sh. for pointing me to the DO_PRAGMA macro

like image 96
2 revs, 2 users 94% Avatar answered Nov 11 '22 11:11

2 revs, 2 users 94%