]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cpperror.c
Makefile.in: Rebuilt.
[thirdparty/gcc.git] / gcc / cpperror.c
CommitLineData
7f2935c7 1/* Default error handlers for CPP Library.
5e7b4e25 2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000
40ea76de 3 Free Software Foundation, Inc.
7f2935c7 4 Written by Per Bothner, 1994.
38e01259 5 Based on CCCP program by Paul Rubin, June 1986
7f2935c7
PB
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
940d9d63 20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7f2935c7
PB
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
84ee6fd4 26#include "config.h"
b04cd507 27#include "system.h"
f32da1f6 28#include "cpplib.h"
88ae23e7 29#include "cpphash.h"
ab87f8c8 30#include "intl.h"
f32da1f6 31
0bda4760
NB
32static void print_containing_files PARAMS ((cpp_buffer *));
33static void print_location PARAMS ((cpp_reader *,
34 const char *,
35 const cpp_lexer_pos *));
58fea6af
ZW
36#define v_message(msgid, ap) \
37do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
c1212d2f 38
7f2935c7
PB
39/* Print the file names and line numbers of the #include
40 commands which led to the current file. */
c1212d2f 41static void
0bda4760 42print_containing_files (ip)
c1212d2f 43 cpp_buffer *ip;
7f2935c7 44{
7f2935c7
PB
45 int first = 1;
46
c1212d2f 47 /* Find the other, outer source files. */
0bda4760 48 for (ip = ip->prev; ip; ip = ip->prev)
38b24ee2 49 {
38b24ee2
ZW
50 if (first)
51 {
52 first = 0;
93c80368
NB
53 /* The current line in each outer source file is now the
54 same as the line of the #include. */
3a2b2c7a 55 fprintf (stderr, _("In file included from %s:%u"),
93c80368 56 ip->nominal_fname, CPP_BUF_LINE (ip));
38b24ee2
ZW
57 }
58 else
59 /* Translators note: this message is used in conjunction
60 with "In file included from %s:%ld" and some other
61 tricks. We want something like this:
7f2935c7 62
041c3194
ZW
63 | In file included from sys/select.h:123,
64 | from sys/types.h:234,
65 | from userfile.c:31:
66 | bits/select.h:45: <error message here>
7f2935c7 67
041c3194 68 with all the "from"s lined up.
38b24ee2
ZW
69 The trailing comma is at the beginning of this message,
70 and the trailing colon is not translated. */
3a2b2c7a 71 fprintf (stderr, _(",\n from %s:%u"),
ad2a084d 72 ip->nominal_fname, CPP_BUF_LINE (ip));
38b24ee2 73 }
0bda4760 74 fputs (":\n", stderr);
7f2935c7
PB
75}
76
c1212d2f 77static void
0bda4760
NB
78print_location (pfile, filename, pos)
79 cpp_reader *pfile;
bcc5cac9 80 const char *filename;
0bda4760 81 const cpp_lexer_pos *pos;
7f2935c7 82{
0bda4760 83 cpp_buffer *buffer = pfile->buffer;
2c8f0515 84
0bda4760
NB
85 if (!buffer)
86 fprintf (stderr, "%s: ", progname);
14fbab6d 87 else
0bda4760 88 {
dfbf62ec 89 unsigned int line, col = 0;
0bda4760
NB
90 enum cpp_buffer_type type = buffer->type;
91
92 /* For _Pragma buffers, we want to print the location as
93 "foo.c:5:8: _Pragma:", where foo.c is the containing buffer.
94 For diagnostics relating to command line options, we want to
95 print "<command line>:" with no line number. */
96 if (type == BUF_CL_OPTION || type == BUF_BUILTIN)
97 line = 0;
98 else
99 {
100 if (type == BUF_PRAGMA)
101 {
102 buffer = buffer->prev;
103 line = CPP_BUF_LINE (buffer);
104 col = CPP_BUF_COL (buffer);
105 }
106 else
107 {
108 if (pos == 0)
109 pos = cpp_get_line (pfile);
110 line = pos->line;
111 col = pos->col;
112 }
113
ad2a084d
NB
114 if (col == 0)
115 col = 1;
116
0bda4760
NB
117 /* Don't repeat the include stack unnecessarily. */
118 if (buffer->prev && ! buffer->include_stack_listed)
119 {
120 buffer->include_stack_listed = 1;
121 print_containing_files (buffer);
122 }
123 }
124
125 if (filename == 0)
126 filename = buffer->nominal_fname;
0bda4760
NB
127
128 if (line == 0)
129 fprintf (stderr, "%s: ", filename);
130 else if (CPP_OPTION (pfile, show_column) == 0)
131 fprintf (stderr, "%s:%u: ", filename, line);
132 else
133 fprintf (stderr, "%s:%u:%u: ", filename, line, col);
134
135 if (type == BUF_PRAGMA)
136 fprintf (stderr, "_Pragma: ");
137 }
7f2935c7
PB
138}
139
58fea6af
ZW
140/* Set up for an error message: print the file and line, bump the error
141 counter, etc.
142 If it returns 0, this error has been suppressed. */
0f41302f 143
58fea6af 144int
93c80368 145_cpp_begin_message (pfile, code, file, pos)
c1212d2f 146 cpp_reader *pfile;
58fea6af 147 enum error_type code;
c1212d2f 148 const char *file;
93c80368 149 const cpp_lexer_pos *pos;
7f2935c7 150{
58fea6af 151 int is_warning = 0;
c1212d2f 152
58fea6af 153 switch (code)
c1212d2f 154 {
c933c209 155 case PEDWARN:
58fea6af 156 case WARNING:
d8090680
NB
157 if (CPP_IN_SYSTEM_HEADER (pfile)
158 && ! CPP_OPTION (pfile, warn_system_headers))
159 return 0;
2f878973 160 case WARNING_SYSHDR:
c933c209
CD
161 if (CPP_OPTION (pfile, warnings_are_errors)
162 || (code == PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
58fea6af
ZW
163 {
164 if (CPP_OPTION (pfile, inhibit_errors))
165 return 0;
166 if (pfile->errors < CPP_FATAL_LIMIT)
167 pfile->errors++;
168 }
c933c209 169 else
58fea6af 170 {
d8090680 171 if (CPP_OPTION (pfile, inhibit_warnings))
58fea6af
ZW
172 return 0;
173 is_warning = 1;
041c3194 174 }
58fea6af
ZW
175 break;
176
177 case ERROR:
178 if (CPP_OPTION (pfile, inhibit_errors))
179 return 0;
ab87f8c8
JL
180 if (pfile->errors < CPP_FATAL_LIMIT)
181 pfile->errors++;
182 break;
58fea6af
ZW
183 /* Fatal errors cannot be inhibited. */
184 case FATAL:
ab87f8c8
JL
185 pfile->errors = CPP_FATAL_LIMIT;
186 break;
58fea6af 187 case ICE:
c1212d2f
ZW
188 fprintf (stderr, _("internal error: "));
189 pfile->errors = CPP_FATAL_LIMIT;
190 break;
ab87f8c8
JL
191 }
192
0bda4760 193 print_location (pfile, file, pos);
58fea6af
ZW
194 if (is_warning)
195 fputs (_("warning: "), stderr);
196
197 return 1;
7f2935c7
PB
198}
199
c1212d2f
ZW
200/* Exported interface. */
201
202/* For reporting internal errors. Prints "internal error: " for you,
203 otherwise identical to cpp_fatal. */
204
487a6e06 205void
c1212d2f
ZW
206cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
207{
5148a72b 208#ifndef ANSI_PROTOTYPES
487a6e06 209 cpp_reader *pfile;
1c5d09e4 210 const char *msgid;
487a6e06
KG
211#endif
212 va_list ap;
213
ab87f8c8 214 VA_START (ap, msgid);
487a6e06 215
5148a72b 216#ifndef ANSI_PROTOTYPES
487a6e06 217 pfile = va_arg (ap, cpp_reader *);
ab87f8c8 218 msgid = va_arg (ap, const char *);
487a6e06
KG
219#endif
220
93c80368 221 if (_cpp_begin_message (pfile, ICE, NULL, 0))
58fea6af 222 v_message (msgid, ap);
487a6e06
KG
223 va_end(ap);
224}
225
05a2b36f
PB
226/* Same as cpp_error, except we consider the error to be "fatal",
227 such as inconsistent options. I.e. there is little point in continuing.
228 (We do not exit, to support use of cpplib as a library.
229 Instead, it is the caller's responsibility to check
230 CPP_FATAL_ERRORS. */
231
232void
f84c2018 233cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
487a6e06 234{
5148a72b 235#ifndef ANSI_PROTOTYPES
487a6e06 236 cpp_reader *pfile;
ab87f8c8 237 const char *msgid;
487a6e06
KG
238#endif
239 va_list ap;
240
ab87f8c8 241 VA_START (ap, msgid);
487a6e06 242
5148a72b 243#ifndef ANSI_PROTOTYPES
487a6e06 244 pfile = va_arg (ap, cpp_reader *);
ab87f8c8 245 msgid = va_arg (ap, const char *);
487a6e06
KG
246#endif
247
93c80368 248 if (_cpp_begin_message (pfile, FATAL, NULL, 0))
58fea6af 249 v_message (msgid, ap);
c1212d2f
ZW
250 va_end(ap);
251}
252
253void
254cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
255{
256#ifndef ANSI_PROTOTYPES
257 cpp_reader *pfile;
258 const char *msgid;
259#endif
260 va_list ap;
261
262 VA_START(ap, msgid);
263
264#ifndef ANSI_PROTOTYPES
265 pfile = va_arg (ap, cpp_reader *);
266 msgid = va_arg (ap, const char *);
267#endif
268
93c80368 269 if (_cpp_begin_message (pfile, ERROR, NULL, 0))
58fea6af 270 v_message (msgid, ap);
c1212d2f
ZW
271 va_end(ap);
272}
273
274void
275cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
276 const char *msgid, ...))
277{
278#ifndef ANSI_PROTOTYPES
279 cpp_reader *pfile;
280 int line;
281 int column;
282 const char *msgid;
283#endif
284 va_list ap;
93c80368 285 cpp_lexer_pos pos;
c1212d2f
ZW
286
287 VA_START (ap, msgid);
288
289#ifndef ANSI_PROTOTYPES
290 pfile = va_arg (ap, cpp_reader *);
291 line = va_arg (ap, int);
292 column = va_arg (ap, int);
293 msgid = va_arg (ap, const char *);
294#endif
295
93c80368
NB
296 pos.line = line;
297 pos.col = column;
298 if (_cpp_begin_message (pfile, ERROR, NULL, &pos))
58fea6af 299 v_message (msgid, ap);
487a6e06 300 va_end(ap);
05a2b36f 301}
c1212d2f
ZW
302
303/* Error including a message from `errno'. */
7f2935c7 304void
c1212d2f 305cpp_error_from_errno (pfile, name)
7f2935c7 306 cpp_reader *pfile;
487a6e06 307 const char *name;
7f2935c7 308{
f95e46b9 309 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
c1212d2f
ZW
310}
311
312void
313cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
314{
315#ifndef ANSI_PROTOTYPES
316 cpp_reader *pfile;
317 const char *msgid;
318#endif
319 va_list ap;
320
321 VA_START (ap, msgid);
322
323#ifndef ANSI_PROTOTYPES
324 pfile = va_arg (ap, cpp_reader *);
325 msgid = va_arg (ap, const char *);
7f2935c7 326#endif
c1212d2f 327
93c80368 328 if (_cpp_begin_message (pfile, WARNING, NULL, 0))
58fea6af 329 v_message (msgid, ap);
c1212d2f 330 va_end(ap);
7f2935c7 331}
ab87f8c8 332
c1212d2f
ZW
333void
334cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
335 const char *msgid, ...))
336{
337#ifndef ANSI_PROTOTYPES
338 cpp_reader *pfile;
339 int line;
340 int column;
341 const char *msgid;
342#endif
343 va_list ap;
93c80368 344 cpp_lexer_pos pos;
c1212d2f
ZW
345
346 VA_START (ap, msgid);
347
348#ifndef ANSI_PROTOTYPES
349 pfile = va_arg (ap, cpp_reader *);
350 line = va_arg (ap, int);
351 column = va_arg (ap, int);
352 msgid = va_arg (ap, const char *);
353#endif
354
93c80368
NB
355 pos.line = line;
356 pos.col = column;
357 if (_cpp_begin_message (pfile, WARNING, NULL, &pos))
58fea6af 358 v_message (msgid, ap);
c1212d2f
ZW
359 va_end(ap);
360}
ab87f8c8
JL
361
362void
c1212d2f
ZW
363cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
364{
365#ifndef ANSI_PROTOTYPES
366 cpp_reader *pfile;
367 const char *msgid;
368#endif
369 va_list ap;
370
371 VA_START (ap, msgid);
372
373#ifndef ANSI_PROTOTYPES
374 pfile = va_arg (ap, cpp_reader *);
375 msgid = va_arg (ap, const char *);
376#endif
377
93c80368 378 if (_cpp_begin_message (pfile, PEDWARN, NULL, 0))
58fea6af 379 v_message (msgid, ap);
c1212d2f
ZW
380 va_end(ap);
381}
382
383void
384cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
385 const char *msgid, ...))
386{
ab87f8c8 387#ifndef ANSI_PROTOTYPES
c1212d2f
ZW
388 cpp_reader *pfile;
389 int line;
390 int column;
ab87f8c8
JL
391 const char *msgid;
392#endif
393 va_list ap;
93c80368 394 cpp_lexer_pos pos;
ab87f8c8
JL
395
396 VA_START (ap, msgid);
397
398#ifndef ANSI_PROTOTYPES
c1212d2f
ZW
399 pfile = va_arg (ap, cpp_reader *);
400 line = va_arg (ap, int);
401 column = va_arg (ap, int);
ab87f8c8
JL
402 msgid = va_arg (ap, const char *);
403#endif
404
93c80368
NB
405 pos.line = line;
406 pos.col = column;
407 if (_cpp_begin_message (pfile, PEDWARN, NULL, &pos))
58fea6af 408 v_message (msgid, ap);
ab87f8c8
JL
409 va_end(ap);
410}
c1212d2f
ZW
411
412/* Report a warning (or an error if pedantic_errors)
413 giving specified file name and line number, not current. */
414
415void
416cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
417 const char *file, int line, int col,
418 const char *msgid, ...))
419{
420#ifndef ANSI_PROTOTYPES
421 cpp_reader *pfile;
422 const char *file;
423 int line;
424 int col;
425 const char *msgid;
426#endif
427 va_list ap;
93c80368 428 cpp_lexer_pos pos;
c1212d2f
ZW
429
430 VA_START (ap, msgid);
431
432#ifndef ANSI_PROTOTYPES
433 pfile = va_arg (ap, cpp_reader *);
434 file = va_arg (ap, const char *);
435 line = va_arg (ap, int);
436 col = va_arg (ap, int);
437 msgid = va_arg (ap, const char *);
438#endif
439
93c80368
NB
440 pos.line = line;
441 pos.col = col;
442 if (_cpp_begin_message (pfile, PEDWARN, file, &pos))
58fea6af 443 v_message (msgid, ap);
c1212d2f
ZW
444 va_end(ap);
445}
446
447/* Print an error message not associated with a file. */
448void
449cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
450{
451#ifndef ANSI_PROTOTYPES
452 cpp_reader *pfile;
453 const char *msgid;
454#endif
455 va_list ap;
456
457 VA_START (ap, msgid);
458
459#ifndef ANSI_PROTOTYPES
460 pfile = va_arg (ap, cpp_reader *);
461 msgid = va_arg (ap, const char *);
462#endif
463
464 if (pfile->errors < CPP_FATAL_LIMIT)
465 pfile->errors++;
466
467 vfprintf (stderr, _(msgid), ap);
468 putc('\n', stderr);
469
470 va_end(ap);
471}
472
473void
474cpp_notice_from_errno (pfile, name)
475 cpp_reader *pfile;
476 const char *name;
477{
f2d5f0cc
ZW
478 if (name[0] == '\0')
479 name = "stdout";
f95e46b9 480 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
c1212d2f 481}