simplecpp
simplecpp copied to clipboard
Function-like macro not expanded a second time in some cases
Preprocessing of this code with simplecpp:
#define CAT(a, b) CAT2(a, b)
#define CAT2(a, b) a ## b
#define FOO x
#define BAR() CAT(F, OO)
#define BAZ CAT(B, AR)()
BAZ
produces this result:
CAT ( F , OO )
All mainstream preprocessors (GCC, Clang, MSVC) produce this result (https://godbolt.org/z/9GhhTarE3):
x
It seems that simplecpp decides to not expand CAT a second time during evaluation of BAZ. I don't know if this is correct from the point of view of the standard.
Some additional observations:
- If the last line is replaced with
CAT(B, AR)()then all preprocessors produce the same result:x. - If
BAR()is converted from function-like macro to regular macro:
then all preprocessors produce the same result:#define BAR CAT(F, OO) #define BAZ CAT(B, AR)CAT ( F , OO ).
I don't know if this is correct from the point of view of the standard. it is not
#define CAT(a, b) CAT2(a, b)
#define CAT2(a, b) a ## b
#define FOO x
#define BAR() CAT(F, OO)
#define BAZ CAT(B, AR)()
BAZ
// expansion:
BAZ -> CAT(B, AR())
CAT(B, AR()) -> B -> B
CAT(B, AR()) -> AR() -> AR()
CAT(a, b) -> CAT2(B, AR())
CAT2(a, b) -> B##AR() -> BAR()
BAR() -> CAT(F, OO)
// should CAT be ommited from expansion since it was already expanded?
// not sure, think standard allows for one more expansion before it stops expanding
// otherwise gcc would not produce x
CAT(a, b) -> CAT2(F, OO)
// should CAT2 be ommited from expansion since it was already expanded?
// not sure, think standard allows for one more expansion before it stops expanding
// otherwise gcc would not produce x
CAT2(a, b) -> F##OO -> FOO
FOO -> x