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_buffer
*));
33 static void print_location
PARAMS ((cpp_reader
*,
35 const cpp_lexer_pos
*));
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. */
42 print_containing_files (ip
)
47 /* Find the other, outer source files. */
48 for (ip
= ip
->prev
; ip
; ip
= ip
->prev
)
53 /* The current line in each outer source file is now the
54 same as the line of the #include. */
55 fprintf (stderr
, _("In file included from %s:%u"),
56 ip
->nominal_fname
, CPP_BUF_LINE (ip
));
59 /* Translators note: this message is used in conjunction
60 with "In file included from %s:%ld" and some other
61 tricks. We want something like this:
63 | In file included from sys/select.h:123,
64 | from sys/types.h:234,
66 | bits/select.h:45: <error message here>
68 with all the "from"s lined up.
69 The trailing comma is at the beginning of this message,
70 and the trailing colon is not translated. */
71 fprintf (stderr
, _(",\n from %s:%u"),
72 ip
->nominal_fname
, CPP_BUF_LINE (ip
));
74 fputs (":\n", stderr
);
78 print_location (pfile
, filename
, pos
)
81 const cpp_lexer_pos
*pos
;
83 cpp_buffer
*buffer
= pfile
->buffer
;
86 fprintf (stderr
, "%s: ", progname
);
89 unsigned int line
, col
= 0;
90 enum cpp_buffer_type type
= buffer
->type
;
92 /* For _Pragma buffers, we want to print the location as
93 "foo.c:5:8: _Pragma:", where foo.c is the containing buffer.
94 For diagnostics relating to command line options, we want to
95 print "<command line>:" with no line number. */
96 if (type
== BUF_CL_OPTION
|| type
== BUF_BUILTIN
)
100 if (type
== BUF_PRAGMA
)
102 buffer
= buffer
->prev
;
103 line
= CPP_BUF_LINE (buffer
);
104 col
= CPP_BUF_COL (buffer
);
109 pos
= cpp_get_line (pfile
);
117 /* Don't repeat the include stack unnecessarily. */
118 if (buffer
->prev
&& ! buffer
->include_stack_listed
)
120 buffer
->include_stack_listed
= 1;
121 print_containing_files (buffer
);
126 filename
= buffer
->nominal_fname
;
127 if (*filename
== '\0')
128 filename
= _("<stdin>");
131 fprintf (stderr
, "%s: ", filename
);
132 else if (CPP_OPTION (pfile
, show_column
) == 0)
133 fprintf (stderr
, "%s:%u: ", filename
, line
);
135 fprintf (stderr
, "%s:%u:%u: ", filename
, line
, col
);
137 if (type
== BUF_PRAGMA
)
138 fprintf (stderr
, "_Pragma: ");
142 /* Set up for an error message: print the file and line, bump the error
144 If it returns 0, this error has been suppressed. */
147 _cpp_begin_message (pfile
, code
, file
, pos
)
149 enum error_type code
;
151 const cpp_lexer_pos
*pos
;
158 if (CPP_IN_SYSTEM_HEADER (pfile
)
159 && ! CPP_OPTION (pfile
, warn_system_headers
))
161 if (! CPP_OPTION (pfile
, warnings_are_errors
))
163 if (CPP_OPTION (pfile
, inhibit_warnings
))
169 if (CPP_OPTION (pfile
, inhibit_errors
))
171 if (pfile
->errors
< CPP_FATAL_LIMIT
)
177 if (CPP_IN_SYSTEM_HEADER (pfile
)
178 && ! CPP_OPTION (pfile
, warn_system_headers
))
180 if (! CPP_OPTION (pfile
, pedantic_errors
))
182 if (CPP_OPTION (pfile
, inhibit_warnings
))
188 if (CPP_OPTION (pfile
, inhibit_errors
))
190 if (pfile
->errors
< CPP_FATAL_LIMIT
)
196 if (CPP_OPTION (pfile
, inhibit_errors
))
198 if (pfile
->errors
< CPP_FATAL_LIMIT
)
201 /* Fatal errors cannot be inhibited. */
203 pfile
->errors
= CPP_FATAL_LIMIT
;
206 fprintf (stderr
, _("internal error: "));
207 pfile
->errors
= CPP_FATAL_LIMIT
;
211 print_location (pfile
, file
, pos
);
213 fputs (_("warning: "), stderr
);
218 /* Exported interface. */
220 /* For reporting internal errors. Prints "internal error: " for you,
221 otherwise identical to cpp_fatal. */
224 cpp_ice
VPARAMS ((cpp_reader
*pfile
, const char *msgid
, ...))
226 #ifndef ANSI_PROTOTYPES
232 VA_START (ap
, msgid
);
234 #ifndef ANSI_PROTOTYPES
235 pfile
= va_arg (ap
, cpp_reader
*);
236 msgid
= va_arg (ap
, const char *);
239 if (_cpp_begin_message (pfile
, ICE
, NULL
, 0))
240 v_message (msgid
, ap
);
244 /* Same as cpp_error, except we consider the error to be "fatal",
245 such as inconsistent options. I.e. there is little point in continuing.
246 (We do not exit, to support use of cpplib as a library.
247 Instead, it is the caller's responsibility to check
251 cpp_fatal
VPARAMS ((cpp_reader
*pfile
, const char *msgid
, ...))
253 #ifndef ANSI_PROTOTYPES
259 VA_START (ap
, msgid
);
261 #ifndef ANSI_PROTOTYPES
262 pfile
= va_arg (ap
, cpp_reader
*);
263 msgid
= va_arg (ap
, const char *);
266 if (_cpp_begin_message (pfile
, FATAL
, NULL
, 0))
267 v_message (msgid
, ap
);
272 cpp_error
VPARAMS ((cpp_reader
* pfile
, const char *msgid
, ...))
274 #ifndef ANSI_PROTOTYPES
282 #ifndef ANSI_PROTOTYPES
283 pfile
= va_arg (ap
, cpp_reader
*);
284 msgid
= va_arg (ap
, const char *);
287 if (_cpp_begin_message (pfile
, ERROR
, NULL
, 0))
288 v_message (msgid
, ap
);
293 cpp_error_with_line
VPARAMS ((cpp_reader
*pfile
, int line
, int column
,
294 const char *msgid
, ...))
296 #ifndef ANSI_PROTOTYPES
305 VA_START (ap
, msgid
);
307 #ifndef ANSI_PROTOTYPES
308 pfile
= va_arg (ap
, cpp_reader
*);
309 line
= va_arg (ap
, int);
310 column
= va_arg (ap
, int);
311 msgid
= va_arg (ap
, const char *);
316 if (_cpp_begin_message (pfile
, ERROR
, NULL
, &pos
))
317 v_message (msgid
, ap
);
321 /* Error including a message from `errno'. */
323 cpp_error_from_errno (pfile
, name
)
327 cpp_error (pfile
, "%s: %s", name
, xstrerror (errno
));
331 cpp_warning
VPARAMS ((cpp_reader
* pfile
, const char *msgid
, ...))
333 #ifndef ANSI_PROTOTYPES
339 VA_START (ap
, msgid
);
341 #ifndef ANSI_PROTOTYPES
342 pfile
= va_arg (ap
, cpp_reader
*);
343 msgid
= va_arg (ap
, const char *);
346 if (_cpp_begin_message (pfile
, WARNING
, NULL
, 0))
347 v_message (msgid
, ap
);
352 cpp_warning_with_line
VPARAMS ((cpp_reader
* pfile
, int line
, int column
,
353 const char *msgid
, ...))
355 #ifndef ANSI_PROTOTYPES
364 VA_START (ap
, msgid
);
366 #ifndef ANSI_PROTOTYPES
367 pfile
= va_arg (ap
, cpp_reader
*);
368 line
= va_arg (ap
, int);
369 column
= va_arg (ap
, int);
370 msgid
= va_arg (ap
, const char *);
375 if (_cpp_begin_message (pfile
, WARNING
, NULL
, &pos
))
376 v_message (msgid
, ap
);
381 cpp_pedwarn
VPARAMS ((cpp_reader
* pfile
, const char *msgid
, ...))
383 #ifndef ANSI_PROTOTYPES
389 VA_START (ap
, msgid
);
391 #ifndef ANSI_PROTOTYPES
392 pfile
= va_arg (ap
, cpp_reader
*);
393 msgid
= va_arg (ap
, const char *);
396 if (_cpp_begin_message (pfile
, PEDWARN
, NULL
, 0))
397 v_message (msgid
, ap
);
402 cpp_pedwarn_with_line
VPARAMS ((cpp_reader
* pfile
, int line
, int column
,
403 const char *msgid
, ...))
405 #ifndef ANSI_PROTOTYPES
414 VA_START (ap
, msgid
);
416 #ifndef ANSI_PROTOTYPES
417 pfile
= va_arg (ap
, cpp_reader
*);
418 line
= va_arg (ap
, int);
419 column
= va_arg (ap
, int);
420 msgid
= va_arg (ap
, const char *);
425 if (_cpp_begin_message (pfile
, PEDWARN
, NULL
, &pos
))
426 v_message (msgid
, ap
);
430 /* Report a warning (or an error if pedantic_errors)
431 giving specified file name and line number, not current. */
434 cpp_pedwarn_with_file_and_line
VPARAMS ((cpp_reader
*pfile
,
435 const char *file
, int line
, int col
,
436 const char *msgid
, ...))
438 #ifndef ANSI_PROTOTYPES
448 VA_START (ap
, msgid
);
450 #ifndef ANSI_PROTOTYPES
451 pfile
= va_arg (ap
, cpp_reader
*);
452 file
= va_arg (ap
, const char *);
453 line
= va_arg (ap
, int);
454 col
= va_arg (ap
, int);
455 msgid
= va_arg (ap
, const char *);
460 if (_cpp_begin_message (pfile
, PEDWARN
, file
, &pos
))
461 v_message (msgid
, ap
);
465 /* Print an error message not associated with a file. */
467 cpp_notice
VPARAMS ((cpp_reader
*pfile
, const char *msgid
, ...))
469 #ifndef ANSI_PROTOTYPES
475 VA_START (ap
, msgid
);
477 #ifndef ANSI_PROTOTYPES
478 pfile
= va_arg (ap
, cpp_reader
*);
479 msgid
= va_arg (ap
, const char *);
482 if (pfile
->errors
< CPP_FATAL_LIMIT
)
485 vfprintf (stderr
, _(msgid
), ap
);
492 cpp_notice_from_errno (pfile
, name
)
498 cpp_notice (pfile
, "%s: %s", name
, xstrerror (errno
));