]>
Commit | Line | Data |
---|---|---|
7f2935c7 | 1 | /* Default error handlers for CPP Library. |
5e7b4e25 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000 |
40ea76de | 3 | Free Software Foundation, Inc. |
7f2935c7 | 4 | Written by Per Bothner, 1994. |
38e01259 | 5 | Based on CCCP program by Paul Rubin, June 1986 |
7f2935c7 PB |
6 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
7 | ||
8 | This program is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by the | |
10 | Free Software Foundation; either version 2, or (at your option) any | |
11 | later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
940d9d63 | 20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
7f2935c7 PB |
21 | |
22 | In other words, you are welcome to use, share and improve this program. | |
23 | You are forbidden to forbid anyone else to use, share and improve | |
24 | what you give them. Help stamp out software-hoarding! */ | |
25 | ||
84ee6fd4 | 26 | #include "config.h" |
b04cd507 | 27 | #include "system.h" |
f32da1f6 | 28 | #include "cpplib.h" |
ab87f8c8 | 29 | #include "intl.h" |
f32da1f6 | 30 | |
b0699dad ZW |
31 | static void print_containing_files PARAMS ((cpp_reader *, cpp_buffer *)); |
32 | static void print_file_and_line PARAMS ((const char *, long, long)); | |
33 | static void v_message PARAMS ((cpp_reader *, int, | |
c1212d2f ZW |
34 | const char *, long, long, |
35 | const char *, va_list)); | |
36 | ||
7f2935c7 PB |
37 | /* Print the file names and line numbers of the #include |
38 | commands which led to the current file. */ | |
39 | ||
c1212d2f | 40 | static void |
b0699dad | 41 | print_containing_files (pfile, ip) |
7f2935c7 | 42 | cpp_reader *pfile; |
c1212d2f | 43 | cpp_buffer *ip; |
7f2935c7 | 44 | { |
7f2935c7 PB |
45 | int first = 1; |
46 | ||
47 | /* If stack of files hasn't changed since we last printed | |
48 | this info, don't repeat it. */ | |
49 | if (pfile->input_stack_listing_current) | |
50 | return; | |
51 | ||
c1212d2f ZW |
52 | /* Find the other, outer source files. */ |
53 | for (ip = CPP_PREV_BUFFER (ip); | |
54 | ip != CPP_NULL_BUFFER (pfile); | |
55 | ip = CPP_PREV_BUFFER (ip)) | |
56 | if (ip->fname != NULL) | |
57 | { | |
58 | long line; | |
59 | cpp_buf_line_and_col (ip, &line, NULL); | |
60 | if (first) | |
61 | { | |
62 | first = 0; | |
63 | fprintf (stderr, _("In file included from %s:%ld"), | |
64 | ip->nominal_fname, line); | |
65 | } | |
66 | else | |
67 | /* Translators note: this message is used in conjunction | |
68 | with "In file included from %s:%ld" and some other | |
69 | tricks. We want something like this: | |
7f2935c7 | 70 | |
c1212d2f ZW |
71 | In file included from sys/select.h:123, |
72 | from sys/types.h:234, | |
73 | from userfile.c:31: | |
74 | bits/select.h:45: <error message here> | |
7f2935c7 | 75 | |
c1212d2f ZW |
76 | The trailing comma is at the beginning of this message, |
77 | and the trailing colon is not translated. */ | |
78 | fprintf (stderr, _(",\n from %s:%ld"), | |
79 | ip->nominal_fname, line); | |
80 | } | |
81 | if (first == 0) | |
74c9467a | 82 | fputs (":\n", stderr); |
7f2935c7 PB |
83 | |
84 | /* Record we have printed the status as of this time. */ | |
85 | pfile->input_stack_listing_current = 1; | |
86 | } | |
87 | ||
c1212d2f | 88 | static void |
b0699dad | 89 | print_file_and_line (filename, line, column) |
bcc5cac9 | 90 | const char *filename; |
c1212d2f | 91 | long line, column; |
7f2935c7 | 92 | { |
59495f38 ZW |
93 | if (filename == 0 || *filename == '\0') |
94 | filename = "<stdin>"; | |
c1212d2f | 95 | if (line <= 0) |
59495f38 ZW |
96 | fputs (_("<command line>: "), stderr); |
97 | else if (column > 0) | |
c1212d2f | 98 | fprintf (stderr, "%s:%ld:%ld: ", filename, line, column); |
7f2935c7 | 99 | else |
c1212d2f | 100 | fprintf (stderr, "%s:%ld: ", filename, line); |
7f2935c7 PB |
101 | } |
102 | ||
c1212d2f ZW |
103 | /* IS_ERROR is 3 for ICE, 2 for merely "fatal" error, |
104 | 1 for error, 0 for warning. */ | |
0f41302f | 105 | |
c1212d2f | 106 | static void |
b0699dad | 107 | v_message (pfile, is_error, file, line, col, msg, ap) |
c1212d2f ZW |
108 | cpp_reader *pfile; |
109 | int is_error; | |
110 | const char *file; | |
111 | long line; | |
112 | long col; | |
113 | const char *msg; | |
114 | va_list ap; | |
7f2935c7 | 115 | { |
c1212d2f ZW |
116 | cpp_buffer *ip = cpp_file_buffer (pfile); |
117 | ||
118 | if (ip) | |
119 | { | |
120 | if (file == NULL) | |
121 | file = ip->nominal_fname; | |
122 | if (line == -1) | |
123 | cpp_buf_line_and_col (ip, &line, &col); | |
124 | ||
b0699dad ZW |
125 | print_containing_files (pfile, ip); |
126 | print_file_and_line (file, line, col); | |
c1212d2f ZW |
127 | } |
128 | else | |
129 | fprintf (stderr, "%s: ", progname); | |
130 | ||
ab87f8c8 JL |
131 | switch (is_error) |
132 | { | |
ab87f8c8 JL |
133 | case 0: |
134 | fprintf (stderr, _("warning: ")); | |
135 | break; | |
136 | case 1: | |
137 | if (pfile->errors < CPP_FATAL_LIMIT) | |
138 | pfile->errors++; | |
139 | break; | |
140 | case 2: | |
141 | pfile->errors = CPP_FATAL_LIMIT; | |
142 | break; | |
c1212d2f ZW |
143 | case 3: |
144 | fprintf (stderr, _("internal error: ")); | |
145 | pfile->errors = CPP_FATAL_LIMIT; | |
146 | break; | |
ab87f8c8 | 147 | default: |
b0699dad | 148 | cpp_ice (pfile, "bad is_error(%d) in v_message", is_error); |
ab87f8c8 JL |
149 | } |
150 | ||
c1212d2f ZW |
151 | vfprintf (stderr, _(msg), ap); |
152 | putc ('\n', stderr); | |
7f2935c7 PB |
153 | } |
154 | ||
c1212d2f ZW |
155 | /* Exported interface. */ |
156 | ||
157 | /* For reporting internal errors. Prints "internal error: " for you, | |
158 | otherwise identical to cpp_fatal. */ | |
159 | ||
487a6e06 | 160 | void |
c1212d2f ZW |
161 | cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...)) |
162 | { | |
5148a72b | 163 | #ifndef ANSI_PROTOTYPES |
487a6e06 | 164 | cpp_reader *pfile; |
1c5d09e4 | 165 | const char *msgid; |
487a6e06 KG |
166 | #endif |
167 | va_list ap; | |
168 | ||
ab87f8c8 | 169 | VA_START (ap, msgid); |
487a6e06 | 170 | |
5148a72b | 171 | #ifndef ANSI_PROTOTYPES |
487a6e06 | 172 | pfile = va_arg (ap, cpp_reader *); |
ab87f8c8 | 173 | msgid = va_arg (ap, const char *); |
487a6e06 KG |
174 | #endif |
175 | ||
b0699dad | 176 | v_message (pfile, 3, NULL, -1, -1, msgid, ap); |
487a6e06 KG |
177 | va_end(ap); |
178 | } | |
179 | ||
05a2b36f PB |
180 | /* Same as cpp_error, except we consider the error to be "fatal", |
181 | such as inconsistent options. I.e. there is little point in continuing. | |
182 | (We do not exit, to support use of cpplib as a library. | |
183 | Instead, it is the caller's responsibility to check | |
184 | CPP_FATAL_ERRORS. */ | |
185 | ||
186 | void | |
f84c2018 | 187 | cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...)) |
487a6e06 | 188 | { |
5148a72b | 189 | #ifndef ANSI_PROTOTYPES |
487a6e06 | 190 | cpp_reader *pfile; |
ab87f8c8 | 191 | const char *msgid; |
487a6e06 KG |
192 | #endif |
193 | va_list ap; | |
194 | ||
ab87f8c8 | 195 | VA_START (ap, msgid); |
487a6e06 | 196 | |
5148a72b | 197 | #ifndef ANSI_PROTOTYPES |
487a6e06 | 198 | pfile = va_arg (ap, cpp_reader *); |
ab87f8c8 | 199 | msgid = va_arg (ap, const char *); |
487a6e06 KG |
200 | #endif |
201 | ||
b0699dad | 202 | v_message (pfile, 2, NULL, -1, -1, msgid, ap); |
c1212d2f ZW |
203 | va_end(ap); |
204 | } | |
205 | ||
206 | void | |
207 | cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...)) | |
208 | { | |
209 | #ifndef ANSI_PROTOTYPES | |
210 | cpp_reader *pfile; | |
211 | const char *msgid; | |
212 | #endif | |
213 | va_list ap; | |
214 | ||
215 | VA_START(ap, msgid); | |
216 | ||
217 | #ifndef ANSI_PROTOTYPES | |
218 | pfile = va_arg (ap, cpp_reader *); | |
219 | msgid = va_arg (ap, const char *); | |
220 | #endif | |
221 | ||
222 | if (CPP_OPTIONS (pfile)->inhibit_errors) | |
223 | return; | |
224 | ||
b0699dad | 225 | v_message (pfile, 1, NULL, -1, -1, msgid, ap); |
c1212d2f ZW |
226 | va_end(ap); |
227 | } | |
228 | ||
229 | void | |
230 | cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column, | |
231 | const char *msgid, ...)) | |
232 | { | |
233 | #ifndef ANSI_PROTOTYPES | |
234 | cpp_reader *pfile; | |
235 | int line; | |
236 | int column; | |
237 | const char *msgid; | |
238 | #endif | |
239 | va_list ap; | |
240 | ||
241 | VA_START (ap, msgid); | |
242 | ||
243 | #ifndef ANSI_PROTOTYPES | |
244 | pfile = va_arg (ap, cpp_reader *); | |
245 | line = va_arg (ap, int); | |
246 | column = va_arg (ap, int); | |
247 | msgid = va_arg (ap, const char *); | |
248 | #endif | |
249 | ||
250 | if (CPP_OPTIONS (pfile)->inhibit_errors) | |
251 | return; | |
252 | ||
b0699dad | 253 | v_message (pfile, 1, NULL, line, column, msgid, ap); |
487a6e06 | 254 | va_end(ap); |
05a2b36f | 255 | } |
c1212d2f ZW |
256 | |
257 | /* Error including a message from `errno'. */ | |
7f2935c7 | 258 | void |
c1212d2f | 259 | cpp_error_from_errno (pfile, name) |
7f2935c7 | 260 | cpp_reader *pfile; |
487a6e06 | 261 | const char *name; |
7f2935c7 | 262 | { |
f95e46b9 | 263 | cpp_error (pfile, "%s: %s", name, xstrerror (errno)); |
c1212d2f ZW |
264 | } |
265 | ||
266 | void | |
267 | cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...)) | |
268 | { | |
269 | #ifndef ANSI_PROTOTYPES | |
270 | cpp_reader *pfile; | |
271 | const char *msgid; | |
272 | #endif | |
273 | va_list ap; | |
274 | ||
275 | VA_START (ap, msgid); | |
276 | ||
277 | #ifndef ANSI_PROTOTYPES | |
278 | pfile = va_arg (ap, cpp_reader *); | |
279 | msgid = va_arg (ap, const char *); | |
7f2935c7 | 280 | #endif |
c1212d2f ZW |
281 | |
282 | if (CPP_OPTIONS (pfile)->inhibit_warnings) | |
283 | return; | |
284 | ||
b0699dad | 285 | v_message (pfile, 0, NULL, -1, -1, msgid, ap); |
c1212d2f | 286 | va_end(ap); |
7f2935c7 | 287 | } |
ab87f8c8 | 288 | |
c1212d2f ZW |
289 | void |
290 | cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column, | |
291 | const char *msgid, ...)) | |
292 | { | |
293 | #ifndef ANSI_PROTOTYPES | |
294 | cpp_reader *pfile; | |
295 | int line; | |
296 | int column; | |
297 | const char *msgid; | |
298 | #endif | |
299 | va_list ap; | |
300 | ||
301 | VA_START (ap, msgid); | |
302 | ||
303 | #ifndef ANSI_PROTOTYPES | |
304 | pfile = va_arg (ap, cpp_reader *); | |
305 | line = va_arg (ap, int); | |
306 | column = va_arg (ap, int); | |
307 | msgid = va_arg (ap, const char *); | |
308 | #endif | |
309 | ||
310 | if (CPP_OPTIONS (pfile)->inhibit_warnings) | |
311 | return; | |
312 | ||
b0699dad | 313 | v_message (pfile, 0, NULL, line, column, msgid, ap); |
c1212d2f ZW |
314 | va_end(ap); |
315 | } | |
ab87f8c8 JL |
316 | |
317 | void | |
c1212d2f ZW |
318 | cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...)) |
319 | { | |
320 | #ifndef ANSI_PROTOTYPES | |
321 | cpp_reader *pfile; | |
322 | const char *msgid; | |
323 | #endif | |
324 | va_list ap; | |
325 | ||
326 | VA_START (ap, msgid); | |
327 | ||
328 | #ifndef ANSI_PROTOTYPES | |
329 | pfile = va_arg (ap, cpp_reader *); | |
330 | msgid = va_arg (ap, const char *); | |
331 | #endif | |
332 | ||
333 | if (CPP_OPTIONS (pfile)->pedantic_errors | |
334 | ? CPP_OPTIONS (pfile)->inhibit_errors | |
335 | : CPP_OPTIONS (pfile)->inhibit_warnings) | |
336 | return; | |
337 | ||
b0699dad | 338 | v_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, |
c1212d2f ZW |
339 | NULL, -1, -1, msgid, ap); |
340 | va_end(ap); | |
341 | } | |
342 | ||
343 | void | |
344 | cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column, | |
345 | const char *msgid, ...)) | |
346 | { | |
ab87f8c8 | 347 | #ifndef ANSI_PROTOTYPES |
c1212d2f ZW |
348 | cpp_reader *pfile; |
349 | int line; | |
350 | int column; | |
ab87f8c8 JL |
351 | const char *msgid; |
352 | #endif | |
353 | va_list ap; | |
354 | ||
355 | VA_START (ap, msgid); | |
356 | ||
357 | #ifndef ANSI_PROTOTYPES | |
c1212d2f ZW |
358 | pfile = va_arg (ap, cpp_reader *); |
359 | line = va_arg (ap, int); | |
360 | column = va_arg (ap, int); | |
ab87f8c8 JL |
361 | msgid = va_arg (ap, const char *); |
362 | #endif | |
363 | ||
c1212d2f ZW |
364 | if (CPP_OPTIONS (pfile)->pedantic_errors |
365 | ? CPP_OPTIONS (pfile)->inhibit_errors | |
366 | : CPP_OPTIONS (pfile)->inhibit_warnings) | |
367 | return; | |
368 | ||
b0699dad | 369 | v_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, |
c1212d2f | 370 | NULL, line, column, msgid, ap); |
ab87f8c8 JL |
371 | va_end(ap); |
372 | } | |
c1212d2f ZW |
373 | |
374 | /* Report a warning (or an error if pedantic_errors) | |
375 | giving specified file name and line number, not current. */ | |
376 | ||
377 | void | |
378 | cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile, | |
379 | const char *file, int line, int col, | |
380 | const char *msgid, ...)) | |
381 | { | |
382 | #ifndef ANSI_PROTOTYPES | |
383 | cpp_reader *pfile; | |
384 | const char *file; | |
385 | int line; | |
386 | int col; | |
387 | const char *msgid; | |
388 | #endif | |
389 | va_list ap; | |
390 | ||
391 | VA_START (ap, msgid); | |
392 | ||
393 | #ifndef ANSI_PROTOTYPES | |
394 | pfile = va_arg (ap, cpp_reader *); | |
395 | file = va_arg (ap, const char *); | |
396 | line = va_arg (ap, int); | |
397 | col = va_arg (ap, int); | |
398 | msgid = va_arg (ap, const char *); | |
399 | #endif | |
400 | ||
401 | if (CPP_OPTIONS (pfile)->pedantic_errors | |
402 | ? CPP_OPTIONS (pfile)->inhibit_errors | |
403 | : CPP_OPTIONS (pfile)->inhibit_warnings) | |
404 | return; | |
405 | ||
b0699dad | 406 | v_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, |
c1212d2f ZW |
407 | file, line, col, msgid, ap); |
408 | va_end(ap); | |
409 | } | |
410 | ||
411 | /* Print an error message not associated with a file. */ | |
412 | void | |
413 | cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...)) | |
414 | { | |
415 | #ifndef ANSI_PROTOTYPES | |
416 | cpp_reader *pfile; | |
417 | const char *msgid; | |
418 | #endif | |
419 | va_list ap; | |
420 | ||
421 | VA_START (ap, msgid); | |
422 | ||
423 | #ifndef ANSI_PROTOTYPES | |
424 | pfile = va_arg (ap, cpp_reader *); | |
425 | msgid = va_arg (ap, const char *); | |
426 | #endif | |
427 | ||
428 | if (pfile->errors < CPP_FATAL_LIMIT) | |
429 | pfile->errors++; | |
430 | ||
431 | vfprintf (stderr, _(msgid), ap); | |
432 | putc('\n', stderr); | |
433 | ||
434 | va_end(ap); | |
435 | } | |
436 | ||
437 | void | |
438 | cpp_notice_from_errno (pfile, name) | |
439 | cpp_reader *pfile; | |
440 | const char *name; | |
441 | { | |
f95e46b9 | 442 | cpp_notice (pfile, "%s: %s", name, xstrerror (errno)); |
c1212d2f | 443 | } |