]> git.sur5r.net Git - cc65/blob - src/cc65/error.c
Changed names of the pragmas to be identical to the corresponding command line
[cc65] / src / cc65 / error.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  error.c                                  */
4 /*                                                                           */
5 /*                  Error handling for the cc65 C compiler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39
40 /* common */
41 #include "print.h"
42
43 /* cc65 */
44 #include "global.h"
45 #include "input.h"
46 #include "lineinfo.h"
47 #include "scanner.h"
48 #include "stmt.h"
49 #include "error.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 /* Count of errors/warnings */
60 unsigned ErrorCount     = 0;
61 unsigned WarningCount   = 0;
62
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 */
70
71 /* Map the name of a warning to the intstack that holds its state */
72 typedef struct WarnMapEntry WarnMapEntry;
73 struct WarnMapEntry {
74     IntStack*   Stack;
75     const char* Name;
76 };
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"            },
84 };
85
86
87
88 /*****************************************************************************/
89 /*                         Handling of fatal errors                          */
90 /*****************************************************************************/
91
92
93
94 void Fatal (const char* Format, ...)
95 /* Print a message about a fatal error and die */
96 {
97     va_list ap;
98
99     const char* FileName;
100     unsigned    LineNum;
101     if (CurTok.LI) {
102         FileName = GetInputName (CurTok.LI);
103         LineNum  = GetInputLine (CurTok.LI);
104     } else {
105         FileName = GetCurrentFile ();
106         LineNum  = GetCurrentLine ();
107     }
108
109     fprintf (stderr, "%s(%u): Fatal: ", FileName, LineNum);
110
111     va_start (ap, Format);
112     vfprintf (stderr, Format, ap);
113     va_end (ap);
114     fprintf (stderr, "\n");
115
116     if (Line) {
117         Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
118     }
119     exit (EXIT_FAILURE);
120 }
121
122
123
124 void Internal (const char* Format, ...)
125 /* Print a message about an internal compiler error and die. */
126 {
127     va_list ap;
128
129     const char* FileName;
130     unsigned    LineNum;
131     if (CurTok.LI) {
132         FileName = GetInputName (CurTok.LI);
133         LineNum  = GetInputLine (CurTok.LI);
134     } else {
135         FileName = GetCurrentFile ();
136         LineNum  = GetCurrentLine ();
137     }
138
139     fprintf (stderr, "%s(%u): Internal compiler error:\n",
140              FileName, LineNum);
141
142     va_start (ap, Format);
143     vfprintf (stderr, Format, ap);
144     va_end (ap);
145     fprintf (stderr, "\n");
146
147     if (Line) {
148         fprintf (stderr, "\nInput: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
149     }
150
151     /* Use abort to create a core dump */
152     abort ();
153 }
154
155
156
157 /*****************************************************************************/
158 /*                            Handling of errors                             */
159 /*****************************************************************************/
160
161
162
163 static void IntError (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
164 /* Print an error message - internal function*/
165 {
166     fprintf (stderr, "%s(%u): Error: ", Filename, LineNo);
167     vfprintf (stderr, Msg, ap);
168     fprintf (stderr, "\n");
169
170     if (Line) {
171         Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
172     }
173     ++ErrorCount;
174     if (ErrorCount > 10) {
175         Fatal ("Too many errors");
176     }
177 }
178
179
180
181 void Error (const char* Format, ...)
182 /* Print an error message */
183 {
184     va_list ap;
185     va_start (ap, Format);
186     IntError (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
187     va_end (ap);
188 }
189
190
191
192 void LIError (const LineInfo* LI, const char* Format, ...)
193 /* Print an error message with the line info given explicitly */
194 {
195     va_list ap;
196     va_start (ap, Format);
197     IntError (GetInputName (LI), GetInputLine (LI), Format, ap);
198     va_end (ap);
199 }
200
201
202
203 void PPError (const char* Format, ...)
204 /* Print an error message. For use within the preprocessor.  */
205 {
206     va_list ap;
207     va_start (ap, Format);
208     IntError (GetCurrentFile(), GetCurrentLine(), Format, ap);
209     va_end (ap);
210 }
211
212
213
214 /*****************************************************************************/
215 /*                           Handling of warnings                            */
216 /*****************************************************************************/
217
218
219
220 static void IntWarning (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
221 /* Print warning message - internal function. */
222 {
223     if (IS_Get (&WarningsAreErrors)) {
224
225         /* Treat the warning as an error */
226         IntError (Filename, LineNo, Msg, ap);
227
228     } else if (IS_Get (&WarnEnable)) {
229
230         fprintf (stderr, "%s(%u): Warning: ", Filename, LineNo);
231         vfprintf (stderr, Msg, ap);
232         fprintf (stderr, "\n");
233
234         if (Line) {
235             Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
236         }
237         ++WarningCount;
238
239     }
240 }
241
242
243
244 void Warning (const char* Format, ...)
245 /* Print warning message. */
246 {
247     va_list ap;
248     va_start (ap, Format);
249     IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
250     va_end (ap);
251 }
252
253
254
255 void LIWarning (const LineInfo* LI, const char* Format, ...)
256 /* Print a warning message with the line info given explicitly */
257 {
258     va_list ap;
259     va_start (ap, Format);
260     IntWarning (GetInputName (LI), GetInputLine (LI), Format, ap);
261     va_end (ap);
262 }
263
264
265
266 void PPWarning (const char* Format, ...)
267 /* Print warning message. For use within the preprocessor. */
268 {
269     va_list ap;
270     va_start (ap, Format);
271     IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
272     va_end (ap);
273 }
274
275
276
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.
280  */
281 {
282     unsigned I;
283
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;
288         }
289     }
290     return 0;
291 }
292
293
294
295 /*****************************************************************************/
296 /*                                   Code                                    */
297 /*****************************************************************************/
298
299
300
301 void ErrorReport (void)
302 /* Report errors (called at end of compile) */
303 {
304     Print (stdout, 1, "%u errors, %u warnings\n", ErrorCount, WarningCount);
305 }
306
307
308