Fix (<cond> ? FOO++ : BAR++) == 2 from misoptimizing FOO++ into ++FOO without bumping...
[official-gcc.git] / gcc / cpperror.c
bloba2769b6dbdab81a16c3fcd4e7cc59d59a3bceda8
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));
35 static void v_message PARAMS ((cpp_reader *, int,
36 const char *,
37 unsigned int, unsigned int,
38 const char *, va_list));
40 /* Print the file names and line numbers of the #include
41 commands which led to the current file. */
43 static void
44 print_containing_files (pfile, ip)
45 cpp_reader *pfile;
46 cpp_buffer *ip;
48 int first = 1;
50 /* If stack of files hasn't changed since we last printed
51 this info, don't repeat it. */
52 if (pfile->input_stack_listing_current)
53 return;
55 /* Find the other, outer source files. */
56 for (ip = CPP_PREV_BUFFER (ip); ip != NULL; ip = CPP_PREV_BUFFER (ip))
58 if (first)
60 first = 0;
61 /* N.B. The current line in each outer source file is one
62 greater than the line of the #include, so we must
63 subtract one to correct for that. */
64 fprintf (stderr, _("In file included from %s:%u"),
65 ip->nominal_fname, CPP_BUF_LINE (ip) - 1);
67 else
68 /* Translators note: this message is used in conjunction
69 with "In file included from %s:%ld" and some other
70 tricks. We want something like this:
72 | In file included from sys/select.h:123,
73 | from sys/types.h:234,
74 | from userfile.c:31:
75 | bits/select.h:45: <error message here>
77 with all the "from"s lined up.
78 The trailing comma is at the beginning of this message,
79 and the trailing colon is not translated. */
80 fprintf (stderr, _(",\n from %s:%u"),
81 ip->nominal_fname, CPP_BUF_LINE (ip) - 1);
83 if (first == 0)
84 fputs (":\n", stderr);
86 /* Record we have printed the status as of this time. */
87 pfile->input_stack_listing_current = 1;
90 static void
91 print_file_and_line (filename, line, column)
92 const char *filename;
93 unsigned int line, column;
95 if (filename == 0 || *filename == '\0')
96 filename = "<stdin>";
97 if (line == 0)
98 fputs (_("<command line>: "), stderr);
99 else if (column > 0)
100 fprintf (stderr, "%s:%u:%u: ", filename, line, column);
101 else
102 fprintf (stderr, "%s:%u: ", filename, line);
105 /* IS_ERROR is 3 for ICE, 2 for merely "fatal" error,
106 1 for error, 0 for warning. */
108 static void
109 v_message (pfile, is_error, file, line, col, msg, ap)
110 cpp_reader *pfile;
111 int is_error;
112 const char *file;
113 unsigned int line;
114 unsigned int col;
115 const char *msg;
116 va_list ap;
118 cpp_buffer *ip = CPP_BUFFER (pfile);
120 if (ip)
122 if (file == NULL)
123 file = ip->nominal_fname;
124 if (line == 0)
125 line = _cpp_get_line (pfile, &col);
126 print_containing_files (pfile, ip);
127 print_file_and_line (file, line,
128 CPP_OPTION (pfile, show_column) ? col : 0);
130 else
131 fprintf (stderr, "%s: ", progname);
133 switch (is_error)
135 case 0:
136 if (! CPP_OPTION (pfile, warnings_are_errors))
138 fprintf (stderr, _("warning: "));
139 break;
141 /* else fall through */
142 case 1:
143 if (pfile->errors < CPP_FATAL_LIMIT)
144 pfile->errors++;
145 break;
146 case 2:
147 pfile->errors = CPP_FATAL_LIMIT;
148 break;
149 case 3:
150 fprintf (stderr, _("internal error: "));
151 pfile->errors = CPP_FATAL_LIMIT;
152 break;
153 default:
154 cpp_ice (pfile, "bad is_error(%d) in v_message", is_error);
157 vfprintf (stderr, _(msg), ap);
158 putc ('\n', stderr);
161 /* Exported interface. */
163 /* For reporting internal errors. Prints "internal error: " for you,
164 otherwise identical to cpp_fatal. */
166 void
167 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
169 #ifndef ANSI_PROTOTYPES
170 cpp_reader *pfile;
171 const char *msgid;
172 #endif
173 va_list ap;
175 VA_START (ap, msgid);
177 #ifndef ANSI_PROTOTYPES
178 pfile = va_arg (ap, cpp_reader *);
179 msgid = va_arg (ap, const char *);
180 #endif
182 v_message (pfile, 3, NULL, 0, 0, msgid, ap);
183 va_end(ap);
186 /* Same as cpp_error, except we consider the error to be "fatal",
187 such as inconsistent options. I.e. there is little point in continuing.
188 (We do not exit, to support use of cpplib as a library.
189 Instead, it is the caller's responsibility to check
190 CPP_FATAL_ERRORS. */
192 void
193 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
195 #ifndef ANSI_PROTOTYPES
196 cpp_reader *pfile;
197 const char *msgid;
198 #endif
199 va_list ap;
201 VA_START (ap, msgid);
203 #ifndef ANSI_PROTOTYPES
204 pfile = va_arg (ap, cpp_reader *);
205 msgid = va_arg (ap, const char *);
206 #endif
208 v_message (pfile, 2, NULL, 0, 0, msgid, ap);
209 va_end(ap);
212 void
213 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
215 #ifndef ANSI_PROTOTYPES
216 cpp_reader *pfile;
217 const char *msgid;
218 #endif
219 va_list ap;
221 VA_START(ap, msgid);
223 #ifndef ANSI_PROTOTYPES
224 pfile = va_arg (ap, cpp_reader *);
225 msgid = va_arg (ap, const char *);
226 #endif
228 if (CPP_OPTION (pfile, inhibit_errors))
229 return;
231 v_message (pfile, 1, NULL, 0, 0, msgid, ap);
232 va_end(ap);
235 void
236 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
237 const char *msgid, ...))
239 #ifndef ANSI_PROTOTYPES
240 cpp_reader *pfile;
241 int line;
242 int column;
243 const char *msgid;
244 #endif
245 va_list ap;
247 VA_START (ap, msgid);
249 #ifndef ANSI_PROTOTYPES
250 pfile = va_arg (ap, cpp_reader *);
251 line = va_arg (ap, int);
252 column = va_arg (ap, int);
253 msgid = va_arg (ap, const char *);
254 #endif
256 if (CPP_OPTION (pfile, inhibit_errors))
257 return;
259 v_message (pfile, 1, NULL, line, column, msgid, ap);
260 va_end(ap);
263 /* Error including a message from `errno'. */
264 void
265 cpp_error_from_errno (pfile, name)
266 cpp_reader *pfile;
267 const char *name;
269 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
272 void
273 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
275 #ifndef ANSI_PROTOTYPES
276 cpp_reader *pfile;
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 msgid = va_arg (ap, const char *);
286 #endif
288 if (CPP_OPTION (pfile, inhibit_warnings))
289 return;
291 v_message (pfile, 0, NULL, 0, 0, msgid, ap);
292 va_end(ap);
295 void
296 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
297 const char *msgid, ...))
299 #ifndef ANSI_PROTOTYPES
300 cpp_reader *pfile;
301 int line;
302 int column;
303 const char *msgid;
304 #endif
305 va_list ap;
307 VA_START (ap, msgid);
309 #ifndef ANSI_PROTOTYPES
310 pfile = va_arg (ap, cpp_reader *);
311 line = va_arg (ap, int);
312 column = va_arg (ap, int);
313 msgid = va_arg (ap, const char *);
314 #endif
316 if (CPP_OPTION (pfile, inhibit_warnings))
317 return;
319 v_message (pfile, 0, NULL, line, column, msgid, ap);
320 va_end(ap);
323 void
324 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
326 #ifndef ANSI_PROTOTYPES
327 cpp_reader *pfile;
328 const char *msgid;
329 #endif
330 va_list ap;
332 VA_START (ap, msgid);
334 #ifndef ANSI_PROTOTYPES
335 pfile = va_arg (ap, cpp_reader *);
336 msgid = va_arg (ap, const char *);
337 #endif
339 if (CPP_OPTION (pfile, pedantic_errors)
340 ? CPP_OPTION (pfile, inhibit_errors)
341 : CPP_OPTION (pfile, inhibit_warnings))
342 return;
344 v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
345 NULL, 0, 0, msgid, ap);
346 va_end(ap);
349 void
350 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
351 const char *msgid, ...))
353 #ifndef ANSI_PROTOTYPES
354 cpp_reader *pfile;
355 int line;
356 int column;
357 const char *msgid;
358 #endif
359 va_list ap;
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 *);
368 #endif
370 if (CPP_OPTION (pfile, pedantic_errors)
371 ? CPP_OPTION (pfile, inhibit_errors)
372 : CPP_OPTION (pfile, inhibit_warnings))
373 return;
375 v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
376 NULL, line, column, msgid, ap);
377 va_end(ap);
380 /* Report a warning (or an error if pedantic_errors)
381 giving specified file name and line number, not current. */
383 void
384 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
385 const char *file, int line, int col,
386 const char *msgid, ...))
388 #ifndef ANSI_PROTOTYPES
389 cpp_reader *pfile;
390 const char *file;
391 int line;
392 int col;
393 const char *msgid;
394 #endif
395 va_list ap;
397 VA_START (ap, msgid);
399 #ifndef ANSI_PROTOTYPES
400 pfile = va_arg (ap, cpp_reader *);
401 file = va_arg (ap, const char *);
402 line = va_arg (ap, int);
403 col = va_arg (ap, int);
404 msgid = va_arg (ap, const char *);
405 #endif
407 if (CPP_OPTION (pfile, pedantic_errors)
408 ? CPP_OPTION (pfile, inhibit_errors)
409 : CPP_OPTION (pfile, inhibit_warnings))
410 return;
412 v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
413 file, line, col, msgid, ap);
414 va_end(ap);
417 /* Print an error message not associated with a file. */
418 void
419 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
421 #ifndef ANSI_PROTOTYPES
422 cpp_reader *pfile;
423 const char *msgid;
424 #endif
425 va_list ap;
427 VA_START (ap, msgid);
429 #ifndef ANSI_PROTOTYPES
430 pfile = va_arg (ap, cpp_reader *);
431 msgid = va_arg (ap, const char *);
432 #endif
434 if (pfile->errors < CPP_FATAL_LIMIT)
435 pfile->errors++;
437 vfprintf (stderr, _(msgid), ap);
438 putc('\n', stderr);
440 va_end(ap);
443 void
444 cpp_notice_from_errno (pfile, name)
445 cpp_reader *pfile;
446 const char *name;
448 if (name[0] == '\0')
449 name = "stdout";
450 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));