rui314 / chibicc

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

Newlines elided by preprocessor after empty macro #99

Open ludocode opened 2 years ago

ludocode commented 2 years ago

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 :)

fuhsnn commented 1 year ago

For an empty expansion (*rest) points to the next token, this part end up clobbering its at_bol status https://github.com/rui314/chibicc/blob/90d1f7f199cc55b13c7fdb5839d1409806633fdb/preprocess.c#L651-L653