Allowed comments right after "#" in preporcessor directives.

This commit is contained in:
acqn
2022-07-21 11:09:59 +08:00
parent d3d6de2704
commit f91c8ad247
2 changed files with 72 additions and 32 deletions

View File

@@ -288,7 +288,7 @@ static void OldStyleComment (void)
/* Remember the current line number, so we can output better error /* Remember the current line number, so we can output better error
** messages if the comment is not terminated in the current file. ** messages if the comment is not terminated in the current file.
*/ */
unsigned StartingLine = GetCurrentLine(); unsigned StartingLine = GetCurrentLine ();
/* Skip the start of comment chars */ /* Skip the start of comment chars */
NextChar (); NextChar ();
@@ -336,8 +336,8 @@ static void NewStyleComment (void)
static int SkipWhitespace (int SkipLines) static int SkipWhitespace (int SkipLines)
/* Skip white space in the input stream. Do also skip newlines if SkipLines /* Skip white space and comments in the input stream. Do also skip newlines if
** is true. Return zero if nothing was skipped, otherwise return a ** SkipLines is true. Return zero if nothing was skipped, otherwise return a
** value != zero. ** value != zero.
*/ */
{ {
@@ -346,6 +346,12 @@ static int SkipWhitespace (int SkipLines)
if (IsSpace (CurC)) { if (IsSpace (CurC)) {
NextChar (); NextChar ();
Skipped = 1; 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) { } else if (CurC == '\0' && SkipLines) {
/* End of line, read next */ /* End of line, read next */
if (NextLine () != 0) { if (NextLine () != 0) {
@@ -479,16 +485,6 @@ static void ReadMacroArgs (MacroExp* E)
if (SB_NotEmpty (&Arg)) { if (SB_NotEmpty (&Arg)) {
SB_AppendChar (&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') { } else if (CurC == '\0') {
/* End of input inside macro argument list */ /* End of input inside macro argument list */
PPError ("Unterminated argument list invoking macro '%s'", E->M->Name); 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 /* Remove whitespace and comments from the line, store the preprocessed
** line into the macro replacement buffer. ** line into the macro replacement buffer.
*/ */
Pass1 (Line, &M->Replacement); TranslationPhase3 (Line, &M->Replacement);
/* Remove whitespace from the end of the line */ /* Remove whitespace from the end of the line */
while (IsSpace (SB_LookAtLast (&M->Replacement))) { 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) static unsigned Pass1 (StrBuf* Source, StrBuf* Target)
/* Preprocessor pass 1. Remove whitespace. Handle old and new style comments /* Preprocessor pass 1. Remove whitespace, old and new style comments. Handle
** and the "defined" operator. ** the "defined" operator.
*/ */
{ {
unsigned IdentCount; unsigned IdentCount;
@@ -915,16 +957,6 @@ static unsigned Pass1 (StrBuf* Source, StrBuf* Target)
} }
} else if (IsQuote (CurC)) { } else if (IsQuote (CurC)) {
CopyQuotedString (Target); 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 { } else {
SB_AppendChar (Target, CurC); SB_AppendChar (Target, CurC);
NextChar (); NextChar ();
@@ -981,7 +1013,7 @@ static void MacroReplacement (StrBuf* Source, StrBuf* Target)
static void PreprocessLine (void) 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 /* Trim whitespace and remove comments. The function returns the number of
** identifiers found. If there were any, we will have to check for macros. ** identifiers found. If there were any, we will have to check for macros.
@@ -1030,7 +1062,10 @@ static void DoError (void)
if (CurC == '\0') { if (CurC == '\0') {
PPError ("#error"); PPError ("#error");
} else { } 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 */ /* Clear the rest of line */
@@ -1174,9 +1209,6 @@ static void DoPragma (void)
** the _Pragma() compiler operator. ** the _Pragma() compiler operator.
*/ */
{ {
/* Skip blanks following the #pragma directive */
SkipWhitespace (0);
/* Copy the remainder of the line into MLine removing comments and ws */ /* Copy the remainder of the line into MLine removing comments and ws */
SB_Clear (MLine); SB_Clear (MLine);
Pass1 (Line, MLine); Pass1 (Line, MLine);
@@ -1214,7 +1246,10 @@ static void DoWarning (void)
if (CurC == '\0') { if (CurC == '\0') {
PPWarning ("#warning"); PPWarning ("#warning");
} else { } 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 */ /* Clear the rest of line */

View File

@@ -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); void Preprocess (void);
/* Preprocess a line */ /* Preprocess a line */