Only create gcc/configargs.h if gcc build directory is present
[official-gcc.git] / gcc / cpperror.c
blobfa44708d50030d5848aa6373fd42858cf6fc41e6
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 /* 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);
66 else
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,
73 | from userfile.c:31:
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);
82 if (first == 0)
83 fputs (":\n", stderr);
85 /* Record we have printed the status as of this time. */
86 pfile->input_stack_listing_current = 1;
89 static void
90 print_file_and_line (filename, line, column)
91 const char *filename;
92 unsigned int line, column;
94 if (filename == 0 || *filename == '\0')
95 filename = "<stdin>";
97 if (line == (unsigned int)-1)
98 fprintf (stderr, "%s: ", filename);
99 else if (column > 0)
100 fprintf (stderr, "%s:%u:%u: ", filename, line, column);
101 else
102 fprintf (stderr, "%s:%u: ", filename, line);
105 /* Set up for an error message: print the file and line, bump the error
106 counter, etc.
107 If it returns 0, this error has been suppressed. */
110 _cpp_begin_message (pfile, code, file, line, col)
111 cpp_reader *pfile;
112 enum error_type code;
113 const char *file;
114 unsigned int line;
115 unsigned int col;
117 cpp_buffer *ip = CPP_BUFFER (pfile);
118 int is_warning = 0;
120 switch (code)
122 case WARNING:
123 if (CPP_IN_SYSTEM_HEADER (pfile)
124 && ! CPP_OPTION (pfile, warn_system_headers))
125 return 0;
126 if (! CPP_OPTION (pfile, warnings_are_errors))
128 if (CPP_OPTION (pfile, inhibit_warnings))
129 return 0;
130 is_warning = 1;
132 else
134 if (CPP_OPTION (pfile, inhibit_errors))
135 return 0;
136 if (pfile->errors < CPP_FATAL_LIMIT)
137 pfile->errors++;
139 break;
141 case PEDWARN:
142 if (CPP_IN_SYSTEM_HEADER (pfile)
143 && ! CPP_OPTION (pfile, warn_system_headers))
144 return 0;
145 if (! CPP_OPTION (pfile, pedantic_errors))
147 if (CPP_OPTION (pfile, inhibit_warnings))
148 return 0;
149 is_warning = 1;
151 else
153 if (CPP_OPTION (pfile, inhibit_errors))
154 return 0;
155 if (pfile->errors < CPP_FATAL_LIMIT)
156 pfile->errors++;
158 break;
160 case ERROR:
161 if (CPP_OPTION (pfile, inhibit_errors))
162 return 0;
163 if (pfile->errors < CPP_FATAL_LIMIT)
164 pfile->errors++;
165 break;
166 /* Fatal errors cannot be inhibited. */
167 case FATAL:
168 pfile->errors = CPP_FATAL_LIMIT;
169 break;
170 case ICE:
171 fprintf (stderr, _("internal error: "));
172 pfile->errors = CPP_FATAL_LIMIT;
173 break;
176 if (ip)
178 if (file == NULL)
179 file = ip->nominal_fname;
180 if (line == 0)
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);
186 else
187 fprintf (stderr, "%s: ", progname);
189 if (is_warning)
190 fputs (_("warning: "), stderr);
192 return 1;
195 /* Exported interface. */
197 /* For reporting internal errors. Prints "internal error: " for you,
198 otherwise identical to cpp_fatal. */
200 void
201 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
203 #ifndef ANSI_PROTOTYPES
204 cpp_reader *pfile;
205 const char *msgid;
206 #endif
207 va_list ap;
209 VA_START (ap, msgid);
211 #ifndef ANSI_PROTOTYPES
212 pfile = va_arg (ap, cpp_reader *);
213 msgid = va_arg (ap, const char *);
214 #endif
216 if (_cpp_begin_message (pfile, ICE, NULL, 0, 0))
217 v_message (msgid, ap);
218 va_end(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
225 CPP_FATAL_ERRORS. */
227 void
228 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
230 #ifndef ANSI_PROTOTYPES
231 cpp_reader *pfile;
232 const char *msgid;
233 #endif
234 va_list ap;
236 VA_START (ap, msgid);
238 #ifndef ANSI_PROTOTYPES
239 pfile = va_arg (ap, cpp_reader *);
240 msgid = va_arg (ap, const char *);
241 #endif
243 if (_cpp_begin_message (pfile, FATAL, NULL, 0, 0))
244 v_message (msgid, ap);
245 va_end(ap);
248 void
249 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
251 #ifndef ANSI_PROTOTYPES
252 cpp_reader *pfile;
253 const char *msgid;
254 #endif
255 va_list ap;
257 VA_START(ap, msgid);
259 #ifndef ANSI_PROTOTYPES
260 pfile = va_arg (ap, cpp_reader *);
261 msgid = va_arg (ap, const char *);
262 #endif
264 if (_cpp_begin_message (pfile, ERROR, NULL, 0, 0))
265 v_message (msgid, ap);
266 va_end(ap);
269 void
270 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
271 const char *msgid, ...))
273 #ifndef ANSI_PROTOTYPES
274 cpp_reader *pfile;
275 int line;
276 int column;
277 const char *msgid;
278 #endif
279 va_list ap;
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 *);
288 #endif
290 if (_cpp_begin_message (pfile, ERROR, NULL, line, column))
291 v_message (msgid, ap);
292 va_end(ap);
295 /* Error including a message from `errno'. */
296 void
297 cpp_error_from_errno (pfile, name)
298 cpp_reader *pfile;
299 const char *name;
301 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
304 void
305 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
307 #ifndef ANSI_PROTOTYPES
308 cpp_reader *pfile;
309 const char *msgid;
310 #endif
311 va_list ap;
313 VA_START (ap, msgid);
315 #ifndef ANSI_PROTOTYPES
316 pfile = va_arg (ap, cpp_reader *);
317 msgid = va_arg (ap, const char *);
318 #endif
320 if (_cpp_begin_message (pfile, WARNING, NULL, 0, 0))
321 v_message (msgid, ap);
322 va_end(ap);
325 void
326 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
327 const char *msgid, ...))
329 #ifndef ANSI_PROTOTYPES
330 cpp_reader *pfile;
331 int line;
332 int column;
333 const char *msgid;
334 #endif
335 va_list ap;
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 *);
344 #endif
346 if (_cpp_begin_message (pfile, WARNING, NULL, line, column))
347 v_message (msgid, ap);
348 va_end(ap);
351 void
352 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
354 #ifndef ANSI_PROTOTYPES
355 cpp_reader *pfile;
356 const char *msgid;
357 #endif
358 va_list ap;
360 VA_START (ap, msgid);
362 #ifndef ANSI_PROTOTYPES
363 pfile = va_arg (ap, cpp_reader *);
364 msgid = va_arg (ap, const char *);
365 #endif
367 if (_cpp_begin_message (pfile, PEDWARN, NULL, 0, 0))
368 v_message (msgid, ap);
369 va_end(ap);
372 void
373 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
374 const char *msgid, ...))
376 #ifndef ANSI_PROTOTYPES
377 cpp_reader *pfile;
378 int line;
379 int column;
380 const char *msgid;
381 #endif
382 va_list ap;
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 *);
391 #endif
393 if (_cpp_begin_message (pfile, PEDWARN, NULL, line, column))
394 v_message (msgid, ap);
395 va_end(ap);
398 /* Report a warning (or an error if pedantic_errors)
399 giving specified file name and line number, not current. */
401 void
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
407 cpp_reader *pfile;
408 const char *file;
409 int line;
410 int col;
411 const char *msgid;
412 #endif
413 va_list ap;
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 *);
423 #endif
425 if (_cpp_begin_message (pfile, PEDWARN, file, line, col))
426 v_message (msgid, ap);
427 va_end(ap);
430 /* Print an error message not associated with a file. */
431 void
432 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
434 #ifndef ANSI_PROTOTYPES
435 cpp_reader *pfile;
436 const char *msgid;
437 #endif
438 va_list ap;
440 VA_START (ap, msgid);
442 #ifndef ANSI_PROTOTYPES
443 pfile = va_arg (ap, cpp_reader *);
444 msgid = va_arg (ap, const char *);
445 #endif
447 if (pfile->errors < CPP_FATAL_LIMIT)
448 pfile->errors++;
450 vfprintf (stderr, _(msgid), ap);
451 putc('\n', stderr);
453 va_end(ap);
456 void
457 cpp_notice_from_errno (pfile, name)
458 cpp_reader *pfile;
459 const char *name;
461 if (name[0] == '\0')
462 name = "stdout";
463 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
466 const char *
467 cpp_type2name (type)
468 enum cpp_ttype type;
470 return (const char *) _cpp_token_spellings[type].name;