diff --git a/preprocess.c b/preprocess.c index b19b8a4b02c2b4539dd4c646cbae02b58d0f8801..4f295d1eb94a8d41668b087fc2877c7609c2dd8d 100644 --- a/preprocess.c +++ b/preprocess.c @@ -53,10 +53,16 @@ static Token *append(Token *tok1, Token *tok2) { } // Skip until next `#endif`. +// Nested `#if` and `#endif` are skipped. static Token *skip_cond_incl(Token *tok) { while (tok->kind != TK_EOF) { + if (is_hash(tok) && equal(tok->next, "if")) { + tok = skip_cond_incl(tok->next->next); + tok = tok->next; + continue; + } if (is_hash(tok) && equal(tok->next, "endif")) - return tok; + break; tok = tok->next; } return tok; diff --git a/test/macro.c b/test/macro.c index dd8262b8c113eb94c676519fd41cba816b4d7e35..999838223200d66803aa876c9b531e3853f1fc01 100644 --- a/test/macro.c +++ b/test/macro.c @@ -17,6 +17,8 @@ int main() { #if 0 #include "/no/such/file" assert(0, 1, "1"); +#if nested +#endif #endif int m = 0;