]> git.sur5r.net Git - cc65/blob - src/ca65/error.c
a81afc07c8f83a013fb1eb0f576a5b106876af82
[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 line",
177         "Unexpected `%s'",
178         "Division by zero",
179         "Modulo operation with zero",
180         "Range error",
181         "Too many macro parameters",
182         "Macro parameter expected",
183         "Circular reference in symbol definition",
184         "Symbol redeclaration mismatch",
185         "Alignment value must be a power of 2",
186         "Duplicate `.ELSE'",
187         "Conditional assembly branch was never closed",
188         "Lexical level was not terminated correctly",
189         "Segment attribute mismatch",
190         "CPU not supported",
191         "Counter underflow",
192         "Undefined label",
193         "Open `%s´",
194     };
195
196     fprintf (stderr, "%s(%lu): Error #%u: ",
197              GetFileName (Pos->Name), Pos->Line, ErrNum);
198     vfprintf (stderr, Msgs [ErrNum-1], ap);
199     fprintf (stderr, "\n");
200     ++ErrorCount;
201 }
202
203
204
205 void Error (unsigned ErrNum, ...)
206 /* Print an error message */
207 {
208     va_list ap;
209     va_start (ap, ErrNum);
210     ErrorMsg (&CurPos, ErrNum, ap);
211     va_end (ap);
212 }
213
214
215
216 void PError (const FilePos* Pos, unsigned ErrNum, ...)
217 /* Print an error message giving an explicit file and position. */
218 {
219     va_list ap;
220     va_start (ap, ErrNum);
221     ErrorMsg (Pos, ErrNum, ap);
222     va_end (ap);
223 }
224
225
226
227 void ErrorSkip (unsigned ErrNum, ...)
228 /* Print an error message and skip the rest of the line */
229 {
230     va_list ap;
231     va_start (ap, ErrNum);
232     ErrorMsg (&CurPos, ErrNum, ap);
233     va_end (ap);
234
235     SkipUntilSep ();
236 }
237
238
239
240 /*****************************************************************************/
241 /*                                   Code                                    */
242 /*****************************************************************************/
243
244
245
246 void Fatal (unsigned FatNum, ...)
247 /* Print a message about a fatal error and die */
248 {
249     static const char* Msgs [FAT_COUNT-1] = {
250         "Maximum number of input files reached",
251         "Out of memory",
252         "Too many segments",
253         "String too long",
254         "Cannot open input file `%s': %s",
255         "Cannot stat input file `%s': %s",
256         "Cannot open output file `%s': %s",
257         "Cannot write to output file `%s': %s",
258         "Cannot open listing file: %s",
259         "Cannot write to listing file: %s",
260         "Cannot read from listing file: %s",
261         "Too many nested constructs",
262         "Too many symbols",
263     };
264     va_list ap;
265
266     va_start (ap, FatNum);
267     fprintf (stderr, "Fatal #%u: ", FatNum);
268     vfprintf (stderr, Msgs [FatNum-1], ap);
269     fprintf (stderr, "\n");
270     va_end (ap);
271
272     /* And die... */
273     exit (EXIT_FAILURE);
274 }
275
276
277
278 void Internal (const char* Format, ...)
279 /* Print a message about an internal compiler error and die. */
280 {
281     va_list ap;
282     va_start (ap, Format);
283     fprintf (stderr, "Internal assembler error\n");
284     vfprintf (stderr, Format, ap);
285     va_end (ap);
286     fprintf (stderr, "\n");
287
288     exit (EXIT_FAILURE);
289 }
290
291
292