diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 153d6a4e7..4b1966437 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -1043,6 +1043,7 @@ static void DoDefine (void) Macro* M; Macro* Existing; int C89; + unsigned Len; /* Read the macro name */ SkipWhitespace (0); @@ -1151,6 +1152,24 @@ static void DoDefine (void) printf ("%s: <%.*s>\n", M->Name, SB_GetLen (&M->Replacement), SB_GetConstBuf (&M->Replacement)); #endif + /* Check for ## at start or end */ + Len = SB_GetLen (&M->Replacement); + if (Len >= 2) { + if (SB_LookAt (&M->Replacement, 0) == '#' && + SB_LookAt (&M->Replacement, 1) == '#') { + /* Diagnose and bail out */ + PPError ("'##' cannot appear at start of macro expansion"); + FreeMacro (M); + return; + } else if (SB_LookAt (&M->Replacement, Len - 1) == '#' && + SB_LookAt (&M->Replacement, Len - 2) == '#') { + /* Diagnose and bail out */ + PPError ("'##' cannot appear at end of macro expansion"); + FreeMacro (M); + return; + } + } + /* Get an existing macro definition with this name */ Existing = FindMacro (M->Name);