OSDN Git Service

* gcc/conflict.c (conflict_graph_add): Pass enum type to
[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 "hashtab.h"
29 #include "cpplib.h"
30 #include "cpphash.h"
31 #include "intl.h"
32
33 static void print_containing_files      PARAMS ((cpp_reader *, cpp_buffer *));
34 static void print_file_and_line         PARAMS ((const char *, unsigned int,
35                                                  unsigned int));
36 static void v_message                   PARAMS ((cpp_reader *, int,
37                                                  const char *,
38                                                  unsigned int, unsigned int,
39                                                  const char *, va_list));
40
41 /* Print the file names and line numbers of the #include
42    commands which led to the current file.  */
43
44 static void
45 print_containing_files (pfile, ip)
46      cpp_reader *pfile;
47      cpp_buffer *ip;
48 {
49   int first = 1;
50
51   /* If stack of files hasn't changed since we last printed
52      this info, don't repeat it.  */
53   if (pfile->input_stack_listing_current)
54     return;
55
56   /* Find the other, outer source files.  */
57   for (ip = CPP_PREV_BUFFER (ip); ip != NULL; ip = CPP_PREV_BUFFER (ip))
58     {
59       if (first)
60         {
61           first = 0;
62           fprintf (stderr,  _("In file included from %s:%u"),
63                    ip->nominal_fname, CPP_BUF_LINE (ip));
64         }
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:
69
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>
74
75            The trailing comma is at the beginning of this message,
76            and the trailing colon is not translated.  */
77         fprintf (stderr, _(",\n                 from %s:%u"),
78                  ip->nominal_fname, CPP_BUF_LINE (ip));
79     }
80   if (first == 0)
81     fputs (":\n", stderr);
82
83   /* Record we have printed the status as of this time.  */
84   pfile->input_stack_listing_current = 1;
85 }
86
87 static void
88 print_file_and_line (filename, line, column)
89      const char *filename;
90      unsigned int line, column;
91 {
92   if (filename == 0 || *filename == '\0')
93     filename = "<stdin>";
94   if (line == 0)
95     fputs (_("<command line>: "), stderr);
96   else if (column > 0)
97     fprintf (stderr, "%s:%u:%u: ", filename, line, column);
98   else
99     fprintf (stderr, "%s:%u: ", filename, line);
100 }
101
102 /* IS_ERROR is 3 for ICE, 2 for merely "fatal" error,
103    1 for error, 0 for warning.  */
104
105 static void
106 v_message (pfile, is_error, file, line, col, msg, ap)
107      cpp_reader *pfile;
108      int is_error;
109      const char *file;
110      unsigned int line;
111      unsigned int col;
112      const char *msg;
113      va_list ap;
114 {
115   cpp_buffer *ip = cpp_file_buffer (pfile);
116
117   if (ip)
118     {
119       if (file == NULL)
120         file = ip->nominal_fname;
121       if (line == 0)
122         {
123           line = CPP_BUF_LINE (ip);
124           col = CPP_BUF_COL (ip);
125         }
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       fprintf (stderr, _("warning: "));
137       break;
138     case 1:
139       if (pfile->errors < CPP_FATAL_LIMIT)
140         pfile->errors++;
141       break;
142     case 2:
143       pfile->errors = CPP_FATAL_LIMIT;
144       break;
145     case 3:
146       fprintf (stderr, _("internal error: "));
147       pfile->errors = CPP_FATAL_LIMIT;
148       break;
149     default:
150       cpp_ice (pfile, "bad is_error(%d) in v_message", is_error);
151     }
152
153   vfprintf (stderr, _(msg), ap);
154   putc ('\n', stderr);
155 }
156
157 /* Exported interface.  */
158
159 /* For reporting internal errors.  Prints "internal error: " for you,
160    otherwise identical to cpp_fatal.  */
161
162 void
163 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
164 {  
165 #ifndef ANSI_PROTOTYPES
166   cpp_reader *pfile;
167   const char *msgid;
168 #endif
169   va_list ap;
170   
171   VA_START (ap, msgid);
172   
173 #ifndef ANSI_PROTOTYPES
174   pfile = va_arg (ap, cpp_reader *);
175   msgid = va_arg (ap, const char *);
176 #endif
177
178   v_message (pfile, 3, NULL, 0, 0, msgid, ap);
179   va_end(ap);
180 }
181
182 /* Same as cpp_error, except we consider the error to be "fatal",
183    such as inconsistent options.  I.e. there is little point in continuing.
184    (We do not exit, to support use of cpplib as a library.
185    Instead, it is the caller's responsibility to check
186    CPP_FATAL_ERRORS.  */
187
188 void
189 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
190 {  
191 #ifndef ANSI_PROTOTYPES
192   cpp_reader *pfile;
193   const char *msgid;
194 #endif
195   va_list ap;
196   
197   VA_START (ap, msgid);
198   
199 #ifndef ANSI_PROTOTYPES
200   pfile = va_arg (ap, cpp_reader *);
201   msgid = va_arg (ap, const char *);
202 #endif
203
204   v_message (pfile, 2, NULL, 0, 0, msgid, ap);
205   va_end(ap);
206 }
207
208 void
209 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
210 {
211 #ifndef ANSI_PROTOTYPES
212   cpp_reader *pfile;
213   const char *msgid;
214 #endif
215   va_list ap;
216
217   VA_START(ap, msgid);
218   
219 #ifndef ANSI_PROTOTYPES
220   pfile = va_arg (ap, cpp_reader *);
221   msgid = va_arg (ap, const char *);
222 #endif
223
224   if (CPP_OPTION (pfile, inhibit_errors))
225     return;
226
227   v_message (pfile, 1, NULL, 0, 0, msgid, ap);
228   va_end(ap);
229 }
230
231 void
232 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
233                              const char *msgid, ...))
234 {
235 #ifndef ANSI_PROTOTYPES
236   cpp_reader *pfile;
237   int line;
238   int column;
239   const char *msgid;
240 #endif
241   va_list ap;
242   
243   VA_START (ap, msgid);
244   
245 #ifndef ANSI_PROTOTYPES
246   pfile = va_arg (ap, cpp_reader *);
247   line = va_arg (ap, int);
248   column = va_arg (ap, int);
249   msgid = va_arg (ap, const char *);
250 #endif
251
252   if (CPP_OPTION (pfile, inhibit_errors))
253     return;
254
255   v_message (pfile, 1, NULL, line, column, msgid, ap);
256   va_end(ap);
257 }
258
259 /* Error including a message from `errno'.  */
260 void
261 cpp_error_from_errno (pfile, name)
262      cpp_reader *pfile;
263      const char *name;
264 {
265   cpp_error (pfile, "%s: %s", name, xstrerror (errno));
266 }
267
268 void
269 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
270 {
271 #ifndef ANSI_PROTOTYPES
272   cpp_reader *pfile;
273   const char *msgid;
274 #endif
275   va_list ap;
276   
277   VA_START (ap, msgid);
278   
279 #ifndef ANSI_PROTOTYPES
280   pfile = va_arg (ap, cpp_reader *);
281   msgid = va_arg (ap, const char *);
282 #endif
283
284   if (CPP_OPTION (pfile, inhibit_warnings))
285     return;
286
287   v_message (pfile, 0, NULL, 0, 0, msgid, ap);
288   va_end(ap);
289 }
290
291 void
292 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
293                                const char *msgid, ...))
294 {
295 #ifndef ANSI_PROTOTYPES
296   cpp_reader *pfile;
297   int line;
298   int column;
299   const char *msgid;
300 #endif
301   va_list ap;
302   
303   VA_START (ap, msgid);
304   
305 #ifndef ANSI_PROTOTYPES
306   pfile = va_arg (ap, cpp_reader *);
307   line = va_arg (ap, int);
308   column = va_arg (ap, int);
309   msgid = va_arg (ap, const char *);
310 #endif
311
312   if (CPP_OPTION (pfile, inhibit_warnings))
313     return;
314
315   v_message (pfile, 0, NULL, line, column, msgid, ap);
316   va_end(ap);
317 }
318
319 void
320 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
321 {
322 #ifndef ANSI_PROTOTYPES
323   cpp_reader *pfile;
324   const char *msgid;
325 #endif
326   va_list ap;
327   
328   VA_START (ap, msgid);
329   
330 #ifndef ANSI_PROTOTYPES
331   pfile = va_arg (ap, cpp_reader *);
332   msgid = va_arg (ap, const char *);
333 #endif
334
335   if (CPP_OPTION (pfile, pedantic_errors)
336       ? CPP_OPTION (pfile, inhibit_errors)
337       : CPP_OPTION (pfile, inhibit_warnings))
338     return;
339
340   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
341                  NULL, 0, 0, msgid, ap);
342   va_end(ap);
343 }
344
345 void
346 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
347                                const char *msgid, ...))
348 {
349 #ifndef ANSI_PROTOTYPES
350   cpp_reader *pfile;
351   int line;
352   int column;
353   const char *msgid;
354 #endif
355   va_list ap;
356   
357   VA_START (ap, msgid);
358   
359 #ifndef ANSI_PROTOTYPES
360   pfile = va_arg (ap, cpp_reader *);
361   line = va_arg (ap, int);
362   column = va_arg (ap, int);
363   msgid = va_arg (ap, const char *);
364 #endif
365
366   if (CPP_OPTION (pfile, pedantic_errors)
367       ? CPP_OPTION (pfile, inhibit_errors)
368       : CPP_OPTION (pfile, inhibit_warnings))
369     return;
370
371   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
372                  NULL, line, column, msgid, ap);
373   va_end(ap);
374 }
375
376 /* Report a warning (or an error if pedantic_errors)
377    giving specified file name and line number, not current.  */
378
379 void
380 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
381                                          const char *file, int line, int col,
382                                          const char *msgid, ...))
383 {
384 #ifndef ANSI_PROTOTYPES
385   cpp_reader *pfile;
386   const char *file;
387   int line;
388   int col;
389   const char *msgid;
390 #endif
391   va_list ap;
392   
393   VA_START (ap, msgid);
394
395 #ifndef ANSI_PROTOTYPES
396   pfile = va_arg (ap, cpp_reader *);
397   file = va_arg (ap, const char *);
398   line = va_arg (ap, int);
399   col = va_arg (ap, int);
400   msgid = va_arg (ap, const char *);
401 #endif
402
403   if (CPP_OPTION (pfile, pedantic_errors)
404       ? CPP_OPTION (pfile, inhibit_errors)
405       : CPP_OPTION (pfile, inhibit_warnings))
406     return;
407
408   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
409                  file, line, col, msgid, ap);
410   va_end(ap);
411 }
412
413 /* Print an error message not associated with a file.  */
414 void
415 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
416 {
417 #ifndef ANSI_PROTOTYPES
418   cpp_reader *pfile;
419   const char *msgid;
420 #endif
421   va_list ap;
422   
423   VA_START (ap, msgid);
424   
425 #ifndef ANSI_PROTOTYPES
426   pfile = va_arg (ap, cpp_reader *);
427   msgid = va_arg (ap, const char *);
428 #endif
429
430   if (pfile->errors < CPP_FATAL_LIMIT)
431     pfile->errors++;
432
433   vfprintf (stderr, _(msgid), ap);
434   putc('\n', stderr);
435
436   va_end(ap);
437 }
438
439 void
440 cpp_notice_from_errno (pfile, name)
441      cpp_reader *pfile;
442      const char *name;
443 {
444   if (name[0] == '\0')
445     name = "stdout";
446   cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
447 }