OSDN Git Service

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