2000-05-02 Jeff Sturm <jsturm@one-point.com>
[official-gcc.git] / gcc / cpperror.c
blob063489dae63072b3458d544ea26a3fe0eac87bdc
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
11 later version.
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! */
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30 #include "intl.h"
32 static void print_containing_files PARAMS ((cpp_buffer *));
33 static void print_location PARAMS ((cpp_reader *,
34 const char *,
35 const cpp_lexer_pos *));
37 /* Don't remove the blank before do, as otherwise the exgettext
38 script will mistake this as a function definition */
39 #define v_message(msgid, ap) \
40 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
42 /* Print the file names and line numbers of the #include
43 commands which led to the current file. */
44 static void
45 print_containing_files (ip)
46 cpp_buffer *ip;
48 int first = 1;
50 /* Find the other, outer source files. */
51 for (ip = ip->prev; ip; ip = ip->prev)
53 if (first)
55 first = 0;
56 /* The current line in each outer source file is now the
57 same as the line of the #include. */
58 fprintf (stderr, _("In file included from %s:%u"),
59 ip->nominal_fname, CPP_BUF_LINE (ip));
61 else
62 /* Translators note: this message is used in conjunction
63 with "In file included from %s:%ld" and some other
64 tricks. We want something like this:
66 | In file included from sys/select.h:123,
67 | from sys/types.h:234,
68 | from userfile.c:31:
69 | bits/select.h:45: <error message here>
71 with all the "from"s lined up.
72 The trailing comma is at the beginning of this message,
73 and the trailing colon is not translated. */
74 fprintf (stderr, _(",\n from %s:%u"),
75 ip->nominal_fname, CPP_BUF_LINE (ip));
77 fputs (":\n", stderr);
80 static void
81 print_location (pfile, filename, pos)
82 cpp_reader *pfile;
83 const char *filename;
84 const cpp_lexer_pos *pos;
86 cpp_buffer *buffer = pfile->buffer;
88 if (!buffer)
89 fprintf (stderr, "%s: ", progname);
90 else
92 unsigned int line, col = 0;
93 enum cpp_buffer_type type = buffer->type;
95 /* For _Pragma buffers, we want to print the location as
96 "foo.c:5:8: _Pragma:", where foo.c is the containing buffer.
97 For diagnostics relating to command line options, we want to
98 print "<command line>:" with no line number. */
99 if (type == BUF_CL_OPTION || type == BUF_BUILTIN)
100 line = 0;
101 else
103 if (type == BUF_PRAGMA)
105 buffer = buffer->prev;
106 line = CPP_BUF_LINE (buffer);
107 col = CPP_BUF_COL (buffer);
109 else
111 if (pos == 0)
112 pos = cpp_get_line (pfile);
113 line = pos->line;
114 col = pos->col;
117 if (col == 0)
118 col = 1;
120 /* Don't repeat the include stack unnecessarily. */
121 if (buffer->prev && ! buffer->include_stack_listed)
123 buffer->include_stack_listed = 1;
124 print_containing_files (buffer);
128 if (filename == 0)
129 filename = buffer->nominal_fname;
131 if (line == 0)
132 fprintf (stderr, "%s: ", filename);
133 else if (CPP_OPTION (pfile, show_column) == 0)
134 fprintf (stderr, "%s:%u: ", filename, line);
135 else
136 fprintf (stderr, "%s:%u:%u: ", filename, line, col);
138 if (type == BUF_PRAGMA)
139 fprintf (stderr, "_Pragma: ");
143 /* Set up for an error message: print the file and line, bump the error
144 counter, etc.
145 If it returns 0, this error has been suppressed. */
148 _cpp_begin_message (pfile, code, file, pos)
149 cpp_reader *pfile;
150 enum error_type code;
151 const char *file;
152 const cpp_lexer_pos *pos;
154 int is_warning = 0;
156 switch (code)
158 case PEDWARN:
159 case WARNING:
160 if (CPP_IN_SYSTEM_HEADER (pfile)
161 && ! CPP_OPTION (pfile, warn_system_headers))
162 return 0;
163 case WARNING_SYSHDR:
164 if (CPP_OPTION (pfile, warnings_are_errors)
165 || (code == PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
167 if (CPP_OPTION (pfile, inhibit_errors))
168 return 0;
169 if (pfile->errors < CPP_FATAL_LIMIT)
170 pfile->errors++;
172 else
174 if (CPP_OPTION (pfile, inhibit_warnings))
175 return 0;
176 is_warning = 1;
178 break;
180 case ERROR:
181 if (CPP_OPTION (pfile, inhibit_errors))
182 return 0;
183 if (pfile->errors < CPP_FATAL_LIMIT)
184 pfile->errors++;
185 break;
186 /* Fatal errors cannot be inhibited. */
187 case FATAL:
188 pfile->errors = CPP_FATAL_LIMIT;
189 break;
190 case ICE:
191 fprintf (stderr, _("internal error: "));
192 pfile->errors = CPP_FATAL_LIMIT;
193 break;
196 print_location (pfile, file, pos);
197 if (is_warning)
198 fputs (_("warning: "), stderr);
200 return 1;
203 /* Exported interface. */
205 /* For reporting internal errors. Prints "internal error: " for you,
206 otherwise identical to cpp_fatal. */
208 void
209 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
211 #ifndef ANSI_PROTOTYPES
212 cpp_reader *pfile;
213 const char *msgid;
214 #endif
215 va_list ap;
217 VA_START (ap, msgid);
219 #ifndef ANSI_PROTOTYPES
220 pfile = va_arg (ap, cpp_reader *);
221 msgid = va_arg (ap, const char *);
222 #endif
224 if (_cpp_begin_message (pfile, ICE, NULL, 0))
225 v_message (msgid, ap);
226 va_end(ap);
229 /* Same as cpp_error, except we consider the error to be "fatal",
230 such as inconsistent options. I.e. there is little point in continuing.
231 (We do not exit, to support use of cpplib as a library.
232 Instead, it is the caller's responsibility to check
233 CPP_FATAL_ERRORS. */
235 void
236 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
238 #ifndef ANSI_PROTOTYPES
239 cpp_reader *pfile;
240 const char *msgid;
241 #endif
242 va_list ap;
244 VA_START (ap, msgid);
246 #ifndef ANSI_PROTOTYPES
247 pfile = va_arg (ap, cpp_reader *);
248 msgid = va_arg (ap, const char *);
249 #endif
251 if (_cpp_begin_message (pfile, FATAL, NULL, 0))
252 v_message (msgid, ap);
253 va_end(ap);
256 void
257 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
259 #ifndef ANSI_PROTOTYPES
260 cpp_reader *pfile;
261 const char *msgid;
262 #endif
263 va_list ap;
265 VA_START(ap, msgid);
267 #ifndef ANSI_PROTOTYPES
268 pfile = va_arg (ap, cpp_reader *);
269 msgid = va_arg (ap, const char *);
270 #endif
272 if (_cpp_begin_message (pfile, ERROR, NULL, 0))
273 v_message (msgid, ap);
274 va_end(ap);
277 void
278 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
279 const char *msgid, ...))
281 #ifndef ANSI_PROTOTYPES
282 cpp_reader *pfile;
283 int line;
284 int column;
285 const char *msgid;
286 #endif
287 va_list ap;
288 cpp_lexer_pos pos;
290 VA_START (ap, msgid);
292 #ifndef ANSI_PROTOTYPES
293 pfile = va_arg (ap, cpp_reader *);
294 line = va_arg (ap, int);
295 column = va_arg (ap, int);
296 msgid = va_arg (ap, const char *);
297 #endif
299 pos.line = line;
300 pos.col = column;
301 if (_cpp_begin_message (pfile, ERROR, NULL, &pos))
302 v_message (msgid, ap);
303 va_end(ap);
306 /* Error including a message from `errno'. */
307 void
308 cpp_error_from_errno (pfile, name)
309 cpp_reader *pfile;
310 const char *name;
312 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
315 void
316 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
318 #ifndef ANSI_PROTOTYPES
319 cpp_reader *pfile;
320 const char *msgid;
321 #endif
322 va_list ap;
324 VA_START (ap, msgid);
326 #ifndef ANSI_PROTOTYPES
327 pfile = va_arg (ap, cpp_reader *);
328 msgid = va_arg (ap, const char *);
329 #endif
331 if (_cpp_begin_message (pfile, WARNING, NULL, 0))
332 v_message (msgid, ap);
333 va_end(ap);
336 void
337 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
338 const char *msgid, ...))
340 #ifndef ANSI_PROTOTYPES
341 cpp_reader *pfile;
342 int line;
343 int column;
344 const char *msgid;
345 #endif
346 va_list ap;
347 cpp_lexer_pos pos;
349 VA_START (ap, msgid);
351 #ifndef ANSI_PROTOTYPES
352 pfile = va_arg (ap, cpp_reader *);
353 line = va_arg (ap, int);
354 column = va_arg (ap, int);
355 msgid = va_arg (ap, const char *);
356 #endif
358 pos.line = line;
359 pos.col = column;
360 if (_cpp_begin_message (pfile, WARNING, NULL, &pos))
361 v_message (msgid, ap);
362 va_end(ap);
365 void
366 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
368 #ifndef ANSI_PROTOTYPES
369 cpp_reader *pfile;
370 const char *msgid;
371 #endif
372 va_list ap;
374 VA_START (ap, msgid);
376 #ifndef ANSI_PROTOTYPES
377 pfile = va_arg (ap, cpp_reader *);
378 msgid = va_arg (ap, const char *);
379 #endif
381 if (_cpp_begin_message (pfile, PEDWARN, NULL, 0))
382 v_message (msgid, ap);
383 va_end(ap);
386 void
387 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
388 const char *msgid, ...))
390 #ifndef ANSI_PROTOTYPES
391 cpp_reader *pfile;
392 int line;
393 int column;
394 const char *msgid;
395 #endif
396 va_list ap;
397 cpp_lexer_pos pos;
399 VA_START (ap, msgid);
401 #ifndef ANSI_PROTOTYPES
402 pfile = va_arg (ap, cpp_reader *);
403 line = va_arg (ap, int);
404 column = va_arg (ap, int);
405 msgid = va_arg (ap, const char *);
406 #endif
408 pos.line = line;
409 pos.col = column;
410 if (_cpp_begin_message (pfile, PEDWARN, NULL, &pos))
411 v_message (msgid, ap);
412 va_end(ap);
415 /* Report a warning (or an error if pedantic_errors)
416 giving specified file name and line number, not current. */
418 void
419 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
420 const char *file, int line, int col,
421 const char *msgid, ...))
423 #ifndef ANSI_PROTOTYPES
424 cpp_reader *pfile;
425 const char *file;
426 int line;
427 int col;
428 const char *msgid;
429 #endif
430 va_list ap;
431 cpp_lexer_pos pos;
433 VA_START (ap, msgid);
435 #ifndef ANSI_PROTOTYPES
436 pfile = va_arg (ap, cpp_reader *);
437 file = va_arg (ap, const char *);
438 line = va_arg (ap, int);
439 col = va_arg (ap, int);
440 msgid = va_arg (ap, const char *);
441 #endif
443 pos.line = line;
444 pos.col = col;
445 if (_cpp_begin_message (pfile, PEDWARN, file, &pos))
446 v_message (msgid, ap);
447 va_end(ap);
450 /* Print an error message not associated with a file. */
451 void
452 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
454 #ifndef ANSI_PROTOTYPES
455 cpp_reader *pfile;
456 const char *msgid;
457 #endif
458 va_list ap;
460 VA_START (ap, msgid);
462 #ifndef ANSI_PROTOTYPES
463 pfile = va_arg (ap, cpp_reader *);
464 msgid = va_arg (ap, const char *);
465 #endif
467 if (pfile->errors < CPP_FATAL_LIMIT)
468 pfile->errors++;
470 vfprintf (stderr, _(msgid), ap);
471 putc('\n', stderr);
473 va_end(ap);
476 void
477 cpp_notice_from_errno (pfile, name)
478 cpp_reader *pfile;
479 const char *name;
481 if (name[0] == '\0')
482 name = "stdout";
483 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));