OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / cpperror.c
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
7
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.
12
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.
17
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.
21
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!  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30 #include "intl.h"
31
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));
39
40 /* Print the file names and line numbers of the #include
41    commands which led to the current file.  */
42
43 static void
44 print_containing_files (pfile, ip)
45      cpp_reader *pfile;
46      cpp_buffer *ip;
47 {
48   int first = 1;
49
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;
54
55   /* Find the other, outer source files.  */
56   for (ip = CPP_PREV_BUFFER (ip); ip != NULL; ip = CPP_PREV_BUFFER (ip))
57     {
58       if (first)
59         {
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);
66         }
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:
71
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>
76
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);
82     }
83   if (first == 0)
84     fputs (":\n", stderr);
85
86   /* Record we have printed the status as of this time.  */
87   pfile->input_stack_listing_current = 1;
88 }
89
90 static void
91 print_file_and_line (filename, line, column)
92      const char *filename;
93      unsigned int line, column;
94 {
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);
103 }
104
105 /* IS_ERROR is 3 for ICE, 2 for merely "fatal" error,
106    1 for error, 0 for warning.  */
107
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;
117 {
118   cpp_buffer *ip = CPP_BUFFER (pfile);
119
120   if (ip)
121     {
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);
129     }
130   else
131     fprintf (stderr, "%s: ", progname);
132
133   switch (is_error)
134     {
135     case 0:
136       if (! CPP_OPTION (pfile, warnings_are_errors))
137         {
138           fprintf (stderr, _("warning: "));
139           break;
140         }
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);
155     }
156
157   vfprintf (stderr, _(msg), ap);
158   putc ('\n', stderr);
159 }
160
161 /* Exported interface.  */
162
163 /* For reporting internal errors.  Prints "internal error: " for you,
164    otherwise identical to cpp_fatal.  */
165
166 void
167 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
168 {  
169 #ifndef ANSI_PROTOTYPES
170   cpp_reader *pfile;
171   const char *msgid;
172 #endif
173   va_list ap;
174   
175   VA_START (ap, msgid);
176   
177 #ifndef ANSI_PROTOTYPES
178   pfile = va_arg (ap, cpp_reader *);
179   msgid = va_arg (ap, const char *);
180 #endif
181
182   v_message (pfile, 3, NULL, 0, 0, msgid, ap);
183   va_end(ap);
184 }
185
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.  */
191
192 void
193 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
194 {  
195 #ifndef ANSI_PROTOTYPES
196   cpp_reader *pfile;
197   const char *msgid;
198 #endif
199   va_list ap;
200   
201   VA_START (ap, msgid);
202   
203 #ifndef ANSI_PROTOTYPES
204   pfile = va_arg (ap, cpp_reader *);
205   msgid = va_arg (ap, const char *);
206 #endif
207
208   v_message (pfile, 2, NULL, 0, 0, msgid, ap);
209   va_end(ap);
210 }
211
212 void
213 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
214 {
215 #ifndef ANSI_PROTOTYPES
216   cpp_reader *pfile;
217   const char *msgid;
218 #endif
219   va_list ap;
220
221   VA_START(ap, msgid);
222   
223 #ifndef ANSI_PROTOTYPES
224   pfile = va_arg (ap, cpp_reader *);
225   msgid = va_arg (ap, const char *);
226 #endif
227
228   if (CPP_OPTION (pfile, inhibit_errors))
229     return;
230
231   v_message (pfile, 1, NULL, 0, 0, msgid, ap);
232   va_end(ap);
233 }
234
235 void
236 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
237                              const char *msgid, ...))
238 {
239 #ifndef ANSI_PROTOTYPES
240   cpp_reader *pfile;
241   int line;
242   int column;
243   const char *msgid;
244 #endif
245   va_list ap;
246   
247   VA_START (ap, msgid);
248   
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
255
256   if (CPP_OPTION (pfile, inhibit_errors))
257     return;
258
259   v_message (pfile, 1, NULL, line, column, msgid, ap);
260   va_end(ap);
261 }
262
263 /* Error including a message from `errno'.  */
264 void
265 cpp_error_from_errno (pfile, name)
266      cpp_reader *pfile;
267      const char *name;
268 {
269   cpp_error (pfile, "%s: %s", name, xstrerror (errno));
270 }
271
272 void
273 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
274 {
275 #ifndef ANSI_PROTOTYPES
276   cpp_reader *pfile;
277   const char *msgid;
278 #endif
279   va_list ap;
280   
281   VA_START (ap, msgid);
282   
283 #ifndef ANSI_PROTOTYPES
284   pfile = va_arg (ap, cpp_reader *);
285   msgid = va_arg (ap, const char *);
286 #endif
287
288   if (CPP_OPTION (pfile, inhibit_warnings))
289     return;
290
291   v_message (pfile, 0, NULL, 0, 0, msgid, ap);
292   va_end(ap);
293 }
294
295 void
296 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
297                                const char *msgid, ...))
298 {
299 #ifndef ANSI_PROTOTYPES
300   cpp_reader *pfile;
301   int line;
302   int column;
303   const char *msgid;
304 #endif
305   va_list ap;
306   
307   VA_START (ap, msgid);
308   
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
315
316   if (CPP_OPTION (pfile, inhibit_warnings))
317     return;
318
319   v_message (pfile, 0, NULL, line, column, msgid, ap);
320   va_end(ap);
321 }
322
323 void
324 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
325 {
326 #ifndef ANSI_PROTOTYPES
327   cpp_reader *pfile;
328   const char *msgid;
329 #endif
330   va_list ap;
331   
332   VA_START (ap, msgid);
333   
334 #ifndef ANSI_PROTOTYPES
335   pfile = va_arg (ap, cpp_reader *);
336   msgid = va_arg (ap, const char *);
337 #endif
338
339   if (CPP_OPTION (pfile, pedantic_errors)
340       ? CPP_OPTION (pfile, inhibit_errors)
341       : CPP_OPTION (pfile, inhibit_warnings))
342     return;
343
344   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
345                  NULL, 0, 0, msgid, ap);
346   va_end(ap);
347 }
348
349 void
350 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
351                                const char *msgid, ...))
352 {
353 #ifndef ANSI_PROTOTYPES
354   cpp_reader *pfile;
355   int line;
356   int column;
357   const char *msgid;
358 #endif
359   va_list ap;
360   
361   VA_START (ap, msgid);
362   
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
369
370   if (CPP_OPTION (pfile, pedantic_errors)
371       ? CPP_OPTION (pfile, inhibit_errors)
372       : CPP_OPTION (pfile, inhibit_warnings))
373     return;
374
375   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
376                  NULL, line, column, msgid, ap);
377   va_end(ap);
378 }
379
380 /* Report a warning (or an error if pedantic_errors)
381    giving specified file name and line number, not current.  */
382
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, ...))
387 {
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;
396   
397   VA_START (ap, msgid);
398
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
406
407   if (CPP_OPTION (pfile, pedantic_errors)
408       ? CPP_OPTION (pfile, inhibit_errors)
409       : CPP_OPTION (pfile, inhibit_warnings))
410     return;
411
412   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
413                  file, line, col, msgid, ap);
414   va_end(ap);
415 }
416
417 /* Print an error message not associated with a file.  */
418 void
419 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
420 {
421 #ifndef ANSI_PROTOTYPES
422   cpp_reader *pfile;
423   const char *msgid;
424 #endif
425   va_list ap;
426   
427   VA_START (ap, msgid);
428   
429 #ifndef ANSI_PROTOTYPES
430   pfile = va_arg (ap, cpp_reader *);
431   msgid = va_arg (ap, const char *);
432 #endif
433
434   if (pfile->errors < CPP_FATAL_LIMIT)
435     pfile->errors++;
436
437   vfprintf (stderr, _(msgid), ap);
438   putc('\n', stderr);
439
440   va_end(ap);
441 }
442
443 void
444 cpp_notice_from_errno (pfile, name)
445      cpp_reader *pfile;
446      const char *name;
447 {
448   if (name[0] == '\0')
449     name = "stdout";
450   cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
451 }