* c-common.c (check_function_format): Don't suggest adding format
[official-gcc.git] / gcc / cpperror.c
blobff83c6fb87b171cd95fc1f3a159320cf6f7ff853
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_reader *, cpp_buffer *));
33 static void print_file_and_line PARAMS ((const char *, unsigned int,
34 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. */
42 static void
43 print_containing_files (pfile, ip)
44 cpp_reader *pfile;
45 cpp_buffer *ip;
47 int first = 1;
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)
52 return;
54 /* Find the other, outer source files. */
55 for (ip = CPP_PREV_BUFFER (ip); ip != NULL; ip = CPP_PREV_BUFFER (ip))
57 if (first)
59 first = 0;
60 /* The current line in each outer source file is now the
61 same as the line of the #include. */
62 fprintf (stderr, _("In file included from %s:%u"),
63 ip->nominal_fname, CPP_BUF_LINE (ip));
65 else
66 /* Translators note: this message is used in conjunction
67 with "In file included from %s:%ld" and some other
68 tricks. We want something like this:
70 | In file included from sys/select.h:123,
71 | from sys/types.h:234,
72 | from userfile.c:31:
73 | bits/select.h:45: <error message here>
75 with all the "from"s lined up.
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:%u"),
79 ip->nominal_fname, CPP_BUF_LINE (ip) - 1);
81 if (first == 0)
82 fputs (":\n", stderr);
84 /* Record we have printed the status as of this time. */
85 pfile->input_stack_listing_current = 1;
88 static void
89 print_file_and_line (filename, line, col)
90 const char *filename;
91 unsigned int line, col;
93 if (filename == 0 || *filename == '\0')
94 filename = "<stdin>";
96 if (line == 0)
97 fprintf (stderr, "%s: ", filename);
98 else if (col == 0)
99 fprintf (stderr, "%s:%u: ", filename, line);
100 else
101 fprintf (stderr, "%s:%u:%u: ", filename, line, col);
104 /* Set up for an error message: print the file and line, bump the error
105 counter, etc.
106 If it returns 0, this error has been suppressed. */
109 _cpp_begin_message (pfile, code, file, pos)
110 cpp_reader *pfile;
111 enum error_type code;
112 const char *file;
113 const cpp_lexer_pos *pos;
115 cpp_buffer *ip = CPP_BUFFER (pfile);
116 int is_warning = 0;
118 switch (code)
120 case WARNING:
121 if (CPP_IN_SYSTEM_HEADER (pfile)
122 && ! CPP_OPTION (pfile, warn_system_headers))
123 return 0;
124 if (! CPP_OPTION (pfile, warnings_are_errors))
126 if (CPP_OPTION (pfile, inhibit_warnings))
127 return 0;
128 is_warning = 1;
130 else
132 if (CPP_OPTION (pfile, inhibit_errors))
133 return 0;
134 if (pfile->errors < CPP_FATAL_LIMIT)
135 pfile->errors++;
137 break;
139 case PEDWARN:
140 if (CPP_IN_SYSTEM_HEADER (pfile)
141 && ! CPP_OPTION (pfile, warn_system_headers))
142 return 0;
143 if (! CPP_OPTION (pfile, pedantic_errors))
145 if (CPP_OPTION (pfile, inhibit_warnings))
146 return 0;
147 is_warning = 1;
149 else
151 if (CPP_OPTION (pfile, inhibit_errors))
152 return 0;
153 if (pfile->errors < CPP_FATAL_LIMIT)
154 pfile->errors++;
156 break;
158 case ERROR:
159 if (CPP_OPTION (pfile, inhibit_errors))
160 return 0;
161 if (pfile->errors < CPP_FATAL_LIMIT)
162 pfile->errors++;
163 break;
164 /* Fatal errors cannot be inhibited. */
165 case FATAL:
166 pfile->errors = CPP_FATAL_LIMIT;
167 break;
168 case ICE:
169 fprintf (stderr, _("internal error: "));
170 pfile->errors = CPP_FATAL_LIMIT;
171 break;
174 if (ip)
176 if (file == NULL)
177 file = ip->nominal_fname;
178 if (pos == 0)
179 pos = cpp_get_line (pfile);
180 print_containing_files (pfile, ip);
181 print_file_and_line (file, pos->line,
182 CPP_OPTION (pfile, show_column) ? pos->col : 0);
184 else
185 fprintf (stderr, "%s: ", progname);
187 if (is_warning)
188 fputs (_("warning: "), stderr);
190 return 1;
193 /* Exported interface. */
195 /* For reporting internal errors. Prints "internal error: " for you,
196 otherwise identical to cpp_fatal. */
198 void
199 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
201 #ifndef ANSI_PROTOTYPES
202 cpp_reader *pfile;
203 const char *msgid;
204 #endif
205 va_list ap;
207 VA_START (ap, msgid);
209 #ifndef ANSI_PROTOTYPES
210 pfile = va_arg (ap, cpp_reader *);
211 msgid = va_arg (ap, const char *);
212 #endif
214 if (_cpp_begin_message (pfile, ICE, NULL, 0))
215 v_message (msgid, ap);
216 va_end(ap);
219 /* Same as cpp_error, except we consider the error to be "fatal",
220 such as inconsistent options. I.e. there is little point in continuing.
221 (We do not exit, to support use of cpplib as a library.
222 Instead, it is the caller's responsibility to check
223 CPP_FATAL_ERRORS. */
225 void
226 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
228 #ifndef ANSI_PROTOTYPES
229 cpp_reader *pfile;
230 const char *msgid;
231 #endif
232 va_list ap;
234 VA_START (ap, msgid);
236 #ifndef ANSI_PROTOTYPES
237 pfile = va_arg (ap, cpp_reader *);
238 msgid = va_arg (ap, const char *);
239 #endif
241 if (_cpp_begin_message (pfile, FATAL, NULL, 0))
242 v_message (msgid, ap);
243 va_end(ap);
246 void
247 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
249 #ifndef ANSI_PROTOTYPES
250 cpp_reader *pfile;
251 const char *msgid;
252 #endif
253 va_list ap;
255 VA_START(ap, msgid);
257 #ifndef ANSI_PROTOTYPES
258 pfile = va_arg (ap, cpp_reader *);
259 msgid = va_arg (ap, const char *);
260 #endif
262 if (_cpp_begin_message (pfile, ERROR, NULL, 0))
263 v_message (msgid, ap);
264 va_end(ap);
267 void
268 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
269 const char *msgid, ...))
271 #ifndef ANSI_PROTOTYPES
272 cpp_reader *pfile;
273 int line;
274 int column;
275 const char *msgid;
276 #endif
277 va_list ap;
278 cpp_lexer_pos pos;
280 VA_START (ap, msgid);
282 #ifndef ANSI_PROTOTYPES
283 pfile = va_arg (ap, cpp_reader *);
284 line = va_arg (ap, int);
285 column = va_arg (ap, int);
286 msgid = va_arg (ap, const char *);
287 #endif
289 pos.line = line;
290 pos.col = column;
291 if (_cpp_begin_message (pfile, ERROR, NULL, &pos))
292 v_message (msgid, ap);
293 va_end(ap);
296 /* Error including a message from `errno'. */
297 void
298 cpp_error_from_errno (pfile, name)
299 cpp_reader *pfile;
300 const char *name;
302 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
305 void
306 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
308 #ifndef ANSI_PROTOTYPES
309 cpp_reader *pfile;
310 const char *msgid;
311 #endif
312 va_list ap;
314 VA_START (ap, msgid);
316 #ifndef ANSI_PROTOTYPES
317 pfile = va_arg (ap, cpp_reader *);
318 msgid = va_arg (ap, const char *);
319 #endif
321 if (_cpp_begin_message (pfile, WARNING, NULL, 0))
322 v_message (msgid, ap);
323 va_end(ap);
326 void
327 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
328 const char *msgid, ...))
330 #ifndef ANSI_PROTOTYPES
331 cpp_reader *pfile;
332 int line;
333 int column;
334 const char *msgid;
335 #endif
336 va_list ap;
337 cpp_lexer_pos pos;
339 VA_START (ap, msgid);
341 #ifndef ANSI_PROTOTYPES
342 pfile = va_arg (ap, cpp_reader *);
343 line = va_arg (ap, int);
344 column = va_arg (ap, int);
345 msgid = va_arg (ap, const char *);
346 #endif
348 pos.line = line;
349 pos.col = column;
350 if (_cpp_begin_message (pfile, WARNING, NULL, &pos))
351 v_message (msgid, ap);
352 va_end(ap);
355 void
356 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
358 #ifndef ANSI_PROTOTYPES
359 cpp_reader *pfile;
360 const char *msgid;
361 #endif
362 va_list ap;
364 VA_START (ap, msgid);
366 #ifndef ANSI_PROTOTYPES
367 pfile = va_arg (ap, cpp_reader *);
368 msgid = va_arg (ap, const char *);
369 #endif
371 if (_cpp_begin_message (pfile, PEDWARN, NULL, 0))
372 v_message (msgid, ap);
373 va_end(ap);
376 void
377 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
378 const char *msgid, ...))
380 #ifndef ANSI_PROTOTYPES
381 cpp_reader *pfile;
382 int line;
383 int column;
384 const char *msgid;
385 #endif
386 va_list ap;
387 cpp_lexer_pos pos;
389 VA_START (ap, msgid);
391 #ifndef ANSI_PROTOTYPES
392 pfile = va_arg (ap, cpp_reader *);
393 line = va_arg (ap, int);
394 column = va_arg (ap, int);
395 msgid = va_arg (ap, const char *);
396 #endif
398 pos.line = line;
399 pos.col = column;
400 if (_cpp_begin_message (pfile, PEDWARN, NULL, &pos))
401 v_message (msgid, ap);
402 va_end(ap);
405 /* Report a warning (or an error if pedantic_errors)
406 giving specified file name and line number, not current. */
408 void
409 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
410 const char *file, int line, int col,
411 const char *msgid, ...))
413 #ifndef ANSI_PROTOTYPES
414 cpp_reader *pfile;
415 const char *file;
416 int line;
417 int col;
418 const char *msgid;
419 #endif
420 va_list ap;
421 cpp_lexer_pos pos;
423 VA_START (ap, msgid);
425 #ifndef ANSI_PROTOTYPES
426 pfile = va_arg (ap, cpp_reader *);
427 file = va_arg (ap, const char *);
428 line = va_arg (ap, int);
429 col = va_arg (ap, int);
430 msgid = va_arg (ap, const char *);
431 #endif
433 pos.line = line;
434 pos.col = col;
435 if (_cpp_begin_message (pfile, PEDWARN, file, &pos))
436 v_message (msgid, ap);
437 va_end(ap);
440 /* Print an error message not associated with a file. */
441 void
442 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
444 #ifndef ANSI_PROTOTYPES
445 cpp_reader *pfile;
446 const char *msgid;
447 #endif
448 va_list ap;
450 VA_START (ap, msgid);
452 #ifndef ANSI_PROTOTYPES
453 pfile = va_arg (ap, cpp_reader *);
454 msgid = va_arg (ap, const char *);
455 #endif
457 if (pfile->errors < CPP_FATAL_LIMIT)
458 pfile->errors++;
460 vfprintf (stderr, _(msgid), ap);
461 putc('\n', stderr);
463 va_end(ap);
466 void
467 cpp_notice_from_errno (pfile, name)
468 cpp_reader *pfile;
469 const char *name;
471 if (name[0] == '\0')
472 name = "stdout";
473 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));