rui314/chibicc

Newlines elided by preprocessor after empty macro

ludocode opened this issue · 1 comments

In a.c:

#define X typedef int unused; Y
#define Y
X
// comment
#include <stddef.h>

chibicc -c a.c:

a.c:5: #include <stddef.h>
       ^ variable name omitted

chibicc -E a.c:

typedef int unused; #include <stddef.h>

chibicc is eliminating the newlines and comments after the line X and concatenating the #include line onto the end of the macro expansion. It doesn't matter how many newlines or comments you add after X; the preprocessor swallows them all and never processes the #include. This has something to do with the empty Y at the end of #define X because it works fine without it.

The only workaround I've found is to put some other declaration (another unused typedef) between the X expansion and the #include. And I can't put this workaround under #ifdef __chibicc__ either because the #ifdef gets concatenated onto the macro expansion instead :)

For an empty expansion (*rest) points to the next token, this part end up clobbering its at_bol status

chibicc/preprocess.c

Lines 651 to 653 in 90d1f7f

*rest = append(body, tok->next);
(*rest)->at_bol = tok->at_bol;
(*rest)->has_space = tok->has_space;