1
0
mirror of https://github.com/cc65/cc65.git synced 2025-08-08 22:25:28 +00:00

Improved naming for stuff related to preprocessing directives.

This commit is contained in:
acqn
2022-07-24 23:19:05 +08:00
parent f838ba7341
commit 36123c9c8f

View File

@@ -121,67 +121,67 @@ static void MacroReplacement (StrBuf* Source, StrBuf* Target, int MultiLine);
/* Types of preprocessor tokens */ /* Types of preprocessor directives */
typedef enum { typedef enum {
PP_ILLEGAL = -1, PPD_ILLEGAL = -1,
PP_DEFINE, PPD_DEFINE,
PP_ELIF, PPD_ELIF,
PP_ELSE, PPD_ELSE,
PP_ENDIF, PPD_ENDIF,
PP_ERROR, PPD_ERROR,
PP_IF, PPD_IF,
PP_IFDEF, PPD_IFDEF,
PP_IFNDEF, PPD_IFNDEF,
PP_INCLUDE, PPD_INCLUDE,
PP_LINE, PPD_LINE,
PP_PRAGMA, PPD_PRAGMA,
PP_UNDEF, PPD_UNDEF,
PP_WARNING, PPD_WARNING,
} pptoken_t; } ppdirective_t;
/* Preprocessor keyword to token mapping table */ /* Preprocessor directive tokens mapping table */
static const struct PPToken { static const struct PPDType {
const char* Key; /* Keyword */ const char* Tok; /* Token */
pptoken_t Tok; /* Token */ ppdirective_t Type; /* Type */
} PPTokens[] = { } PPDTypes[] = {
{ "define", PP_DEFINE }, { "define", PPD_DEFINE },
{ "elif", PP_ELIF }, { "elif", PPD_ELIF },
{ "else", PP_ELSE }, { "else", PPD_ELSE },
{ "endif", PP_ENDIF }, { "endif", PPD_ENDIF },
{ "error", PP_ERROR }, { "error", PPD_ERROR },
{ "if", PP_IF }, { "if", PPD_IF },
{ "ifdef", PP_IFDEF }, { "ifdef", PPD_IFDEF },
{ "ifndef", PP_IFNDEF }, { "ifndef", PPD_IFNDEF },
{ "include", PP_INCLUDE }, { "include", PPD_INCLUDE },
{ "line", PP_LINE }, { "line", PPD_LINE },
{ "pragma", PP_PRAGMA }, { "pragma", PPD_PRAGMA },
{ "undef", PP_UNDEF }, { "undef", PPD_UNDEF },
{ "warning", PP_WARNING }, { "warning", PPD_WARNING },
}; };
/* Number of preprocessor tokens */ /* Number of preprocessor directive types */
#define PPTOKEN_COUNT (sizeof(PPTokens) / sizeof(PPTokens[0])) #define PPDTOKEN_COUNT (sizeof(PPDTypes) / sizeof(PPDTypes[0]))
static int CmpToken (const void* Key, const void* Elem) static int CmpToken (const void* Key, const void* Elem)
/* Compare function for bsearch */ /* Compare function for bsearch */
{ {
return strcmp ((const char*) Key, ((const struct PPToken*) Elem)->Key); return strcmp ((const char*) Key, ((const struct PPDType*) Elem)->Tok);
} }
static pptoken_t FindPPToken (const char* Ident) static ppdirective_t FindPPDirectiveType (const char* Ident)
/* Find a preprocessor token and return it. Return PP_ILLEGAL if the identifier /* Find a preprocessor directive type and return it. Return PPD_ILLEGAL if the
** is not a valid preprocessor token. ** identifier is not a valid preprocessor directive token.
*/ */
{ {
struct PPToken* P; struct PPDType* P;
P = bsearch (Ident, PPTokens, PPTOKEN_COUNT, sizeof (PPTokens[0]), CmpToken); P = bsearch (Ident, PPDTypes, PPDTOKEN_COUNT, sizeof (PPDTypes[0]), CmpToken);
return P? P->Tok : PP_ILLEGAL; return P? P->Type : PPD_ILLEGAL;
} }
@@ -257,6 +257,26 @@ static int ME_ArgIsVariadic (const MacroExp* E, const Macro* M)
static int MacName (char* Ident)
/* Get a macro symbol name into Ident. If we have an error, print a
** diagnostic message and clear the line.
*/
{
if (IsSym (Ident) == 0) {
if (CurC != '\0') {
PPError ("Macro name must be an identifier");
} else {
PPError ("Missing macro name");
}
ClearLine ();
return 0;
} else {
return 1;
}
}
static void AddPreLine (StrBuf* Str) static void AddPreLine (StrBuf* Str)
/* Add newlines to the string buffer */ /* Add newlines to the string buffer */
{ {
@@ -502,26 +522,6 @@ static int CheckExtraTokens (const char* Name)
static int MacName (char* Ident)
/* Get a macro symbol name into Ident. If we have an error, print a
** diagnostic message and clear the line.
*/
{
if (IsSym (Ident) == 0) {
if (CurC != '\0') {
PPError ("Macro name must be an identifier");
} else {
PPError ("Missing macro name");
}
ClearLine ();
return 0;
} else {
return 1;
}
}
static void ReadMacroArgs (MacroExp* E, const Macro* M, int MultiLine) static void ReadMacroArgs (MacroExp* E, const Macro* M, int MultiLine)
/* Identify the arguments to a macro call as-is */ /* Identify the arguments to a macro call as-is */
{ {
@@ -637,7 +637,7 @@ static void ReadMacroArgs (MacroExp* E, const Macro* M, int MultiLine)
static void MacroArgSubst (MacroExp* E, Macro* M) static void SubstMacroArgs (MacroExp* E, Macro* M)
/* Argument substitution according to ISO/IEC 9899:1999 (E), 6.10.3.1ff */ /* Argument substitution according to ISO/IEC 9899:1999 (E), 6.10.3.1ff */
{ {
ident Ident; ident Ident;
@@ -792,7 +792,7 @@ static void ExpandMacro (StrBuf* Target, Macro* M, int MultiLine)
} }
/* Replace macro arguments handling the # and ## operators */ /* Replace macro arguments handling the # and ## operators */
MacroArgSubst (&E, M); SubstMacroArgs (&E, M);
/* Forbide repeated expansion of the same macro in use */ /* Forbide repeated expansion of the same macro in use */
M->Expanding = 1; M->Expanding = 1;
@@ -1036,6 +1036,12 @@ static void MacroReplacement (StrBuf* Source, StrBuf* Target, int MultiLine)
/*****************************************************************************/
/* Directives */
/*****************************************************************************/
static void DoDefine (void) static void DoDefine (void)
/* Process #define directive */ /* Process #define directive */
{ {
@@ -1468,15 +1474,15 @@ static int ParseDirectives (int InArgList)
} }
ClearLine (); ClearLine ();
} else { } else {
switch (FindPPToken (Directive)) { switch (FindPPDirectiveType (Directive)) {
case PP_DEFINE: case PPD_DEFINE:
if (!PPSkip) { if (!PPSkip) {
DoDefine (); DoDefine ();
} }
break; break;
case PP_ELIF: case PPD_ELIF:
if (PPStack->Index >= 0) { if (PPStack->Index >= 0) {
if ((PPStack->Stack[PPStack->Index] & IFCOND_ELSE) == 0) { if ((PPStack->Stack[PPStack->Index] & IFCOND_ELSE) == 0) {
/* Handle as #else/#if combination */ /* Handle as #else/#if combination */
@@ -1496,7 +1502,7 @@ static int ParseDirectives (int InArgList)
} }
break; break;
case PP_ELSE: case PPD_ELSE:
if (PPStack->Index >= 0) { if (PPStack->Index >= 0) {
if ((PPStack->Stack[PPStack->Index] & IFCOND_ELSE) == 0) { if ((PPStack->Stack[PPStack->Index] & IFCOND_ELSE) == 0) {
if ((PPStack->Stack[PPStack->Index] & IFCOND_SKIP) == 0) { if ((PPStack->Stack[PPStack->Index] & IFCOND_SKIP) == 0) {
@@ -1514,7 +1520,7 @@ static int ParseDirectives (int InArgList)
} }
break; break;
case PP_ENDIF: case PPD_ENDIF:
if (PPStack->Index >= 0) { if (PPStack->Index >= 0) {
/* Remove any clauses on top of stack that do not /* Remove any clauses on top of stack that do not
** need a terminating #endif. ** need a terminating #endif.
@@ -1537,38 +1543,38 @@ static int ParseDirectives (int InArgList)
} }
break; break;
case PP_ERROR: case PPD_ERROR:
if (!PPSkip) { if (!PPSkip) {
DoError (); DoError ();
} }
break; break;
case PP_IF: case PPD_IF:
PPSkip = DoIf (PPSkip); PPSkip = DoIf (PPSkip);
break; break;
case PP_IFDEF: case PPD_IFDEF:
PPSkip = DoIfDef (PPSkip, 1); PPSkip = DoIfDef (PPSkip, 1);
break; break;
case PP_IFNDEF: case PPD_IFNDEF:
PPSkip = DoIfDef (PPSkip, 0); PPSkip = DoIfDef (PPSkip, 0);
break; break;
case PP_INCLUDE: case PPD_INCLUDE:
if (!PPSkip) { if (!PPSkip) {
DoInclude (); DoInclude ();
} }
break; break;
case PP_LINE: case PPD_LINE:
/* Should do something in C99 at least, but we ignore it */ /* Should do something in C99 at least, but we ignore it */
if (!PPSkip) { if (!PPSkip) {
ClearLine (); ClearLine ();
} }
break; break;
case PP_PRAGMA: case PPD_PRAGMA:
if (!PPSkip) { if (!PPSkip) {
if (!InArgList) { if (!InArgList) {
DoPragma (); DoPragma ();
@@ -1578,13 +1584,13 @@ static int ParseDirectives (int InArgList)
} }
break; break;
case PP_UNDEF: case PPD_UNDEF:
if (!PPSkip) { if (!PPSkip) {
DoUndef (); DoUndef ();
} }
break; break;
case PP_WARNING: case PPD_WARNING:
/* #warning is a non standard extension */ /* #warning is a non standard extension */
if (IS_Get (&Standard) > STD_C99) { if (IS_Get (&Standard) > STD_C99) {
if (!PPSkip) { if (!PPSkip) {
@@ -1619,6 +1625,12 @@ static int ParseDirectives (int InArgList)
/*****************************************************************************/
/* Preprocessor */
/*****************************************************************************/
void Preprocess (void) void Preprocess (void)
/* Preprocess lines count of which is affected by directives */ /* Preprocess lines count of which is affected by directives */
{ {