* rtl.h (rtunion_def): Constify member `rtstr'.
[official-gcc.git] / gcc / cpperror.c
blobb72740ce70d99ef8195d90a17d4b9d2e1e61f5cc
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 "intl.h"
31 static void cpp_print_containing_files PARAMS ((cpp_reader *, cpp_buffer *));
32 static void cpp_print_file_and_line PARAMS ((const char *, long, long));
33 static void v_cpp_message PARAMS ((cpp_reader *, int,
34 const char *, long, long,
35 const char *, va_list));
37 /* Print the file names and line numbers of the #include
38 commands which led to the current file. */
40 static void
41 cpp_print_containing_files (pfile, ip)
42 cpp_reader *pfile;
43 cpp_buffer *ip;
45 int first = 1;
47 /* If stack of files hasn't changed since we last printed
48 this info, don't repeat it. */
49 if (pfile->input_stack_listing_current)
50 return;
52 /* Find the other, outer source files. */
53 for (ip = CPP_PREV_BUFFER (ip);
54 ip != CPP_NULL_BUFFER (pfile);
55 ip = CPP_PREV_BUFFER (ip))
56 if (ip->fname != NULL)
58 long line;
59 cpp_buf_line_and_col (ip, &line, NULL);
60 if (first)
62 first = 0;
63 fprintf (stderr, _("In file included from %s:%ld"),
64 ip->nominal_fname, line);
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 The trailing comma is at the beginning of this message,
77 and the trailing colon is not translated. */
78 fprintf (stderr, _(",\n from %s:%ld"),
79 ip->nominal_fname, line);
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 cpp_print_file_and_line (filename, line, column)
90 const char *filename;
91 long line, column;
93 if (filename == 0 || *filename == '\0')
94 filename = "<stdin>";
95 if (line <= 0)
96 fputs (_("<command line>: "), stderr);
97 else if (column > 0)
98 fprintf (stderr, "%s:%ld:%ld: ", filename, line, column);
99 else
100 fprintf (stderr, "%s:%ld: ", filename, line);
103 /* IS_ERROR is 3 for ICE, 2 for merely "fatal" error,
104 1 for error, 0 for warning. */
106 static void
107 v_cpp_message (pfile, is_error, file, line, col, msg, ap)
108 cpp_reader *pfile;
109 int is_error;
110 const char *file;
111 long line;
112 long col;
113 const char *msg;
114 va_list ap;
116 cpp_buffer *ip = cpp_file_buffer (pfile);
118 if (ip)
120 if (file == NULL)
121 file = ip->nominal_fname;
122 if (line == -1)
123 cpp_buf_line_and_col (ip, &line, &col);
125 cpp_print_containing_files (pfile, ip);
126 cpp_print_file_and_line (file, line, col);
128 else
129 fprintf (stderr, "%s: ", progname);
131 switch (is_error)
133 case 0:
134 fprintf (stderr, _("warning: "));
135 break;
136 case 1:
137 if (pfile->errors < CPP_FATAL_LIMIT)
138 pfile->errors++;
139 break;
140 case 2:
141 pfile->errors = CPP_FATAL_LIMIT;
142 break;
143 case 3:
144 fprintf (stderr, _("internal error: "));
145 pfile->errors = CPP_FATAL_LIMIT;
146 break;
147 default:
148 cpp_ice (pfile, "bad is_error(%d) in v_cpp_message", is_error);
151 vfprintf (stderr, _(msg), ap);
152 putc ('\n', stderr);
155 /* Exported interface. */
157 /* For reporting internal errors. Prints "internal error: " for you,
158 otherwise identical to cpp_fatal. */
160 void
161 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
163 #ifndef ANSI_PROTOTYPES
164 cpp_reader *pfile;
165 const char *msgid;
166 #endif
167 va_list ap;
169 VA_START (ap, msgid);
171 #ifndef ANSI_PROTOTYPES
172 pfile = va_arg (ap, cpp_reader *);
173 msgid = va_arg (ap, const char *);
174 #endif
176 v_cpp_message (pfile, 3, NULL, -1, -1, msgid, ap);
177 va_end(ap);
180 /* Same as cpp_error, except we consider the error to be "fatal",
181 such as inconsistent options. I.e. there is little point in continuing.
182 (We do not exit, to support use of cpplib as a library.
183 Instead, it is the caller's responsibility to check
184 CPP_FATAL_ERRORS. */
186 void
187 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
189 #ifndef ANSI_PROTOTYPES
190 cpp_reader *pfile;
191 const char *msgid;
192 #endif
193 va_list ap;
195 VA_START (ap, msgid);
197 #ifndef ANSI_PROTOTYPES
198 pfile = va_arg (ap, cpp_reader *);
199 msgid = va_arg (ap, const char *);
200 #endif
202 v_cpp_message (pfile, 2, NULL, -1, -1, msgid, ap);
203 va_end(ap);
206 void
207 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
209 #ifndef ANSI_PROTOTYPES
210 cpp_reader *pfile;
211 const char *msgid;
212 #endif
213 va_list ap;
215 VA_START(ap, msgid);
217 #ifndef ANSI_PROTOTYPES
218 pfile = va_arg (ap, cpp_reader *);
219 msgid = va_arg (ap, const char *);
220 #endif
222 if (CPP_OPTIONS (pfile)->inhibit_errors)
223 return;
225 v_cpp_message (pfile, 1, NULL, -1, -1, msgid, ap);
226 va_end(ap);
229 void
230 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
231 const char *msgid, ...))
233 #ifndef ANSI_PROTOTYPES
234 cpp_reader *pfile;
235 int line;
236 int column;
237 const char *msgid;
238 #endif
239 va_list ap;
241 VA_START (ap, msgid);
243 #ifndef ANSI_PROTOTYPES
244 pfile = va_arg (ap, cpp_reader *);
245 line = va_arg (ap, int);
246 column = va_arg (ap, int);
247 msgid = va_arg (ap, const char *);
248 #endif
250 if (CPP_OPTIONS (pfile)->inhibit_errors)
251 return;
253 v_cpp_message (pfile, 1, NULL, line, column, msgid, ap);
254 va_end(ap);
257 /* Error including a message from `errno'. */
258 void
259 cpp_error_from_errno (pfile, name)
260 cpp_reader *pfile;
261 const char *name;
263 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
266 void
267 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
269 #ifndef ANSI_PROTOTYPES
270 cpp_reader *pfile;
271 const char *msgid;
272 #endif
273 va_list ap;
275 VA_START (ap, msgid);
277 #ifndef ANSI_PROTOTYPES
278 pfile = va_arg (ap, cpp_reader *);
279 msgid = va_arg (ap, const char *);
280 #endif
282 if (CPP_OPTIONS (pfile)->inhibit_warnings)
283 return;
285 v_cpp_message (pfile, 0, NULL, -1, -1, msgid, ap);
286 va_end(ap);
289 void
290 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
291 const char *msgid, ...))
293 #ifndef ANSI_PROTOTYPES
294 cpp_reader *pfile;
295 int line;
296 int column;
297 const char *msgid;
298 #endif
299 va_list ap;
301 VA_START (ap, msgid);
303 #ifndef ANSI_PROTOTYPES
304 pfile = va_arg (ap, cpp_reader *);
305 line = va_arg (ap, int);
306 column = va_arg (ap, int);
307 msgid = va_arg (ap, const char *);
308 #endif
310 if (CPP_OPTIONS (pfile)->inhibit_warnings)
311 return;
313 v_cpp_message (pfile, 0, NULL, line, column, msgid, ap);
314 va_end(ap);
317 void
318 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
320 #ifndef ANSI_PROTOTYPES
321 cpp_reader *pfile;
322 const char *msgid;
323 #endif
324 va_list ap;
326 VA_START (ap, msgid);
328 #ifndef ANSI_PROTOTYPES
329 pfile = va_arg (ap, cpp_reader *);
330 msgid = va_arg (ap, const char *);
331 #endif
333 if (CPP_OPTIONS (pfile)->pedantic_errors
334 ? CPP_OPTIONS (pfile)->inhibit_errors
335 : CPP_OPTIONS (pfile)->inhibit_warnings)
336 return;
338 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
339 NULL, -1, -1, msgid, ap);
340 va_end(ap);
343 void
344 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
345 const char *msgid, ...))
347 #ifndef ANSI_PROTOTYPES
348 cpp_reader *pfile;
349 int line;
350 int column;
351 const char *msgid;
352 #endif
353 va_list ap;
355 VA_START (ap, msgid);
357 #ifndef ANSI_PROTOTYPES
358 pfile = va_arg (ap, cpp_reader *);
359 line = va_arg (ap, int);
360 column = va_arg (ap, int);
361 msgid = va_arg (ap, const char *);
362 #endif
364 if (CPP_OPTIONS (pfile)->pedantic_errors
365 ? CPP_OPTIONS (pfile)->inhibit_errors
366 : CPP_OPTIONS (pfile)->inhibit_warnings)
367 return;
369 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
370 NULL, line, column, msgid, ap);
371 va_end(ap);
374 /* Report a warning (or an error if pedantic_errors)
375 giving specified file name and line number, not current. */
377 void
378 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
379 const char *file, int line, int col,
380 const char *msgid, ...))
382 #ifndef ANSI_PROTOTYPES
383 cpp_reader *pfile;
384 const char *file;
385 int line;
386 int col;
387 const char *msgid;
388 #endif
389 va_list ap;
391 VA_START (ap, msgid);
393 #ifndef ANSI_PROTOTYPES
394 pfile = va_arg (ap, cpp_reader *);
395 file = va_arg (ap, const char *);
396 line = va_arg (ap, int);
397 col = va_arg (ap, int);
398 msgid = va_arg (ap, const char *);
399 #endif
401 if (CPP_OPTIONS (pfile)->pedantic_errors
402 ? CPP_OPTIONS (pfile)->inhibit_errors
403 : CPP_OPTIONS (pfile)->inhibit_warnings)
404 return;
406 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
407 file, line, col, msgid, ap);
408 va_end(ap);
411 /* Print an error message not associated with a file. */
412 void
413 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
415 #ifndef ANSI_PROTOTYPES
416 cpp_reader *pfile;
417 const char *msgid;
418 #endif
419 va_list ap;
421 VA_START (ap, msgid);
423 #ifndef ANSI_PROTOTYPES
424 pfile = va_arg (ap, cpp_reader *);
425 msgid = va_arg (ap, const char *);
426 #endif
428 if (pfile->errors < CPP_FATAL_LIMIT)
429 pfile->errors++;
431 vfprintf (stderr, _(msgid), ap);
432 putc('\n', stderr);
434 va_end(ap);
437 void
438 cpp_notice_from_errno (pfile, name)
439 cpp_reader *pfile;
440 const char *name;
442 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));