]> git.sur5r.net Git - cc65/blob - src/cc65/error.c
fix merge fxxxup
[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-2011, 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                                             /* Warn about: */
67 IntStack WarnConstComparison= INTSTACK(1);  /* - constant comparison results */
68 IntStack WarnNoEffect       = INTSTACK(1);  /* - statements without an effect */
69 IntStack WarnStructParam    = INTSTACK(1);  /* - structs passed by val */
70 IntStack WarnUnusedLabel    = INTSTACK(1);  /* - unused labels */
71 IntStack WarnUnusedParam    = INTSTACK(1);  /* - unused parameters */
72 IntStack WarnUnusedVar      = INTSTACK(1);  /* - unused variables */
73 IntStack WarnUnknownPragma  = INTSTACK(1);  /* - unknown #pragmas */
74
75 /* Map the name of a warning to the intstack that holds its state */
76 typedef struct WarnMapEntry WarnMapEntry;
77 struct WarnMapEntry {
78     IntStack*   Stack;
79     const char* Name;
80 };
81 static WarnMapEntry WarnMap[] = {
82     /* Keep sorted, even if this isn't used for now */
83     { &WarningsAreErrors,       "error"                 },
84     { &WarnConstComparison,     "const-comparison"      },
85     { &WarnNoEffect,            "no-effect"             },
86     { &WarnStructParam,         "struct-param"          },
87     { &WarnUnknownPragma,       "unknown-pragma"        },
88     { &WarnUnusedLabel,         "unused-label"          },
89     { &WarnUnusedParam,         "unused-param"          },
90     { &WarnUnusedVar,           "unused-var"            },
91 };
92
93
94
95 /*****************************************************************************/
96 /*                         Handling of fatal errors                          */
97 /*****************************************************************************/
98
99
100
101 void Fatal (const char* Format, ...)
102 /* Print a message about a fatal error and die */
103 {
104     va_list ap;
105
106     const char* FileName;
107     unsigned    LineNum;
108     if (CurTok.LI) {
109         FileName = GetInputName (CurTok.LI);
110         LineNum  = GetInputLine (CurTok.LI);
111     } else {
112         FileName = GetCurrentFile ();
113         LineNum  = GetCurrentLine ();
114     }
115
116     fprintf (stderr, "%s(%u): Fatal: ", FileName, LineNum);
117
118     va_start (ap, Format);
119     vfprintf (stderr, Format, ap);
120     va_end (ap);
121     fprintf (stderr, "\n");
122
123     if (Line) {
124         Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
125     }
126     exit (EXIT_FAILURE);
127 }
128
129
130
131 void Internal (const char* Format, ...)
132 /* Print a message about an internal compiler error and die. */
133 {
134     va_list ap;
135
136     const char* FileName;
137     unsigned    LineNum;
138     if (CurTok.LI) {
139         FileName = GetInputName (CurTok.LI);
140         LineNum  = GetInputLine (CurTok.LI);
141     } else {
142         FileName = GetCurrentFile ();
143         LineNum  = GetCurrentLine ();
144     }
145
146     fprintf (stderr, "%s(%u): Internal compiler error:\n",
147              FileName, LineNum);
148
149     va_start (ap, Format);
150     vfprintf (stderr, Format, ap);
151     va_end (ap);
152     fprintf (stderr, "\n");
153
154     if (Line) {
155         fprintf (stderr, "\nInput: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
156     }
157
158     /* Use abort to create a core dump */
159     abort ();
160 }
161
162
163
164 /*****************************************************************************/
165 /*                            Handling of errors                             */
166 /*****************************************************************************/
167
168
169
170 static void IntError (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
171 /* Print an error message - internal function*/
172 {
173     fprintf (stderr, "%s(%u): Error: ", Filename, LineNo);
174     vfprintf (stderr, Msg, ap);
175     fprintf (stderr, "\n");
176
177     if (Line) {
178         Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
179     }
180     ++ErrorCount;
181     if (ErrorCount > 10) {
182         Fatal ("Too many errors");
183     }
184 }
185
186
187
188 void Error (const char* Format, ...)
189 /* Print an error message */
190 {
191     va_list ap;
192     va_start (ap, Format);
193     IntError (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
194     va_end (ap);
195 }
196
197
198
199 void LIError (const LineInfo* LI, const char* Format, ...)
200 /* Print an error message with the line info given explicitly */
201 {
202     va_list ap;
203     va_start (ap, Format);
204     IntError (GetInputName (LI), GetInputLine (LI), Format, ap);
205     va_end (ap);
206 }
207
208
209
210 void PPError (const char* Format, ...)
211 /* Print an error message. For use within the preprocessor.  */
212 {
213     va_list ap;
214     va_start (ap, Format);
215     IntError (GetCurrentFile(), GetCurrentLine(), Format, ap);
216     va_end (ap);
217 }
218
219
220
221 /*****************************************************************************/
222 /*                           Handling of warnings                            */
223 /*****************************************************************************/
224
225
226
227 static void IntWarning (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
228 /* Print warning message - internal function. */
229 {
230     if (IS_Get (&WarningsAreErrors)) {
231
232         /* Treat the warning as an error */
233         IntError (Filename, LineNo, Msg, ap);
234
235     } else if (IS_Get (&WarnEnable)) {
236
237         fprintf (stderr, "%s(%u): Warning: ", Filename, LineNo);
238         vfprintf (stderr, Msg, ap);
239         fprintf (stderr, "\n");
240
241         if (Line) {
242             Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
243         }
244         ++WarningCount;
245
246     }
247 }
248
249
250
251 void Warning (const char* Format, ...)
252 /* Print warning message. */
253 {
254     va_list ap;
255     va_start (ap, Format);
256     IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
257     va_end (ap);
258 }
259
260
261
262 void LIWarning (const LineInfo* LI, const char* Format, ...)
263 /* Print a warning message with the line info given explicitly */
264 {
265     va_list ap;
266     va_start (ap, Format);
267     IntWarning (GetInputName (LI), GetInputLine (LI), Format, ap);
268     va_end (ap);
269 }
270
271
272
273 void PPWarning (const char* Format, ...)
274 /* Print warning message. For use within the preprocessor. */
275 {
276     va_list ap;
277     va_start (ap, Format);
278     IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
279     va_end (ap);
280 }
281
282
283
284 IntStack* FindWarning (const char* Name)
285 /* Search for a warning in the WarnMap table and return a pointer to the
286 ** intstack that holds its state. Return NULL if there is no such warning.
287 */
288 {
289     unsigned I;
290
291     /* For now, do a linear search */
292     for (I = 0; I < sizeof(WarnMap) / sizeof (WarnMap[0]); ++I) {
293         if (strcmp (WarnMap[I].Name, Name) == 0) {
294             return WarnMap[I].Stack;
295         }
296     }
297     return 0;
298 }
299
300
301
302 void ListWarnings (FILE* F)
303 /* Print a list of warning types/names to the given file */
304 {
305     unsigned I;
306     for (I = 0; I < sizeof(WarnMap) / sizeof (WarnMap[0]); ++I) {
307         fprintf (F, "%s\n", WarnMap[I].Name);
308     }
309 }
310
311
312
313 /*****************************************************************************/
314 /*                                   Code                                    */
315 /*****************************************************************************/
316
317
318
319 void ErrorReport (void)
320 /* Report errors (called at end of compile) */
321 {
322     Print (stdout, 1, "%u errors, %u warnings\n", ErrorCount, WarningCount);
323 }