]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cpperror.c
configure.in: Place definition of MULTISUBDIR in libsupc++/Makefile as is done for...
[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;
127 if (*filename == '\0')
128 filename = _("<stdin>");
129
130 if (line == 0)
131 fprintf (stderr, "%s: ", filename);
132 else if (CPP_OPTION (pfile, show_column) == 0)
133 fprintf (stderr, "%s:%u: ", filename, line);
134 else
135 fprintf (stderr, "%s:%u:%u: ", filename, line, col);
136
137 if (type == BUF_PRAGMA)
138 fprintf (stderr, "_Pragma: ");
139 }
7f2935c7
PB
140}
141
58fea6af
ZW
142/* Set up for an error message: print the file and line, bump the error
143 counter, etc.
144 If it returns 0, this error has been suppressed. */
0f41302f 145
58fea6af 146int
93c80368 147_cpp_begin_message (pfile, code, file, pos)
c1212d2f 148 cpp_reader *pfile;
58fea6af 149 enum error_type code;
c1212d2f 150 const char *file;
93c80368 151 const cpp_lexer_pos *pos;
7f2935c7 152{
58fea6af 153 int is_warning = 0;
c1212d2f 154
58fea6af 155 switch (code)
c1212d2f 156 {
58fea6af 157 case WARNING:
d8090680
NB
158 if (CPP_IN_SYSTEM_HEADER (pfile)
159 && ! CPP_OPTION (pfile, warn_system_headers))
160 return 0;
041c3194
ZW
161 if (! CPP_OPTION (pfile, warnings_are_errors))
162 {
d8090680 163 if (CPP_OPTION (pfile, inhibit_warnings))
58fea6af
ZW
164 return 0;
165 is_warning = 1;
166 }
167 else
168 {
169 if (CPP_OPTION (pfile, inhibit_errors))
170 return 0;
171 if (pfile->errors < CPP_FATAL_LIMIT)
172 pfile->errors++;
173 }
174 break;
175
176 case PEDWARN:
d8090680
NB
177 if (CPP_IN_SYSTEM_HEADER (pfile)
178 && ! CPP_OPTION (pfile, warn_system_headers))
179 return 0;
58fea6af
ZW
180 if (! CPP_OPTION (pfile, pedantic_errors))
181 {
d8090680 182 if (CPP_OPTION (pfile, inhibit_warnings))
58fea6af
ZW
183 return 0;
184 is_warning = 1;
041c3194 185 }
58fea6af
ZW
186 else
187 {
188 if (CPP_OPTION (pfile, inhibit_errors))
189 return 0;
190 if (pfile->errors < CPP_FATAL_LIMIT)
191 pfile->errors++;
192 }
193 break;
194
195 case ERROR:
196 if (CPP_OPTION (pfile, inhibit_errors))
197 return 0;
ab87f8c8
JL
198 if (pfile->errors < CPP_FATAL_LIMIT)
199 pfile->errors++;
200 break;
58fea6af
ZW
201 /* Fatal errors cannot be inhibited. */
202 case FATAL:
ab87f8c8
JL
203 pfile->errors = CPP_FATAL_LIMIT;
204 break;
58fea6af 205 case ICE:
c1212d2f
ZW
206 fprintf (stderr, _("internal error: "));
207 pfile->errors = CPP_FATAL_LIMIT;
208 break;
ab87f8c8
JL
209 }
210
0bda4760 211 print_location (pfile, file, pos);
58fea6af
ZW
212 if (is_warning)
213 fputs (_("warning: "), stderr);
214
215 return 1;
7f2935c7
PB
216}
217
c1212d2f
ZW
218/* Exported interface. */
219
220/* For reporting internal errors. Prints "internal error: " for you,
221 otherwise identical to cpp_fatal. */
222
487a6e06 223void
c1212d2f
ZW
224cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
225{
5148a72b 226#ifndef ANSI_PROTOTYPES
487a6e06 227 cpp_reader *pfile;
1c5d09e4 228 const char *msgid;
487a6e06
KG
229#endif
230 va_list ap;
231
ab87f8c8 232 VA_START (ap, msgid);
487a6e06 233
5148a72b 234#ifndef ANSI_PROTOTYPES
487a6e06 235 pfile = va_arg (ap, cpp_reader *);
ab87f8c8 236 msgid = va_arg (ap, const char *);
487a6e06
KG
237#endif
238
93c80368 239 if (_cpp_begin_message (pfile, ICE, NULL, 0))
58fea6af 240 v_message (msgid, ap);
487a6e06
KG
241 va_end(ap);
242}
243
05a2b36f
PB
244/* Same as cpp_error, except we consider the error to be "fatal",
245 such as inconsistent options. I.e. there is little point in continuing.
246 (We do not exit, to support use of cpplib as a library.
247 Instead, it is the caller's responsibility to check
248 CPP_FATAL_ERRORS. */
249
250void
f84c2018 251cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
487a6e06 252{
5148a72b 253#ifndef ANSI_PROTOTYPES
487a6e06 254 cpp_reader *pfile;
ab87f8c8 255 const char *msgid;
487a6e06
KG
256#endif
257 va_list ap;
258
ab87f8c8 259 VA_START (ap, msgid);
487a6e06 260
5148a72b 261#ifndef ANSI_PROTOTYPES
487a6e06 262 pfile = va_arg (ap, cpp_reader *);
ab87f8c8 263 msgid = va_arg (ap, const char *);
487a6e06
KG
264#endif
265
93c80368 266 if (_cpp_begin_message (pfile, FATAL, NULL, 0))
58fea6af 267 v_message (msgid, ap);
c1212d2f
ZW
268 va_end(ap);
269}
270
271void
272cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
273{
274#ifndef ANSI_PROTOTYPES
275 cpp_reader *pfile;
276 const char *msgid;
277#endif
278 va_list ap;
279
280 VA_START(ap, msgid);
281
282#ifndef ANSI_PROTOTYPES
283 pfile = va_arg (ap, cpp_reader *);
284 msgid = va_arg (ap, const char *);
285#endif
286
93c80368 287 if (_cpp_begin_message (pfile, ERROR, NULL, 0))
58fea6af 288 v_message (msgid, ap);
c1212d2f
ZW
289 va_end(ap);
290}
291
292void
293cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
294 const char *msgid, ...))
295{
296#ifndef ANSI_PROTOTYPES
297 cpp_reader *pfile;
298 int line;
299 int column;
300 const char *msgid;
301#endif
302 va_list ap;
93c80368 303 cpp_lexer_pos pos;
c1212d2f
ZW
304
305 VA_START (ap, msgid);
306
307#ifndef ANSI_PROTOTYPES
308 pfile = va_arg (ap, cpp_reader *);
309 line = va_arg (ap, int);
310 column = va_arg (ap, int);
311 msgid = va_arg (ap, const char *);
312#endif
313
93c80368
NB
314 pos.line = line;
315 pos.col = column;
316 if (_cpp_begin_message (pfile, ERROR, NULL, &pos))
58fea6af 317 v_message (msgid, ap);
487a6e06 318 va_end(ap);
05a2b36f 319}
c1212d2f
ZW
320
321/* Error including a message from `errno'. */
7f2935c7 322void
c1212d2f 323cpp_error_from_errno (pfile, name)
7f2935c7 324 cpp_reader *pfile;
487a6e06 325 const char *name;
7f2935c7 326{
f95e46b9 327 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
c1212d2f
ZW
328}
329
330void
331cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
332{
333#ifndef ANSI_PROTOTYPES
334 cpp_reader *pfile;
335 const char *msgid;
336#endif
337 va_list ap;
338
339 VA_START (ap, msgid);
340
341#ifndef ANSI_PROTOTYPES
342 pfile = va_arg (ap, cpp_reader *);
343 msgid = va_arg (ap, const char *);
7f2935c7 344#endif
c1212d2f 345
93c80368 346 if (_cpp_begin_message (pfile, WARNING, NULL, 0))
58fea6af 347 v_message (msgid, ap);
c1212d2f 348 va_end(ap);
7f2935c7 349}
ab87f8c8 350
c1212d2f
ZW
351void
352cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
353 const char *msgid, ...))
354{
355#ifndef ANSI_PROTOTYPES
356 cpp_reader *pfile;
357 int line;
358 int column;
359 const char *msgid;
360#endif
361 va_list ap;
93c80368 362 cpp_lexer_pos pos;
c1212d2f
ZW
363
364 VA_START (ap, msgid);
365
366#ifndef ANSI_PROTOTYPES
367 pfile = va_arg (ap, cpp_reader *);
368 line = va_arg (ap, int);
369 column = va_arg (ap, int);
370 msgid = va_arg (ap, const char *);
371#endif
372
93c80368
NB
373 pos.line = line;
374 pos.col = column;
375 if (_cpp_begin_message (pfile, WARNING, NULL, &pos))
58fea6af 376 v_message (msgid, ap);
c1212d2f
ZW
377 va_end(ap);
378}
ab87f8c8
JL
379
380void
c1212d2f
ZW
381cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
382{
383#ifndef ANSI_PROTOTYPES
384 cpp_reader *pfile;
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 msgid = va_arg (ap, const char *);
394#endif
395
93c80368 396 if (_cpp_begin_message (pfile, PEDWARN, NULL, 0))
58fea6af 397 v_message (msgid, ap);
c1212d2f
ZW
398 va_end(ap);
399}
400
401void
402cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
403 const char *msgid, ...))
404{
ab87f8c8 405#ifndef ANSI_PROTOTYPES
c1212d2f
ZW
406 cpp_reader *pfile;
407 int line;
408 int column;
ab87f8c8
JL
409 const char *msgid;
410#endif
411 va_list ap;
93c80368 412 cpp_lexer_pos pos;
ab87f8c8
JL
413
414 VA_START (ap, msgid);
415
416#ifndef ANSI_PROTOTYPES
c1212d2f
ZW
417 pfile = va_arg (ap, cpp_reader *);
418 line = va_arg (ap, int);
419 column = va_arg (ap, int);
ab87f8c8
JL
420 msgid = va_arg (ap, const char *);
421#endif
422
93c80368
NB
423 pos.line = line;
424 pos.col = column;
425 if (_cpp_begin_message (pfile, PEDWARN, NULL, &pos))
58fea6af 426 v_message (msgid, ap);
ab87f8c8
JL
427 va_end(ap);
428}
c1212d2f
ZW
429
430/* Report a warning (or an error if pedantic_errors)
431 giving specified file name and line number, not current. */
432
433void
434cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
435 const char *file, int line, int col,
436 const char *msgid, ...))
437{
438#ifndef ANSI_PROTOTYPES
439 cpp_reader *pfile;
440 const char *file;
441 int line;
442 int col;
443 const char *msgid;
444#endif
445 va_list ap;
93c80368 446 cpp_lexer_pos pos;
c1212d2f
ZW
447
448 VA_START (ap, msgid);
449
450#ifndef ANSI_PROTOTYPES
451 pfile = va_arg (ap, cpp_reader *);
452 file = va_arg (ap, const char *);
453 line = va_arg (ap, int);
454 col = va_arg (ap, int);
455 msgid = va_arg (ap, const char *);
456#endif
457
93c80368
NB
458 pos.line = line;
459 pos.col = col;
460 if (_cpp_begin_message (pfile, PEDWARN, file, &pos))
58fea6af 461 v_message (msgid, ap);
c1212d2f
ZW
462 va_end(ap);
463}
464
465/* Print an error message not associated with a file. */
466void
467cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
468{
469#ifndef ANSI_PROTOTYPES
470 cpp_reader *pfile;
471 const char *msgid;
472#endif
473 va_list ap;
474
475 VA_START (ap, msgid);
476
477#ifndef ANSI_PROTOTYPES
478 pfile = va_arg (ap, cpp_reader *);
479 msgid = va_arg (ap, const char *);
480#endif
481
482 if (pfile->errors < CPP_FATAL_LIMIT)
483 pfile->errors++;
484
485 vfprintf (stderr, _(msgid), ap);
486 putc('\n', stderr);
487
488 va_end(ap);
489}
490
491void
492cpp_notice_from_errno (pfile, name)
493 cpp_reader *pfile;
494 const char *name;
495{
f2d5f0cc
ZW
496 if (name[0] == '\0')
497 name = "stdout";
f95e46b9 498 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
c1212d2f 499}