]> git.sur5r.net Git - cc65/blobdiff - src/cc65/error.c
Adding functionality to StrBuf
[cc65] / src / cc65 / error.c
index 95db80a45b140e081be74082d8697d53f2034142..b61312855cf0411839ea92f834cc57ad6a8304c5 100644 (file)
 #include <stdlib.h>
 #include <stdarg.h>
 
+/* common */
+#include "print.h"
+
+/* cc65 */
 #include "global.h"
 #include "input.h"
+#include "lineinfo.h"
 #include "scanner.h"
 #include "stmt.h"
 #include "error.h"
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                  Data                                    */
 /*****************************************************************************/
 
 
 
-/* Error messages sorted by ErrTypes */
-static char* ErrMsg [ERR_COUNT-1] = {
-    "Syntax error",
-    "`\"' expected",
-    "`:' expected",
-    "`;' expected",
-    "`,' expected",
-    "`(' expected",
-    "`)' expected",
-    "`[' expected",
-    "`]' expected",
-    "`{' expected",
-    "`}' expected",
-    "Identifier expected",
-    "Type expected",
-    "Incompatible types",
-    "Incompatible pointer types",
-    "Too many arguments in function call",
-    "Too few arguments in function call",
-    "Duplicate macro parameter: %s",
-    "Variable identifier expected",
-    "Integer expression expected",
-    "Constant expression expected",
-    "No active loop",
-    "Redefinition of `%s'",
-    "Conflicting types for `%s'",
-    "String literal expected",
-    "`while' expected",
-    "Function must return a value",
-    "Function cannot return a value",
-    "Unexpected `continue'",
-    "Undefined symbol: `%s'",
-    "Undefined label: `%s'",
-    "Too many local variables",
-    "Too many initializers",
-    "Cannot initialize incomplete type",
-    "Cannot subscript",
-    "Operation not allowed with this type of argument",
-    "Struct expected",
-    "Struct/union has no field named `%s'",
-    "Struct pointer expected",
-    "lvalue expected",
-    "Expression expected",
-    "Preprocessor expression expected",
-    "Illegal type",
-    "Illegal function call",
-    "Illegal indirection",
-    "Illegal address",
-    "Illegal hex digit",
-    "Illegal character constant",
-    "Illegal modifier",
-    "Illegal type qualifier",
-    "Illegal storage class",
-    "Illegal attribute",
-    "Illegal segment name: `%s'",
-    "Division by zero",
-    "Modulo operation with zero",
-    "Range error",
-    "Symbol is already different kind",
-    "Too many lexical levels",
-    "Parameter name omitted",
-    "Old style function decl used as prototype",
-    "Declaration for parameter `%s' but no such parameter",
-    "Cannot take address of a register variable",
-    "Illegal size of data type",
-    "__fastcall__ is not allowed for C functions",
-    "Variable has unknown size",
-    "Unknown identifier: `%s'",
-    "Duplicate qualifier: `%s'",
-    "Assignment to const",
-    "Pointer types differ in type qualifiers",
-};
-
-
-
 /* Count of errors/warnings */
 unsigned ErrorCount    = 0;
 unsigned WarningCount  = 0;
@@ -146,9 +76,7 @@ static void IntWarning (const char* Filename, unsigned Line, const char* Msg, va
        vfprintf (stderr, Msg, ap);
        fprintf (stderr, "\n");
 
-       if (Verbose) {
-           fprintf (stderr, "Line: %s\n", line);
-       }
+       Print (stderr, 1, "Line: %s\n", line);
        ++WarningCount;
     }
 }
@@ -160,7 +88,7 @@ void Warning (const char* Format, ...)
 {
     va_list ap;
     va_start (ap, Format);
-    IntWarning (GetCurrentFile(), curpos, Format, ap);
+    IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
     va_end (ap);
 }
 
@@ -184,9 +112,7 @@ static void IntError (const char* Filename, unsigned Line, const char* Msg, va_l
     vfprintf (stderr, Msg, ap);
     fprintf (stderr, "\n");
 
-    if (Verbose) {
-               fprintf (stderr, "Line: %s\n", line);
-    }
+    Print (stderr, 1, "Line: %s\n", line);
     ++ErrorCount;
     if (ErrorCount > 10) {
                Fatal ("Too many errors");
@@ -195,23 +121,12 @@ static void IntError (const char* Filename, unsigned Line, const char* Msg, va_l
 
 
 
-void Error (unsigned ErrNum, ...)
-/* Print an error message */
-{
-    va_list ap;
-    va_start (ap, ErrNum);
-    IntError (GetCurrentFile(), curpos, ErrMsg [ErrNum-1], ap);
-    va_end (ap);
-}
-
-
-
-void MError (const char* Format, ...)
+void Error (const char* Format, ...)
 /* Print an error message */
 {
     va_list ap;
     va_start (ap, Format);
-    IntError (GetCurrentFile(), curpos, Format, ap);
+    IntError (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
     va_end (ap);
 }
 
@@ -232,17 +147,25 @@ void Fatal (const char* Format, ...)
 /* Print a message about a fatal error and die */
 {
     va_list ap;
+                                                                 
+    const char* FileName;
+    unsigned    LineNum;
+    if (CurTok.LI) {
+       FileName = GetInputName (CurTok.LI);
+       LineNum  = GetInputLine (CurTok.LI);
+    } else {
+       FileName = GetCurrentFile ();
+       LineNum  = GetCurrentLine ();
+    }
 
-    fprintf (stderr, "%s(%u): Fatal: ", GetCurrentFile(), curpos);
+    fprintf (stderr, "%s(%u): Fatal: ", FileName, LineNum);
 
     va_start (ap, Format);
     vfprintf (stderr, Format, ap);
     va_end (ap);
     fprintf (stderr, "\n");
 
-    if (Verbose) {
-               fprintf (stderr, "Line: %s\n", line);
-    }
+    Print (stderr, 1, "Line: %s\n", line);
     exit (EXIT_FAILURE);
 }
 
@@ -253,8 +176,18 @@ void Internal (char* Format, ...)
 {
     va_list ap;
 
+    const char* FileName;
+    unsigned    LineNum;
+    if (CurTok.LI) {
+       FileName = GetInputName (CurTok.LI);
+       LineNum  = GetInputLine (CurTok.LI);
+    } else {
+       FileName = GetCurrentFile ();
+       LineNum  = GetCurrentLine ();
+    }
+
     fprintf (stderr, "%s(%u): Internal compiler error:\n",
-            GetCurrentFile(), curpos);
+            FileName, LineNum);
 
     va_start (ap, Format);
     vfprintf (stderr, Format, ap);
@@ -270,7 +203,7 @@ void Internal (char* Format, ...)
 void ErrorReport (void)
 /* Report errors (called at end of compile) */
 {
-    if (ErrorCount == 0 && Verbose) {
+    if (ErrorCount == 0 && Verbosity > 0) {
        printf ("No errors.\n");
     }
 }
@@ -278,3 +211,4 @@ void ErrorReport (void)
 
 
 
+