]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cpperror.c
Daily bump.
[thirdparty/gcc.git] / gcc / cpperror.c
CommitLineData
7f2935c7 1/* Default error handlers for CPP Library.
5d8ebbd8
NB
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000,
3 2001, 2002 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
dc3786d8 32static void print_location (cpp_reader *, fileline, unsigned int);
146ef880 33
5d8ebbd8 34/* Print the logical file location (LINE, COL) in preparation for a
ebef4e8c
NB
35 diagnostic. Outputs the #include chain if it has changed. A line
36 of zero suppresses the include stack, and outputs the program name
37 instead. */
c1212d2f 38static void
dc3786d8 39print_location (cpp_reader *pfile, fileline line, unsigned int col)
7f2935c7 40{
ebef4e8c 41 if (!pfile->buffer || line == 0)
0bda4760 42 fprintf (stderr, "%s: ", progname);
14fbab6d 43 else
0bda4760 44 {
bb74c963 45 const struct line_map *map;
dc3786d8 46 unsigned int lin;
0bda4760 47
a2f7be91
ZW
48 map = linemap_lookup (&pfile->line_maps, line);
49 linemap_print_containing_files (&pfile->line_maps, map);
0bda4760 50
dc3786d8 51 lin = SOURCE_LINE (map, line);
bb74c963
NB
52 if (col == 0)
53 col = 1;
0bda4760 54
dc3786d8 55 if (lin == 0)
bb74c963 56 fprintf (stderr, "%s:", map->to_file);
0bda4760 57 else if (CPP_OPTION (pfile, show_column) == 0)
dc3786d8 58 fprintf (stderr, "%s:%u:", map->to_file, lin);
0bda4760 59 else
dc3786d8 60 fprintf (stderr, "%s:%u:%u:", map->to_file, lin, col);
0bda4760 61
d82fc108 62 fputc (' ', stderr);
0bda4760 63 }
7f2935c7
PB
64}
65
ebef4e8c 66/* Set up for a diagnostic: print the file and line, bump the error
97293897 67 counter, etc. LINE is the logical line number; zero means to print
ebef4e8c
NB
68 at the location of the previously lexed token, which tends to be
69 the correct place by default. Returns 0 if the error has been
70 suppressed. */
58fea6af 71int
dc3786d8 72_cpp_begin_message (cpp_reader *pfile, int code, fileline line,
6cf87ca4 73 unsigned int column)
7f2935c7 74{
ebef4e8c 75 int level = DL_EXTRACT (code);
c1212d2f 76
ebef4e8c 77 switch (level)
c1212d2f 78 {
ebef4e8c
NB
79 case DL_WARNING:
80 case DL_PEDWARN:
d8090680
NB
81 if (CPP_IN_SYSTEM_HEADER (pfile)
82 && ! CPP_OPTION (pfile, warn_system_headers))
83 return 0;
ebef4e8c
NB
84 /* Fall through. */
85
86 case DL_WARNING_SYSHDR:
c933c209 87 if (CPP_OPTION (pfile, warnings_are_errors)
ebef4e8c 88 || (level == DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
58fea6af
ZW
89 {
90 if (CPP_OPTION (pfile, inhibit_errors))
91 return 0;
ceeedfc1 92 level = DL_ERROR;
bdee42b1 93 pfile->errors++;
58fea6af 94 }
ebef4e8c
NB
95 else if (CPP_OPTION (pfile, inhibit_warnings))
96 return 0;
58fea6af 97 break;
df383483 98
ebef4e8c 99 case DL_ERROR:
58fea6af
ZW
100 if (CPP_OPTION (pfile, inhibit_errors))
101 return 0;
bdee42b1 102 /* ICEs cannot be inhibited. */
ebef4e8c 103 case DL_ICE:
bdee42b1 104 pfile->errors++;
c1212d2f 105 break;
ab87f8c8
JL
106 }
107
97293897 108 print_location (pfile, line, column);
ebef4e8c 109 if (DL_WARNING_P (level))
58fea6af 110 fputs (_("warning: "), stderr);
ebef4e8c
NB
111 else if (level == DL_ICE)
112 fputs (_("internal error: "), stderr);
58fea6af
ZW
113
114 return 1;
7f2935c7
PB
115}
116
ebef4e8c
NB
117/* Don't remove the blank before do, as otherwise the exgettext
118 script will mistake this as a function definition */
119#define v_message(msgid, ap) \
120 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
b649398a 121
ebef4e8c 122/* Exported interface. */
c1212d2f 123
5d8ebbd8 124/* Print an error at the location of the previously lexed token. */
c1212d2f 125void
e34d07f2 126cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
c1212d2f 127{
dc3786d8
PB
128 fileline line;
129 unsigned int column;
e34d07f2
KG
130 va_list ap;
131
132 va_start (ap, msgid);
c1212d2f 133
ebef4e8c
NB
134 if (pfile->buffer)
135 {
00b94a44
NB
136 if (CPP_OPTION (pfile, traditional))
137 {
cd98faa1
NB
138 if (pfile->state.in_directive)
139 line = pfile->directive_line;
140 else
141 line = pfile->line;
00b94a44
NB
142 column = 0;
143 }
144 else
145 {
146 line = pfile->cur_token[-1].line;
147 column = pfile->cur_token[-1].col;
148 }
ebef4e8c
NB
149 }
150 else
151 line = column = 0;
c1212d2f 152
ebef4e8c 153 if (_cpp_begin_message (pfile, level, line, column))
58fea6af 154 v_message (msgid, ap);
b649398a 155
e34d07f2 156 va_end (ap);
c1212d2f
ZW
157}
158
ebef4e8c 159/* Print an error at a specific location. */
c1212d2f 160void
e34d07f2 161cpp_error_with_line (cpp_reader *pfile, int level,
dc3786d8 162 fileline line, unsigned int column,
e34d07f2 163 const char *msgid, ...)
c1212d2f 164{
e34d07f2
KG
165 va_list ap;
166
167 va_start (ap, msgid);
ab87f8c8 168
ebef4e8c 169 if (_cpp_begin_message (pfile, level, line, column))
58fea6af 170 v_message (msgid, ap);
b649398a 171
e34d07f2 172 va_end (ap);
c1212d2f
ZW
173}
174
c1212d2f 175void
6cf87ca4 176cpp_errno (cpp_reader *pfile, int level, const char *msgid)
c1212d2f 177{
ebef4e8c
NB
178 if (msgid[0] == '\0')
179 msgid = _("stdout");
180
181 cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));
c1212d2f 182}