]> git.sur5r.net Git - cc65/blob - src/cc65/error.c
44825287e067fb9efcf5daf02a08627988108d5f
[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         Print (stderr, 1, "Input: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
80         ++WarningCount;
81     }
82 }
83
84
85
86 void Warning (const char* Format, ...)
87 /* Print warning message. */
88 {
89     va_list ap;
90     va_start (ap, Format);
91     IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
92     va_end (ap);
93 }
94
95
96
97 void PPWarning (const char* Format, ...)
98 /* Print warning message. For use within the preprocessor. */
99 {
100     va_list ap;
101     va_start (ap, Format);
102     IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
103     va_end (ap);
104 }
105
106
107
108 static void IntError (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
109 /* Print an error message - internal function*/
110 {
111     fprintf (stderr, "%s(%u): Error: ", Filename, LineNo);
112     vfprintf (stderr, Msg, ap);
113     fprintf (stderr, "\n");
114
115     Print (stderr, 1, "Input: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
116     ++ErrorCount;
117     if (ErrorCount > 10) {
118         Fatal ("Too many errors");
119     }
120 }
121
122
123
124 void Error (const char* Format, ...)
125 /* Print an error message */
126 {
127     va_list ap;
128     va_start (ap, Format);
129     IntError (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
130     va_end (ap);
131 }
132
133
134
135 void PPError (const char* Format, ...)
136 /* Print an error message. For use within the preprocessor.  */
137 {
138     va_list ap;
139     va_start (ap, Format);
140     IntError (GetCurrentFile(), GetCurrentLine(), Format, ap);
141     va_end (ap);
142 }
143
144
145
146 void Fatal (const char* Format, ...)
147 /* Print a message about a fatal error and die */
148 {
149     va_list ap;
150
151     const char* FileName;
152     unsigned    LineNum;
153     if (CurTok.LI) {
154         FileName = GetInputName (CurTok.LI);
155         LineNum  = GetInputLine (CurTok.LI);
156     } else {
157         FileName = GetCurrentFile ();
158         LineNum  = GetCurrentLine ();
159     }
160
161     fprintf (stderr, "%s(%u): Fatal: ", FileName, LineNum);
162
163     va_start (ap, Format);
164     vfprintf (stderr, Format, ap);
165     va_end (ap);
166     fprintf (stderr, "\n");
167
168     Print (stderr, 1, "Input: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
169     exit (EXIT_FAILURE);
170 }
171
172
173
174 void Internal (const char* Format, ...)
175 /* Print a message about an internal compiler error and die. */
176 {
177     va_list ap;
178
179     const char* FileName;
180     unsigned    LineNum;
181     if (CurTok.LI) {
182         FileName = GetInputName (CurTok.LI);
183         LineNum  = GetInputLine (CurTok.LI);
184     } else {
185         FileName = GetCurrentFile ();
186         LineNum  = GetCurrentLine ();
187     }
188
189     fprintf (stderr, "%s(%u): Internal compiler error:\n",
190              FileName, LineNum);
191
192     va_start (ap, Format);
193     vfprintf (stderr, Format, ap);
194     va_end (ap);
195     fprintf (stderr, "\nInput: %.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
196
197     /* Use abort to create a core dump */
198     abort ();
199 }
200
201
202
203 void ErrorReport (void)
204 /* Report errors (called at end of compile) */
205 {
206     Print (stdout, 1, "%u errors, %u warnings\n", ErrorCount, WarningCount);
207 }
208
209
210