OSDN Git Service

6b137abcd75a22a2822132fd10d312fc146f3fce
[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_location              PARAMS ((cpp_reader *,
33                                                  const cpp_lexer_pos *));
34
35 /* Don't remove the blank before do, as otherwise the exgettext
36    script will mistake this as a function definition */
37 #define v_message(msgid, ap) \
38  do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
39
40 static void
41 print_location (pfile, pos)
42      cpp_reader *pfile;
43      const cpp_lexer_pos *pos;
44 {
45   cpp_buffer *buffer = pfile->buffer;
46
47   if (!buffer)
48     fprintf (stderr, "%s: ", progname);
49   else
50     {
51       unsigned int line, col;
52       const struct line_map *map;
53
54       if (pos == 0)
55         pos = cpp_get_line (pfile);
56       map = lookup_line (&pfile->line_maps, pos->line);
57
58       print_containing_files (&pfile->line_maps, map);
59
60       line = SOURCE_LINE (map, pos->line);
61       col = pos->col;
62       if (col == 0)
63         col = 1;
64
65       if (line == 0)
66         fprintf (stderr, "%s:", map->to_file);
67       else if (CPP_OPTION (pfile, show_column) == 0)
68         fprintf (stderr, "%s:%u:", map->to_file, line);
69       else
70         fprintf (stderr, "%s:%u:%u:", map->to_file, line, col);
71
72       if (buffer->type == BUF_PRAGMA)
73         fprintf (stderr, "_Pragma:");
74       fputc (' ', stderr);
75     }
76 }
77
78 /* Set up for an error message: print the file and line, bump the error
79    counter, etc.
80    If it returns 0, this error has been suppressed.  */
81
82 int
83 _cpp_begin_message (pfile, code, pos)
84      cpp_reader *pfile;
85      enum error_type code;
86      const cpp_lexer_pos *pos;
87 {
88   int is_warning = 0;
89
90   switch (code)
91     {
92     case PEDWARN:
93     case WARNING:
94       if (CPP_IN_SYSTEM_HEADER (pfile)
95           && ! CPP_OPTION (pfile, warn_system_headers))
96         return 0;
97     case WARNING_SYSHDR:
98       if (CPP_OPTION (pfile, warnings_are_errors)
99           || (code == PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
100         {
101           if (CPP_OPTION (pfile, inhibit_errors))
102             return 0;
103           if (pfile->errors < CPP_FATAL_LIMIT)
104             pfile->errors++;
105         }
106       else
107         {
108           if (CPP_OPTION (pfile, inhibit_warnings))
109             return 0;
110           is_warning = 1;
111         }
112       break;
113         
114     case ERROR:
115       if (CPP_OPTION (pfile, inhibit_errors))
116         return 0;
117       if (pfile->errors < CPP_FATAL_LIMIT)
118         pfile->errors++;
119       break;
120       /* Fatal errors cannot be inhibited.  */
121     case FATAL:
122       pfile->errors = CPP_FATAL_LIMIT;
123       break;
124     case ICE:
125       fprintf (stderr, _("internal error: "));
126       pfile->errors = CPP_FATAL_LIMIT;
127       break;
128     }
129
130   print_location (pfile, pos);
131   if (is_warning)
132     fputs (_("warning: "), stderr);
133
134   return 1;
135 }
136
137 /* Exported interface.  */
138
139 /* For reporting internal errors.  Prints "internal error: " for you,
140    otherwise identical to cpp_fatal.  */
141
142 void
143 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
144 {  
145   VA_OPEN (ap, msgid);
146   VA_FIXEDARG (ap, cpp_reader *, pfile);
147   VA_FIXEDARG (ap, const char *, msgid);
148
149   if (_cpp_begin_message (pfile, ICE, 0))
150     v_message (msgid, ap);
151
152   VA_CLOSE (ap);
153 }
154
155 /* Same as cpp_error, except we consider the error to be "fatal",
156    such as inconsistent options.  I.e. there is little point in continuing.
157    (We do not exit, to support use of cpplib as a library.
158    Instead, it is the caller's responsibility to check
159    CPP_FATAL_ERRORS.  */
160
161 void
162 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
163 {  
164   VA_OPEN (ap, msgid);
165   VA_FIXEDARG (ap, cpp_reader *, pfile);
166   VA_FIXEDARG (ap, const char *, msgid);
167
168   if (_cpp_begin_message (pfile, FATAL, 0))
169     v_message (msgid, ap);
170
171   VA_CLOSE (ap);
172 }
173
174 void
175 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
176 {
177   VA_OPEN (ap, msgid);
178   VA_FIXEDARG (ap, cpp_reader *, pfile);
179   VA_FIXEDARG (ap, const char *, msgid);
180
181   if (_cpp_begin_message (pfile, ERROR, 0))
182     v_message (msgid, ap);
183
184   VA_CLOSE (ap);
185 }
186
187 void
188 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
189                              const char *msgid, ...))
190 {
191   cpp_lexer_pos pos;
192   
193   VA_OPEN (ap, msgid);
194   VA_FIXEDARG (ap, cpp_reader *, pfile);
195   VA_FIXEDARG (ap, int, line);
196   VA_FIXEDARG (ap, int, column);
197   VA_FIXEDARG (ap, const char *, msgid);
198
199   pos.line = line;
200   pos.col = column;
201   if (_cpp_begin_message (pfile, ERROR, &pos))
202     v_message (msgid, ap);
203
204   VA_CLOSE (ap);
205 }
206
207 /* Error including a message from `errno'.  */
208 void
209 cpp_error_from_errno (pfile, name)
210      cpp_reader *pfile;
211      const char *name;
212 {
213   cpp_error (pfile, "%s: %s", name, xstrerror (errno));
214 }
215
216 void
217 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
218 {
219   VA_OPEN (ap, msgid);
220   VA_FIXEDARG (ap, cpp_reader *, pfile);
221   VA_FIXEDARG (ap, const char *, msgid);
222
223   if (_cpp_begin_message (pfile, WARNING, 0))
224     v_message (msgid, ap);
225
226   VA_CLOSE (ap);
227 }
228
229 void
230 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
231                                const char *msgid, ...))
232 {
233   cpp_lexer_pos pos;
234
235   VA_OPEN (ap, msgid);
236   VA_FIXEDARG (ap, cpp_reader *, pfile);
237   VA_FIXEDARG (ap, int, line);
238   VA_FIXEDARG (ap, int, column);
239   VA_FIXEDARG (ap, const char *, msgid);
240
241   pos.line = line;
242   pos.col = column;
243   if (_cpp_begin_message (pfile, WARNING, &pos))
244     v_message (msgid, ap);
245
246   VA_CLOSE (ap);
247 }
248
249 void
250 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
251 {
252   VA_OPEN (ap, msgid);
253   VA_FIXEDARG (ap, cpp_reader *, pfile);
254   VA_FIXEDARG (ap, const char *, msgid);
255
256   if (_cpp_begin_message (pfile, PEDWARN, 0))
257     v_message (msgid, ap);
258
259   VA_CLOSE (ap);
260 }
261
262 void
263 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
264                                const char *msgid, ...))
265 {
266   cpp_lexer_pos pos;
267   
268   VA_OPEN (ap, msgid);
269   VA_FIXEDARG (ap, cpp_reader *, pfile);
270   VA_FIXEDARG (ap, int, line);
271   VA_FIXEDARG (ap, int, column);
272   VA_FIXEDARG (ap, const char *, msgid);
273
274   pos.line = line;
275   pos.col = column;
276   if (_cpp_begin_message (pfile, PEDWARN, &pos))
277     v_message (msgid, ap);
278
279   VA_CLOSE (ap);
280 }
281
282 /* Print an error message not associated with a file.  */
283 void
284 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
285 {
286   VA_OPEN (ap, msgid);
287   VA_FIXEDARG (ap, cpp_reader *, pfile);
288   VA_FIXEDARG (ap, const char *, msgid);
289
290   if (pfile->errors < CPP_FATAL_LIMIT)
291     pfile->errors++;
292
293   v_message (msgid, ap);
294
295   VA_CLOSE (ap);
296 }
297
298 void
299 cpp_notice_from_errno (pfile, name)
300      cpp_reader *pfile;
301      const char *name;
302 {
303   if (name[0] == '\0')
304     name = "stdout";
305   cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
306 }