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