OSDN Git Service

982bcc334491373068fbb4b38b76fb18f5a50d16
[pf3gnuchains/gcc-fork.git] / gcc / cpperror.c
1 /* Default error handlers for CPP Library.
2    Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3    Written by Per Bothner, 1994.
4    Based on CCCP program by by Paul Rubin, June 1986
5    Adapted to ANSI C, Richard Stallman, Jan 1987
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
21  In other words, you are welcome to use, share and improve this program.
22  You are forbidden to forbid anyone else to use, share and improve
23  what you give them.   Help stamp out software-hoarding!  */
24
25 #include "cpplib.h"
26 #include <stdio.h>
27
28 /* This defines "errno" properly for VMS, and gives us EACCES. */
29 #include <errno.h>
30 #ifndef errno
31 extern int errno;
32 #endif
33
34 #ifndef VMS
35 #ifndef HAVE_STRERROR
36 extern int sys_nerr;
37 #if defined(bsd4_4)
38 extern const char *const sys_errlist[];
39 #else
40 extern char *sys_errlist[];
41 #endif
42 #else   /* HAVE_STERRROR */
43 char *strerror ();
44 #endif
45 #else   /* VMS */
46 char *strerror (int,...);
47 #endif
48
49 /* Print the file names and line numbers of the #include
50    commands which led to the current file.  */
51
52 void
53 cpp_print_containing_files (pfile)
54      cpp_reader *pfile;
55 {
56   cpp_buffer *ip;
57   int i;
58   int first = 1;
59
60   /* If stack of files hasn't changed since we last printed
61      this info, don't repeat it.  */
62   if (pfile->input_stack_listing_current)
63     return;
64
65   ip = cpp_file_buffer (pfile);
66
67   /* Give up if we don't find a source file.  */
68   if (ip == NULL)
69     return;
70
71   /* Find the other, outer source files.  */
72   while ((ip = CPP_PREV_BUFFER (ip)), ip != CPP_NULL_BUFFER (pfile))
73     {
74       long line, col;
75       cpp_buf_line_and_col (ip, &line, &col);
76       if (ip->fname != NULL)
77         {
78           if (first)
79             {
80               first = 0;
81               fprintf (stderr, "In file included");
82             }
83           else
84             fprintf (stderr, ",\n                ");
85         }
86
87 /* start-sanitize-mpw */
88 #ifdef MPW
89       fprintf (stderr, " File \"%s\"; Line %d  # ", ip->nominal_fname, line);
90 #else
91 /* end-sanitize-mpw */
92       fprintf (stderr, " from %s:%d", ip->nominal_fname, line);
93 /* start-sanitize-mpw */
94 #endif /* MPW */
95 /* end-sanitize-mpw */
96     }
97   if (! first)
98     fprintf (stderr, ":\n");
99
100   /* Record we have printed the status as of this time.  */
101   pfile->input_stack_listing_current = 1;
102 }
103
104 void
105 cpp_print_file_and_line (pfile)
106      cpp_reader *pfile;
107 {
108   cpp_buffer *ip = cpp_file_buffer (pfile);
109
110   if (ip != NULL)
111     {
112       long line, col;
113       cpp_buf_line_and_col (ip, &line, &col);
114       if (pfile->show_column)
115         fprintf (stderr, "%s:%d:%d: ", ip->nominal_fname, line, col);
116       else
117         fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
118     }
119 }
120
121 void
122 cpp_error (pfile, msg, arg1, arg2, arg3)
123      cpp_reader *pfile;
124      char *msg;
125      char *arg1, *arg2, *arg3;
126 {
127   cpp_print_containing_files (pfile);
128   cpp_print_file_and_line (pfile);
129   fprintf (stderr, msg, arg1, arg2, arg3);
130   fprintf (stderr, "\n");
131   pfile->errors++;
132 }
133
134 /* Print error message but don't count it.  */
135
136 void
137 cpp_warning (pfile, msg, arg1, arg2, arg3)
138      cpp_reader *pfile;
139      char *msg;
140      char *arg1, *arg2, *arg3;
141 {
142   if (CPP_OPTIONS (pfile)->inhibit_warnings)
143     return;
144
145   if (CPP_OPTIONS (pfile)->warnings_are_errors)
146     pfile->errors++;
147
148   cpp_print_containing_files (pfile);
149   cpp_print_file_and_line (pfile);
150   fprintf (stderr, "warning: ");
151   fprintf (stderr, msg, arg1, arg2, arg3);
152   fprintf (stderr, "\n");
153 }
154
155 void
156 cpp_error_with_line (pfile, line, msg, arg1, arg2, arg3)
157      cpp_reader *pfile;
158      int line;
159      char *msg;
160      char *arg1, *arg2, *arg3;
161 {
162   int i;
163   cpp_buffer *ip = cpp_file_buffer (pfile);
164
165   cpp_print_containing_files (pfile);
166
167   if (ip != NULL)
168     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
169
170   fprintf (stderr, msg, arg1, arg2, arg3);
171   fprintf (stderr, "\n");
172   pfile->errors++;
173 }
174
175 void
176 cpp_warning_with_line (pfile, line, msg, arg1, arg2, arg3)
177      cpp_reader *pfile;
178      int line;
179      char *msg;
180      char *arg1, *arg2, *arg3;
181 {
182   int i;
183   cpp_buffer *ip;
184
185   if (CPP_OPTIONS (pfile)->inhibit_warnings)
186     return;
187
188   if (CPP_OPTIONS (pfile)->warnings_are_errors)
189     pfile->errors++;
190
191   cpp_print_containing_files (pfile);
192
193   ip = cpp_file_buffer (pfile);
194
195   if (ip != NULL)
196     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
197   fprintf (stderr, "warning: ");
198   fprintf (stderr, msg, arg1, arg2, arg3);
199   fprintf (stderr, "\n");
200 }
201
202 /* Print an error message and maybe count it.  */
203
204 void
205 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
206      cpp_reader *pfile;
207      char *msg;
208      char *arg1, *arg2, *arg3;
209 {
210   if (CPP_OPTIONS (pfile)->pedantic_errors)
211     cpp_error (pfile, msg, arg1, arg2, arg3);
212   else
213     cpp_warning (pfile, msg, arg1, arg2, arg3);
214 }
215
216 void
217 cpp_pedwarn_with_line (pfile, line, msg, arg1, arg2, arg3)
218      cpp_reader *pfile;
219      int line;
220      char *msg;
221      char *arg1, *arg2, *arg3;
222 {
223   if (CPP_OPTIONS (pfile)->pedantic_errors)
224     cpp_error_with_line (pfile, line, msg, arg1, arg2, arg3);
225   else
226     cpp_warning_with_line (pfile, line, msg, arg1, arg2, arg3);
227 }
228
229 /* Report a warning (or an error if pedantic_errors)
230    giving specified file name and line number, not current.  */
231
232 void
233 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
234      cpp_reader *pfile;
235      char *file;
236      int line;
237      char *msg;
238      char *arg1, *arg2, *arg3;
239 {
240   if (!CPP_OPTIONS (pfile)->pedantic_errors
241       && CPP_OPTIONS (pfile)->inhibit_warnings)
242     return;
243   if (file != NULL)
244     fprintf (stderr, "%s:%d: ", file, line);
245   if (CPP_OPTIONS (pfile)->pedantic_errors)
246     pfile->errors++;
247   else
248     fprintf (stderr, "warning: ");
249   fprintf (stderr, msg, arg1, arg2, arg3);
250   fprintf (stderr, "\n");
251 }
252
253 void
254 fatal (str, arg)
255      char *str, *arg;
256 {
257   fprintf (stderr, "%s: ", progname);
258   fprintf (stderr, str, arg);
259   fprintf (stderr, "\n");
260   exit (FAILURE_EXIT_CODE);
261 }
262
263 \f
264 /*
265  * my_strerror - return the descriptive text associated with an `errno' code.
266  */
267
268 char *
269 my_strerror (errnum)
270      int errnum;
271 {
272   char *result;
273
274 #ifndef VMS
275 #ifndef HAVE_STRERROR
276   result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
277 #else
278   result = strerror (errnum);
279 #endif
280 #else   /* VMS */
281   /* VAXCRTL's strerror() takes an optional second argument, which only
282      matters when the first argument is EVMSERR.  However, it's simplest
283      just to pass it unconditionally.  `vaxc$errno' is declared in
284      <errno.h>, and maintained by the library in parallel with `errno'.
285      We assume that caller's `errnum' either matches the last setting of
286      `errno' by the library or else does not have the value `EVMSERR'.  */
287
288   result = strerror (errnum, vaxc$errno);
289 #endif
290
291   if (!result)
292     result = "undocumented I/O error";
293
294   return result;
295 }
296
297 /* Error including a message from `errno'.  */
298
299 void
300 cpp_error_from_errno (pfile, name)
301      cpp_reader *pfile;
302      char *name;
303 {
304   int i;
305   cpp_buffer *ip = cpp_file_buffer (pfile);
306
307   cpp_print_containing_files (pfile);
308
309   if (ip != NULL)
310     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
311
312   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
313
314   pfile->errors++;
315 }
316
317 void
318 cpp_perror_with_name (pfile, name)
319      cpp_reader *pfile;
320      char *name;
321 {
322   fprintf (stderr, "%s: ", progname);
323   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
324   pfile->errors++;
325 }
326
327 void
328 cpp_pfatal_with_name (pfile, name)
329      cpp_reader *pfile;
330      char *name;
331 {
332   cpp_perror_with_name (pfile, name);
333 #ifdef VMS
334   exit (vaxc$errno);
335 #else
336   exit (FAILURE_EXIT_CODE);
337 #endif
338 }