1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
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
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.
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
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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! */
32 static void print_containing_files PARAMS ((cpp_reader *, cpp_buffer *));
33 static void print_file_and_line PARAMS ((const char *, unsigned int,
36 #define v_message(msgid, ap) \
37 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
39 /* Print the file names and line numbers of the #include
40 commands which led to the current file. */
43 print_containing_files (pfile, ip)
49 /* If stack of files hasn't changed since we last printed
50 this info, don't repeat it. */
51 if (pfile->input_stack_listing_current)
54 /* Find the other, outer source files. */
55 for (ip = CPP_PREV_BUFFER (ip); ip != NULL; ip = CPP_PREV_BUFFER (ip))
60 /* N.B. The current line in each outer source file is one
61 greater than the line of the #include, so we must
62 subtract one to correct for that. */
63 fprintf (stderr, _("In file included from %s:%u"),
64 ip->nominal_fname, CPP_BUF_LINE (ip) - 1);
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:
71 | In file included from sys/select.h:123,
72 | from sys/types.h:234,
74 | bits/select.h:45: <error message here>
76 with all the "from"s lined up.
77 The trailing comma is at the beginning of this message,
78 and the trailing colon is not translated. */
79 fprintf (stderr, _(",\n from %s:%u"),
80 ip->nominal_fname, CPP_BUF_LINE (ip) - 1);
83 fputs (":\n", stderr);
85 /* Record we have printed the status as of this time. */
86 pfile->input_stack_listing_current = 1;
90 print_file_and_line (filename, line, column)
92 unsigned int line, column;
94 if (filename == 0 || *filename == '\0')
97 if (line == (unsigned int)-1)
98 fprintf (stderr, "%s: ", filename);
100 fprintf (stderr, "%s:%u:%u: ", filename, line, column);
102 fprintf (stderr, "%s:%u: ", filename, line);
105 /* Set up for an error message: print the file and line, bump the error
107 If it returns 0, this error has been suppressed. */
110 _cpp_begin_message (pfile, code, file, line, col)
112 enum error_type code;
117 cpp_buffer *ip = CPP_BUFFER (pfile);
123 if (CPP_IN_SYSTEM_HEADER (pfile)
124 && ! CPP_OPTION (pfile, warn_system_headers))
126 if (! CPP_OPTION (pfile, warnings_are_errors))
128 if (CPP_OPTION (pfile, inhibit_warnings))
134 if (CPP_OPTION (pfile, inhibit_errors))
136 if (pfile->errors < CPP_FATAL_LIMIT)
142 if (CPP_IN_SYSTEM_HEADER (pfile)
143 && ! CPP_OPTION (pfile, warn_system_headers))
145 if (! CPP_OPTION (pfile, pedantic_errors))
147 if (CPP_OPTION (pfile, inhibit_warnings))
153 if (CPP_OPTION (pfile, inhibit_errors))
155 if (pfile->errors < CPP_FATAL_LIMIT)
161 if (CPP_OPTION (pfile, inhibit_errors))
163 if (pfile->errors < CPP_FATAL_LIMIT)
166 /* Fatal errors cannot be inhibited. */
168 pfile->errors = CPP_FATAL_LIMIT;
171 fprintf (stderr, _("internal error: "));
172 pfile->errors = CPP_FATAL_LIMIT;
179 file = ip->nominal_fname;
181 line = _cpp_get_line (pfile, &col);
182 print_containing_files (pfile, ip);
183 print_file_and_line (file, line,
184 CPP_OPTION (pfile, show_column) ? col : 0);
187 fprintf (stderr, "%s: ", progname);
190 fputs (_("warning: "), stderr);
195 /* Exported interface. */
197 /* For reporting internal errors. Prints "internal error: " for you,
198 otherwise identical to cpp_fatal. */
201 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
203 #ifndef ANSI_PROTOTYPES
209 VA_START (ap, msgid);
211 #ifndef ANSI_PROTOTYPES
212 pfile = va_arg (ap, cpp_reader *);
213 msgid = va_arg (ap, const char *);
216 if (_cpp_begin_message (pfile, ICE, NULL, 0, 0))
217 v_message (msgid, ap);
221 /* Same as cpp_error, except we consider the error to be "fatal",
222 such as inconsistent options. I.e. there is little point in continuing.
223 (We do not exit, to support use of cpplib as a library.
224 Instead, it is the caller's responsibility to check
228 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
230 #ifndef ANSI_PROTOTYPES
236 VA_START (ap, msgid);
238 #ifndef ANSI_PROTOTYPES
239 pfile = va_arg (ap, cpp_reader *);
240 msgid = va_arg (ap, const char *);
243 if (_cpp_begin_message (pfile, FATAL, NULL, 0, 0))
244 v_message (msgid, ap);
249 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
251 #ifndef ANSI_PROTOTYPES
259 #ifndef ANSI_PROTOTYPES
260 pfile = va_arg (ap, cpp_reader *);
261 msgid = va_arg (ap, const char *);
264 if (_cpp_begin_message (pfile, ERROR, NULL, 0, 0))
265 v_message (msgid, ap);
270 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
271 const char *msgid, ...))
273 #ifndef ANSI_PROTOTYPES
281 VA_START (ap, msgid);
283 #ifndef ANSI_PROTOTYPES
284 pfile = va_arg (ap, cpp_reader *);
285 line = va_arg (ap, int);
286 column = va_arg (ap, int);
287 msgid = va_arg (ap, const char *);
290 if (_cpp_begin_message (pfile, ERROR, NULL, line, column))
291 v_message (msgid, ap);
295 /* Error including a message from `errno'. */
297 cpp_error_from_errno (pfile, name)
301 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
305 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
307 #ifndef ANSI_PROTOTYPES
313 VA_START (ap, msgid);
315 #ifndef ANSI_PROTOTYPES
316 pfile = va_arg (ap, cpp_reader *);
317 msgid = va_arg (ap, const char *);
320 if (_cpp_begin_message (pfile, WARNING, NULL, 0, 0))
321 v_message (msgid, ap);
326 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
327 const char *msgid, ...))
329 #ifndef ANSI_PROTOTYPES
337 VA_START (ap, msgid);
339 #ifndef ANSI_PROTOTYPES
340 pfile = va_arg (ap, cpp_reader *);
341 line = va_arg (ap, int);
342 column = va_arg (ap, int);
343 msgid = va_arg (ap, const char *);
346 if (_cpp_begin_message (pfile, WARNING, NULL, line, column))
347 v_message (msgid, ap);
352 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
354 #ifndef ANSI_PROTOTYPES
360 VA_START (ap, msgid);
362 #ifndef ANSI_PROTOTYPES
363 pfile = va_arg (ap, cpp_reader *);
364 msgid = va_arg (ap, const char *);
367 if (_cpp_begin_message (pfile, PEDWARN, NULL, 0, 0))
368 v_message (msgid, ap);
373 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
374 const char *msgid, ...))
376 #ifndef ANSI_PROTOTYPES
384 VA_START (ap, msgid);
386 #ifndef ANSI_PROTOTYPES
387 pfile = va_arg (ap, cpp_reader *);
388 line = va_arg (ap, int);
389 column = va_arg (ap, int);
390 msgid = va_arg (ap, const char *);
393 if (_cpp_begin_message (pfile, PEDWARN, NULL, line, column))
394 v_message (msgid, ap);
398 /* Report a warning (or an error if pedantic_errors)
399 giving specified file name and line number, not current. */
402 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
403 const char *file, int line, int col,
404 const char *msgid, ...))
406 #ifndef ANSI_PROTOTYPES
415 VA_START (ap, msgid);
417 #ifndef ANSI_PROTOTYPES
418 pfile = va_arg (ap, cpp_reader *);
419 file = va_arg (ap, const char *);
420 line = va_arg (ap, int);
421 col = va_arg (ap, int);
422 msgid = va_arg (ap, const char *);
425 if (_cpp_begin_message (pfile, PEDWARN, file, line, col))
426 v_message (msgid, ap);
430 /* Print an error message not associated with a file. */
432 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
434 #ifndef ANSI_PROTOTYPES
440 VA_START (ap, msgid);
442 #ifndef ANSI_PROTOTYPES
443 pfile = va_arg (ap, cpp_reader *);
444 msgid = va_arg (ap, const char *);
447 if (pfile->errors < CPP_FATAL_LIMIT)
450 vfprintf (stderr, _(msgid), ap);
457 cpp_notice_from_errno (pfile, name)
463 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
470 return (const char *) _cpp_token_spellings[type].name;