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