OSDN Git Service

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