1 /*****************************************************************************/
5 /* Error handling for the ca65 macroassembler */
9 /* (C) 1998-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
58 unsigned WarnLevel = 1;
61 unsigned ErrorCount = 0;
62 unsigned WarningCount = 0;
66 /*****************************************************************************/
67 /* Helper functions */
68 /*****************************************************************************/
72 static void FormatVMsg (StrBuf* S, const FilePos* Pos, const char* Desc,
73 const char* Format, va_list ap)
74 /* Format an error/warning message into S. A trailing newline and a NUL
75 * terminator will be added to S.
78 /* Format the actual message */
79 StrBuf Msg = STATIC_STRBUF_INITIALIZER;
80 SB_VPrintf (&Msg, Format, ap);
83 /* Format the message header */
84 SB_Printf (S, "%s(%lu): %s: ",
85 SB_GetConstBuf (GetFileName (Pos->Name)),
89 /* Append the message to the message header */
92 /* Delete the formatted message */
95 /* Add a new line and terminate the generated message */
96 SB_AppendChar (S, '\n');
102 static void FormatMsg (StrBuf* S, const FilePos* Pos, const char* Desc,
103 const char* Format, ...)
104 /* Format an error/warning message into S. A trailing newline and a NUL
105 * terminator will be added to S.
109 va_start (ap, Format);
110 FormatVMsg (S, Pos, Desc, Format, ap);
116 static void AddNotifications (const Collection* LineInfos)
117 /* Output additional notifications for an error or warning */
119 StrBuf Msg = STATIC_STRBUF_INITIALIZER;
121 /* The basic line info is always in slot zero. It has been used to
122 * output the actual error or warning. The following slots may contain
123 * more information. Check them and additional notifications if they're
127 for (I = 1; I < CollCount (LineInfos); ++I) {
128 /* Get next line info */
129 const LineInfo* LI = CollConstAt (LineInfos, I);
130 /* Check the type and output an appropriate note */
131 unsigned Type = GetLineInfoType (LI);
132 if (Type == LI_TYPE_EXT) {
133 FormatMsg (&Msg, GetSourcePos (LI), "Note",
134 "Assembler code generated from this line");
135 fputs (SB_GetConstBuf (&Msg), stderr);
137 } else if (Type == LI_TYPE_MACRO) {
147 /*****************************************************************************/
149 /*****************************************************************************/
153 void WarningMsg (const FilePos* Pos, unsigned Level, const char* Format, va_list ap)
154 /* Print warning message. */
156 if (Level <= WarnLevel) {
158 StrBuf Msg = STATIC_STRBUF_INITIALIZER;
159 FormatVMsg (&Msg, Pos, "Warning", Format, ap);
160 fputs (SB_GetConstBuf (&Msg), stderr);
169 void Warning (unsigned Level, const char* Format, ...)
170 /* Print warning message. */
173 va_start (ap, Format);
174 WarningMsg (&CurTok.Pos, Level, Format, ap);
180 void PWarning (const FilePos* Pos, unsigned Level, const char* Format, ...)
181 /* Print warning message giving an explicit file and position. */
184 va_start (ap, Format);
185 WarningMsg (Pos, Level, Format, ap);
191 void LIWarning (const Collection* LineInfos, unsigned Level, const char* Format, ...)
192 /* Print warning message using the given line infos */
194 if (Level <= WarnLevel) {
198 /* The first entry in the collection is that of the actual source pos */
199 const LineInfo* LI = CollConstAt (LineInfos, 0);
201 /* Output a warning for this position */
202 va_start (ap, Format);
203 WarningMsg (GetSourcePos (LI), Level, Format, ap);
206 /* Add additional notifications if necessary */
207 AddNotifications (LineInfos);
213 /*****************************************************************************/
215 /*****************************************************************************/
219 void ErrorMsg (const FilePos* Pos, const char* Format, va_list ap)
220 /* Print an error message */
222 StrBuf Msg = STATIC_STRBUF_INITIALIZER;
223 FormatVMsg (&Msg, Pos, "Error", Format, ap);
224 fputs (SB_GetConstBuf (&Msg), stderr);
232 void Error (const char* Format, ...)
233 /* Print an error message */
236 va_start (ap, Format);
237 ErrorMsg (&CurTok.Pos, Format, ap);
243 void PError (const FilePos* Pos, const char* Format, ...)
244 /* Print an error message giving an explicit file and position. */
247 va_start (ap, Format);
248 ErrorMsg (Pos, Format, ap);
254 void LIError (const Collection* LineInfos, const char* Format, ...)
255 /* Print an error message using the given line infos. */
259 /* The first entry in the collection is that of the actual source pos */
260 const LineInfo* LI = CollConstAt (LineInfos, 0);
262 /* Output an error for this position */
263 va_start (ap, Format);
264 ErrorMsg (GetSourcePos (LI), Format, ap);
267 /* Add additional notifications if necessary */
268 AddNotifications (LineInfos);
273 void ErrorSkip (const char* Format, ...)
274 /* Print an error message and skip the rest of the line */
277 va_start (ap, Format);
278 ErrorMsg (&CurTok.Pos, Format, ap);
286 /*****************************************************************************/
288 /*****************************************************************************/
292 void Fatal (const char* Format, ...)
293 /* Print a message about a fatal error and die */
296 StrBuf S = STATIC_STRBUF_INITIALIZER;
298 va_start (ap, Format);
299 SB_VPrintf (&S, Format, ap);
303 fprintf (stderr, "Fatal error: %s\n", SB_GetConstBuf (&S));
313 void Internal (const char* Format, ...)
314 /* Print a message about an internal assembler error and die. */
317 StrBuf S = STATIC_STRBUF_INITIALIZER;
319 va_start (ap, Format);
320 SB_VPrintf (&S, Format, ap);
324 fprintf (stderr, "Internal assembler error: %s\n", SB_GetConstBuf (&S));