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