C preprocessor macro specialisation based on an argument
Well first, you can do pattern matching with the preprocessor using the ##
. This is how an IIF
macro could be defined:
#define IIF(cond) IIF_ ## cond
#define IIF_0(t, f) f
#define IIF_1(t, f) t
However there is one problem with this approach. A subtle side effect of the ##
operator is that it inhibits expansion. Heres an example:
#define A() 1
//This correctly expands to true
IIF(1)(true, false)
// This will however expand to IIF_A()(true, false)
// This is because A() doesn't expand to 1,
// because its inhibited by the ## operator
IIF(A())(true, false)
The way to work around this is to use another indirection. Since this is commonly done, we can write a macro called CAT
that will concatenate without inhibition.
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
So now we can write the IIF
macro:
#define IIF(c) PRIMITIVE_CAT(IIF_, c)
#define IIF_0(t, ...) __VA_ARGS__
#define IIF_1(t, ...) t
#define A() 1
//This correctly expands to true
IIF(1)(true, false)
// And this will also now correctly expand to true
IIF(A())(true, false)
With pattern matching we can define other operations, such as COMPL
which takes the complement:
// A complement operator
#define COMPL(b) PRIMITIVE_CAT(COMPL_, b)
#define COMPL_0 1
#define COMPL_1 0
// An and operator
#define BITAND(x) PRIMITIVE_CAT(BITAND_, x)
#define BITAND_0(y) 0
#define BITAND_1(y) y
Next, detection techniques can be used to detect if the parameter is a certain value or if it is parenthesis. It relies on variadic arguments expanding to different number of parameters. At the core of detection is a CHECK
macro with a PROBE
macro like this:
#define CHECK_N(x, n, ...) n
#define CHECK(...) CHECK_N(__VA_ARGS__, 0,)
#define PROBE(x) x, 1,
This is very simple. When the probe is given to the CHECK
macro like this:
CHECK(PROBE(~)) // Expands to 1
But if we give it a single token:
CHECK(xxx) // Expands to 0
So with this, we can create some detection macros. For instance, if we want to detect for parenthesis:
#define IS_PAREN(x) CHECK(IS_PAREN_PROBE x)
#define IS_PAREN_PROBE(...) PROBE(~)
IS_PAREN(()) // Expands to 1
IS_PAREN(xxx) // Expands to 0
Next, we need to do a comparison of two tokens, we can rely on the fact that macros don't expand recursively. We force the macro to expand recursively inside of the other macro. If the two tokens are the same then the it will be expanding the macros recursively, which we will detect by trying detect if they expanded to parenthesis or not, here is the COMPARE
macro:
#define COMPARE(a, b) PRIMITIVE_COMPARE(a, b)
#define PRIMITIVE_COMPARE(a, b) \
IIF( \
BITAND \
(IS_PAREN(COMPARE_ ## a(()))) \
(IS_PAREN(COMPARE_ ## b(()))) \
)( \
COMPL(IS_PAREN( \
COMPARE_ ## a( \
COMPARE_ ## b \
)(()) \
)), \
0 \
) \
Each token you want to compare you would define like this:
// So you would define one for each user
#define COMPARE_john_smith(x) x
#define COMPARE_another_user_name(x) x
Now, I don't fully understand the final output you want generated, so say you have a macro for generating code for the current user and one for other users:
#define MACRO_CURRENT_USER(user) ...
#define MACRO_OTHER_USER(user) ...
Then you can write something like this:
// Detects if its the current user
#define IS_CURRENT_USER(user) COMPARE(user, CURRENT_USER)
// Your macro
#define MACRO(user) IIF(IS_CURRENT_USER(user))(MACRO_CURRENT_USER, MACRO_OTHER_USER)(user)
The code below is not sensitive to the MSVC bug. The ... arguments are not separated.
#define IF_USER_ENABLED(x,...) IF_USER_ARGS_GT2 (ENABLE_USER_ ## x,__VA_ARGS__)
#define IF_USER_ARGS_GT2(x,...) ARGS_ARG2 (x,GT4,3,,__VA_ARGS__)
#define ARGS_ARG2(x,y,z,...) ARGS_ ## z (x,y,z,__VA_ARGS__)
#define ARGS_3(x,y,z,w,...) w
#define ARGS_GT4(x,y,z,w,v,...) __VA_ARGS__
#define IF_USER_DISABLED(x,...) IF_NOT_USER_ARGS_GT2 (ENABLE_USER_ ## x,__VA_ARGS__)
#define IF_NOT_USER_ARGS_GT2(x,...) ARGS_ARG2 (x,4,GT3,,__VA_ARGS__)
#define ARGS_4(x,y,z,w,v,...) v
#define ARGS_GT3(x,y,z,w,...) __VA_ARGS__
#define ENABLE_USER_foo ,
//#define ENABLE_USER_bar ,
Tuns out it is possible. This anwser is based on Pauls macros, but much simpler and does not need definition for each user.
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
#define IIF(c) PRIMITIVE_CAT(IIF_, c)
#define IIF_0(t, ...) __VA_ARGS__
#define IIF_1(t, ...) t
#define PROBE(x) x, 1
Now, because of the MSVC bug I had to modify CHECK
macro a bit.
#define MSVC_VA_ARGS_WORKAROUND(define, args) define args
#define CHECK(...) MSVC_VA_ARGS_WORKAROUND(CHECK_N, (__VA_ARGS__, 0))
#define CHECK_N(x, n, ...) n
Instead of defining CURRENT_USER
I switched to following macros.
#define ENABLE_USER_gwiazdorrr () // gwiazdorrr is now enabled
#define ENABLE_USER_foo () // foo is also enabled
// #define ENABLE_USER_bar () // bar is NOT enabled
It actually gives more flexibility, because one can enable multiple user at the same time. The parenthesis is required. The macros below actually detect, whether ENABLE_USER_<user>
is expanded into parenthesis or not.
#define USER_ENABLED_PROBE(user) USER_ENABLED_PROBE_PROXY( ENABLE_USER_##user ) // concatenate prefix with user name
#define USER_ENABLED_PROBE_PROXY(...) USER_ENABLED_PROBE_PRIMIVIE(__VA_ARGS__) // expand arguments
#define USER_ENABLED_PROBE_PRIMIVIE(x) USER_ENABLED_PROBE_COMBINE_##x // merge
#define USER_ENABLED_PROBE_COMBINE_(...) PROBE(~) // if merge successful, expand to probe
USER_ENABLED_PROBE(gwiazdorrr) // expands to ~, 1
USER_ENABLED_PROBE(bar) // expands to USER_ENABLED_PROBE_COMBINE_bar
From now it is a childs play:
#define IS_USER_ENABLED(user) CHECK(USER_ENABLED_PROBE(user))
IS_USER_ENABLED(gwiazdorrr) // expands to 1
IS_USER_ENABLED(bar) // expands to 0
Having this macro and IIF
(thanks Paul!) I decided to implement the optimisation macro mentioned in the original question:
#define TURN_OPTIMISATION_OFF(user) IIF( IS_USER_ENABLED(user) ) \
(\
__pragma optimize("", off),\
/* nothing */ \
)
TURN_OPTIMISATION_OFF(gwiazdorrr) // expands into __pragma optimize("", off)
TURN_OPTIMISATION_OFF(foo) // expands into __pragma optimize("", off)
TURN_OPTIMISATION_OFF(bar) // nothing emitted
Thanks for input!
EDIT: here's the GCC version: http://ideone.com/129eo
Preprocessing takes place before compilation.
If user is known to the preprocessor, then yes:
#define user 4
#define CURRENT_USER 4
#define IS_CURRENT_USER 1
#if user == CURRENT_USER
#define IS_CURRENT_USER(user) 1
#else
#define IS_CURRENT_USER(user) 0
#endif
But this is utterly useless and I doubt it's what you actually have.
Otherwise, no. Don't abuse mecros and the preprocessor.
After your edit:
No, what you want is definitely not possible (turn off optimizations depending on user).