OSDN Git Service

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