]> git.sur5r.net Git - cc65/blob - src/cc65/error.c
dd2d17c57248dd8b31722c4ef106d2f3d4c09412
[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     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
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 #include "global.h"
41 #include "input.h"
42 #include "io.h"
43 #include "scanner.h"
44 #include "stmt.h"
45 #include "error.h"
46
47
48
49 /*****************************************************************************/
50 /*                                   Data                                    */
51 /*****************************************************************************/
52
53
54
55 static char* WarnMsg [WARN_COUNT-1] = {
56     "Unreachable code",
57     "Condition is never true",
58     "Condition is always true",
59     "Converting pointer to integer without a cast",
60     "Converting integer to pointer without a cast",
61     "Function call without a prototype",
62     "Unknown #pragma",
63     "No case labels",
64     "Function must be extern",
65     "Parameter `%s' is never used",
66     "`%s' is defined but never used",
67     "Constant is long",
68     "`/*' found inside a comment",
69     "Useless declaration",
70 };
71
72
73
74 /* Error messages sorted by ErrTypes */
75 static char* ErrMsg [ERR_COUNT-1] = {
76     "Invalid character (%u)",
77     "Unexpected newline",
78     "End-of-file reached in comment starting at line %u",
79     "Syntax error",
80     "`\"' expected",
81     "`:' expected",
82     "`;' expected",
83     "`(' expected",
84     "`)' expected",
85     "`[' expected",
86     "`]' expected",
87     "`{' expected",
88     "`}' expected",
89     "Identifier expected",
90     "Type expected",
91     "Incompatible types",
92     "Incompatible pointer types",
93     "Too many arguments in function call",
94     "Too few arguments in function call",
95     "Macro argument count mismatch",
96     "Duplicate macro parameter: %s",
97     "Variable identifier expected",
98     "Integer expression expected",
99     "Constant expression expected",
100     "No active loop",
101     "`\"' or `<' expected",
102     "Missing terminator or name too long",
103     "Include file `%s' not found",
104     "Cannot open include file `%s': %s",
105     "Invalid #error directive",
106     "#error: %s",
107     "Unexpected `#endif'",
108     "Unexpected `#else'",
109     "`#endif' expected",
110     "Compiler directive expected",
111     "Symbol `%s' defined more than once",
112     "String literal expected",
113     "`while' expected",
114     "Function must return a value",
115     "Function cannot return a value",
116     "Unexpected `continue'",
117     "Undefined symbol: `%s'",
118     "Undefined label: `%s'",
119     "Include nesting too deep",
120     "Too many local variables",
121     "Too many initializers",
122     "Cannot initialize incomplete type",
123     "Cannot subscript",
124     "Operation not allowed on these types",
125     "Struct expected",
126     "Struct/union has no field named `%s'",
127     "Struct pointer expected",
128     "lvalue expected",
129     "Expression expected",
130     "Preprocessor expression expected",
131     "Illegal type",
132     "Illegal function call",
133     "Illegal indirection",
134     "Illegal address",
135     "Illegal macro call",
136     "Illegal hex digit",
137     "Illegal character constant",
138     "Illegal modifier",
139     "Illegal storage class",
140     "Division by zero",
141     "Modulo operation with zero",
142     "Range error",
143     "Symbol is already different kind",
144     "Too many lexical levels",
145     "Parameter name omitted",
146     "Old style function decl used as prototype",
147     "Declaration for parameter `%s' but no such parameter",
148     "Cannot take address of a register variable",
149     "Illegal size of data type",
150     "__fastcall__ is not allowed for C functions",
151     "Variable has unknown size",
152     "Unknown identifier: `%s'",
153 };
154
155
156
157 static char* FatMsg [FAT_COUNT-1] = {
158     "Too many errors",
159     "Cannot open output file: %s",
160     "Cannot write to output file (disk full?)",
161     "Cannot open input file: %s",
162     "Out of memory",
163     "Stack overflow",
164     "Stack empty",
165     "Out of string space",
166     "Too many case labels",
167 };
168
169
170
171 /* Count of errors/warnings */
172 unsigned ErrorCount     = 0;
173 unsigned WarningCount   = 0;
174
175
176
177 /*****************************************************************************/
178 /*                                   Code                                    */
179 /*****************************************************************************/
180
181
182
183 void Warning (unsigned WarnNum, ...)
184 /* Print warning message. */
185 {
186     va_list ap;
187
188     if (!NoWarn) {
189         fprintf (stderr, "%s(%u): Warning #%u: ",
190                  GetCurrentFile(), curpos, WarnNum);
191
192         va_start (ap, WarnNum);
193         vfprintf (stderr, WarnMsg [WarnNum-1], ap);
194         va_end (ap);
195         fprintf (stderr, "\n");
196
197         if (Verbose) {
198             fprintf (stderr, "Line: %s\n", line);
199         }
200     }
201     ++ WarningCount;
202 }
203
204
205
206 void PPWarning (unsigned WarnNum, ...)
207 /* Print warning message. For use within the preprocessor. */
208 {
209     va_list ap;
210
211     if (!NoWarn) {
212         fprintf (stderr, "%s(%u): Warning #%u: ",
213                  GetCurrentFile(), GetCurrentLine(), WarnNum);
214
215         va_start (ap, WarnNum);
216         vfprintf (stderr, WarnMsg [WarnNum-1], ap);
217         va_end (ap);
218         fprintf (stderr, "\n");
219     }
220     ++WarningCount;
221 }
222
223
224
225 void Error (unsigned ErrNum, ...)
226 /* Print an error message */
227 {
228     va_list ap;
229
230     fprintf (stderr, "%s(%u): Error #%u: ",
231              GetCurrentFile(), curpos, ErrNum);
232
233     va_start (ap, ErrNum);
234     vfprintf (stderr, ErrMsg [ErrNum-1], ap);
235     va_end (ap);
236     fprintf (stderr, "\n");
237
238     if (Verbose) {
239         fprintf (stderr, "Line: %s\n", line);
240     }
241     ++ErrorCount;
242     if (ErrorCount > 10) {
243         Fatal (FAT_TOO_MANY_ERRORS);
244     }
245 }
246
247
248
249 void PPError (unsigned ErrNum, ...)
250 /* Print an error message. For use within the preprocessor.  */
251 {
252     va_list ap;
253
254     fprintf (stderr, "%s(%u): Error #%u: ",
255              GetCurrentFile(), GetCurrentLine(), ErrNum);
256
257     va_start (ap, ErrNum);
258     vfprintf (stderr, ErrMsg [ErrNum-1], ap);
259     va_end (ap);
260     fprintf (stderr, "\n");
261
262     ++ErrorCount;
263     if (ErrorCount > 10) {
264         Fatal (FAT_TOO_MANY_ERRORS);
265     }
266 }
267
268
269
270 void Fatal (unsigned FatNum, ...)
271 /* Print a message about a fatal error and die */
272 {
273     va_list ap;
274
275     fprintf (stderr, "%s(%u): Fatal #%u: ",
276              GetCurrentFile(), curpos, FatNum);
277
278     va_start (ap, FatNum);
279     vfprintf (stderr, FatMsg [FatNum-1], ap);
280     va_end (ap);
281     fprintf (stderr, "\n");
282
283     if (Verbose) {
284         fprintf (stderr, "Line: %s\n", line);
285     }
286     exit (EXIT_FAILURE);
287 }
288
289
290
291 void Internal (char* Format, ...)
292 /* Print a message about an internal compiler error and die. */
293 {
294     va_list ap;
295
296     fprintf (stderr, "%s(%u): Internal compiler error:\n",
297              GetCurrentFile(), curpos);
298
299     va_start (ap, Format);
300     vfprintf (stderr, Format, ap);
301     va_end (ap);
302     fprintf (stderr, "\nLine: %s\n", line);
303
304     /* Use abort to create a core dump */
305     abort ();
306 }
307
308
309
310 void ErrorReport (void)
311 /* Report errors (called at end of compile) */
312 {
313     if (ErrorCount == 0 && Verbose) {
314         printf ("No errors.\n");
315     }
316 }
317
318
319
320