OSDN Git Service

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