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