OSDN Git Service

* crontab, doc_exclude, update_branch_version, update_version,
[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_buffer *));
33 static void print_location              PARAMS ((cpp_reader *,
34                                                  const char *,
35                                                  const cpp_lexer_pos *));
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 static void
42 print_containing_files (ip)
43      cpp_buffer *ip;
44 {
45   int first = 1;
46
47   /* Find the other, outer source files.  */
48   for (ip = ip->prev; ip; ip = ip->prev)
49     {
50       if (first)
51         {
52           first = 0;
53           /* The current line in each outer source file is now the
54              same as the line of the #include.  */
55           fprintf (stderr,  _("In file included from %s:%u"),
56                    ip->nominal_fname, CPP_BUF_LINE (ip));
57         }
58       else
59         /* Translators note: this message is used in conjunction
60            with "In file included from %s:%ld" and some other
61            tricks.  We want something like this:
62
63            | In file included from sys/select.h:123,
64            |                  from sys/types.h:234,
65            |                  from userfile.c:31:
66            | bits/select.h:45: <error message here>
67
68            with all the "from"s lined up.
69            The trailing comma is at the beginning of this message,
70            and the trailing colon is not translated.  */
71         fprintf (stderr, _(",\n                 from %s:%u"),
72                  ip->nominal_fname, CPP_BUF_LINE (ip));
73     }
74   fputs (":\n", stderr);
75 }
76
77 static void
78 print_location (pfile, filename, pos)
79      cpp_reader *pfile;
80      const char *filename;
81      const cpp_lexer_pos *pos;
82 {
83   cpp_buffer *buffer = pfile->buffer;
84
85   if (!buffer)
86     fprintf (stderr, "%s: ", progname);
87   else
88     {
89       unsigned int line, col = 0;
90       enum cpp_buffer_type type = buffer->type;
91
92       /* For _Pragma buffers, we want to print the location as
93          "foo.c:5:8: _Pragma:", where foo.c is the containing buffer.
94          For diagnostics relating to command line options, we want to
95          print "<command line>:" with no line number.  */
96       if (type == BUF_CL_OPTION || type == BUF_BUILTIN)
97         line = 0;
98       else
99         {
100           if (type == BUF_PRAGMA)
101             {
102               buffer = buffer->prev;
103               line = CPP_BUF_LINE (buffer);
104               col = CPP_BUF_COL (buffer);
105             }
106           else
107             {
108               if (pos == 0)
109                 pos = cpp_get_line (pfile);
110               line = pos->line;
111               col = pos->col;
112             }
113
114           if (col == 0)
115             col = 1;
116
117           /* Don't repeat the include stack unnecessarily.  */
118           if (buffer->prev && ! buffer->include_stack_listed)
119             {
120               buffer->include_stack_listed = 1;
121               print_containing_files (buffer);
122             }
123         }
124
125       if (filename == 0)
126         filename = buffer->nominal_fname;
127       if (*filename == '\0')
128         filename = _("<stdin>");
129
130       if (line == 0)
131         fprintf (stderr, "%s: ", filename);
132       else if (CPP_OPTION (pfile, show_column) == 0)
133         fprintf (stderr, "%s:%u: ", filename, line);
134       else
135         fprintf (stderr, "%s:%u:%u: ", filename, line, col);
136
137       if (type == BUF_PRAGMA)
138         fprintf (stderr, "_Pragma: ");
139     }
140 }
141
142 /* Set up for an error message: print the file and line, bump the error
143    counter, etc.
144    If it returns 0, this error has been suppressed.  */
145
146 int
147 _cpp_begin_message (pfile, code, file, pos)
148      cpp_reader *pfile;
149      enum error_type code;
150      const char *file;
151      const cpp_lexer_pos *pos;
152 {
153   int is_warning = 0;
154
155   switch (code)
156     {
157     case PEDWARN:
158     case WARNING:
159       if (CPP_IN_SYSTEM_HEADER (pfile)
160           && ! CPP_OPTION (pfile, warn_system_headers))
161         return 0;
162       if (CPP_OPTION (pfile, warnings_are_errors)
163           || (code == PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
164         {
165           if (CPP_OPTION (pfile, inhibit_errors))
166             return 0;
167           if (pfile->errors < CPP_FATAL_LIMIT)
168             pfile->errors++;
169         }
170       else
171         {
172           if (CPP_OPTION (pfile, inhibit_warnings))
173             return 0;
174           is_warning = 1;
175         }
176       break;
177         
178     case ERROR:
179       if (CPP_OPTION (pfile, inhibit_errors))
180         return 0;
181       if (pfile->errors < CPP_FATAL_LIMIT)
182         pfile->errors++;
183       break;
184       /* Fatal errors cannot be inhibited.  */
185     case FATAL:
186       pfile->errors = CPP_FATAL_LIMIT;
187       break;
188     case ICE:
189       fprintf (stderr, _("internal error: "));
190       pfile->errors = CPP_FATAL_LIMIT;
191       break;
192     }
193
194   print_location (pfile, file, pos);
195   if (is_warning)
196     fputs (_("warning: "), stderr);
197
198   return 1;
199 }
200
201 /* Exported interface.  */
202
203 /* For reporting internal errors.  Prints "internal error: " for you,
204    otherwise identical to cpp_fatal.  */
205
206 void
207 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
208 {  
209 #ifndef ANSI_PROTOTYPES
210   cpp_reader *pfile;
211   const char *msgid;
212 #endif
213   va_list ap;
214   
215   VA_START (ap, msgid);
216   
217 #ifndef ANSI_PROTOTYPES
218   pfile = va_arg (ap, cpp_reader *);
219   msgid = va_arg (ap, const char *);
220 #endif
221
222   if (_cpp_begin_message (pfile, ICE, NULL, 0))
223     v_message (msgid, ap);
224   va_end(ap);
225 }
226
227 /* Same as cpp_error, except we consider the error to be "fatal",
228    such as inconsistent options.  I.e. there is little point in continuing.
229    (We do not exit, to support use of cpplib as a library.
230    Instead, it is the caller's responsibility to check
231    CPP_FATAL_ERRORS.  */
232
233 void
234 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
235 {  
236 #ifndef ANSI_PROTOTYPES
237   cpp_reader *pfile;
238   const char *msgid;
239 #endif
240   va_list ap;
241   
242   VA_START (ap, msgid);
243   
244 #ifndef ANSI_PROTOTYPES
245   pfile = va_arg (ap, cpp_reader *);
246   msgid = va_arg (ap, const char *);
247 #endif
248
249   if (_cpp_begin_message (pfile, FATAL, NULL, 0))
250     v_message (msgid, ap);
251   va_end(ap);
252 }
253
254 void
255 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
256 {
257 #ifndef ANSI_PROTOTYPES
258   cpp_reader *pfile;
259   const char *msgid;
260 #endif
261   va_list ap;
262
263   VA_START(ap, msgid);
264   
265 #ifndef ANSI_PROTOTYPES
266   pfile = va_arg (ap, cpp_reader *);
267   msgid = va_arg (ap, const char *);
268 #endif
269
270   if (_cpp_begin_message (pfile, ERROR, NULL, 0))
271     v_message (msgid, ap);
272   va_end(ap);
273 }
274
275 void
276 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
277                              const char *msgid, ...))
278 {
279 #ifndef ANSI_PROTOTYPES
280   cpp_reader *pfile;
281   int line;
282   int column;
283   const char *msgid;
284 #endif
285   va_list ap;
286   cpp_lexer_pos pos;
287   
288   VA_START (ap, msgid);
289   
290 #ifndef ANSI_PROTOTYPES
291   pfile = va_arg (ap, cpp_reader *);
292   line = va_arg (ap, int);
293   column = va_arg (ap, int);
294   msgid = va_arg (ap, const char *);
295 #endif
296
297   pos.line = line;
298   pos.col = column;
299   if (_cpp_begin_message (pfile, ERROR, NULL, &pos))
300     v_message (msgid, ap);
301   va_end(ap);
302 }
303
304 /* Error including a message from `errno'.  */
305 void
306 cpp_error_from_errno (pfile, name)
307      cpp_reader *pfile;
308      const char *name;
309 {
310   cpp_error (pfile, "%s: %s", name, xstrerror (errno));
311 }
312
313 void
314 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
315 {
316 #ifndef ANSI_PROTOTYPES
317   cpp_reader *pfile;
318   const char *msgid;
319 #endif
320   va_list ap;
321   
322   VA_START (ap, msgid);
323   
324 #ifndef ANSI_PROTOTYPES
325   pfile = va_arg (ap, cpp_reader *);
326   msgid = va_arg (ap, const char *);
327 #endif
328
329   if (_cpp_begin_message (pfile, WARNING, NULL, 0))
330     v_message (msgid, ap);
331   va_end(ap);
332 }
333
334 void
335 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
336                                const char *msgid, ...))
337 {
338 #ifndef ANSI_PROTOTYPES
339   cpp_reader *pfile;
340   int line;
341   int column;
342   const char *msgid;
343 #endif
344   va_list ap;
345   cpp_lexer_pos pos;
346   
347   VA_START (ap, msgid);
348   
349 #ifndef ANSI_PROTOTYPES
350   pfile = va_arg (ap, cpp_reader *);
351   line = va_arg (ap, int);
352   column = va_arg (ap, int);
353   msgid = va_arg (ap, const char *);
354 #endif
355
356   pos.line = line;
357   pos.col = column;
358   if (_cpp_begin_message (pfile, WARNING, NULL, &pos))
359     v_message (msgid, ap);
360   va_end(ap);
361 }
362
363 void
364 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
365 {
366 #ifndef ANSI_PROTOTYPES
367   cpp_reader *pfile;
368   const char *msgid;
369 #endif
370   va_list ap;
371   
372   VA_START (ap, msgid);
373   
374 #ifndef ANSI_PROTOTYPES
375   pfile = va_arg (ap, cpp_reader *);
376   msgid = va_arg (ap, const char *);
377 #endif
378
379   if (_cpp_begin_message (pfile, PEDWARN, NULL, 0))
380     v_message (msgid, ap);
381   va_end(ap);
382 }
383
384 void
385 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
386                                const char *msgid, ...))
387 {
388 #ifndef ANSI_PROTOTYPES
389   cpp_reader *pfile;
390   int line;
391   int column;
392   const char *msgid;
393 #endif
394   va_list ap;
395   cpp_lexer_pos pos;
396   
397   VA_START (ap, msgid);
398   
399 #ifndef ANSI_PROTOTYPES
400   pfile = va_arg (ap, cpp_reader *);
401   line = va_arg (ap, int);
402   column = va_arg (ap, int);
403   msgid = va_arg (ap, const char *);
404 #endif
405
406   pos.line = line;
407   pos.col = column;
408   if (_cpp_begin_message (pfile, PEDWARN, NULL, &pos))
409     v_message (msgid, ap);
410   va_end(ap);
411 }
412
413 /* Report a warning (or an error if pedantic_errors)
414    giving specified file name and line number, not current.  */
415
416 void
417 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
418                                          const char *file, int line, int col,
419                                          const char *msgid, ...))
420 {
421 #ifndef ANSI_PROTOTYPES
422   cpp_reader *pfile;
423   const char *file;
424   int line;
425   int col;
426   const char *msgid;
427 #endif
428   va_list ap;
429   cpp_lexer_pos pos;
430   
431   VA_START (ap, msgid);
432
433 #ifndef ANSI_PROTOTYPES
434   pfile = va_arg (ap, cpp_reader *);
435   file = va_arg (ap, const char *);
436   line = va_arg (ap, int);
437   col = va_arg (ap, int);
438   msgid = va_arg (ap, const char *);
439 #endif
440
441   pos.line = line;
442   pos.col = col;
443   if (_cpp_begin_message (pfile, PEDWARN, file, &pos))
444     v_message (msgid, ap);
445   va_end(ap);
446 }
447
448 /* Print an error message not associated with a file.  */
449 void
450 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
451 {
452 #ifndef ANSI_PROTOTYPES
453   cpp_reader *pfile;
454   const char *msgid;
455 #endif
456   va_list ap;
457   
458   VA_START (ap, msgid);
459   
460 #ifndef ANSI_PROTOTYPES
461   pfile = va_arg (ap, cpp_reader *);
462   msgid = va_arg (ap, const char *);
463 #endif
464
465   if (pfile->errors < CPP_FATAL_LIMIT)
466     pfile->errors++;
467
468   vfprintf (stderr, _(msgid), ap);
469   putc('\n', stderr);
470
471   va_end(ap);
472 }
473
474 void
475 cpp_notice_from_errno (pfile, name)
476      cpp_reader *pfile;
477      const char *name;
478 {
479   if (name[0] == '\0')
480     name = "stdout";
481   cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
482 }