From f91c8ad247397f66d567cd4a5c84531725f088d5 Mon Sep 17 00:00:00 2001 From: acqn Date: Thu, 21 Jul 2022 11:09:59 +0800 Subject: [PATCH] Allowed comments right after "#" in preporcessor directives. --- src/cc65/preproc.c | 99 +++++++++++++++++++++++++++++++--------------- src/cc65/preproc.h | 5 +++ 2 files changed, 72 insertions(+), 32 deletions(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 5057b5d71..58b924f6a 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -288,7 +288,7 @@ static void OldStyleComment (void) /* Remember the current line number, so we can output better error ** messages if the comment is not terminated in the current file. */ - unsigned StartingLine = GetCurrentLine(); + unsigned StartingLine = GetCurrentLine (); /* Skip the start of comment chars */ NextChar (); @@ -336,8 +336,8 @@ static void NewStyleComment (void) static int SkipWhitespace (int SkipLines) -/* Skip white space in the input stream. Do also skip newlines if SkipLines -** is true. Return zero if nothing was skipped, otherwise return a +/* Skip white space and comments in the input stream. Do also skip newlines if +** SkipLines is true. Return zero if nothing was skipped, otherwise return a ** value != zero. */ { @@ -346,6 +346,12 @@ static int SkipWhitespace (int SkipLines) if (IsSpace (CurC)) { NextChar (); Skipped = 1; + } else if (CurC == '/' && NextC == '*') { + OldStyleComment (); + Skipped = 1; + } else if (IS_Get (&Standard) >= STD_C99 && CurC == '/' && NextC == '/') { + NewStyleComment (); + Skipped = 1; } else if (CurC == '\0' && SkipLines) { /* End of line, read next */ if (NextLine () != 0) { @@ -479,16 +485,6 @@ static void ReadMacroArgs (MacroExp* E) if (SB_NotEmpty (&Arg)) { SB_AppendChar (&Arg, ' '); } - } else if (CurC == '/' && NextC == '*') { - if (SB_NotEmpty (&Arg)) { - SB_AppendChar (&Arg, ' '); - } - OldStyleComment (); - } else if (IS_Get (&Standard) >= STD_C99 && CurC == '/' && NextC == '/') { - if (SB_NotEmpty (&Arg)) { - SB_AppendChar (&Arg, ' '); - } - NewStyleComment (); } else if (CurC == '\0') { /* End of input inside macro argument list */ PPError ("Unterminated argument list invoking macro '%s'", E->M->Name); @@ -839,7 +835,7 @@ static void DefineMacro (void) /* Remove whitespace and comments from the line, store the preprocessed ** line into the macro replacement buffer. */ - Pass1 (Line, &M->Replacement); + TranslationPhase3 (Line, &M->Replacement); /* Remove whitespace from the end of the line */ while (IsSpace (SB_LookAtLast (&M->Replacement))) { @@ -865,9 +861,55 @@ static void DefineMacro (void) +void TranslationPhase3 (StrBuf* Source, StrBuf* Target) +/* Mimic Translation Phase 3. Handle old and new style comments. Collapse +** non-newline whitespace sequences. +*/ +{ + /* Switch to the new input source */ + StrBuf* OldSource = InitLine (Source); + + /* Loop removing ws and comments */ + while (CurC != '\0') { + int HasWhiteSpace = 0; + while (1) { + /* Squeeze runs of blanks */ + if (IsSpace (CurC)) { + NextChar (); + HasWhiteSpace = 1; + } else if (CurC == '/' && NextC == '*') { + OldStyleComment (); + HasWhiteSpace = 1; + } else if (IS_Get (&Standard) >= STD_C99 && CurC == '/' && NextC == '/') { + NewStyleComment (); + HasWhiteSpace = 1; + } else { + /* No more white space */ + break; + } + } + if (HasWhiteSpace) { + SB_AppendChar (Target, ' '); + } else if (IsQuote (CurC)) { + CopyQuotedString (Target); + } else { + SB_AppendChar (Target, CurC); + NextChar (); + } + } + + /* Terminate the new input line */ + SB_Terminate (Target); + + /* Switch back to the old source */ + InitLine (OldSource); +} + + + static unsigned Pass1 (StrBuf* Source, StrBuf* Target) -/* Preprocessor pass 1. Remove whitespace. Handle old and new style comments -** and the "defined" operator. +/* Preprocessor pass 1. Remove whitespace, old and new style comments. Handle +** the "defined" operator. */ { unsigned IdentCount; @@ -915,16 +957,6 @@ static unsigned Pass1 (StrBuf* Source, StrBuf* Target) } } else if (IsQuote (CurC)) { CopyQuotedString (Target); - } else if (CurC == '/' && NextC == '*') { - if (!IsSpace (SB_LookAtLast (Target))) { - SB_AppendChar (Target, ' '); - } - OldStyleComment (); - } else if (IS_Get (&Standard) >= STD_C99 && CurC == '/' && NextC == '/') { - if (!IsSpace (SB_LookAtLast (Target))) { - SB_AppendChar (Target, ' '); - } - NewStyleComment (); } else { SB_AppendChar (Target, CurC); NextChar (); @@ -981,7 +1013,7 @@ static void MacroReplacement (StrBuf* Source, StrBuf* Target) static void PreprocessLine (void) -/* Translate one line. */ +/* Translate one line with defined macros replaced */ { /* Trim whitespace and remove comments. The function returns the number of ** identifiers found. If there were any, we will have to check for macros. @@ -1030,7 +1062,10 @@ static void DoError (void) if (CurC == '\0') { PPError ("#error"); } else { - PPError ("#error: %s", SB_GetConstBuf (Line) + SB_GetIndex (Line)); + StrBuf MsgLine = AUTO_STRBUF_INITIALIZER; + TranslationPhase3 (Line, &MsgLine); + PPError ("#error: %s", SB_GetConstBuf (&MsgLine) + SB_GetIndex (&MsgLine)); + SB_Done (&MsgLine); } /* Clear the rest of line */ @@ -1174,9 +1209,6 @@ static void DoPragma (void) ** the _Pragma() compiler operator. */ { - /* Skip blanks following the #pragma directive */ - SkipWhitespace (0); - /* Copy the remainder of the line into MLine removing comments and ws */ SB_Clear (MLine); Pass1 (Line, MLine); @@ -1214,7 +1246,10 @@ static void DoWarning (void) if (CurC == '\0') { PPWarning ("#warning"); } else { - PPWarning ("#warning: %s", SB_GetConstBuf (Line) + SB_GetIndex (Line)); + StrBuf MsgLine = AUTO_STRBUF_INITIALIZER; + TranslationPhase3 (Line, &MsgLine); + PPWarning ("#warning: %s", SB_GetConstBuf (&MsgLine) + SB_GetIndex (&MsgLine)); + SB_Done (&MsgLine); } /* Clear the rest of line */ diff --git a/src/cc65/preproc.h b/src/cc65/preproc.h index 78a91a590..c65636ef0 100644 --- a/src/cc65/preproc.h +++ b/src/cc65/preproc.h @@ -55,6 +55,11 @@ extern unsigned char Preprocessing; +void TranslationPhase3 (StrBuf* Source, StrBuf* Target); +/* Mimic Translation Phase 3. Handle old and new style comments. Collapse +** non-newline whitespace sequences. +*/ + void Preprocess (void); /* Preprocess a line */