OSDN Git Service

2000-09-25 Kazu Hirata <kazu@hxi.com>
[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
36 #define v_message(msgid, ap) \
37 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
38
39 /* Print the file names and line numbers of the #include
40    commands which led to the current file.  */
41
42 static void
43 print_containing_files (pfile, ip)
44      cpp_reader *pfile;
45      cpp_buffer *ip;
46 {
47   int first = 1;
48
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;
53
54   /* Find the other, outer source files.  */
55   for (ip = CPP_PREV_BUFFER (ip); ip != NULL; ip = CPP_PREV_BUFFER (ip))
56     {
57       if (first)
58         {
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);
65         }
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:
70
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>
75
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);
81     }
82   if (first == 0)
83     fputs (":\n", stderr);
84
85   /* Record we have printed the status as of this time.  */
86   pfile->input_stack_listing_current = 1;
87 }
88
89 static void
90 print_file_and_line (filename, line, column)
91      const char *filename;
92      unsigned int line, column;
93 {
94   if (filename == 0 || *filename == '\0')
95     filename = "<stdin>";
96
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);
103 }
104
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.  */
108
109 int
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;
116 {
117   cpp_buffer *ip = CPP_BUFFER (pfile);
118   int is_warning = 0;
119
120   switch (code)
121     {
122     case WARNING:
123       if (! CPP_OPTION (pfile, warnings_are_errors))
124         {
125           if (CPP_OPTION (pfile, inhibit_warnings))
126             return 0;
127           is_warning = 1;
128         }
129       else
130         {
131           if (CPP_OPTION (pfile, inhibit_errors))
132             return 0;
133           if (pfile->errors < CPP_FATAL_LIMIT)
134             pfile->errors++;
135         }
136       break;
137
138     case PEDWARN:
139       if (! CPP_OPTION (pfile, pedantic_errors))
140         {
141           if (CPP_OPTION (pfile, inhibit_warnings))
142             return 0;
143           is_warning = 1;
144         }
145       else
146         {
147           if (CPP_OPTION (pfile, inhibit_errors))
148             return 0;
149           if (pfile->errors < CPP_FATAL_LIMIT)
150             pfile->errors++;
151         }
152       break;
153         
154     case ERROR:
155       if (CPP_OPTION (pfile, inhibit_errors))
156         return 0;
157       if (pfile->errors < CPP_FATAL_LIMIT)
158         pfile->errors++;
159       break;
160       /* Fatal errors cannot be inhibited.  */
161     case FATAL:
162       pfile->errors = CPP_FATAL_LIMIT;
163       break;
164     case ICE:
165       fprintf (stderr, _("internal error: "));
166       pfile->errors = CPP_FATAL_LIMIT;
167       break;
168     }
169
170   if (ip)
171     {
172       if (file == NULL)
173         file = ip->nominal_fname;
174       if (line == 0)
175         line = _cpp_get_line (pfile, &col);
176       print_containing_files (pfile, ip);
177       print_file_and_line (file, line,
178                            CPP_OPTION (pfile, show_column) ? col : 0);
179     }
180   else
181     fprintf (stderr, "%s: ", progname);
182
183   if (is_warning)
184     fputs (_("warning: "), stderr);
185
186   return 1;
187 }
188
189 /* Exported interface.  */
190
191 /* For reporting internal errors.  Prints "internal error: " for you,
192    otherwise identical to cpp_fatal.  */
193
194 void
195 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
196 {  
197 #ifndef ANSI_PROTOTYPES
198   cpp_reader *pfile;
199   const char *msgid;
200 #endif
201   va_list ap;
202   
203   VA_START (ap, msgid);
204   
205 #ifndef ANSI_PROTOTYPES
206   pfile = va_arg (ap, cpp_reader *);
207   msgid = va_arg (ap, const char *);
208 #endif
209
210   if (_cpp_begin_message (pfile, ICE, NULL, 0, 0))
211     v_message (msgid, ap);
212   va_end(ap);
213 }
214
215 /* Same as cpp_error, except we consider the error to be "fatal",
216    such as inconsistent options.  I.e. there is little point in continuing.
217    (We do not exit, to support use of cpplib as a library.
218    Instead, it is the caller's responsibility to check
219    CPP_FATAL_ERRORS.  */
220
221 void
222 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
223 {  
224 #ifndef ANSI_PROTOTYPES
225   cpp_reader *pfile;
226   const char *msgid;
227 #endif
228   va_list ap;
229   
230   VA_START (ap, msgid);
231   
232 #ifndef ANSI_PROTOTYPES
233   pfile = va_arg (ap, cpp_reader *);
234   msgid = va_arg (ap, const char *);
235 #endif
236
237   if (_cpp_begin_message (pfile, FATAL, NULL, 0, 0))
238     v_message (msgid, ap);
239   va_end(ap);
240 }
241
242 void
243 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
244 {
245 #ifndef ANSI_PROTOTYPES
246   cpp_reader *pfile;
247   const char *msgid;
248 #endif
249   va_list ap;
250
251   VA_START(ap, msgid);
252   
253 #ifndef ANSI_PROTOTYPES
254   pfile = va_arg (ap, cpp_reader *);
255   msgid = va_arg (ap, const char *);
256 #endif
257
258   if (_cpp_begin_message (pfile, ERROR, NULL, 0, 0))
259     v_message (msgid, ap);
260   va_end(ap);
261 }
262
263 void
264 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
265                              const char *msgid, ...))
266 {
267 #ifndef ANSI_PROTOTYPES
268   cpp_reader *pfile;
269   int line;
270   int column;
271   const char *msgid;
272 #endif
273   va_list ap;
274   
275   VA_START (ap, msgid);
276   
277 #ifndef ANSI_PROTOTYPES
278   pfile = va_arg (ap, cpp_reader *);
279   line = va_arg (ap, int);
280   column = va_arg (ap, int);
281   msgid = va_arg (ap, const char *);
282 #endif
283
284   if (_cpp_begin_message (pfile, ERROR, NULL, line, column))
285     v_message (msgid, ap);
286   va_end(ap);
287 }
288
289 /* Error including a message from `errno'.  */
290 void
291 cpp_error_from_errno (pfile, name)
292      cpp_reader *pfile;
293      const char *name;
294 {
295   cpp_error (pfile, "%s: %s", name, xstrerror (errno));
296 }
297
298 void
299 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
300 {
301 #ifndef ANSI_PROTOTYPES
302   cpp_reader *pfile;
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   msgid = va_arg (ap, const char *);
312 #endif
313
314   if (_cpp_begin_message (pfile, WARNING, NULL, 0, 0))
315     v_message (msgid, ap);
316   va_end(ap);
317 }
318
319 void
320 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
321                                const char *msgid, ...))
322 {
323 #ifndef ANSI_PROTOTYPES
324   cpp_reader *pfile;
325   int line;
326   int column;
327   const char *msgid;
328 #endif
329   va_list ap;
330   
331   VA_START (ap, msgid);
332   
333 #ifndef ANSI_PROTOTYPES
334   pfile = va_arg (ap, cpp_reader *);
335   line = va_arg (ap, int);
336   column = va_arg (ap, int);
337   msgid = va_arg (ap, const char *);
338 #endif
339
340   if (_cpp_begin_message (pfile, WARNING, NULL, line, column))
341     v_message (msgid, ap);
342   va_end(ap);
343 }
344
345 void
346 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
347 {
348 #ifndef ANSI_PROTOTYPES
349   cpp_reader *pfile;
350   const char *msgid;
351 #endif
352   va_list ap;
353   
354   VA_START (ap, msgid);
355   
356 #ifndef ANSI_PROTOTYPES
357   pfile = va_arg (ap, cpp_reader *);
358   msgid = va_arg (ap, const char *);
359 #endif
360
361   if (_cpp_begin_message (pfile, PEDWARN, NULL, 0, 0))
362     v_message (msgid, ap);
363   va_end(ap);
364 }
365
366 void
367 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
368                                const char *msgid, ...))
369 {
370 #ifndef ANSI_PROTOTYPES
371   cpp_reader *pfile;
372   int line;
373   int column;
374   const char *msgid;
375 #endif
376   va_list ap;
377   
378   VA_START (ap, msgid);
379   
380 #ifndef ANSI_PROTOTYPES
381   pfile = va_arg (ap, cpp_reader *);
382   line = va_arg (ap, int);
383   column = va_arg (ap, int);
384   msgid = va_arg (ap, const char *);
385 #endif
386
387   if (_cpp_begin_message (pfile, PEDWARN, NULL, line, column))
388     v_message (msgid, ap);
389   va_end(ap);
390 }
391
392 /* Report a warning (or an error if pedantic_errors)
393    giving specified file name and line number, not current.  */
394
395 void
396 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
397                                          const char *file, int line, int col,
398                                          const char *msgid, ...))
399 {
400 #ifndef ANSI_PROTOTYPES
401   cpp_reader *pfile;
402   const char *file;
403   int line;
404   int col;
405   const char *msgid;
406 #endif
407   va_list ap;
408   
409   VA_START (ap, msgid);
410
411 #ifndef ANSI_PROTOTYPES
412   pfile = va_arg (ap, cpp_reader *);
413   file = va_arg (ap, const char *);
414   line = va_arg (ap, int);
415   col = va_arg (ap, int);
416   msgid = va_arg (ap, const char *);
417 #endif
418
419   if (_cpp_begin_message (pfile, PEDWARN, file, line, col))
420     v_message (msgid, ap);
421   va_end(ap);
422 }
423
424 /* Print an error message not associated with a file.  */
425 void
426 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
427 {
428 #ifndef ANSI_PROTOTYPES
429   cpp_reader *pfile;
430   const char *msgid;
431 #endif
432   va_list ap;
433   
434   VA_START (ap, msgid);
435   
436 #ifndef ANSI_PROTOTYPES
437   pfile = va_arg (ap, cpp_reader *);
438   msgid = va_arg (ap, const char *);
439 #endif
440
441   if (pfile->errors < CPP_FATAL_LIMIT)
442     pfile->errors++;
443
444   vfprintf (stderr, _(msgid), ap);
445   putc('\n', stderr);
446
447   va_end(ap);
448 }
449
450 void
451 cpp_notice_from_errno (pfile, name)
452      cpp_reader *pfile;
453      const char *name;
454 {
455   if (name[0] == '\0')
456     name = "stdout";
457   cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
458 }
459
460 const char *
461 cpp_type2name (type)
462      enum cpp_ttype type;
463 {
464   return (const char *) _cpp_token_spellings[type].name;
465 }
466