]> git.sur5r.net Git - cc65/blob - src/cc65/error.c
Fix problem with OptPtrLoad1 - never insert before the deleted code, always
[cc65] / src / cc65 / error.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  error.c                                  */
4 /*                                                                           */
5 /*                  Error handling for the cc65 C compiler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2004 Ullrich von Bassewitz                                       */
10 /*               Römerstraße 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 "print.h"
42
43 /* cc65 */
44 #include "global.h"
45 #include "input.h"
46 #include "lineinfo.h"
47 #include "scanner.h"
48 #include "stmt.h"
49 #include "error.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 /* Count of errors/warnings */
60 unsigned ErrorCount     = 0;
61 unsigned WarningCount   = 0;
62
63
64
65 /*****************************************************************************/
66 /*                                   Code                                    */
67 /*****************************************************************************/
68
69
70
71 static void IntWarning (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
72 /* Print warning message - internal function. */
73 {
74     if (!IS_Get (&WarnDisable)) {
75         fprintf (stderr, "%s(%u): Warning: ", Filename, LineNo);
76         vfprintf (stderr, Msg, ap);
77         fprintf (stderr, "\n");
78
79         if (Line) {
80             Print (stderr, 1, "Input: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
81         }
82         ++WarningCount;
83     }
84 }
85
86
87
88 void Warning (const char* Format, ...)
89 /* Print warning message. */
90 {
91     va_list ap;
92     va_start (ap, Format);
93     IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
94     va_end (ap);
95 }
96
97
98
99 void LIWarning (const LineInfo* LI, const char* Format, ...)
100 /* Print a warning message with the line info given explicitly */
101 {
102     va_list ap;
103     va_start (ap, Format);
104     IntWarning (GetInputName (LI), GetInputLine (LI), Format, ap);
105     va_end (ap);
106 }
107
108
109
110 void PPWarning (const char* Format, ...)
111 /* Print warning message. For use within the preprocessor. */
112 {
113     va_list ap;
114     va_start (ap, Format);
115     IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
116     va_end (ap);
117 }
118
119
120
121 static void IntError (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
122 /* Print an error message - internal function*/
123 {
124     fprintf (stderr, "%s(%u): Error: ", Filename, LineNo);
125     vfprintf (stderr, Msg, ap);
126     fprintf (stderr, "\n");
127
128     if (Line) {
129         Print (stderr, 1, "Input: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
130     }
131     ++ErrorCount;
132     if (ErrorCount > 10) {
133         Fatal ("Too many errors");
134     }
135 }
136
137
138
139 void Error (const char* Format, ...)
140 /* Print an error message */
141 {
142     va_list ap;
143     va_start (ap, Format);
144     IntError (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
145     va_end (ap);
146 }
147
148
149
150 void LIError (const LineInfo* LI, const char* Format, ...)
151 /* Print an error message with the line info given explicitly */
152 {
153     va_list ap;
154     va_start (ap, Format);
155     IntError (GetInputName (LI), GetInputLine (LI), Format, ap);
156     va_end (ap);
157 }
158
159
160
161 void PPError (const char* Format, ...)
162 /* Print an error message. For use within the preprocessor.  */
163 {
164     va_list ap;
165     va_start (ap, Format);
166     IntError (GetCurrentFile(), GetCurrentLine(), Format, ap);
167     va_end (ap);
168 }
169
170
171
172 void Fatal (const char* Format, ...)
173 /* Print a message about a fatal error and die */
174 {
175     va_list ap;
176
177     const char* FileName;
178     unsigned    LineNum;
179     if (CurTok.LI) {
180         FileName = GetInputName (CurTok.LI);
181         LineNum  = GetInputLine (CurTok.LI);
182     } else {
183         FileName = GetCurrentFile ();
184         LineNum  = GetCurrentLine ();
185     }
186
187     fprintf (stderr, "%s(%u): Fatal: ", FileName, LineNum);
188
189     va_start (ap, Format);
190     vfprintf (stderr, Format, ap);
191     va_end (ap);
192     fprintf (stderr, "\n");
193
194     if (Line) {
195         Print (stderr, 1, "Input: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
196     }
197     exit (EXIT_FAILURE);
198 }
199
200
201
202 void Internal (const char* Format, ...)
203 /* Print a message about an internal compiler error and die. */
204 {
205     va_list ap;
206
207     const char* FileName;
208     unsigned    LineNum;
209     if (CurTok.LI) {
210         FileName = GetInputName (CurTok.LI);
211         LineNum  = GetInputLine (CurTok.LI);
212     } else {
213         FileName = GetCurrentFile ();
214         LineNum  = GetCurrentLine ();
215     }
216
217     fprintf (stderr, "%s(%u): Internal compiler error:\n",
218              FileName, LineNum);
219
220     va_start (ap, Format);
221     vfprintf (stderr, Format, ap);
222     va_end (ap);
223     fprintf (stderr, "\n");
224
225     if (Line) {
226         fprintf (stderr, "\nInput: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
227     }
228
229     /* Use abort to create a core dump */
230     abort ();
231 }
232
233
234
235 void ErrorReport (void)
236 /* Report errors (called at end of compile) */
237 {
238     Print (stdout, 1, "%u errors, %u warnings\n", ErrorCount, WarningCount);
239 }
240
241
242