Better preprocess macro expansion.

This commit is contained in:
acqn
2022-08-22 14:34:50 +08:00
parent 6fc2cd9f61
commit 95e5471cd6
5 changed files with 1414 additions and 332 deletions

View File

@@ -103,8 +103,8 @@ static Collection IFiles = STATIC_COLLECTION_INITIALIZER;
/* List of all active files */ /* List of all active files */
static Collection AFiles = STATIC_COLLECTION_INITIALIZER; static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
/* Input stack used when preprocessing. */ /* Input stack used when preprocessing */
static Collection InputStack = STATIC_COLLECTION_INITIALIZER; static Collection* CurrentInputStack;
/* Counter for the __COUNTER__ macro */ /* Counter for the __COUNTER__ macro */
static unsigned MainFileCounter; static unsigned MainFileCounter;
@@ -394,34 +394,37 @@ static void GetInputChar (void)
** are read by this function. ** are read by this function.
*/ */
{ {
/* Drop all pushed fragments that don't have data left */ /* Get the next-next character from the line */
while (SB_GetIndex (Line) >= SB_GetLen (Line)) { if (SB_GetIndex (Line) + 1 < SB_GetLen (Line)) {
/* Cannot read more from this line, check next line on stack if any */ /* CurC and NextC come from this fragment */
if (CollCount (&InputStack) == 0) { CurC = SB_AtUnchecked (Line, SB_GetIndex (Line));
/* This is THE line */ NextC = SB_AtUnchecked (Line, SB_GetIndex (Line) + 1);
break;
}
FreeStrBuf (Line);
Line = CollPop (&InputStack);
}
/* Now get the next characters from the line */
if (SB_GetIndex (Line) >= SB_GetLen (Line)) {
CurC = NextC = '\0';
} else { } else {
CurC = SB_AtUnchecked (Line, SB_GetIndex (Line)); /* NextC is '\0' by default */
if (SB_GetIndex (Line) + 1 < SB_GetLen (Line)) { NextC = '\0';
/* NextC comes from this fragment */
NextC = SB_AtUnchecked (Line, SB_GetIndex (Line) + 1); /* Drop all pushed fragments that don't have data left */
} else { if (CurrentInputStack != 0) {
while (SB_GetIndex (Line) >= SB_GetLen (Line)) {
/* Cannot read more from this line, check next line on stack if any */
if (CollCount (CurrentInputStack) == 0) {
/* This is THE line */
break;
}
FreeStrBuf (Line);
Line = CollPop (CurrentInputStack);
}
/* NextC comes from next fragment */ /* NextC comes from next fragment */
if (CollCount (&InputStack) > 0) { if (CollCount (CurrentInputStack) > 0) {
NextC = ' '; NextC = ' ';
} else {
NextC = '\0';
} }
} }
/* Get CurC from the line */
CurC = SB_LookAt (Line, SB_GetIndex (Line));
} }
} }
@@ -441,16 +444,46 @@ void NextChar (void)
Collection* UseInputStack (Collection* InputStack)
/* Use the provided input stack for incoming input. Return the previously used
** InputStack.
*/
{
Collection* OldInputStack = CurrentInputStack;
CurrentInputStack = InputStack;
return OldInputStack;
}
void PushLine (StrBuf* L)
/* Save the current input line and use a new one */
{
PRECONDITION (CurrentInputStack != 0);
if (SB_GetIndex (L) < SB_GetLen (L)) {
CollAppend (CurrentInputStack, Line);
Line = L;
GetInputChar ();
} else {
FreeStrBuf (L);
}
}
void ClearLine (void) void ClearLine (void)
/* Clear the current input line */ /* Clear the current input line */
{ {
unsigned I; if (CurrentInputStack != 0) {
unsigned I;
/* Remove all pushed fragments from the input stack */ /* Remove all pushed fragments from the input stack */
for (I = 0; I < CollCount (&InputStack); ++I) { for (I = 0; I < CollCount (CurrentInputStack); ++I) {
FreeStrBuf (CollAtUnchecked (&InputStack, I)); FreeStrBuf (Line);
Line = CollPop (CurrentInputStack);
}
} }
CollDeleteAll (&InputStack);
/* Clear the contents of Line */ /* Clear the contents of Line */
SB_Clear (Line); SB_Clear (Line);

View File

@@ -70,6 +70,8 @@ extern StrBuf* Line;
extern char CurC; extern char CurC;
extern char NextC; extern char NextC;
typedef struct Collection Collection;
/*****************************************************************************/ /*****************************************************************************/
@@ -95,6 +97,14 @@ void NextChar (void);
** are read by this function. ** are read by this function.
*/ */
Collection* UseInputStack (Collection* InputStack);
/* Use the provided input stack for incoming input. Return the previously used
** InputStack.
*/
void PushLine (StrBuf* L);
/* Save the current input line and use a new one */
void ClearLine (void); void ClearLine (void);
/* Clear the current input line */ /* Clear the current input line */

View File

@@ -74,19 +74,17 @@ Macro* NewMacro (const char* Name)
*/ */
{ {
/* Get the length of the macro name */ /* Get the length of the macro name */
unsigned Len = strlen(Name); unsigned Len = strlen (Name);
/* Allocate the structure */ /* Allocate the structure */
Macro* M = (Macro*) xmalloc (sizeof(Macro) + Len); Macro* M = (Macro*) xmalloc (sizeof(Macro) + Len);
/* Initialize the data */ /* Initialize the data */
M->Next = 0; M->Next = 0;
M->Expanding = 0; M->ParamCount = -1; /* Flag: Not a function-like macro */
M->ArgCount = -1; /* Flag: Not a function like macro */ InitCollection (&M->Params);
M->MaxArgs = 0;
InitCollection (&M->FormalArgs);
SB_Init (&M->Replacement); SB_Init (&M->Replacement);
M->Variadic = 0; M->Variadic = 0;
memcpy (M->Name, Name, Len+1); memcpy (M->Name, Name, Len+1);
/* Return the new macro */ /* Return the new macro */
@@ -102,10 +100,10 @@ void FreeMacro (Macro* M)
{ {
unsigned I; unsigned I;
for (I = 0; I < CollCount (&M->FormalArgs); ++I) { for (I = 0; I < CollCount (&M->Params); ++I) {
xfree (CollAtUnchecked (&M->FormalArgs, I)); xfree (CollAtUnchecked (&M->Params, I));
} }
DoneCollection (&M->FormalArgs); DoneCollection (&M->Params);
SB_Done (&M->Replacement); SB_Done (&M->Replacement);
xfree (M); xfree (M);
} }
@@ -121,12 +119,12 @@ Macro* CloneMacro (const Macro* M)
Macro* New = NewMacro (M->Name); Macro* New = NewMacro (M->Name);
unsigned I; unsigned I;
for (I = 0; I < CollCount (&M->FormalArgs); ++I) { for (I = 0; I < CollCount (&M->Params); ++I) {
/* Copy the argument */ /* Copy the parameter */
const char* Arg = CollAtUnchecked (&M->FormalArgs, I); const char* Param = CollAtUnchecked (&M->Params, I);
CollAppend (&New->FormalArgs, xstrdup (Arg)); CollAppend (&New->Params, xstrdup (Param));
} }
New->ArgCount = M->ArgCount; New->ParamCount = M->ParamCount;
New->Variadic = M->Variadic; New->Variadic = M->Variadic;
SB_Copy (&New->Replacement, &M->Replacement); SB_Copy (&New->Replacement, &M->Replacement);
@@ -265,14 +263,14 @@ Macro* FindMacro (const char* Name)
int FindMacroArg (Macro* M, const char* Arg) int FindMacroParam (const Macro* M, const char* Param)
/* Search for a formal macro argument. If found, return the index of the /* Search for a macro parameter. If found, return the index of the parameter.
** argument. If the argument was not found, return -1. ** If the parameter was not found, return -1.
*/ */
{ {
unsigned I; unsigned I;
for (I = 0; I < CollCount (&M->FormalArgs); ++I) { for (I = 0; I < CollCount (&M->Params); ++I) {
if (strcmp (CollAtUnchecked (&M->FormalArgs, I), Arg) == 0) { if (strcmp (CollAtUnchecked (&M->Params, I), Param) == 0) {
/* Found */ /* Found */
return I; return I;
} }
@@ -284,25 +282,25 @@ int FindMacroArg (Macro* M, const char* Arg)
void AddMacroArg (Macro* M, const char* Arg) void AddMacroParam (Macro* M, const char* Param)
/* Add a formal macro argument. */ /* Add a macro parameter. */
{ {
/* Check if we have a duplicate macro argument, but add it anyway. /* Check if we have a duplicate macro parameter, but add it anyway.
** Beware: Don't use FindMacroArg here, since the actual argument array ** Beware: Don't use FindMacroParam here, since the actual argument array
** may not be initialized. ** may not be initialized.
*/ */
unsigned I; unsigned I;
for (I = 0; I < CollCount (&M->FormalArgs); ++I) { for (I = 0; I < CollCount (&M->Params); ++I) {
if (strcmp (CollAtUnchecked (&M->FormalArgs, I), Arg) == 0) { if (strcmp (CollAtUnchecked (&M->Params, I), Param) == 0) {
/* Found */ /* Found */
PPError ("Duplicate macro parameter: '%s'", Arg); PPError ("Duplicate macro parameter: '%s'", Param);
break; break;
} }
} }
/* Add the new argument */ /* Add the new parameter */
CollAppend (&M->FormalArgs, xstrdup (Arg)); CollAppend (&M->Params, xstrdup (Param));
++M->ArgCount; ++M->ParamCount;
} }
@@ -313,14 +311,14 @@ int MacroCmp (const Macro* M1, const Macro* M2)
int I; int I;
/* Argument count must be identical */ /* Argument count must be identical */
if (M1->ArgCount != M2->ArgCount) { if (M1->ParamCount != M2->ParamCount) {
return 1; return 1;
} }
/* Compare the arguments */ /* Compare the parameters */
for (I = 0; I < M1->ArgCount; ++I) { for (I = 0; I < M1->ParamCount; ++I) {
if (strcmp (CollConstAt (&M1->FormalArgs, I), if (strcmp (CollConstAt (&M1->Params, I),
CollConstAt (&M2->FormalArgs, I)) != 0) { CollConstAt (&M2->Params, I)) != 0) {
return 1; return 1;
} }
} }

View File

@@ -55,10 +55,8 @@
typedef struct Macro Macro; typedef struct Macro Macro;
struct Macro { struct Macro {
Macro* Next; /* Next macro with same hash value */ Macro* Next; /* Next macro with same hash value */
int Expanding; /* Are we currently expanding this macro? */ int ParamCount; /* Number of parameters, -1 = no parens */
int ArgCount; /* Number of parameters, -1 = no parens */ Collection Params; /* Parameter list (char*) */
unsigned MaxArgs; /* Size of formal argument list */
Collection FormalArgs; /* Formal argument list (char*) */
StrBuf Replacement; /* Replacement text */ StrBuf Replacement; /* Replacement text */
unsigned char Variadic; /* C99 variadic macro */ unsigned char Variadic; /* C99 variadic macro */
char Name[1]; /* Name, dynamically allocated */ char Name[1]; /* Name, dynamically allocated */
@@ -120,13 +118,13 @@ INLINE int IsMacro (const char* Name)
# define IsMacro(Name) (FindMacro (Name) != 0) # define IsMacro(Name) (FindMacro (Name) != 0)
#endif #endif
int FindMacroArg (Macro* M, const char* Arg); int FindMacroParam (const Macro* M, const char* Param);
/* Search for a formal macro argument. If found, return the index of the /* Search for a macro parameter. If found, return the index of the parameter.
** argument. If the argument was not found, return -1. ** If the parameter was not found, return -1.
*/ */
void AddMacroArg (Macro* M, const char* Arg); void AddMacroParam (Macro* M, const char* Param);
/* Add a formal macro argument. */ /* Add a macro parameter. */
int MacroCmp (const Macro* M1, const Macro* M2); int MacroCmp (const Macro* M1, const Macro* M2);
/* Compare two macros and return zero if both are identical. */ /* Compare two macros and return zero if both are identical. */

File diff suppressed because it is too large Load Diff