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 ((struct line_map *,
34 static void print_location PARAMS ((cpp_reader *,
36 const cpp_lexer_pos *));
38 /* Don't remove the blank before do, as otherwise the exgettext
39 script will mistake this as a function definition */
40 #define v_message(msgid, ap) \
41 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
43 /* Print the file names and line numbers of the #include
44 commands which led to the current file. */
46 print_containing_files (map_array, map)
47 struct line_map *map_array;
54 if (MAIN_FILE_P (map))
56 map = &map_array[map->included_from];
61 /* The current line in each outer source file is now the
62 same as the line of the #include. */
63 fprintf (stderr, _("In file included from %s:%u"),
64 map->to_file, LAST_SOURCE_LINE (map));
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 map->to_file, LAST_SOURCE_LINE (map));
83 fputs (":\n", stderr);
87 print_location (pfile, filename, pos)
90 const cpp_lexer_pos *pos;
92 cpp_buffer *buffer = pfile->buffer;
95 fprintf (stderr, "%s: ", progname);
98 unsigned int line, col = 0;
99 enum cpp_buffer_type type = buffer->type;
101 /* For _Pragma buffers, we want to print the location as
102 "foo.c:5:8: _Pragma:", where foo.c is the containing buffer.
103 For diagnostics relating to command line options, we want to
104 print "<command line>:" with no line number. */
105 if (type == BUF_CL_OPTION || type == BUF_BUILTIN)
109 struct line_map *map;
112 if (type == BUF_PRAGMA)
114 buffer = buffer->prev;
115 col = CPP_BUF_COL (buffer);
118 map = lookup_line (&pfile->line_maps, line);
121 pos = cpp_get_line (pfile);
122 line = SOURCE_LINE (map, line);
131 /* Don't repeat the include stack unnecessarily. */
132 if (buffer->prev && ! buffer->include_stack_listed)
134 buffer->include_stack_listed = 1;
135 print_containing_files (pfile->line_maps.maps, map);
140 filename = buffer->nominal_fname;
143 fprintf (stderr, "%s:", filename);
144 else if (CPP_OPTION (pfile, show_column) == 0)
145 fprintf (stderr, "%s:%u:", filename, line);
147 fprintf (stderr, "%s:%u:%u:", filename, line, col);
149 if (type == BUF_PRAGMA)
150 fprintf (stderr, "_Pragma:");
155 /* Set up for an error message: print the file and line, bump the error
157 If it returns 0, this error has been suppressed. */
160 _cpp_begin_message (pfile, code, file, pos)
162 enum error_type code;
164 const cpp_lexer_pos *pos;
172 if (CPP_IN_SYSTEM_HEADER (pfile)
173 && ! CPP_OPTION (pfile, warn_system_headers))
176 if (CPP_OPTION (pfile, warnings_are_errors)
177 || (code == PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
179 if (CPP_OPTION (pfile, inhibit_errors))
181 if (pfile->errors < CPP_FATAL_LIMIT)
186 if (CPP_OPTION (pfile, inhibit_warnings))
193 if (CPP_OPTION (pfile, inhibit_errors))
195 if (pfile->errors < CPP_FATAL_LIMIT)
198 /* Fatal errors cannot be inhibited. */
200 pfile->errors = CPP_FATAL_LIMIT;
203 fprintf (stderr, _("internal error: "));
204 pfile->errors = CPP_FATAL_LIMIT;
208 print_location (pfile, file, pos);
210 fputs (_("warning: "), stderr);
215 /* Exported interface. */
217 /* For reporting internal errors. Prints "internal error: " for you,
218 otherwise identical to cpp_fatal. */
221 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
223 #ifndef ANSI_PROTOTYPES
229 VA_START (ap, msgid);
231 #ifndef ANSI_PROTOTYPES
232 pfile = va_arg (ap, cpp_reader *);
233 msgid = va_arg (ap, const char *);
236 if (_cpp_begin_message (pfile, ICE, NULL, 0))
237 v_message (msgid, ap);
241 /* Same as cpp_error, except we consider the error to be "fatal",
242 such as inconsistent options. I.e. there is little point in continuing.
243 (We do not exit, to support use of cpplib as a library.
244 Instead, it is the caller's responsibility to check
248 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
250 #ifndef ANSI_PROTOTYPES
256 VA_START (ap, msgid);
258 #ifndef ANSI_PROTOTYPES
259 pfile = va_arg (ap, cpp_reader *);
260 msgid = va_arg (ap, const char *);
263 if (_cpp_begin_message (pfile, FATAL, NULL, 0))
264 v_message (msgid, ap);
269 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
271 #ifndef ANSI_PROTOTYPES
279 #ifndef ANSI_PROTOTYPES
280 pfile = va_arg (ap, cpp_reader *);
281 msgid = va_arg (ap, const char *);
284 if (_cpp_begin_message (pfile, ERROR, NULL, 0))
285 v_message (msgid, ap);
290 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
291 const char *msgid, ...))
293 #ifndef ANSI_PROTOTYPES
302 VA_START (ap, msgid);
304 #ifndef ANSI_PROTOTYPES
305 pfile = va_arg (ap, cpp_reader *);
306 line = va_arg (ap, int);
307 column = va_arg (ap, int);
308 msgid = va_arg (ap, const char *);
313 if (_cpp_begin_message (pfile, ERROR, NULL, &pos))
314 v_message (msgid, ap);
318 /* Error including a message from `errno'. */
320 cpp_error_from_errno (pfile, name)
324 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
328 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
330 #ifndef ANSI_PROTOTYPES
336 VA_START (ap, msgid);
338 #ifndef ANSI_PROTOTYPES
339 pfile = va_arg (ap, cpp_reader *);
340 msgid = va_arg (ap, const char *);
343 if (_cpp_begin_message (pfile, WARNING, NULL, 0))
344 v_message (msgid, ap);
349 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
350 const char *msgid, ...))
352 #ifndef ANSI_PROTOTYPES
361 VA_START (ap, msgid);
363 #ifndef ANSI_PROTOTYPES
364 pfile = va_arg (ap, cpp_reader *);
365 line = va_arg (ap, int);
366 column = va_arg (ap, int);
367 msgid = va_arg (ap, const char *);
372 if (_cpp_begin_message (pfile, WARNING, NULL, &pos))
373 v_message (msgid, ap);
378 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
380 #ifndef ANSI_PROTOTYPES
386 VA_START (ap, msgid);
388 #ifndef ANSI_PROTOTYPES
389 pfile = va_arg (ap, cpp_reader *);
390 msgid = va_arg (ap, const char *);
393 if (_cpp_begin_message (pfile, PEDWARN, NULL, 0))
394 v_message (msgid, ap);
399 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
400 const char *msgid, ...))
402 #ifndef ANSI_PROTOTYPES
411 VA_START (ap, msgid);
413 #ifndef ANSI_PROTOTYPES
414 pfile = va_arg (ap, cpp_reader *);
415 line = va_arg (ap, int);
416 column = va_arg (ap, int);
417 msgid = va_arg (ap, const char *);
422 if (_cpp_begin_message (pfile, PEDWARN, NULL, &pos))
423 v_message (msgid, ap);
427 /* Report a warning (or an error if pedantic_errors)
428 giving specified file name and line number, not current. */
431 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
432 const char *file, int line, int col,
433 const char *msgid, ...))
435 #ifndef ANSI_PROTOTYPES
445 VA_START (ap, msgid);
447 #ifndef ANSI_PROTOTYPES
448 pfile = va_arg (ap, cpp_reader *);
449 file = va_arg (ap, const char *);
450 line = va_arg (ap, int);
451 col = va_arg (ap, int);
452 msgid = va_arg (ap, const char *);
457 if (_cpp_begin_message (pfile, PEDWARN, file, &pos))
458 v_message (msgid, ap);
462 /* Print an error message not associated with a file. */
464 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
466 #ifndef ANSI_PROTOTYPES
472 VA_START (ap, msgid);
474 #ifndef ANSI_PROTOTYPES
475 pfile = va_arg (ap, cpp_reader *);
476 msgid = va_arg (ap, const char *);
479 if (pfile->errors < CPP_FATAL_LIMIT)
482 vfprintf (stderr, _(msgid), ap);
489 cpp_notice_from_errno (pfile, name)
495 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));