]> git.sur5r.net Git - cc65/blob - src/ca65/error.c
89616206aad29e3c43c3833d72be3aa43ba5ffe3
[cc65] / src / ca65 / error.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  error.c                                  */
4 /*                                                                           */
5 /*                Error handling for the ca65 macroassembler                 */
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 "nexttok.h"
41 #include "error.h"
42
43
44
45 /*****************************************************************************/
46 /*                                   Data                                    */
47 /*****************************************************************************/
48
49
50
51 /* Warning level */
52 unsigned WarnLevel          = 1;
53
54 /* Messages for internal compiler errors */
55 const char _MsgCheckFailed [] =
56     "Check failed: `%s' (= %d), file `%s', line %u\n";
57 const char _MsgPrecondition [] =
58     "Precondition violated: `%s' (= %d), file `%s', line %u\n";
59 const char _MsgFail [] =
60     "%s, file `%s', line %u\n";
61
62
63 /* Statistics */
64 unsigned ErrorCount     = 0;
65 unsigned WarningCount   = 0;
66
67
68
69 /*****************************************************************************/
70 /*                                 Warnings                                  */
71 /*****************************************************************************/
72
73
74
75 void WarningMsg (const FilePos* Pos, unsigned WarnNum, va_list ap)
76 /* Print warning message. */
77 {
78     static const struct {
79         unsigned char   Level;
80         const char*     Msg;
81     } Warnings [WARN_COUNT-1] = {
82         {   1,  "Mask error"                                    },
83         {   2,  "Symbol `%s' is defined but never used"         },
84         {   2,  "Symbol `%s' is imported but never used"        },
85         {   1,  "Cannot track processor status byte"            },
86     };
87
88     if (Warnings [WarnNum-1].Level <= WarnLevel) {
89         fprintf (stderr, "%s(%lu): Warning #%u: ",
90                  GetFileName (Pos->Name), Pos->Line, WarnNum);
91         vfprintf (stderr, Warnings [WarnNum-1].Msg, ap);
92         fprintf (stderr, "\n");
93         ++WarningCount;
94     }
95 }
96
97
98
99 void Warning (unsigned WarnNum, ...)
100 /* Print warning message. */
101 {
102     va_list ap;
103     va_start (ap, WarnNum);
104     WarningMsg (&CurPos, WarnNum, ap);
105     va_end (ap);
106 }
107
108
109
110 void PWarning (const FilePos* Pos, unsigned WarnNum, ...)
111 /* Print warning message giving an explicit file and position. */
112 {
113     va_list ap;
114     va_start (ap, WarnNum);
115     WarningMsg (Pos, WarnNum, ap);
116     va_end (ap);
117 }
118
119
120
121 /*****************************************************************************/
122 /*                                  Errors                                   */
123 /*****************************************************************************/
124
125
126
127 void ErrorMsg (const FilePos* Pos, unsigned ErrNum, va_list ap)
128 /* Print an error message */
129 {
130     static const char* Msgs [ERR_COUNT-1] = {
131         "Command/operation not implemented",
132         "Cannot open include file `%s': %s",
133         "Include nesting too deep",
134         "Invalid input character: %02X",
135         "Hex digit expected",
136         "Digit expected",
137         "`0' or `1' expected",
138         "Numerical overflow",
139         "Control statement expected",
140         "Too many characters",
141         "`:' expected",
142         "`(' expected",
143         "`)' expected",
144         "`]' expected",
145         "`,' expected",
146         "Boolean switch value expected (on/off/+/-)",
147         "`Y' expected",
148         "`X' expected",
149         "Integer constant expected",
150         "String constant expected",
151         "Character constant expected",
152         "Constant expression expected",
153         "Identifier expected",
154         "`.endmacro' expected",
155         "Option key expected",
156         "Command is only valid in 65816 mode",
157         "User error: %s",
158         "String constant too long",
159         "Newline in string constant",
160         "Illegal character constant",
161         "Illegal addressing mode",
162         "Illegal character to start local symbols",
163         "Illegal use of local symbol",
164         "Illegal segment name: `%s'",
165         "Illegal segment attribute",
166         "Illegal macro package name",
167         "Illegal emulation feature",
168         "Syntax error",
169         "Symbol `%s' is already defined",
170         "Undefined symbol `%s'",
171         "Symbol `%s' is marked as import",
172         "Symbol `%s' is marked as export",
173         "Exported symbol `%s' is undefined",
174         "Exported values must be constant",
175         ".IF nesting too deep",
176         "Unexpected end of file",
177         "Unexpected end of line",
178         "Unexpected `%s'",
179         "Division by zero",
180         "Modulo operation with zero",
181         "Range error",
182         "Too many macro parameters",
183         "Macro parameter expected",
184         "Circular reference in symbol definition",
185         "Symbol redeclaration mismatch",
186         "Alignment value must be a power of 2",
187         "Duplicate `.ELSE'",
188         "Conditional assembly branch was never closed",
189         "Lexical level was not terminated correctly",
190         "Segment attribute mismatch",
191         "CPU not supported",
192         "Counter underflow",
193         "Undefined label",
194         "Open `%s´",
195     };
196
197     fprintf (stderr, "%s(%lu): Error #%u: ",
198              GetFileName (Pos->Name), Pos->Line, ErrNum);
199     vfprintf (stderr, Msgs [ErrNum-1], ap);
200     fprintf (stderr, "\n");
201     ++ErrorCount;
202 }
203
204
205
206 void Error (unsigned ErrNum, ...)
207 /* Print an error message */
208 {
209     va_list ap;
210     va_start (ap, ErrNum);
211     ErrorMsg (&CurPos, ErrNum, ap);
212     va_end (ap);
213 }
214
215
216
217 void PError (const FilePos* Pos, unsigned ErrNum, ...)
218 /* Print an error message giving an explicit file and position. */
219 {
220     va_list ap;
221     va_start (ap, ErrNum);
222     ErrorMsg (Pos, ErrNum, ap);
223     va_end (ap);
224 }
225
226
227
228 void ErrorSkip (unsigned ErrNum, ...)
229 /* Print an error message and skip the rest of the line */
230 {
231     va_list ap;
232     va_start (ap, ErrNum);
233     ErrorMsg (&CurPos, ErrNum, ap);
234     va_end (ap);
235
236     SkipUntilSep ();
237 }
238
239
240
241 /*****************************************************************************/
242 /*                                   Code                                    */
243 /*****************************************************************************/
244
245
246
247 void Fatal (unsigned FatNum, ...)
248 /* Print a message about a fatal error and die */
249 {
250     static const char* Msgs [FAT_COUNT-1] = {
251         "Maximum number of input files reached",
252         "Out of memory",
253         "Too many segments",
254         "String too long",
255         "Cannot open input file `%s': %s",
256         "Cannot stat input file `%s': %s",
257         "Cannot open output file `%s': %s",
258         "Cannot write to output file `%s': %s",
259         "Cannot open listing file: %s",
260         "Cannot write to listing file: %s",
261         "Cannot read from listing file: %s",
262         "Too many nested constructs",
263         "Too many symbols",
264     };
265     va_list ap;
266
267     va_start (ap, FatNum);
268     fprintf (stderr, "Fatal #%u: ", FatNum);
269     vfprintf (stderr, Msgs [FatNum-1], ap);
270     fprintf (stderr, "\n");
271     va_end (ap);
272
273     /* And die... */
274     exit (EXIT_FAILURE);
275 }
276
277
278
279 void Internal (const char* Format, ...)
280 /* Print a message about an internal compiler error and die. */
281 {
282     va_list ap;
283     va_start (ap, Format);
284     fprintf (stderr, "Internal assembler error\n");
285     vfprintf (stderr, Format, ap);
286     va_end (ap);
287     fprintf (stderr, "\n");
288
289     exit (EXIT_FAILURE);
290 }
291
292
293