From aa570f3086ce3e2c5ac8bf6107c051fed5aabf89 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 30 Mar 2020 09:54:47 +0900 Subject: [PATCH] Skip nested #if in a skipped #if-clause --- preprocess.c | 8 +++++++- test/macro.c | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/preprocess.c b/preprocess.c index b19b8a4..4f295d1 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 dd8262b..9998382 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;