From: cuz Date: Tue, 10 Oct 2000 21:20:34 +0000 (+0000) Subject: Check for macro redefinitions that are not identical and flag them as an X-Git-Tag: V2.12.0~3169 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=04c9e1e3c70effe6ae80bd0db4f111039040206b;p=cc65 Check for macro redefinitions that are not identical and flag them as an error. git-svn-id: svn://svn.cc65.org/cc65/trunk@357 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/cc65/error.c b/src/cc65/error.c index 221a4649c..8fdea9068 100644 --- a/src/cc65/error.c +++ b/src/cc65/error.c @@ -94,6 +94,7 @@ static char* ErrMsg [ERR_COUNT-1] = { "Too few arguments in function call", "Macro argument count mismatch", "Duplicate macro parameter: %s", + "Macro redefinition is not identical", "Variable identifier expected", "Integer expression expected", "Constant expression expected", diff --git a/src/cc65/error.h b/src/cc65/error.h index 326ce37be..c44518300 100644 --- a/src/cc65/error.h +++ b/src/cc65/error.h @@ -70,7 +70,7 @@ enum Warnings { }; /* Error numbers */ -enum Errors { +enum Errors { ERR_NONE, /* No error */ ERR_INVALID_CHAR, ERR_UNEXPECTED_NEWLINE, @@ -94,6 +94,7 @@ enum Errors { ERR_TOO_FEW_FUNC_ARGS, ERR_MACRO_ARGCOUNT, ERR_DUPLICATE_MACRO_ARG, + ERR_MACRO_REDEF, ERR_VAR_IDENT_EXPECTED, ERR_INT_EXPR_EXPECTED, ERR_CONST_EXPR_EXPECTED, diff --git a/src/cc65/macrotab.c b/src/cc65/macrotab.c index 632142c76..c616d9bbe 100644 --- a/src/cc65/macrotab.c +++ b/src/cc65/macrotab.c @@ -36,9 +36,11 @@ #include #include -#include "../common/hashstr.h" -#include "../common/xmalloc.h" - +/* common */ +#include "hashstr.h" +#include "xmalloc.h" + +/* cc65 */ #include "error.h" #include "macrotab.h" @@ -304,6 +306,29 @@ void AddMacroArg (Macro* M, const char* Arg) +int MacroCmp (const Macro* M1, const Macro* M2) +/* Compare two macros and return zero if both are identical. */ +{ + int I; + + /* Argument count must be identical */ + if (M1->ArgCount != M2->ArgCount) { + return 1; + } + + /* Compare the arguments */ + for (I = 0; I < M1->ArgCount; ++I) { + if (strcmp (M1->FormalArgs[I], M2->FormalArgs[I]) != 0) { + return 1; + } + } + + /* Compare the replacement */ + return strcmp (M1->Replacement, M2->Replacement); +} + + + void PrintMacroStats (FILE* F) /* Print macro statistics to the given text file. */ { diff --git a/src/cc65/macrotab.h b/src/cc65/macrotab.h index 3f5bbaba6..374702171 100644 --- a/src/cc65/macrotab.h +++ b/src/cc65/macrotab.h @@ -109,6 +109,9 @@ const char* FindMacroArg (Macro* M, const char* Arg); void AddMacroArg (Macro* M, const char* Arg); /* Add a formal macro argument. */ +int MacroCmp (const Macro* M1, const Macro* M2); +/* Compare two macros and return zero if both are identical. */ + void PrintMacroStats (FILE* F); /* Print macro statistics to the given text file. */ diff --git a/src/cc65/optimize.c b/src/cc65/optimize.c index 3b98d64f5..c9890a244 100644 --- a/src/cc65/optimize.c +++ b/src/cc65/optimize.c @@ -42,6 +42,7 @@ #include "attrib.h" #include "check.h" #include "xmalloc.h" +#include "xsprintf.h" /* cc65 */ #include "asmlabel.h" diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 321b3b175..e884f49ac 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -355,6 +355,7 @@ static void addmac (void) ident Ident; char Buf[LINESIZE]; Macro* M; + Macro* Existing; /* Read the macro name */ SkipBlank (); @@ -362,6 +363,9 @@ static void addmac (void) return; } + /* Get an existing macro definition with this name */ + Existing = FindMacro (Ident); + /* Create a new macro definition */ M = NewMacro (Ident); @@ -411,6 +415,15 @@ static void addmac (void) /* Create a copy of the replacement */ M->Replacement = xstrdup (Buf); + + /* If we have an existing macro, check if the redefinition is identical. + * Print a diagnostic if not. + */ + if (Existing) { + if (MacroCmp (M, Existing) != 0) { + PPError (ERR_MACRO_REDEF); + } + } }