#ifdef inside #define

CC Preprocessor

C Problem Overview


I am trying to write something like this:

#define COV_ON(x) \
                #ifdef COVERAGE_TOOL \
                    _Pragma (COVERAGE #x)
                #endif

Is there any way to define COV_ON like this? I know what I have done above is wrong as I can't have #ifdef inside #define. (# is not an allowed character in #define). So is there any solution?

C Solutions


Solution 1 - C

Not possible. Do it the other way around:

#ifdef COVERAGE_TOOL
#define COV_ON(x) _Pragma (COVERAGE #x)
#else
#define COV_ON(x)
#endif

Solution 2 - C

Simply turn it around:

#ifdef COVERAGE_TOOL
#define COV_ON(x) _Pragma (COVERAGE #x)
#else
#define COV_ON(x) /* foo */
#endif

Solution 3 - C

This is an old question, but it needed an up to date answer.

Instead of using an inline ifdef within the macro you can selectively define a __VA_ARGS__ macro to do the same thing

#ifdef COVERAGE_TOOL
#define IF_COVERAGE_TOOL(...) __VA_ARGS__
#else
#define IF_COVERAGE_TOOL(...)
#endif
#define COV_ON(x) IF_COVERAGE_TOOL( _Pragma (COVERAGE #x) )

This has similar functionality to an ifdef except that you get parentheses to delineate the beginning and end (which most IDEs have no problems code-folding) While you can still use #define and #ifdef within the context, #include is not allowed. In order to get inline capabilities similar to #else, you can define a corresponding macro like this:

//#define FOO
#ifdef FOO
#define IF_FOO(...) __VA_ARGS__ 
#define NO_FOO(...)
#else
#define IF_FOO(...)
#define NO_FOO(...) __VA_ARGS__
#endif

IF_FOO(
  #define BAR 5
  int foo = BAR;
)
NO_FOO(
  #define foo 5
)

Only one of NO_FOO()/IF_FOO will produce code.

OK, that's a handy hack, but can we make it MORE useful than #ifdefs... Boolean logic and configuration perhaps? Lets set up some truth tables (and a couple helper macros).

#define PASTE_(x,y) x##y
#define PASTE(x,y) PASTE_(x,y)
#define PASTE3_(x,y,z) x##y##z
#define PASTE3(x,y,z) PASTE3_(x,y,z)
#define Y(...) __VA_ARGS__
#define N(...)
#define IF(x) x //alternate method similar to IFNOT()

#define NOT_N Y
#define NOT_Y N
#define IF_NOT(x) PASTE(NOT_,x)
#define NOT(x) PASTE(NOT_,x)

#define N_OR_N N
#define N_OR_Y Y
#define Y_OR_N Y
#define Y_OR_Y Y
#define OR(x,y) PASTE3(x,_OR_,y)

#define N_AND_N N
#define N_AND_Y N
#define Y_AND_N N
#define Y_AND_Y Y
#define AND(x,y) PASTE3(x,_AND_,y)

#define N_XOR_N N
#define N_XOR_Y Y
#define Y_XOR_N Y
#define Y_XOR_Y N
#define XOR(x,y) PASTE3(x,_XOR_,y)

#define N_NOR_N Y
#define N_NOR_Y N
#define Y_NOR_N N
#define Y_NOR_Y N
#define NOR(x,y) PASTE3(x,_NOR_,y)

#define N_NAND_N Y
#define N_NAND_Y Y
#define Y_NAND_N Y
#define Y_NAND_Y N
#define NAND(x,y) PASTE3(x,_NAND_,y)

#define N_XNOR_N Y
#define N_XNOR_Y N
#define Y_XNOR_N N
#define Y_XNOR_Y Y
#define XNOR(x,y) PASTE3(x,_XNOR_,y)

#define IF2(x,y,z) PASTE3(x,y,z)

config.h

#define FOO Y
#define BAR N
#define BAZ Y

code.c

AND(FOO,BAR)(/*do stuff if both FOO and BAR are enabled*/)
IF2(FOO,_AND_,BAR)( /*do stuff if both FOO and BAR are enabled*/ )
OR(BAZ,AND(FOO,BAR))(
  /*do stuff if both FOO and BAR are enabled or BAZ is enabled*/
)

Solution 4 - C

#ifdef COVERAGE_TOOL
    #define COV_ON(x) _Pragma (COVERAGE #x)
#else
    #define COV_ON(x)
#endif

Solution 5 - C

You cannot. But you can swap #ifdef and #define:

#ifdef COVERAGE_TOOL
#   define COV_ON(x) _Pragma (COVERAGE #x)
#else
#   define COV_ON(x)
#endif

Solution 6 - C

As you mentioned it is not possible to have an #ifdef in a #define. What you should do instead is reverse the order:

#ifdef COVERAGE_TOOL \
  #define COV_ON(x) \
    etc.
#endif

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionagent.smithView Question on Stackoverflow
Solution 1 - CHans PassantView Answer on Stackoverflow
Solution 2 - CPhilipView Answer on Stackoverflow
Solution 3 - CtechnosaurusView Answer on Stackoverflow
Solution 4 - CEboMikeView Answer on Stackoverflow
Solution 5 - Csam hocevarView Answer on Stackoverflow
Solution 6 - CjbergView Answer on Stackoverflow