/*****************************************************************************/
/* */
-/* error.c */
+/* error.c */
/* */
-/* Error handling for the cc65 C compiler */
+/* Error handling for the cc65 C compiler */
/* */
/* */
/* */
-/* (C) 1998-2004 Ullrich von Bassewitz */
-/* Römerstraße 52 */
-/* D-70794 Filderstadt */
-/* EMail: uz@cc65.org */
+/* (C) 1998-2011, Ullrich von Bassewitz */
+/* Roemerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/*****************************************************************************/
-/* Data */
+/* Data */
/*****************************************************************************/
/* Count of errors/warnings */
-unsigned ErrorCount = 0;
-unsigned WarningCount = 0;
+unsigned ErrorCount = 0;
+unsigned WarningCount = 0;
+
+/* Warning and error options */
+IntStack WarnEnable = INTSTACK(1); /* Enable warnings */
+IntStack WarningsAreErrors = INTSTACK(0); /* Treat warnings as errors */
+ /* Warn about: */
+IntStack WarnConstComparison= INTSTACK(1); /* - constant comparison results */
+IntStack WarnNoEffect = INTSTACK(1); /* - statements without an effect */
+IntStack WarnStructParam = INTSTACK(1); /* - structs passed by val */
+IntStack WarnUnusedLabel = INTSTACK(1); /* - unused labels */
+IntStack WarnUnusedParam = INTSTACK(1); /* - unused parameters */
+IntStack WarnUnusedVar = INTSTACK(1); /* - unused variables */
+IntStack WarnUnknownPragma = INTSTACK(1); /* - unknown #pragmas */
+
+/* Map the name of a warning to the intstack that holds its state */
+typedef struct WarnMapEntry WarnMapEntry;
+struct WarnMapEntry {
+ IntStack* Stack;
+ const char* Name;
+};
+static WarnMapEntry WarnMap[] = {
+ /* Keep sorted, even if this isn't used for now */
+ { &WarningsAreErrors, "error" },
+ { &WarnConstComparison, "const-comparison" },
+ { &WarnNoEffect, "no-effect" },
+ { &WarnStructParam, "struct-param" },
+ { &WarnUnknownPragma, "unknown-pragma" },
+ { &WarnUnusedLabel, "unused-label" },
+ { &WarnUnusedParam, "unused-param" },
+ { &WarnUnusedVar, "unused-var" },
+};
/*****************************************************************************/
-/* Code */
+/* Handling of fatal errors */
/*****************************************************************************/
-static void IntWarning (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
-/* Print warning message - internal function. */
+void Fatal (const char* Format, ...)
+/* Print a message about a fatal error and die */
{
- if (!IS_Get (&WarnDisable)) {
- fprintf (stderr, "%s(%u): Warning: ", Filename, LineNo);
- vfprintf (stderr, Msg, ap);
- fprintf (stderr, "\n");
+ va_list ap;
- if (Line) {
- Print (stderr, 1, "Input: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
- }
- ++WarningCount;
+ 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: ", FileName, LineNum);
-void Warning (const char* Format, ...)
-/* Print warning message. */
-{
- va_list ap;
va_start (ap, Format);
- IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
+ vfprintf (stderr, Format, ap);
va_end (ap);
+ fprintf (stderr, "\n");
+
+ if (Line) {
+ Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
+ }
+ exit (EXIT_FAILURE);
}
-void LIWarning (const LineInfo* LI, const char* Format, ...)
-/* Print a warning message with the line info given explicitly */
+void Internal (const char* Format, ...)
+/* Print a message about an internal compiler error and die. */
{
va_list ap;
- va_start (ap, Format);
- IntWarning (GetInputName (LI), GetInputLine (LI), Format, ap);
- va_end (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",
+ FileName, LineNum);
-void PPWarning (const char* Format, ...)
-/* Print warning message. For use within the preprocessor. */
-{
- va_list ap;
va_start (ap, Format);
- IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
+ vfprintf (stderr, Format, ap);
va_end (ap);
+ fprintf (stderr, "\n");
+
+ if (Line) {
+ fprintf (stderr, "\nInput: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
+ }
+
+ /* Use abort to create a core dump */
+ abort ();
}
+/*****************************************************************************/
+/* Handling of errors */
+/*****************************************************************************/
+
+
+
static void IntError (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
/* Print an error message - internal function*/
{
fprintf (stderr, "\n");
if (Line) {
- Print (stderr, 1, "Input: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
+ Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
}
++ErrorCount;
if (ErrorCount > 10) {
- Fatal ("Too many errors");
+ Fatal ("Too many errors");
}
}
-void Fatal (const char* Format, ...)
-/* Print a message about a fatal error and die */
+/*****************************************************************************/
+/* Handling of warnings */
+/*****************************************************************************/
+
+
+
+static void IntWarning (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
+/* Print warning message - internal function. */
{
- va_list ap;
+ if (IS_Get (&WarningsAreErrors)) {
+
+ /* Treat the warning as an error */
+ IntError (Filename, LineNo, Msg, ap);
+
+ } else if (IS_Get (&WarnEnable)) {
+
+ fprintf (stderr, "%s(%u): Warning: ", Filename, LineNo);
+ vfprintf (stderr, Msg, ap);
+ fprintf (stderr, "\n");
+
+ if (Line) {
+ Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
+ }
+ ++WarningCount;
- 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: ", FileName, LineNum);
+void Warning (const char* Format, ...)
+/* Print warning message. */
+{
+ va_list ap;
va_start (ap, Format);
- vfprintf (stderr, Format, ap);
+ IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
va_end (ap);
- fprintf (stderr, "\n");
-
- if (Line) {
- Print (stderr, 1, "Input: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
- }
- exit (EXIT_FAILURE);
}
-void Internal (const char* Format, ...)
-/* Print a message about an internal compiler error and die. */
+void LIWarning (const LineInfo* LI, const char* Format, ...)
+/* Print a warning message with the line info given explicitly */
{
va_list ap;
+ va_start (ap, Format);
+ IntWarning (GetInputName (LI), GetInputLine (LI), Format, ap);
+ va_end (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",
- FileName, LineNum);
+void PPWarning (const char* Format, ...)
+/* Print warning message. For use within the preprocessor. */
+{
+ va_list ap;
va_start (ap, Format);
- vfprintf (stderr, Format, ap);
+ IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
va_end (ap);
- fprintf (stderr, "\n");
+}
- if (Line) {
- fprintf (stderr, "\nInput: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
+
+
+IntStack* FindWarning (const char* Name)
+/* Search for a warning in the WarnMap table and return a pointer to the
+ * intstack that holds its state. Return NULL if there is no such warning.
+ */
+{
+ unsigned I;
+
+ /* For now, do a linear search */
+ for (I = 0; I < sizeof(WarnMap) / sizeof (WarnMap[0]); ++I) {
+ if (strcmp (WarnMap[I].Name, Name) == 0) {
+ return WarnMap[I].Stack;
+ }
}
+ return 0;
+}
- /* Use abort to create a core dump */
- abort ();
+
+
+void ListWarnings (FILE* F)
+/* Print a list of warning types/names to the given file */
+{
+ unsigned I;
+ for (I = 0; I < sizeof(WarnMap) / sizeof (WarnMap[0]); ++I) {
+ fprintf (F, "%s\n", WarnMap[I].Name);
+ }
}
+/*****************************************************************************/
+/* Code */
+/*****************************************************************************/
+
+
+
void ErrorReport (void)
/* Report errors (called at end of compile) */
{
Print (stdout, 1, "%u errors, %u warnings\n", ErrorCount, WarningCount);
}
-
-
-