From 533aeb1dfa508e00ccc9acc375140e9f9b81eb41 Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Thu, 20 Jan 2022 07:38:01 -0500 Subject: [PATCH] nested #if blocks now behave correctly --- cc_macro.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/cc_macro.c b/cc_macro.c index 6e44139..4f493ca 100644 --- a/cc_macro.c +++ b/cc_macro.c @@ -808,6 +808,38 @@ struct token_list* expand_macro_functions(struct token_list* expansion, struct t return hold; } +void eat_until_endif() +{ + /* This #if block is nested inside of an #if block that needs to be dropped, lose EVERYTHING */ + do + { + if(match("#if", macro_token->s) || match("#ifdef", macro_token->s) || match("#ifndef", macro_token->s)) + { + eat_current_token(); + eat_until_endif(); + } + + eat_current_token(); + require(NULL != macro_token, "Unterminated #if block\n"); + } while(!match("#endif", macro_token->s)); +} + +void eat_block() +{ + /* This conditional #if block is wrong, drop everything until the #elif/#else/#endif */ + do + { + if(match("#if", macro_token->s) || match("#ifdef", macro_token->s) || match("#ifndef", macro_token->s)) + { + eat_current_token(); + eat_until_endif(); + } + + eat_current_token(); + require(NULL != macro_token, "Unterminated #if block\n"); + } while(!match("#elif", macro_token->s) && !match("#else", macro_token->s) && !match("#endif", macro_token->s)); +} + struct token_list* maybe_expand(struct token_list* token) { if(NULL == token) @@ -919,7 +951,8 @@ void preprocess() else if(!conditional_inclusion_top->include) { /* rewrite the token stream to exclude the current token */ - eat_current_token(); + eat_block(); + start_of_line = TRUE; } else {