OSDN Git Service

* sh.h (CPP_SPEC): Add -D__NOMACSAVE__ for -mnomacsave.
[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 static void v_message                   PARAMS ((cpp_reader *, int,
36                                                  const char *,
37                                                  unsigned int, unsigned int,
38                                                  const char *, va_list));
39
40 /* Print the file names and line numbers of the #include
41    commands which led to the current file.  */
42
43 static void
44 print_containing_files (pfile, ip)
45      cpp_reader *pfile;
46      cpp_buffer *ip;
47 {
48   int first = 1;
49
50   /* If stack of files hasn't changed since we last printed
51      this info, don't repeat it.  */
52   if (pfile->input_stack_listing_current)
53     return;
54
55   /* Find the other, outer source files.  */
56   for (ip = CPP_PREV_BUFFER (ip); ip != NULL; ip = CPP_PREV_BUFFER (ip))
57     {
58       if (first)
59         {
60           first = 0;
61           fprintf (stderr,  _("In file included from %s:%u"),
62                    ip->nominal_fname, CPP_BUF_LINE (ip));
63         }
64       else
65         /* Translators note: this message is used in conjunction
66            with "In file included from %s:%ld" and some other
67            tricks.  We want something like this:
68
69            In file included from sys/select.h:123,
70                             from sys/types.h:234,
71                             from userfile.c:31:
72            bits/select.h:45: <error message here>
73
74            The trailing comma is at the beginning of this message,
75            and the trailing colon is not translated.  */
76         fprintf (stderr, _(",\n                 from %s:%u"),
77                  ip->nominal_fname, CPP_BUF_LINE (ip));
78     }
79   if (first == 0)
80     fputs (":\n", stderr);
81
82   /* Record we have printed the status as of this time.  */
83   pfile->input_stack_listing_current = 1;
84 }
85
86 static void
87 print_file_and_line (filename, line, column)
88      const char *filename;
89      unsigned int line, column;
90 {
91   if (filename == 0 || *filename == '\0')
92     filename = "<stdin>";
93   if (line == 0)
94     fputs (_("<command line>: "), stderr);
95   else if (column > 0)
96     fprintf (stderr, "%s:%u:%u: ", filename, line, column);
97   else
98     fprintf (stderr, "%s:%u: ", filename, line);
99 }
100
101 /* IS_ERROR is 3 for ICE, 2 for merely "fatal" error,
102    1 for error, 0 for warning.  */
103
104 static void
105 v_message (pfile, is_error, file, line, col, msg, ap)
106      cpp_reader *pfile;
107      int is_error;
108      const char *file;
109      unsigned int line;
110      unsigned int col;
111      const char *msg;
112      va_list ap;
113 {
114   cpp_buffer *ip = cpp_file_buffer (pfile);
115
116   if (ip)
117     {
118       if (file == NULL)
119         file = ip->nominal_fname;
120       if (line == 0)
121         {
122           line = CPP_BUF_LINE (ip);
123           col = CPP_BUF_COL (ip);
124         }
125       print_containing_files (pfile, ip);
126       print_file_and_line (file, line,
127                            CPP_OPTION (pfile, show_column) ? col : 0);
128     }
129   else
130     fprintf (stderr, "%s: ", progname);
131
132   switch (is_error)
133     {
134     case 0:
135       fprintf (stderr, _("warning: "));
136       break;
137     case 1:
138       if (pfile->errors < CPP_FATAL_LIMIT)
139         pfile->errors++;
140       break;
141     case 2:
142       pfile->errors = CPP_FATAL_LIMIT;
143       break;
144     case 3:
145       fprintf (stderr, _("internal error: "));
146       pfile->errors = CPP_FATAL_LIMIT;
147       break;
148     default:
149       cpp_ice (pfile, "bad is_error(%d) in v_message", is_error);
150     }
151
152   vfprintf (stderr, _(msg), ap);
153   putc ('\n', stderr);
154 }
155
156 /* Exported interface.  */
157
158 /* For reporting internal errors.  Prints "internal error: " for you,
159    otherwise identical to cpp_fatal.  */
160
161 void
162 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
163 {  
164 #ifndef ANSI_PROTOTYPES
165   cpp_reader *pfile;
166   const char *msgid;
167 #endif
168   va_list ap;
169   
170   VA_START (ap, msgid);
171   
172 #ifndef ANSI_PROTOTYPES
173   pfile = va_arg (ap, cpp_reader *);
174   msgid = va_arg (ap, const char *);
175 #endif
176
177   v_message (pfile, 3, NULL, 0, 0, msgid, ap);
178   va_end(ap);
179 }
180
181 /* Same as cpp_error, except we consider the error to be "fatal",
182    such as inconsistent options.  I.e. there is little point in continuing.
183    (We do not exit, to support use of cpplib as a library.
184    Instead, it is the caller's responsibility to check
185    CPP_FATAL_ERRORS.  */
186
187 void
188 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
189 {  
190 #ifndef ANSI_PROTOTYPES
191   cpp_reader *pfile;
192   const char *msgid;
193 #endif
194   va_list ap;
195   
196   VA_START (ap, msgid);
197   
198 #ifndef ANSI_PROTOTYPES
199   pfile = va_arg (ap, cpp_reader *);
200   msgid = va_arg (ap, const char *);
201 #endif
202
203   v_message (pfile, 2, NULL, 0, 0, msgid, ap);
204   va_end(ap);
205 }
206
207 void
208 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
209 {
210 #ifndef ANSI_PROTOTYPES
211   cpp_reader *pfile;
212   const char *msgid;
213 #endif
214   va_list ap;
215
216   VA_START(ap, msgid);
217   
218 #ifndef ANSI_PROTOTYPES
219   pfile = va_arg (ap, cpp_reader *);
220   msgid = va_arg (ap, const char *);
221 #endif
222
223   if (CPP_OPTION (pfile, inhibit_errors))
224     return;
225
226   v_message (pfile, 1, NULL, 0, 0, msgid, ap);
227   va_end(ap);
228 }
229
230 void
231 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
232                              const char *msgid, ...))
233 {
234 #ifndef ANSI_PROTOTYPES
235   cpp_reader *pfile;
236   int line;
237   int column;
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   line = va_arg (ap, int);
247   column = va_arg (ap, int);
248   msgid = va_arg (ap, const char *);
249 #endif
250
251   if (CPP_OPTION (pfile, inhibit_errors))
252     return;
253
254   v_message (pfile, 1, NULL, line, column, msgid, ap);
255   va_end(ap);
256 }
257
258 /* Error including a message from `errno'.  */
259 void
260 cpp_error_from_errno (pfile, name)
261      cpp_reader *pfile;
262      const char *name;
263 {
264   cpp_error (pfile, "%s: %s", name, xstrerror (errno));
265 }
266
267 void
268 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
269 {
270 #ifndef ANSI_PROTOTYPES
271   cpp_reader *pfile;
272   const char *msgid;
273 #endif
274   va_list ap;
275   
276   VA_START (ap, msgid);
277   
278 #ifndef ANSI_PROTOTYPES
279   pfile = va_arg (ap, cpp_reader *);
280   msgid = va_arg (ap, const char *);
281 #endif
282
283   if (CPP_OPTION (pfile, inhibit_warnings))
284     return;
285
286   v_message (pfile, 0, NULL, 0, 0, msgid, ap);
287   va_end(ap);
288 }
289
290 void
291 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
292                                const char *msgid, ...))
293 {
294 #ifndef ANSI_PROTOTYPES
295   cpp_reader *pfile;
296   int line;
297   int column;
298   const char *msgid;
299 #endif
300   va_list ap;
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   if (CPP_OPTION (pfile, inhibit_warnings))
312     return;
313
314   v_message (pfile, 0, NULL, line, column, msgid, ap);
315   va_end(ap);
316 }
317
318 void
319 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
320 {
321 #ifndef ANSI_PROTOTYPES
322   cpp_reader *pfile;
323   const char *msgid;
324 #endif
325   va_list ap;
326   
327   VA_START (ap, msgid);
328   
329 #ifndef ANSI_PROTOTYPES
330   pfile = va_arg (ap, cpp_reader *);
331   msgid = va_arg (ap, const char *);
332 #endif
333
334   if (CPP_OPTION (pfile, pedantic_errors)
335       ? CPP_OPTION (pfile, inhibit_errors)
336       : CPP_OPTION (pfile, inhibit_warnings))
337     return;
338
339   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
340                  NULL, 0, 0, msgid, ap);
341   va_end(ap);
342 }
343
344 void
345 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
346                                const char *msgid, ...))
347 {
348 #ifndef ANSI_PROTOTYPES
349   cpp_reader *pfile;
350   int line;
351   int column;
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   line = va_arg (ap, int);
361   column = va_arg (ap, int);
362   msgid = va_arg (ap, const char *);
363 #endif
364
365   if (CPP_OPTION (pfile, pedantic_errors)
366       ? CPP_OPTION (pfile, inhibit_errors)
367       : CPP_OPTION (pfile, inhibit_warnings))
368     return;
369
370   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
371                  NULL, line, column, msgid, ap);
372   va_end(ap);
373 }
374
375 /* Report a warning (or an error if pedantic_errors)
376    giving specified file name and line number, not current.  */
377
378 void
379 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
380                                          const char *file, int line, int col,
381                                          const char *msgid, ...))
382 {
383 #ifndef ANSI_PROTOTYPES
384   cpp_reader *pfile;
385   const char *file;
386   int line;
387   int col;
388   const char *msgid;
389 #endif
390   va_list ap;
391   
392   VA_START (ap, msgid);
393
394 #ifndef ANSI_PROTOTYPES
395   pfile = va_arg (ap, cpp_reader *);
396   file = va_arg (ap, const char *);
397   line = va_arg (ap, int);
398   col = va_arg (ap, int);
399   msgid = va_arg (ap, const char *);
400 #endif
401
402   if (CPP_OPTION (pfile, pedantic_errors)
403       ? CPP_OPTION (pfile, inhibit_errors)
404       : CPP_OPTION (pfile, inhibit_warnings))
405     return;
406
407   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
408                  file, line, col, msgid, ap);
409   va_end(ap);
410 }
411
412 /* Print an error message not associated with a file.  */
413 void
414 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
415 {
416 #ifndef ANSI_PROTOTYPES
417   cpp_reader *pfile;
418   const char *msgid;
419 #endif
420   va_list ap;
421   
422   VA_START (ap, msgid);
423   
424 #ifndef ANSI_PROTOTYPES
425   pfile = va_arg (ap, cpp_reader *);
426   msgid = va_arg (ap, const char *);
427 #endif
428
429   if (pfile->errors < CPP_FATAL_LIMIT)
430     pfile->errors++;
431
432   vfprintf (stderr, _(msgid), ap);
433   putc('\n', stderr);
434
435   va_end(ap);
436 }
437
438 void
439 cpp_notice_from_errno (pfile, name)
440      cpp_reader *pfile;
441      const char *name;
442 {
443   if (name[0] == '\0')
444     name = "stdout";
445   cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
446 }