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