]> git.sur5r.net Git - cc65/blob - src/cc65/error.c
95db80a45b140e081be74082d8697d53f2034142
[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 /* Error messages sorted by ErrTypes */
55 static char* ErrMsg [ERR_COUNT-1] = {
56     "Syntax error",
57     "`\"' expected",
58     "`:' expected",
59     "`;' expected",
60     "`,' expected",
61     "`(' expected",
62     "`)' expected",
63     "`[' expected",
64     "`]' expected",
65     "`{' expected",
66     "`}' expected",
67     "Identifier expected",
68     "Type expected",
69     "Incompatible types",
70     "Incompatible pointer types",
71     "Too many arguments in function call",
72     "Too few arguments in function call",
73     "Duplicate macro parameter: %s",
74     "Variable identifier expected",
75     "Integer expression expected",
76     "Constant expression expected",
77     "No active loop",
78     "Redefinition of `%s'",
79     "Conflicting types for `%s'",
80     "String literal expected",
81     "`while' expected",
82     "Function must return a value",
83     "Function cannot return a value",
84     "Unexpected `continue'",
85     "Undefined symbol: `%s'",
86     "Undefined label: `%s'",
87     "Too many local variables",
88     "Too many initializers",
89     "Cannot initialize incomplete type",
90     "Cannot subscript",
91     "Operation not allowed with this type of argument",
92     "Struct expected",
93     "Struct/union has no field named `%s'",
94     "Struct pointer expected",
95     "lvalue expected",
96     "Expression expected",
97     "Preprocessor expression expected",
98     "Illegal type",
99     "Illegal function call",
100     "Illegal indirection",
101     "Illegal address",
102     "Illegal hex digit",
103     "Illegal character constant",
104     "Illegal modifier",
105     "Illegal type qualifier",
106     "Illegal storage class",
107     "Illegal attribute",
108     "Illegal segment name: `%s'",
109     "Division by zero",
110     "Modulo operation with zero",
111     "Range error",
112     "Symbol is already different kind",
113     "Too many lexical levels",
114     "Parameter name omitted",
115     "Old style function decl used as prototype",
116     "Declaration for parameter `%s' but no such parameter",
117     "Cannot take address of a register variable",
118     "Illegal size of data type",
119     "__fastcall__ is not allowed for C functions",
120     "Variable has unknown size",
121     "Unknown identifier: `%s'",
122     "Duplicate qualifier: `%s'",
123     "Assignment to const",
124     "Pointer types differ in type qualifiers",
125 };
126
127
128
129 /* Count of errors/warnings */
130 unsigned ErrorCount     = 0;
131 unsigned WarningCount   = 0;
132
133
134
135 /*****************************************************************************/
136 /*                                   Code                                    */
137 /*****************************************************************************/
138
139
140
141 static void IntWarning (const char* Filename, unsigned Line, const char* Msg, va_list ap)
142 /* Print warning message - internal function. */
143 {
144     if (!NoWarn) {
145         fprintf (stderr, "%s(%u): Warning: ", Filename, Line);
146         vfprintf (stderr, Msg, ap);
147         fprintf (stderr, "\n");
148
149         if (Verbose) {
150             fprintf (stderr, "Line: %s\n", line);
151         }
152         ++WarningCount;
153     }
154 }
155
156
157
158 void Warning (const char* Format, ...)
159 /* Print warning message. */
160 {
161     va_list ap;
162     va_start (ap, Format);
163     IntWarning (GetCurrentFile(), curpos, Format, ap);
164     va_end (ap);
165 }
166
167
168
169 void PPWarning (const char* Format, ...)
170 /* Print warning message. For use within the preprocessor. */
171 {
172     va_list ap;
173     va_start (ap, Format);
174     IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
175     va_end (ap);
176 }
177
178
179
180 static void IntError (const char* Filename, unsigned Line, const char* Msg, va_list ap)
181 /* Print an error message - internal function*/
182 {
183     fprintf (stderr, "%s(%u): Error: ", Filename, Line);
184     vfprintf (stderr, Msg, ap);
185     fprintf (stderr, "\n");
186
187     if (Verbose) {
188         fprintf (stderr, "Line: %s\n", line);
189     }
190     ++ErrorCount;
191     if (ErrorCount > 10) {
192         Fatal ("Too many errors");
193     }
194 }
195
196
197
198 void Error (unsigned ErrNum, ...)
199 /* Print an error message */
200 {
201     va_list ap;
202     va_start (ap, ErrNum);
203     IntError (GetCurrentFile(), curpos, ErrMsg [ErrNum-1], ap);
204     va_end (ap);
205 }
206
207
208
209 void MError (const char* Format, ...)
210 /* Print an error message */
211 {
212     va_list ap;
213     va_start (ap, Format);
214     IntError (GetCurrentFile(), curpos, Format, ap);
215     va_end (ap);
216 }
217
218
219
220 void PPError (const char* Format, ...)
221 /* Print an error message. For use within the preprocessor.  */
222 {
223     va_list ap;
224     va_start (ap, Format);
225     IntError (GetCurrentFile(), GetCurrentLine(), Format, ap);
226     va_end (ap);
227 }
228
229
230
231 void Fatal (const char* Format, ...)
232 /* Print a message about a fatal error and die */
233 {
234     va_list ap;
235
236     fprintf (stderr, "%s(%u): Fatal: ", GetCurrentFile(), curpos);
237
238     va_start (ap, Format);
239     vfprintf (stderr, Format, ap);
240     va_end (ap);
241     fprintf (stderr, "\n");
242
243     if (Verbose) {
244         fprintf (stderr, "Line: %s\n", line);
245     }
246     exit (EXIT_FAILURE);
247 }
248
249
250
251 void Internal (char* Format, ...)
252 /* Print a message about an internal compiler error and die. */
253 {
254     va_list ap;
255
256     fprintf (stderr, "%s(%u): Internal compiler error:\n",
257              GetCurrentFile(), curpos);
258
259     va_start (ap, Format);
260     vfprintf (stderr, Format, ap);
261     va_end (ap);
262     fprintf (stderr, "\nLine: %s\n", line);
263
264     /* Use abort to create a core dump */
265     abort ();
266 }
267
268
269
270 void ErrorReport (void)
271 /* Report errors (called at end of compile) */
272 {
273     if (ErrorCount == 0 && Verbose) {
274         printf ("No errors.\n");
275     }
276 }
277
278
279
280