1 /*****************************************************************************/
5 /* Error handling for the cc65 C compiler */
9 /* (C) 1998-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 /* Count of errors/warnings */
60 unsigned ErrorCount = 0;
61 unsigned WarningCount = 0;
63 /* Warning and error options */
64 IntStack WarnEnable = INTSTACK(1); /* Enable warnings */
65 IntStack WarningsAreErrors = INTSTACK(0); /* Treat warnings as errors */
66 IntStack WarnUnusedLabel = INTSTACK(1); /* Warn about unused labels */
67 IntStack WarnUnusedParam = INTSTACK(1); /* Warn about unused parameters */
68 IntStack WarnUnusedVar = INTSTACK(1); /* Warn about unused variables */
69 IntStack WarnUnknownPragma = INTSTACK(1); /* Warn about unknown #pragmas */
71 /* Map the name of a warning to the intstack that holds its state */
72 typedef struct WarnMapEntry WarnMapEntry;
77 static WarnMapEntry WarnMap[] = {
78 /* Keep sorted, even if this isn't used for now */
79 { &WarningsAreErrors, "error" },
80 { &WarnUnknownPragma, "unknown-pragma" },
81 { &WarnUnusedLabel, "unused-label" },
82 { &WarnUnusedParam, "unused-param" },
83 { &WarnUnusedVar, "unused-var" },
88 /*****************************************************************************/
89 /* Handling of fatal errors */
90 /*****************************************************************************/
94 void Fatal (const char* Format, ...)
95 /* Print a message about a fatal error and die */
102 FileName = GetInputName (CurTok.LI);
103 LineNum = GetInputLine (CurTok.LI);
105 FileName = GetCurrentFile ();
106 LineNum = GetCurrentLine ();
109 fprintf (stderr, "%s(%u): Fatal: ", FileName, LineNum);
111 va_start (ap, Format);
112 vfprintf (stderr, Format, ap);
114 fprintf (stderr, "\n");
117 Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
124 void Internal (const char* Format, ...)
125 /* Print a message about an internal compiler error and die. */
129 const char* FileName;
132 FileName = GetInputName (CurTok.LI);
133 LineNum = GetInputLine (CurTok.LI);
135 FileName = GetCurrentFile ();
136 LineNum = GetCurrentLine ();
139 fprintf (stderr, "%s(%u): Internal compiler error:\n",
142 va_start (ap, Format);
143 vfprintf (stderr, Format, ap);
145 fprintf (stderr, "\n");
148 fprintf (stderr, "\nInput: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
151 /* Use abort to create a core dump */
157 /*****************************************************************************/
158 /* Handling of errors */
159 /*****************************************************************************/
163 static void IntError (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
164 /* Print an error message - internal function*/
166 fprintf (stderr, "%s(%u): Error: ", Filename, LineNo);
167 vfprintf (stderr, Msg, ap);
168 fprintf (stderr, "\n");
171 Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
174 if (ErrorCount > 10) {
175 Fatal ("Too many errors");
181 void Error (const char* Format, ...)
182 /* Print an error message */
185 va_start (ap, Format);
186 IntError (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
192 void LIError (const LineInfo* LI, const char* Format, ...)
193 /* Print an error message with the line info given explicitly */
196 va_start (ap, Format);
197 IntError (GetInputName (LI), GetInputLine (LI), Format, ap);
203 void PPError (const char* Format, ...)
204 /* Print an error message. For use within the preprocessor. */
207 va_start (ap, Format);
208 IntError (GetCurrentFile(), GetCurrentLine(), Format, ap);
214 /*****************************************************************************/
215 /* Handling of warnings */
216 /*****************************************************************************/
220 static void IntWarning (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
221 /* Print warning message - internal function. */
223 if (IS_Get (&WarningsAreErrors)) {
225 /* Treat the warning as an error */
226 IntError (Filename, LineNo, Msg, ap);
228 } else if (IS_Get (&WarnEnable)) {
230 fprintf (stderr, "%s(%u): Warning: ", Filename, LineNo);
231 vfprintf (stderr, Msg, ap);
232 fprintf (stderr, "\n");
235 Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
244 void Warning (const char* Format, ...)
245 /* Print warning message. */
248 va_start (ap, Format);
249 IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
255 void LIWarning (const LineInfo* LI, const char* Format, ...)
256 /* Print a warning message with the line info given explicitly */
259 va_start (ap, Format);
260 IntWarning (GetInputName (LI), GetInputLine (LI), Format, ap);
266 void PPWarning (const char* Format, ...)
267 /* Print warning message. For use within the preprocessor. */
270 va_start (ap, Format);
271 IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
277 IntStack* FindWarning (const char* Name)
278 /* Search for a warning in the WarnMap table and return a pointer to the
279 * intstack that holds its state. Return NULL if there is no such warning.
284 /* For now, do a linear search */
285 for (I = 0; I < sizeof(WarnMap) / sizeof (WarnMap[0]); ++I) {
286 if (strcmp (WarnMap[I].Name, Name) == 0) {
287 return WarnMap[I].Stack;
295 /*****************************************************************************/
297 /*****************************************************************************/
301 void ErrorReport (void)
302 /* Report errors (called at end of compile) */
304 Print (stdout, 1, "%u errors, %u warnings\n", ErrorCount, WarningCount);