From 30629320b781a3d594b285a66a7e47a3924aecaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Sat, 27 Nov 2021 21:22:58 +0000 Subject: [PATCH] Deal with whitespace tokens in define statements. --- cc_macro.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/cc_macro.c b/cc_macro.c index 21b6365..7395c43 100644 --- a/cc_macro.c +++ b/cc_macro.c @@ -57,20 +57,33 @@ void init_macro_env(char* sym, char* value, char* source, int num) macro_env->expansion->linenumber = num; } -void eat_current_token() +void _eat_current_token(int eat_whitespace) { int update_global_token = FALSE; if (macro_token == global_token) update_global_token = TRUE; macro_token = eat_token(macro_token); - while (macro_token->s[0] == ' ') + if(eat_whitespace) + { + while (macro_token->s[0] == ' ') macro_token = eat_token(macro_token); + } if(update_global_token) global_token = macro_token; } +void eat_current_token() +{ + _eat_current_token(TRUE); +} + +void eat_current_token_without_space() +{ + _eat_current_token(FALSE); +} + void remove_whitespace() { macro_token = global_token; @@ -500,7 +513,7 @@ void handle_define() if(conditional_define) macro_env = hold; /* discard the macro name */ - eat_current_token(); + eat_current_token_without_space(); /* Handle macro arguments */ if(macro_token->s[0] == '(') @@ -534,6 +547,10 @@ void handle_define() require(NULL != macro_token, "got an EOF terminated #define\n"); } + else if(macro_token->s[0] == ' ') + { + eat_current_token(); + } while (TRUE) { @@ -602,7 +619,6 @@ void handle_error() if ('\n' == macro_token->s[0]) break; fputs(macro_token->s, stderr); macro_token = macro_token->next; - fputs(" ", stderr); } fputs("\n", stderr); exit(EXIT_FAILURE); @@ -855,6 +871,10 @@ struct token_list* maybe_expand(struct token_list* token) hold3 = hold->arguments; if(NULL != hold3) { + if(token->s[0] == ' ') + { + token = eat_token(token); + } require('(' == token->s[0], "missing open parenthesis for macro function\n"); token = eat_token(token); require(NULL != token, "got an EOF terminated macro function\n");