]> git.sur5r.net Git - cc65/blob - src/ca65/error.c
No need to search for the correct line info entry, the source position is
[cc65] / src / ca65 / error.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  error.c                                  */
4 /*                                                                           */
5 /*                Error handling for the ca65 macroassembler                 */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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 /* common */
41 #include "strbuf.h"
42
43 /* ca65 */
44 #include "error.h"
45 #include "filetab.h"
46 #include "lineinfo.h"
47 #include "nexttok.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* Warning level */
58 unsigned WarnLevel      = 1;
59
60 /* Statistics */
61 unsigned ErrorCount     = 0;
62 unsigned WarningCount   = 0;
63
64
65
66 /*****************************************************************************/
67 /*                                 Warnings                                  */
68 /*****************************************************************************/
69
70
71
72 void WarningMsg (const FilePos* Pos, unsigned Level, const char* Format, va_list ap)
73 /* Print warning message. */
74 {
75     if (Level <= WarnLevel) {
76
77         StrBuf S = STATIC_STRBUF_INITIALIZER;
78         SB_VPrintf (&S, Format, ap);
79         SB_Terminate (&S);
80
81         fprintf (stderr, "%s(%lu): Warning: %s\n",
82                  SB_GetConstBuf (GetFileName (Pos->Name)),
83                  Pos->Line,
84                  SB_GetConstBuf (&S));
85         ++WarningCount;
86
87         SB_Done (&S);
88     }
89 }
90
91
92
93 void Warning (unsigned Level, const char* Format, ...)
94 /* Print warning message. */
95 {
96     va_list ap;
97     va_start (ap, Format);
98     WarningMsg (&CurTok.Pos, Level, Format, ap);
99     va_end (ap);
100 }
101
102
103
104 void PWarning (const FilePos* Pos, unsigned Level, const char* Format, ...)
105 /* Print warning message giving an explicit file and position. */
106 {
107     va_list ap;
108     va_start (ap, Format);
109     WarningMsg (Pos, Level, Format, ap);
110     va_end (ap);
111 }
112
113
114
115 void LIWarning (const Collection* LineInfos, unsigned Level, const char* Format, ...)
116 /* Print warning message using the given line infos */
117 {
118     va_list ap;
119
120     /* The first entry in the collection is that of the actual source pos */
121     const LineInfo* LI = CollConstAt (LineInfos, 0);
122
123     /* Output a warning for this position */
124     va_start (ap, Format);
125     WarningMsg (&LI->Pos, Level, Format, ap);
126     va_end (ap);
127 }
128
129
130
131 /*****************************************************************************/
132 /*                                  Errors                                   */
133 /*****************************************************************************/
134
135
136
137 void ErrorMsg (const FilePos* Pos, const char* Format, va_list ap)
138 /* Print an error message */
139 {
140     StrBuf S = STATIC_STRBUF_INITIALIZER;
141     SB_VPrintf (&S, Format, ap);
142     SB_Terminate (&S);
143
144     fprintf (stderr, "%s(%lu): Error: %s\n",
145              SB_GetConstBuf (GetFileName (Pos->Name)),
146              Pos->Line,
147              SB_GetConstBuf (&S));
148     ++ErrorCount;
149
150     SB_Done (&S);
151 }
152
153
154
155 void Error (const char* Format, ...)
156 /* Print an error message */
157 {
158     va_list ap;
159     va_start (ap, Format);
160     ErrorMsg (&CurTok.Pos, Format, ap);
161     va_end (ap);
162 }
163
164
165
166 void PError (const FilePos* Pos, const char* Format, ...)
167 /* Print an error message giving an explicit file and position. */
168 {
169     va_list ap;
170     va_start (ap, Format);
171     ErrorMsg (Pos, Format, ap);
172     va_end (ap);
173 }
174
175
176
177 void LIError (const Collection* LineInfos, const char* Format, ...)
178 /* Print an error message using the given line infos. */
179 {
180     va_list ap;
181
182     /* The first entry in the collection is that of the actual source pos */
183     const LineInfo* LI = CollConstAt (LineInfos, 0);
184
185     /* Output an error for this position */
186     va_start (ap, Format);
187     ErrorMsg (&LI->Pos, Format, ap);
188     va_end (ap);
189 }
190
191
192
193 void ErrorSkip (const char* Format, ...)
194 /* Print an error message and skip the rest of the line */
195 {
196     va_list ap;
197     va_start (ap, Format);
198     ErrorMsg (&CurTok.Pos, Format, ap);
199     va_end (ap);
200
201     SkipUntilSep ();
202 }
203
204
205
206 /*****************************************************************************/
207 /*                                   Code                                    */
208 /*****************************************************************************/
209
210
211
212 void Fatal (const char* Format, ...)
213 /* Print a message about a fatal error and die */
214 {
215     va_list ap;
216     StrBuf S = STATIC_STRBUF_INITIALIZER;
217
218     va_start (ap, Format);
219     SB_VPrintf (&S, Format, ap);
220     SB_Terminate (&S);
221     va_end (ap);
222
223     fprintf (stderr, "Fatal error: %s\n", SB_GetConstBuf (&S));
224
225     SB_Done (&S);
226
227     /* And die... */
228     exit (EXIT_FAILURE);
229 }
230
231
232
233 void Internal (const char* Format, ...)
234 /* Print a message about an internal assembler error and die. */
235 {
236     va_list ap;
237     StrBuf S = STATIC_STRBUF_INITIALIZER;
238
239     va_start (ap, Format);
240     SB_VPrintf (&S, Format, ap);
241     SB_Terminate (&S);
242     va_end (ap);
243
244     fprintf (stderr, "Internal assembler error: %s\n", SB_GetConstBuf (&S));
245
246     SB_Done (&S);
247
248     exit (EXIT_FAILURE);
249 }
250
251
252