OSDN Git Service

* sh.h (FUNCTION_ARG_PARTIAL_NREGS): Accomodate an unsigned
[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 *, long, long));
34 static void v_message                   PARAMS ((cpp_reader *, int,
35                                                  const char *, long, long,
36                                                  const char *, va_list));
37
38 /* Print the file names and line numbers of the #include
39    commands which led to the current file.  */
40
41 static void
42 print_containing_files (pfile, ip)
43      cpp_reader *pfile;
44      cpp_buffer *ip;
45 {
46   int first = 1;
47
48   /* If stack of files hasn't changed since we last printed
49      this info, don't repeat it.  */
50   if (pfile->input_stack_listing_current)
51     return;
52
53   /* Find the other, outer source files.  */
54   for (ip = CPP_PREV_BUFFER (ip); ip != NULL; ip = CPP_PREV_BUFFER (ip))
55     {
56       long line;
57       cpp_buf_line_and_col (ip, &line, NULL);
58       if (first)
59         {
60           first = 0;
61           fprintf (stderr,  _("In file included from %s:%ld"),
62                    ip->nominal_fname, line);
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:%ld"),
77                  ip->nominal_fname, line);
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      long 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:%ld:%ld: ", filename, line, column);
97   else
98     fprintf (stderr, "%s:%ld: ", 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      long line;
110      long 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 == -1)
121         cpp_buf_line_and_col (ip, &line, &col);
122
123       print_containing_files (pfile, ip);
124       print_file_and_line (file, line,
125                            CPP_OPTION (pfile, show_column) ? col : 0);
126     }
127   else
128     fprintf (stderr, "%s: ", progname);
129
130   switch (is_error)
131     {
132     case 0:
133       fprintf (stderr, _("warning: "));
134       break;
135     case 1:
136       if (pfile->errors < CPP_FATAL_LIMIT)
137         pfile->errors++;
138       break;
139     case 2:
140       pfile->errors = CPP_FATAL_LIMIT;
141       break;
142     case 3:
143       fprintf (stderr, _("internal error: "));
144       pfile->errors = CPP_FATAL_LIMIT;
145       break;
146     default:
147       cpp_ice (pfile, "bad is_error(%d) in v_message", is_error);
148     }
149
150   vfprintf (stderr, _(msg), ap);
151   putc ('\n', stderr);
152 }
153
154 /* Exported interface.  */
155
156 /* For reporting internal errors.  Prints "internal error: " for you,
157    otherwise identical to cpp_fatal.  */
158
159 void
160 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
161 {  
162 #ifndef ANSI_PROTOTYPES
163   cpp_reader *pfile;
164   const char *msgid;
165 #endif
166   va_list ap;
167   
168   VA_START (ap, msgid);
169   
170 #ifndef ANSI_PROTOTYPES
171   pfile = va_arg (ap, cpp_reader *);
172   msgid = va_arg (ap, const char *);
173 #endif
174
175   v_message (pfile, 3, NULL, -1, -1, msgid, ap);
176   va_end(ap);
177 }
178
179 /* Same as cpp_error, except we consider the error to be "fatal",
180    such as inconsistent options.  I.e. there is little point in continuing.
181    (We do not exit, to support use of cpplib as a library.
182    Instead, it is the caller's responsibility to check
183    CPP_FATAL_ERRORS.  */
184
185 void
186 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
187 {  
188 #ifndef ANSI_PROTOTYPES
189   cpp_reader *pfile;
190   const char *msgid;
191 #endif
192   va_list ap;
193   
194   VA_START (ap, msgid);
195   
196 #ifndef ANSI_PROTOTYPES
197   pfile = va_arg (ap, cpp_reader *);
198   msgid = va_arg (ap, const char *);
199 #endif
200
201   v_message (pfile, 2, NULL, -1, -1, msgid, ap);
202   va_end(ap);
203 }
204
205 void
206 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
207 {
208 #ifndef ANSI_PROTOTYPES
209   cpp_reader *pfile;
210   const char *msgid;
211 #endif
212   va_list ap;
213
214   VA_START(ap, msgid);
215   
216 #ifndef ANSI_PROTOTYPES
217   pfile = va_arg (ap, cpp_reader *);
218   msgid = va_arg (ap, const char *);
219 #endif
220
221   if (CPP_OPTION (pfile, inhibit_errors))
222     return;
223
224   v_message (pfile, 1, NULL, -1, -1, msgid, ap);
225   va_end(ap);
226 }
227
228 void
229 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
230                              const char *msgid, ...))
231 {
232 #ifndef ANSI_PROTOTYPES
233   cpp_reader *pfile;
234   int line;
235   int column;
236   const char *msgid;
237 #endif
238   va_list ap;
239   
240   VA_START (ap, msgid);
241   
242 #ifndef ANSI_PROTOTYPES
243   pfile = va_arg (ap, cpp_reader *);
244   line = va_arg (ap, int);
245   column = va_arg (ap, int);
246   msgid = va_arg (ap, const char *);
247 #endif
248
249   if (CPP_OPTION (pfile, inhibit_errors))
250     return;
251
252   v_message (pfile, 1, NULL, line, column, msgid, ap);
253   va_end(ap);
254 }
255
256 /* Error including a message from `errno'.  */
257 void
258 cpp_error_from_errno (pfile, name)
259      cpp_reader *pfile;
260      const char *name;
261 {
262   cpp_error (pfile, "%s: %s", name, xstrerror (errno));
263 }
264
265 void
266 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
267 {
268 #ifndef ANSI_PROTOTYPES
269   cpp_reader *pfile;
270   const char *msgid;
271 #endif
272   va_list ap;
273   
274   VA_START (ap, msgid);
275   
276 #ifndef ANSI_PROTOTYPES
277   pfile = va_arg (ap, cpp_reader *);
278   msgid = va_arg (ap, const char *);
279 #endif
280
281   if (CPP_OPTION (pfile, inhibit_warnings))
282     return;
283
284   v_message (pfile, 0, NULL, -1, -1, msgid, ap);
285   va_end(ap);
286 }
287
288 void
289 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
290                                const char *msgid, ...))
291 {
292 #ifndef ANSI_PROTOTYPES
293   cpp_reader *pfile;
294   int line;
295   int column;
296   const char *msgid;
297 #endif
298   va_list ap;
299   
300   VA_START (ap, msgid);
301   
302 #ifndef ANSI_PROTOTYPES
303   pfile = va_arg (ap, cpp_reader *);
304   line = va_arg (ap, int);
305   column = va_arg (ap, int);
306   msgid = va_arg (ap, const char *);
307 #endif
308
309   if (CPP_OPTION (pfile, inhibit_warnings))
310     return;
311
312   v_message (pfile, 0, NULL, line, column, msgid, ap);
313   va_end(ap);
314 }
315
316 void
317 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
318 {
319 #ifndef ANSI_PROTOTYPES
320   cpp_reader *pfile;
321   const char *msgid;
322 #endif
323   va_list ap;
324   
325   VA_START (ap, msgid);
326   
327 #ifndef ANSI_PROTOTYPES
328   pfile = va_arg (ap, cpp_reader *);
329   msgid = va_arg (ap, const char *);
330 #endif
331
332   if (CPP_OPTION (pfile, pedantic_errors)
333       ? CPP_OPTION (pfile, inhibit_errors)
334       : CPP_OPTION (pfile, inhibit_warnings))
335     return;
336
337   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
338                  NULL, -1, -1, msgid, ap);
339   va_end(ap);
340 }
341
342 void
343 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
344                                const char *msgid, ...))
345 {
346 #ifndef ANSI_PROTOTYPES
347   cpp_reader *pfile;
348   int line;
349   int column;
350   const char *msgid;
351 #endif
352   va_list ap;
353   
354   VA_START (ap, msgid);
355   
356 #ifndef ANSI_PROTOTYPES
357   pfile = va_arg (ap, cpp_reader *);
358   line = va_arg (ap, int);
359   column = va_arg (ap, int);
360   msgid = va_arg (ap, const char *);
361 #endif
362
363   if (CPP_OPTION (pfile, pedantic_errors)
364       ? CPP_OPTION (pfile, inhibit_errors)
365       : CPP_OPTION (pfile, inhibit_warnings))
366     return;
367
368   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
369                  NULL, line, column, msgid, ap);
370   va_end(ap);
371 }
372
373 /* Report a warning (or an error if pedantic_errors)
374    giving specified file name and line number, not current.  */
375
376 void
377 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
378                                          const char *file, int line, int col,
379                                          const char *msgid, ...))
380 {
381 #ifndef ANSI_PROTOTYPES
382   cpp_reader *pfile;
383   const char *file;
384   int line;
385   int col;
386   const char *msgid;
387 #endif
388   va_list ap;
389   
390   VA_START (ap, msgid);
391
392 #ifndef ANSI_PROTOTYPES
393   pfile = va_arg (ap, cpp_reader *);
394   file = va_arg (ap, const char *);
395   line = va_arg (ap, int);
396   col = va_arg (ap, int);
397   msgid = va_arg (ap, const char *);
398 #endif
399
400   if (CPP_OPTION (pfile, pedantic_errors)
401       ? CPP_OPTION (pfile, inhibit_errors)
402       : CPP_OPTION (pfile, inhibit_warnings))
403     return;
404
405   v_message (pfile, CPP_OPTION (pfile, pedantic_errors),
406                  file, line, col, msgid, ap);
407   va_end(ap);
408 }
409
410 /* Print an error message not associated with a file.  */
411 void
412 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
413 {
414 #ifndef ANSI_PROTOTYPES
415   cpp_reader *pfile;
416   const char *msgid;
417 #endif
418   va_list ap;
419   
420   VA_START (ap, msgid);
421   
422 #ifndef ANSI_PROTOTYPES
423   pfile = va_arg (ap, cpp_reader *);
424   msgid = va_arg (ap, const char *);
425 #endif
426
427   if (pfile->errors < CPP_FATAL_LIMIT)
428     pfile->errors++;
429
430   vfprintf (stderr, _(msgid), ap);
431   putc('\n', stderr);
432
433   va_end(ap);
434 }
435
436 void
437 cpp_notice_from_errno (pfile, name)
438      cpp_reader *pfile;
439      const char *name;
440 {
441   cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
442 }