]> git.sur5r.net Git - cc65/blobdiff - src/cc65/preproc.c
Moved verbose output to a shared module in the common/ directory.
[cc65] / src / cc65 / preproc.c
index 321b3b1756082756b40c2e65e69088bcf1deeb70..5c652aad4a037af99b09b387da89d9f4484aaeb2 100644 (file)
@@ -5,10 +5,13 @@
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <ctype.h>
 
-#include "../common/xmalloc.h"
+/* common */
+#include "chartype.h"
+#include "print.h"
+#include "xmalloc.h"
 
+/* cc65 */
 #include "codegen.h"
 #include "error.h"
 #include "expr.h"
@@ -98,12 +101,13 @@ static void Comment (void)
     while (CurC != '*' || NextC != '/') {
        if (CurC == '\0') {
            if (NextLine () == 0) {
-               PPError (ERR_EOF_IN_COMMENT, StartingLine);
+               PPError ("End-of-file reached in comment starting at line %u",
+                        StartingLine);
                return;
            }
        } else {
            if (CurC == '/' && NextC == '*') {
-               PPWarning (WARN_NESTED_COMMENT);
+               PPWarning ("`/*' found inside a comment");
            }
            NextChar ();
        }
@@ -170,7 +174,7 @@ static int MacName (char* Ident)
 /* Get macro symbol name.  If error, print message and clear line. */
 {
     if (IsSym (Ident) == 0) {
-       PPError (ERR_IDENT_EXPECTED);
+       PPError ("Identifier expected");
        ClearLine ();
        return 0;
     } else {
@@ -216,7 +220,7 @@ static void ExpandMacroArgs (Macro* M)
                keepch ('#');
                keepstr (Ident);
            }
-       } else if (IsQuoteChar (CurC)) {
+       } else if (IsQuote (CurC)) {
            mptr = CopyQuotedString (mptr);
        } else {
            *mptr++ = CurC;
@@ -242,7 +246,7 @@ static int MacroCall (Macro* M)
     /* Expect an argument list */
     SkipBlank ();
     if (CurC != '(') {
-       PPError (ERR_ILLEGAL_MACRO_CALL);
+       PPError ("Illegal macro call");
        return 0;
     }
 
@@ -262,7 +266,7 @@ static int MacroCall (Macro* M)
            *B++ = CurC;
            NextChar ();
            ++ParCount;
-       } else if (IsQuoteChar (CurC)) {
+       } else if (IsQuote (CurC)) {
            B = CopyQuotedString (B);
        } else if (CurC == ',' || CurC == ')') {
            if (ParCount == 0) {
@@ -315,7 +319,7 @@ static int MacroCall (Macro* M)
 
     /* Compare formal argument count with actual */
     if (M->ArgCount != ArgCount) {
-       PPError (ERR_MACRO_ARGCOUNT);
+       PPError ("Macro argument count mismatch");
        /* Be sure to make enough empty arguments available */
        while (ArgCount < M->ArgCount) {
            M->ActualArgs [ArgCount++] = "";
@@ -355,6 +359,7 @@ static void addmac (void)
     ident      Ident;
     char       Buf[LINESIZE];
     Macro*     M;
+    Macro*     Existing;
 
     /* Read the macro name */
     SkipBlank ();
@@ -362,6 +367,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);
 
@@ -391,7 +399,7 @@ static void addmac (void)
 
        /* Check for a right paren and eat it if we find one */
        if (CurC != ')') {
-                   PPError (ERR_RPAREN_EXPECTED);
+                   PPError ("`)' expected");
            ClearLine ();
            return;
        }
@@ -411,6 +419,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 ("Macro redefinition is not identical");
+       }
+    }
 }
 
 
@@ -452,7 +469,7 @@ static int Pass1 (const char* From, char* To)
                    SkipBlank();
                }
                if (!IsIdent (CurC)) {
-                   PPError (ERR_IDENT_EXPECTED);
+                   PPError ("Identifier expected");
                    *mptr++ = '0';
                } else {
                    SymName (Ident);
@@ -460,7 +477,7 @@ static int Pass1 (const char* From, char* To)
                    if (HaveParen) {
                        SkipBlank();
                        if (CurC != ')') {
-                           PPError (ERR_RPAREN_EXPECTED);
+                           PPError ("`)' expected");
                        } else {
                            NextChar ();
                        }
@@ -472,7 +489,7 @@ static int Pass1 (const char* From, char* To)
                }
                keepstr (Ident);
            }
