Makefile.in (TARGET_H, [...]): New.
[gcc.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_buffer *));
33 static void print_location PARAMS ((cpp_reader *,
34 const char *,
35 const cpp_lexer_pos *));
36
37 /* Don't remove the blank before do, as otherwise the exgettext
38 script will mistake this as a function definition */
39 #define v_message(msgid, ap) \
40 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
41
42 /* Print the file names and line numbers of the #include
43 commands which led to the current file. */
44 static void
45 print_containing_files (ip)
46 cpp_buffer *ip;
47 {
48 int first = 1;
49
50 /* Find the other, outer source files. */
51 for (ip = ip->prev; ip; ip = ip->prev)
52 {
53 if (first)
54 {
55 first = 0;
56 /* The current line in each outer source file is now the
57 same as the line of the #include. */
58 fprintf (stderr, _("In file included from %s:%u"),
59 ip->nominal_fname, CPP_BUF_LINE (ip));
60 }
61 else
62 /* Translators note: this message is used in conjunction
63 with "In file included from %s:%ld" and some other
64 tricks. We want something like this:
65
66 | In file included from sys/select.h:123,
67 | from sys/types.h:234,
68 | from userfile.c:31:
69 | bits/select.h:45: <error message here>
70
71 with all the "from"s lined up.
72 The trailing comma is at the beginning of this message,
73 and the trailing colon is not translated. */
74 fprintf (stderr, _(",\n from %s:%u"),
75 ip->nominal_fname, CPP_BUF_LINE (ip));
76 }
77 fputs (":\n", stderr);
78 }
79
80 static void
81 print_location (pfile, filename, pos)
82 cpp_reader *pfile;
83 const char *filename;
84 const cpp_lexer_pos *pos;
85 {
86 cpp_buffer *buffer = pfile->buffer;
87
88 if (!buffer)
89 fprintf (stderr, "%s: ", progname);
90 else
91 {
92 unsigned int line, col = 0;
93 enum cpp_buffer_type type = buffer->type;
94
95 /* For _Pragma buffers, we want to print the location as
96 "foo.c:5:8: _Pragma:", where foo.c is the containing buffer.
97 For diagnostics relating to command line options, we want to
98 print "<command line>:" with no line number. */
99 if (type == BUF_CL_OPTION || type == BUF_BUILTIN)
100 line = 0;
101 else
102 {
103 if (type == BUF_PRAGMA)
104 {
105 buffer = buffer->prev;
106 line = CPP_BUF_LINE (buffer);
107 col = CPP_BUF_COL (buffer);
108 }
109 else
110 {
111 if (pos == 0)
112 pos = cpp_get_line (pfile);
113 line = pos->line;
114 col = pos->col;
115 }
116
117 if (col == 0)
118 col = 1;
119
120 /* Don't repeat the include stack unnecessarily. */
121 if (buffer->prev && ! buffer->include_stack_listed)
122 {
123 buffer->include_stack_listed = 1;
124 print_containing_files (buffer);
125 }
126 }
127
128 if (filename == 0)
129 filename = buffer->nominal_fname;
130
131 if (line == 0)
132 fprintf (stderr, "%s: ", filename);
133 else if (CPP_OPTION (pfile, show_column) == 0)
134 fprintf (stderr, "%s:%u: ", filename, line);
135 else
136 fprintf (stderr, "%s:%u:%u: ", filename, line, col);
137
138 if (type == BUF_PRAGMA)
139 fprintf (stderr, "_Pragma: ");
140 }
141 }
142
143 /* Set up for an error message: print the file and line, bump the error
144 counter, etc.
145 If it returns 0, this error has been suppressed. */
146
147 int
148 _cpp_begin_message (pfile, code, file, pos)
149 cpp_reader *pfile;
150 enum error_type code;
151 const char *file;
152 const cpp_lexer_pos *pos;
153 {
154 int is_warning = 0;
155
156 switch (code)
157 {
158 case PEDWARN:
159 case WARNING:
160 if (CPP_IN_SYSTEM_HEADER (pfile)
161 && ! CPP_OPTION (pfile, warn_system_headers))
162 return 0;
163 case WARNING_SYSHDR:
164 if (CPP_OPTION (pfile, warnings_are_errors)
165 || (code == PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
166 {
167 if (CPP_OPTION (pfile, inhibit_errors))
168 return 0;
169 if (pfile->errors < CPP_FATAL_LIMIT)
170 pfile->errors++;
171 }
172 else
173 {
174 if (CPP_OPTION (pfile, inhibit_warnings))
175 return 0;
176 is_warning = 1;
177 }
178 break;
179
180 case ERROR:
181 if (CPP_OPTION (pfile, inhibit_errors))
182 return 0;
183 if (pfile->errors < CPP_FATAL_LIMIT)
184 pfile->errors++;
185 break;
186 /* Fatal errors cannot be inhibited. */
187 case FATAL:
188 pfile->errors = CPP_FATAL_LIMIT;
189 break;
190 case ICE:
191 fprintf (stderr, _("internal error: "));
192 pfile->errors = CPP_FATAL_LIMIT;
193 break;
194 }
195
196 print_location (pfile, file, pos);
197 if (is_warning)
198 fputs (_("warning: "), stderr);
199
200 return 1;
201 }
202
203 /* Exported interface. */
204
205 /* For reporting internal errors. Prints "internal error: " for you,
206 otherwise identical to cpp_fatal. */
207
208 void
209 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
210 {
211 #ifndef ANSI_PROTOTYPES
212 cpp_reader *pfile;
213 const char *msgid;
214 #endif
215 va_list ap;
216
217 VA_START (ap, msgid);
218
219 #ifndef ANSI_PROTOTYPES
220 pfile = va_arg (ap, cpp_reader *);
221 msgid = va_arg (ap, const char *);
222 #endif
223
224 if (_cpp_begin_message (pfile, ICE, NULL, 0))
225 v_message (msgid, ap);
226 va_end(ap);
227 }
228
229 /* Same as cpp_error, except we consider the error to be "fatal",
230 such as inconsistent options. I.e. there is little point in continuing.
231 (We do not exit, to support use of cpplib as a library.
232 Instead, it is the caller's responsibility to check
233 CPP_FATAL_ERRORS. */
234
235 void
236 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
237 {
238 #ifndef ANSI_PROTOTYPES
239 cpp_reader *pfile;
240 const char *msgid;
241 #endif
242 va_list ap;
243
244 VA_START (ap, msgid);
245
246 #ifndef ANSI_PROTOTYPES
247 pfile = va_arg (ap, cpp_reader *);
248 msgid = va_arg (ap, const char *);
249 #endif
250
251 if (_cpp_begin_message (pfile, FATAL, NULL, 0))
252 v_message (msgid, ap);
253 va_end(ap);
254 }
255
256 void
257 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
258 {
259 #ifndef ANSI_PROTOTYPES
260 cpp_reader *pfile;
261 const char *msgid;
262 #endif
263 va_list ap;
264
265 VA_START(ap, msgid);
266
267 #ifndef ANSI_PROTOTYPES
268 pfile = va_arg (ap, cpp_reader *);
269 msgid = va_arg (ap, const char *);
270 #endif
271
272 if (_cpp_begin_message (pfile, ERROR, NULL, 0))
273 v_message (msgid, ap);
274 va_end(ap);
275 }
276
277 void
278 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
279 const char *msgid, ...))
280 {
281 #ifndef ANSI_PROTOTYPES
282 cpp_reader *pfile;
283 int line;
284 int column;
285 const char *msgid;
286 #endif
287 va_list ap;
288 cpp_lexer_pos pos;
289
290 VA_START (ap, msgid);
291
292 #ifndef ANSI_PROTOTYPES
293 pfile = va_arg (ap, cpp_reader *);
294 line = va_arg (ap, int);
295 column = va_arg (ap, int);
296 msgid = va_arg (ap, const char *);
297 #endif
298
299 pos.line = line;
300 pos.col = column;
301 if (_cpp_begin_message (pfile, ERROR, NULL, &pos))
302 v_message (msgid, ap);
303 va_end(ap);
304 }
305
306 /* Error including a message from `errno'. */
307 void
308 cpp_error_from_errno (pfile, name)
309 cpp_reader *pfile;
310 const char *name;
311 {
312 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
313 }
314
315 void
316 cpp_warning 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_begin_message (pfile, WARNING, NULL, 0))
332 v_message (msgid, ap);
333 va_end(ap);
334 }
335
336 void
337 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
338 const char *msgid, ...))
339 {
340 #ifndef ANSI_PROTOTYPES
341 cpp_reader *pfile;
342 int line;
343 int column;
344 const char *msgid;
345 #endif
346 va_list ap;
347 cpp_lexer_pos pos;
348
349 VA_START (ap, msgid);
350
351 #ifndef ANSI_PROTOTYPES
352 pfile = va_arg (ap, cpp_reader *);
353 line = va_arg (ap, int);
354 column = va_arg (ap, int);
355 msgid = va_arg (ap, const char *);
356 #endif
357
358 pos.line = line;
359 pos.col = column;
360 if (_cpp_begin_message (pfile, WARNING, NULL, &pos))
361 v_message (msgid, ap);
362 va_end(ap);
363 }
364
365 void
366 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
367 {
368 #ifndef ANSI_PROTOTYPES
369 cpp_reader *pfile;
370 const char *msgid;
371 #endif
372 va_list ap;
373
374 VA_START (ap, msgid);
375
376 #ifndef ANSI_PROTOTYPES
377 pfile = va_arg (ap, cpp_reader *);
378 msgid = va_arg (ap, const char *);
379 #endif
380
381 if (_cpp_begin_message (pfile, PEDWARN, NULL, 0))
382 v_message (msgid, ap);
383 va_end(ap);
384 }
385
386 void
387 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
388 const char *msgid, ...))
389 {
390 #ifndef ANSI_PROTOTYPES
391 cpp_reader *pfile;
392 int line;
393 int column;
394 const char *msgid;
395 #endif
396 va_list ap;
397 cpp_lexer_pos pos;
398
399 VA_START (ap, msgid);
400
401 #ifndef ANSI_PROTOTYPES
402 pfile = va_arg (ap, cpp_reader *);
403 line = va_arg (ap, int);
404 column = va_arg (ap, int);
405 msgid = va_arg (ap, const char *);
406 #endif
407
408 pos.line = line;
409 pos.col = column;
410 if (_cpp_begin_message (pfile, PEDWARN, NULL, &pos))
411 v_message (msgid, ap);
412 va_end(ap);
413 }
414
415 /* Report a warning (or an error if pedantic_errors)
416 giving specified file name and line number, not current. */
417
418 void
419 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
420 const char *file, int line, int col,
421 const char *msgid, ...))
422 {
423 #ifndef ANSI_PROTOTYPES
424 cpp_reader *pfile;
425 const char *file;
426 int line;
427 int col;
428 const char *msgid;
429 #endif
430 va_list ap;
431 cpp_lexer_pos pos;
432
433 VA_START (ap, msgid);
434
435 #ifndef ANSI_PROTOTYPES
436 pfile = va_arg (ap, cpp_reader *);
437 file = va_arg (ap, const char *);
438 line = va_arg (ap, int);
439 col = va_arg (ap, int);
440 msgid = va_arg (ap, const char *);
441 #endif
442
443 pos.line = line;
444 pos.col = col;
445 if (_cpp_begin_message (pfile, PEDWARN, file, &pos))
446 v_message (msgid, ap);
447 va_end(ap);
448 }
449
450 /* Print an error message not associated with a file. */
451 void
452 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
453 {
454 #ifndef ANSI_PROTOTYPES
455 cpp_reader *pfile;
456 const char *msgid;
457 #endif
458 va_list ap;
459
460 VA_START (ap, msgid);
461
462 #ifndef ANSI_PROTOTYPES
463 pfile = va_arg (ap, cpp_reader *);
464 msgid = va_arg (ap, const char *);
465 #endif
466
467 if (pfile->errors < CPP_FATAL_LIMIT)
468 pfile->errors++;
469
470 vfprintf (stderr, _(msgid), ap);
471 putc('\n', stderr);
472
473 va_end(ap);
474 }
475
476 void
477 cpp_notice_from_errno (pfile, name)
478 cpp_reader *pfile;
479 const char *name;
480 {
481 if (name[0] == '\0')
482 name = "stdout";
483 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
484 }