OSDN Git Service

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