reitermarkus / cmacro-rs

A C macro parser, evaluator and code generator.
2 stars 3 forks source link

Correct handling of spaces between tokens for stringification #4

Open pftbest opened 1 year ago

pftbest commented 1 year ago

Given this C code (godbolt link) it should produce correct strings for this macros

#define HASH(x...) #x
#define NO_SPACE HASH(,,)
#define ONE_SPACE HASH(   ,    ,    )

const char *a = NO_SPACE; // ",,"
const char *b = ONE_SPACE; // ", ,"

You can see that macro stringification depends on the whitespace between the tokens. But clang does not give this information easily. When I tried to implement this in my crate I had to ask for extent for each token and then compare source location for the current token's Start with the previous token's End. If they are equal it means the tokens are next to each other with no space in between. If locations are not equal than we just assume there is at least one space between them. It doesn't matter how many spaces are there, they should all be replaced with only one in the end.