Skip nested #if in a skipped #if-clause

This commit is contained in:
Rui Ueyama 2020-03-30 09:54:47 +09:00
parent bf6ff928ad
commit aa570f3086
2 changed files with 9 additions and 1 deletions

View File

@ -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;

View File

@ -17,6 +17,8 @@ int main() {
#if 0
#include "/no/such/file"
assert(0, 1, "1");
#if nested
#endif
#endif
int m = 0;