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