OSDN Git Service

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