]> git.sur5r.net Git - cc65/blob - src/ca65/error.c
fc984268bf60d373234d77d001698d4704e06d83
[cc65] / src / ca65 / error.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  error.c                                  */
4 /*                                                                           */
5 /*                Error handling for the ca65 macroassembler                 */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
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 /* ca65 */
41 #include "filetab.h"
42 #include "nexttok.h"
43 #include "error.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Data                                    */
49 /*****************************************************************************/
50
51
52
53 /* Warning level */
54 unsigned WarnLevel          = 1;
55
56 /* Statistics */
57 unsigned ErrorCount     = 0;
58 unsigned WarningCount   = 0;
59
60
61
62 /*****************************************************************************/
63 /*                                 Warnings                                  */
64 /*****************************************************************************/
65
66
67
68 void WarningMsg (const FilePos* Pos, unsigned WarnNum, va_list ap)
69 /* Print warning message. */
70 {
71     static const struct {
72         unsigned char   Level;
73         const char*     Msg;
74     } Warnings [WARN_COUNT-1] = {
75         {   2,  "Symbol `%s' is defined but never used"         },
76         {   2,  "Symbol `%s' is imported but never used"        },
77         {   1,  "Cannot track processor status byte"            },
78         {   0,  "User warning: %s"                              },
79     };
80
81     if (Warnings [WarnNum-1].Level <= WarnLevel) {
82         fprintf (stderr, "%s(%lu): Warning #%u: ",
83                  GetFileName (Pos->Name), Pos->Line, WarnNum);
84         vfprintf (stderr, Warnings [WarnNum-1].Msg, ap);
85         fprintf (stderr, "\n");
86         ++WarningCount;
87     }
88 }
89
90
91
92 void Warning (unsigned WarnNum, ...)
93 /* Print warning message. */
94 {
95     va_list ap;
96     va_start (ap, WarnNum);
97     WarningMsg (&CurPos, WarnNum, ap);
98     va_end (ap);
99 }
100
101
102
103 void PWarning (const FilePos* Pos, unsigned WarnNum, ...)
104 /* Print warning message giving an explicit file and position. */
105 {
106     va_list ap;
107     va_start (ap, WarnNum);
108     WarningMsg (Pos, WarnNum, ap);
109     va_end (ap);
110 }
111
112
113
114 /*****************************************************************************/
115 /*                                  Errors                                   */
116 /*****************************************************************************/
117
118
119
120 void ErrorMsg (const FilePos* Pos, unsigned ErrNum, va_list ap)
121 /* Print an error message */
122 {
123     static const char* Msgs [ERR_COUNT-1] = {
124         "Command/operation not implemented",
125         "Cannot open include file `%s': %s",
126         "Cannot read from include file `%s': %s",
127         "Include nesting too deep",
128         "Invalid input character: %02X",
129         "Hex digit expected",
130         "Digit expected",
131         "`0' or `1' expected",
132         "Numerical overflow",
133         "Control statement expected",
134         "Too many characters",
135         "`:' expected",
136         "`(' expected",
137         "`)' expected",
138         "`]' expected",
139         "`,' expected",
140         "Boolean switch value expected (on/off/+/-)",
141         "`Y' expected",
142         "`X' expected",
143         "Integer constant expected",
144         "String constant expected",
145         "Character constant expected",
146         "Constant expression expected",
147         "Identifier expected",
148         "`.ENDMACRO' expected",
149         "Option key expected",
150         "`=' expected",
151         "Command is only valid in 65816 mode",
152         "User error: %s",
153         "String constant too long",
154         "Newline in string constant",
155         "Illegal character constant",
156         "Illegal addressing mode",
157         "Illegal character to start local symbols",
158         "Illegal use of local symbol",
159         "Illegal segment name: `%s'",
160         "Illegal segment attribute",
161         "Illegal macro package name",
162         "Illegal emulation feature",
163         "Illegal scope specifier",
164         "Syntax error",
165         "Symbol `%s' is already defined",
166         "Undefined symbol `%s'",
167         "Symbol `%s' is already marked as import",
168         "Symbol `%s' is already marked as export",
169         "Exported symbol `%s' is undefined",
170         "Exported values must be constant",
171         "Unexpected end of file",
172         "Unexpected end of line",
173         "Unexpected `%s'",
174         "Division by zero",
175         "Modulo operation with zero",
176         "Range error",
177         "Too many macro parameters",
178         "Macro parameter expected",
179         "Circular reference in symbol definition",
180         "Symbol `%s' redeclaration mismatch",
181         "Alignment value must be a power of 2",
182         "Duplicate `.ELSE'",
183         "Conditional assembly branch was never closed",
184         "Lexical level was not terminated correctly",
185         "No open lexical level",
186         "Segment attribute mismatch",
187         "Segment stack overflow",
188         "Segment stack is empty",
189         "Segment stack is not empty at end of assembly",
190         "CPU not supported",
191         "Counter underflow",
192         "Undefined label",
193         "Open `%s'",
194         "File name `%s' not found in file table",
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         ".IF nesting too deep",
264         "Too many symbols",
265     };
266     va_list ap;
267
268     va_start (ap, FatNum);
269     fprintf (stderr, "Fatal #%u: ", FatNum);
270     vfprintf (stderr, Msgs [FatNum-1], ap);
271     fprintf (stderr, "\n");
272     va_end (ap);
273
274     /* And die... */
275     exit (EXIT_FAILURE);
276 }
277
278
279
280 void Internal (const char* Format, ...)
281 /* Print a message about an internal compiler error and die. */
282 {
283     va_list ap;
284     va_start (ap, Format);
285     fprintf (stderr, "Internal assembler error\n");
286     vfprintf (stderr, Format, ap);
287     va_end (ap);
288     fprintf (stderr, "\n");
289
290     exit (EXIT_FAILURE);
291 }
292
293
294