From 7381a2c4201fcd7edc07d3fbc499d11bc28ec2f4 Mon Sep 17 00:00:00 2001 From: acqn Date: Sun, 24 Jul 2022 19:07:16 +0800 Subject: [PATCH] Added check for extra tokens at the end of directives. Improved diagnostics on missing/illegal macro names. --- src/cc65/preproc.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 96bb0bf1c..016abf11e 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -404,6 +404,21 @@ static void CopyQuotedString (StrBuf* Target) +static int CheckExtraTokens (const char* Name) +/* Check for extra tokens at the end of the directive. Return 1 if there are +** extra tokens, otherwise 0. +*/ +{ + SkipWhitespace (0); + if (SB_GetIndex (Line) != SB_GetLen (Line)) { + PPWarning ("Extra tokens at end of #%s directive", Name); + return 1; + } + return 0; +} + + + /*****************************************************************************/ /* Macro stuff */ /*****************************************************************************/ @@ -416,7 +431,11 @@ static int MacName (char* Ident) */ { if (IsSym (Ident) == 0) { - PPError ("Identifier expected"); + if (CurC != '\0') { + PPError ("Macro name must be an identifier"); + } else { + PPError ("Missing macro name"); + } ClearLine (); return 0; } else { @@ -1144,6 +1163,8 @@ static int DoIfDef (int skip, int flag) SkipWhitespace (0); if (MacName (Ident)) { Value = IsMacro (Ident); + /* Check for extra tokens */ + CheckExtraTokens (flag ? "ifdef" : "ifndef"); } } @@ -1196,6 +1217,10 @@ static void DoInclude (void) /* Check if we got a terminator */ if (CurC == RTerm) { + /* Skip the terminator */ + NextChar (); + /* Check for extra tokens following the filename */ + CheckExtraTokens ("include"); /* Open the include file */ OpenIncludeFile (SB_GetConstBuf (&Filename), IT); } else if (CurC == '\0') { @@ -1246,6 +1271,8 @@ static void DoUndef (void) if (MacName (Ident)) { UndefineMacro (Ident); } + /* Check for extra tokens */ + CheckExtraTokens ("undef"); } @@ -1312,7 +1339,6 @@ void Preprocess (void) case PP_ELIF: if (IfIndex >= 0) { if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) { - /* Handle as #else/#if combination */ if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) { Skip = !Skip; @@ -1337,6 +1363,9 @@ void Preprocess (void) Skip = !Skip; } IfStack[IfIndex] |= IFCOND_ELSE; + + /* Check for extra tokens */ + CheckExtraTokens ("else"); } else { PPError ("Duplicate #else"); } @@ -1359,6 +1388,9 @@ void Preprocess (void) /* Remove the clause that needs a terminator */ Skip = (IfStack[IfIndex--] & IFCOND_SKIP) != 0; + + /* Check for extra tokens */ + CheckExtraTokens ("endif"); } else { PPError ("Unexpected '#endif'"); }