2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* error.c */
|
|
|
|
/* */
|
|
|
|
/* Error handling for the cc65 C compiler */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2011-05-01 17:42:49 +00:00
|
|
|
/* (C) 1998-2011, Ullrich von Bassewitz */
|
2009-09-28 15:59:18 +00:00
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* EMail: uz@cc65.org */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2001-03-10 10:21:03 +00:00
|
|
|
/* common */
|
|
|
|
#include "print.h"
|
|
|
|
|
|
|
|
/* cc65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "global.h"
|
2000-06-14 21:01:37 +00:00
|
|
|
#include "input.h"
|
2001-05-22 09:32:24 +00:00
|
|
|
#include "lineinfo.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "scanner.h"
|
|
|
|
#include "stmt.h"
|
|
|
|
#include "error.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2000-11-02 21:22:33 +00:00
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Count of errors/warnings */
|
|
|
|
unsigned ErrorCount = 0;
|
|
|
|
unsigned WarningCount = 0;
|
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
/* Warning and error options */
|
|
|
|
IntStack WarnEnable = INTSTACK(1); /* Enable warnings */
|
|
|
|
IntStack WarningsAreErrors = INTSTACK(0); /* Treat warnings as errors */
|
2010-10-02 19:32:11 +00:00
|
|
|
/* Warn about: */
|
2010-11-28 21:16:46 +00:00
|
|
|
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 */
|
2009-10-08 13:29:35 +00:00
|
|
|
|
|
|
|
/* 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" },
|
2010-11-28 21:16:46 +00:00
|
|
|
{ &WarnConstComparison, "const-comparison" },
|
2010-10-02 19:32:11 +00:00
|
|
|
{ &WarnNoEffect, "no-effect" },
|
2009-10-15 19:18:34 +00:00
|
|
|
{ &WarnStructParam, "struct-param" },
|
2009-10-08 13:29:35 +00:00
|
|
|
{ &WarnUnknownPragma, "unknown-pragma" },
|
|
|
|
{ &WarnUnusedLabel, "unused-label" },
|
|
|
|
{ &WarnUnusedParam, "unused-param" },
|
|
|
|
{ &WarnUnusedVar, "unused-var" },
|
|
|
|
};
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2009-10-08 13:29:35 +00:00
|
|
|
/* Handling of fatal errors */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
void Fatal (const char* Format, ...)
|
|
|
|
/* Print a message about a fatal error and die */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2009-10-08 13:29:35 +00:00
|
|
|
va_list ap;
|
2005-11-20 21:51:50 +00:00
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
const char* FileName;
|
|
|
|
unsigned LineNum;
|
|
|
|
if (CurTok.LI) {
|
|
|
|
FileName = GetInputName (CurTok.LI);
|
|
|
|
LineNum = GetInputLine (CurTok.LI);
|
|
|
|
} else {
|
|
|
|
FileName = GetCurrentFile ();
|
|
|
|
LineNum = GetCurrentLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
fprintf (stderr, "%s(%u): Fatal: ", FileName, LineNum);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-10-15 19:52:01 +00:00
|
|
|
va_start (ap, Format);
|
2009-10-08 13:29:35 +00:00
|
|
|
vfprintf (stderr, Format, ap);
|
2000-10-15 19:52:01 +00:00
|
|
|
va_end (ap);
|
2009-10-08 13:29:35 +00:00
|
|
|
fprintf (stderr, "\n");
|
|
|
|
|
|
|
|
if (Line) {
|
|
|
|
Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
|
|
|
|
}
|
|
|
|
exit (EXIT_FAILURE);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
void Internal (const char* Format, ...)
|
|
|
|
/* Print a message about an internal compiler error and die. */
|
2009-03-08 14:54:53 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
const char* FileName;
|
|
|
|
unsigned LineNum;
|
|
|
|
if (CurTok.LI) {
|
|
|
|
FileName = GetInputName (CurTok.LI);
|
|
|
|
LineNum = GetInputLine (CurTok.LI);
|
|
|
|
} else {
|
|
|
|
FileName = GetCurrentFile ();
|
|
|
|
LineNum = GetCurrentLine ();
|
|
|
|
}
|
2009-03-08 14:54:53 +00:00
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
fprintf (stderr, "%s(%u): Internal compiler error:\n",
|
|
|
|
FileName, LineNum);
|
2009-03-08 14:54:53 +00:00
|
|
|
|
2000-10-15 19:52:01 +00:00
|
|
|
va_start (ap, Format);
|
2009-10-08 13:29:35 +00:00
|
|
|
vfprintf (stderr, Format, ap);
|
2000-10-15 19:52:01 +00:00
|
|
|
va_end (ap);
|
2009-10-08 13:29:35 +00:00
|
|
|
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 ();
|
2000-10-15 19:52:01 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
2000-10-15 19:52:01 +00:00
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* Handling of errors */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-09-16 12:42:41 +00:00
|
|
|
static void IntError (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
|
2000-10-15 19:52:01 +00:00
|
|
|
/* Print an error message - internal function*/
|
|
|
|
{
|
2004-09-16 12:42:41 +00:00
|
|
|
fprintf (stderr, "%s(%u): Error: ", Filename, LineNo);
|
2000-10-15 19:52:01 +00:00
|
|
|
vfprintf (stderr, Msg, ap);
|
2000-05-28 13:40:48 +00:00
|
|
|
fprintf (stderr, "\n");
|
|
|
|
|
2004-10-10 10:30:03 +00:00
|
|
|
if (Line) {
|
2009-09-28 15:59:18 +00:00
|
|
|
Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
|
2004-10-10 10:30:03 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
++ErrorCount;
|
|
|
|
if (ErrorCount > 10) {
|
2000-10-15 19:52:01 +00:00
|
|
|
Fatal ("Too many errors");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-11-02 21:22:33 +00:00
|
|
|
void Error (const char* Format, ...)
|
2000-10-15 19:52:01 +00:00
|
|
|
/* Print an error message */
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, Format);
|
2001-05-22 09:32:24 +00:00
|
|
|
IntError (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
|
2000-05-28 13:40:48 +00:00
|
|
|
va_end (ap);
|
2000-10-15 19:52:01 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-10-15 19:52:01 +00:00
|
|
|
|
|
|
|
|
2009-03-08 14:54:53 +00:00
|
|
|
void LIError (const LineInfo* LI, const char* Format, ...)
|
|
|
|
/* Print an error message with the line info given explicitly */
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, Format);
|
|
|
|
IntError (GetInputName (LI), GetInputLine (LI), Format, ap);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-10-15 19:52:01 +00:00
|
|
|
void PPError (const char* Format, ...)
|
|
|
|
/* Print an error message. For use within the preprocessor. */
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, Format);
|
|
|
|
IntError (GetCurrentFile(), GetCurrentLine(), Format, ap);
|
|
|
|
va_end (ap);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* Handling of warnings */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void IntWarning (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
|
|
|
|
/* Print warning message - internal function. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2009-10-08 13:29:35 +00:00
|
|
|
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;
|
2001-10-12 18:21:43 +00:00
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
}
|
2009-10-08 13:29:35 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
|
|
|
|
void Warning (const char* Format, ...)
|
|
|
|
/* Print warning message. */
|
|
|
|
{
|
|
|
|
va_list ap;
|
2000-10-15 19:52:01 +00:00
|
|
|
va_start (ap, Format);
|
2009-10-08 13:29:35 +00:00
|
|
|
IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
|
2000-05-28 13:40:48 +00:00
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
void LIWarning (const LineInfo* LI, const char* Format, ...)
|
|
|
|
/* Print a warning message with the line info given explicitly */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
2009-10-08 13:29:35 +00:00
|
|
|
va_start (ap, Format);
|
|
|
|
IntWarning (GetInputName (LI), GetInputLine (LI), Format, ap);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
void PPWarning (const char* Format, ...)
|
|
|
|
/* Print warning message. For use within the preprocessor. */
|
|
|
|
{
|
|
|
|
va_list ap;
|
2000-05-28 13:40:48 +00:00
|
|
|
va_start (ap, Format);
|
2009-10-08 13:29:35 +00:00
|
|
|
IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
|
2000-05-28 13:40:48 +00:00
|
|
|
va_end (ap);
|
2009-10-08 13:29:35 +00:00
|
|
|
}
|
2004-10-10 10:30:03 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
|
|
|
|
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;
|
2004-06-15 21:05:40 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-01 17:42:49 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-08 13:29:35 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* Code */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
void ErrorReport (void)
|
|
|
|
/* Report errors (called at end of compile) */
|
|
|
|
{
|
2004-09-16 12:42:41 +00:00
|
|
|
Print (stdout, 1, "%u errors, %u warnings\n", ErrorCount, WarningCount);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|