rui314 / chibicc

A small C compiler
MIT License
9.57k stars 871 forks source link

Token concatenation not done in object-like macros #38

Open kukrimate opened 3 years ago

kukrimate commented 3 years ago

Hello,

chibicc does not evaluate the ## operator in the replacement list of an object-like macros.

The C standard says, to quote ISO/IEC 9899:1999:

For both object-like and function-like macro invocations, before the replacement list is reexamined for more macro names to replace, each instance of a ## preprocessing token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token.

For example the following:

#define obj a ## b
obj

expands to:

a ## b

even though the expected expansion by the standard (which gcc and clang conforms to) is:

ab

EDIT: typo

rui314 commented 3 years ago

Confirmed. I have no idea how this behavior can be useful, but this is indeed a bug in chibicc.

kukrimate commented 3 years ago

I agree, it's not useful behaviour. I came across it when comparing the output of chibicc to my own implementation of macro expansion and noticed the difference.