-       } else if (IsQuoteChar (CurC)) {
+       } else if (IsQuote (CurC)) {
            mptr = CopyQuotedString (mptr);
        } else if (CurC == '/' && NextC == '*') {
            keepch (' ');
@@ -527,7 +544,7 @@ static int Pass2 (const char* From, char* To)
            } else {
                keepstr (Ident);
            }
-       } else if (IsQuoteChar(CurC)) {
+       } else if (IsQuote (CurC)) {
            mptr = CopyQuotedString (mptr);
        } else {
            *mptr++ = CurC;
@@ -688,7 +705,7 @@ static void doinclude (void)
                    break;
 
                default:
-                   PPError (ERR_INCLUDE_LTERM_EXPECTED);
+                   PPError ("`\"' or `<' expected");
                    goto Done;
     }
     NextChar ();
@@ -706,7 +723,7 @@ static void doinclude (void)
     /* Check if we got a terminator */
     if (CurC != RTerm) {
                /* No terminator found */
-               PPError (ERR_INCLUDE_RTERM_EXPECTED);
+               PPError ("Missing terminator or file name too long");
                goto Done;
     }
 
@@ -727,9 +744,9 @@ static void doerror (void)
 {
     SkipBlank ();
     if (CurC == '\0') {
-       PPError (ERR_INVALID_USER_ERROR);
+       PPError ("Invalid #error directive");
     } else {
-        PPError (ERR_USER_ERROR, lptr);
+        PPError ("#error: %s", lptr);
     }
 
     /* clear rest of line */
@@ -805,7 +822,7 @@ void Preprocess (void)
                        continue;
                    }
                    if (!IsSym (Directive)) {
-                       PPError (ERR_CPP_DIRECTIVE_EXPECTED);
+                       PPError ("Preprocessor directive expected");
                        ClearLine ();
                    } else {
                        switch (searchtok (Directive, pre_toks)) {
@@ -823,7 +840,7 @@ void Preprocess (void)
                                    }
                                    s_ifdef[i_ifdef] ^= 2;
                                } else {
-                                   PPError (ERR_UNEXPECTED_CPP_ELSE);
+                                   PPError ("Unexpected `#else'");
                                }
                                break;
 
@@ -831,7 +848,7 @@ void Preprocess (void)
                                if (i_ifdef >= 0) {
                                    Skip = s_ifdef[i_ifdef--] & 1;
                                } else {
-                                   PPError (ERR_UNEXPECTED_CPP_ENDIF);
+                                   PPError ("Unexpected `#endif'");
                                }
                                break;
 
@@ -862,7 +879,7 @@ void Preprocess (void)
                            case PP_LINE:
                        /* Not allowed in strict ANSI mode */
                        if (ANSI) {
-                           PPError (ERR_CPP_DIRECTIVE_EXPECTED);
+                           PPError ("Preprocessor directive expected");
                            ClearLine ();
                        }
                        break;
@@ -883,7 +900,7 @@ void Preprocess (void)
                        break;
 
                    default:
-                       PPError (ERR_CPP_DIRECTIVE_EXPECTED);
+                       PPError ("Preprocessor directive expected");
                        ClearLine ();
                }
            }
@@ -891,7 +908,7 @@ void Preprocess (void)
        }
        if (NextLine () == 0) {
            if (i_ifdef >= 0) {
-               PPError (ERR_CPP_ENDIF_EXPECTED);
+               PPError ("`#endif' expected");
            }
            return;
        }
@@ -900,8 +917,6 @@ void Preprocess (void)
 
 Done:
     xlateline ();
-    if (Verbose > 1) {
-       printf ("line: %s\n", line);
-    }
+    Print (stdout, 2, "line: %s\n", line);
 }