From 2c9c8ee19615fbae1463d9db3ccf0ce0972e76af Mon Sep 17 00:00:00 2001 From: acqn Date: Tue, 26 Jul 2022 21:10:31 +0800 Subject: [PATCH] Fixed extra "Macro argument count mismatch" message when a macro argument list is unterminated. --- src/cc65/preproc.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 7b33d4b0f..6de50d0b5 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -446,10 +446,14 @@ static int MacName (char* Ident) static void ReadMacroArgs (MacroExp* E) -/* Identify the arguments to a macro call */ +/* Identify the arguments to a macro call as-is */ { + int MissingParen = 0; unsigned Parens; /* Number of open parenthesis */ - StrBuf Arg = STATIC_STRBUF_INITIALIZER; + StrBuf Arg = AUTO_STRBUF_INITIALIZER; + + /* Eat the left paren */ + NextChar (); /* Read the actual macro arguments */ Parens = 0; @@ -512,7 +516,7 @@ static void ReadMacroArgs (MacroExp* E) } else if (CurC == '\0') { /* End of input inside macro argument list */ PPError ("Unterminated argument list invoking macro '%s'", E->M->Name); - + MissingParen = 1; ClearLine (); break; } else { @@ -522,6 +526,21 @@ static void ReadMacroArgs (MacroExp* E) } } + /* Compare formal and actual argument count */ + if (CollCount (&E->ActualArgs) != (unsigned) E->M->ArgCount) { + + if (!MissingParen) { + /* Argument count mismatch */ + PPError ("Macro argument count mismatch"); + } + + /* Be sure to make enough empty arguments available */ + SB_Clear (&Arg); + while (CollCount (&E->ActualArgs) < (unsigned) E->M->ArgCount) { + ME_AppendActual (E, &Arg); + } + } + /* Deallocate string buf resources */ SB_Done (&Arg); } @@ -666,29 +685,12 @@ static void MacroCall (StrBuf* Target, Macro* M) { MacroExp E; - /* Eat the left paren */ - NextChar (); - /* Initialize our MacroExp structure */ InitMacroExp (&E, M); - /* Read the actual macro arguments */ + /* Read the actual macro arguments (with the enclosing parentheses) */ ReadMacroArgs (&E); - /* Compare formal and actual argument count */ - if (CollCount (&E.ActualArgs) != (unsigned) M->ArgCount) { - - StrBuf Arg = STATIC_STRBUF_INITIALIZER; - - /* Argument count mismatch */ - PPError ("Macro argument count mismatch"); - - /* Be sure to make enough empty arguments available */ - while (CollCount (&E.ActualArgs) < (unsigned) M->ArgCount) { - ME_AppendActual (&E, &Arg); - } - } - /* Replace macro arguments handling the # and ## operators */ MacroArgSubst (&E);