]> git.sur5r.net Git - cc65/blob - src/ca65/error.c
This commit was generated by cvs2svn to compensate for changes in r2,
[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 "scanner.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     };
194
195     fprintf (stderr, "%s(%lu): Error #%u: ",
196              GetFileName (Pos->Name), Pos->Line, ErrNum);
197     vfprintf (stderr, Msgs [ErrNum-1], ap);
198     fprintf (stderr, "\n");
199     ++ErrorCount;
200 }
201
202
203
204 void Error (unsigned ErrNum, ...)
205 /* Print an error message */
206 {
207     va_list ap;
208     va_start (ap, ErrNum);
209     ErrorMsg (&CurPos, ErrNum, ap);
210     va_end (ap);
211 }
212
213
214
215 void PError (const FilePos* Pos, unsigned ErrNum, ...)
216 /* Print an error message giving an explicit file and position. */
217 {
218     va_list ap;
219     va_start (ap, ErrNum);
220     ErrorMsg (Pos, ErrNum, ap);
221     va_end (ap);
222 }
223
224
225
226 void ErrorSkip (unsigned ErrNum, ...)
227 /* Print an error message and skip the rest of the line */
228 {
229     va_list ap;
230     va_start (ap, ErrNum);
231     ErrorMsg (&CurPos, ErrNum, ap);
232     va_end (ap);
233
234     SkipUntilSep ();
235 }
236
237
238
239 /*****************************************************************************/
240 /*                                   Code                                    */
241 /*****************************************************************************/
242
243
244
245 void Fatal (unsigned FatNum, ...)
246 /* Print a message about a fatal error and die */
247 {
248     static const char* Msgs [FAT_COUNT-1] = {
249         "Maximum number of input files reached",
250         "Out of memory",
251         "Too many segments",
252         "String too long",
253         "Cannot open input file `%s': %s",
254         "Cannot stat input file `%s': %s",
255         "Cannot open output file `%s': %s",
256         "Cannot write to output file `%s': %s",
257         "Cannot open listing file: %s",
258         "Cannot write to listing file: %s",
259         "Cannot read from listing file: %s",
260         "Macro nesting too deep",
261         "Too many symbols",
262     };
263     va_list ap;
264
265     va_start (ap, FatNum);
266     fprintf (stderr, "Fatal #%u: ", FatNum);
267     vfprintf (stderr, Msgs [FatNum-1], ap);
268     fprintf (stderr, "\n");
269     va_end (ap);
270
271     /* And die... */
272     exit (EXIT_FAILURE);
273 }
274
275
276
277 void Internal (const char* Format, ...)
278 /* Print a message about an internal compiler error and die. */
279 {
280     va_list ap;
281     va_start (ap, Format);
282     fprintf (stderr, "Internal assembler error\n");
283     vfprintf (stderr, Format, ap);
284     va_end (ap);
285     fprintf (stderr, "\n");
286
287     exit (EXIT_FAILURE);
288 }
289
290
291