* config/xtensa/xtensa.h (ASM_OUTPUT_POOL_PROLOGUE): Emit a
[official-gcc.git] / gcc / cpperror.c
blob375dc2ba5b38143344d7d9eff29ecb4b5e8befad
1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000,
3 2001, 2002 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
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.
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.
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.
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! */
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30 #include "intl.h"
32 static void print_location PARAMS ((cpp_reader *, unsigned int, unsigned int));
34 /* Don't remove the blank before do, as otherwise the exgettext
35 script will mistake this as a function definition */
36 #define v_message(msgid, ap) \
37 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
39 /* Print the logical file location (LINE, COL) in preparation for a
40 diagnostic. Outputs the #include chain if it has changed. */
41 static void
42 print_location (pfile, line, col)
43 cpp_reader *pfile;
44 unsigned int line, col;
46 cpp_buffer *buffer = pfile->buffer;
48 if (!buffer)
49 fprintf (stderr, "%s: ", progname);
50 else
52 const struct line_map *map;
54 if (line == 0)
56 line = pfile->cur_token[-1].line;
57 col = pfile->cur_token[-1].col;
60 map = lookup_line (&pfile->line_maps, line);
61 print_containing_files (&pfile->line_maps, map);
63 line = SOURCE_LINE (map, line);
64 if (col == 0)
65 col = 1;
67 if (line == 0)
68 fprintf (stderr, "%s:", map->to_file);
69 else if (CPP_OPTION (pfile, show_column) == 0)
70 fprintf (stderr, "%s:%u:", map->to_file, line);
71 else
72 fprintf (stderr, "%s:%u:%u:", map->to_file, line, col);
74 fputc (' ', stderr);
78 /* Set up for an error message: print the file and line, bump the error
79 counter, etc. LINE is the logical line number; zero means to print
80 at the location of the previously lexed token, which tends to be the
81 correct place by default. Returns 0 if the error has been suppressed. */
82 int
83 _cpp_begin_message (pfile, code, line, column)
84 cpp_reader *pfile;
85 enum error_type code;
86 unsigned int line, column;
88 int is_warning = 0;
90 switch (code)
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)))
101 if (CPP_OPTION (pfile, inhibit_errors))
102 return 0;
103 if (pfile->errors < CPP_FATAL_LIMIT)
104 pfile->errors++;
106 else
108 if (CPP_OPTION (pfile, inhibit_warnings))
109 return 0;
110 is_warning = 1;
112 break;
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;
130 print_location (pfile, line, column);
131 if (is_warning)
132 fputs (_("warning: "), stderr);
134 return 1;
137 /* Exported interface. */
139 /* For reporting internal errors. Prints "internal error: " for you,
140 otherwise identical to cpp_fatal. */
141 void
142 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
144 VA_OPEN (ap, msgid);
145 VA_FIXEDARG (ap, cpp_reader *, pfile);
146 VA_FIXEDARG (ap, const char *, msgid);
148 if (_cpp_begin_message (pfile, ICE, 0, 0))
149 v_message (msgid, ap);
151 VA_CLOSE (ap);
154 /* Same as cpp_error, except we consider the error to be "fatal",
155 such as inconsistent options. I.e. there is little point in continuing.
156 (We do not exit, to support use of cpplib as a library.
157 Instead, it is the caller's responsibility to check
158 CPP_FATAL_ERRORS. */
159 void
160 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
162 VA_OPEN (ap, msgid);
163 VA_FIXEDARG (ap, cpp_reader *, pfile);
164 VA_FIXEDARG (ap, const char *, msgid);
166 if (_cpp_begin_message (pfile, FATAL, 0, 0))
167 v_message (msgid, ap);
169 VA_CLOSE (ap);
172 /* Print an error at the location of the previously lexed token. */
173 void
174 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
176 VA_OPEN (ap, msgid);
177 VA_FIXEDARG (ap, cpp_reader *, pfile);
178 VA_FIXEDARG (ap, const char *, msgid);
180 if (_cpp_begin_message (pfile, ERROR, 0, 0))
181 v_message (msgid, ap);
183 VA_CLOSE (ap);
186 /* Print an error at a specific location. */
187 void
188 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
189 const char *msgid, ...))
191 VA_OPEN (ap, msgid);
192 VA_FIXEDARG (ap, cpp_reader *, pfile);
193 VA_FIXEDARG (ap, int, line);
194 VA_FIXEDARG (ap, int, column);
195 VA_FIXEDARG (ap, const char *, msgid);
197 if (_cpp_begin_message (pfile, ERROR, line, column))
198 v_message (msgid, ap);
200 VA_CLOSE (ap);
203 /* Error including a message from `errno'. */
204 void
205 cpp_error_from_errno (pfile, name)
206 cpp_reader *pfile;
207 const char *name;
209 cpp_error (pfile, "%s: %s", name, xstrerror (errno));
212 /* Print a warning at the location of the previously lexed token. */
213 void
214 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
216 VA_OPEN (ap, msgid);
217 VA_FIXEDARG (ap, cpp_reader *, pfile);
218 VA_FIXEDARG (ap, const char *, msgid);
220 if (_cpp_begin_message (pfile, WARNING, 0, 0))
221 v_message (msgid, ap);
223 VA_CLOSE (ap);
226 /* Print a warning at a specific location. */
227 void
228 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
229 const char *msgid, ...))
231 VA_OPEN (ap, msgid);
232 VA_FIXEDARG (ap, cpp_reader *, pfile);
233 VA_FIXEDARG (ap, int, line);
234 VA_FIXEDARG (ap, int, column);
235 VA_FIXEDARG (ap, const char *, msgid);
237 if (_cpp_begin_message (pfile, WARNING, line, column))
238 v_message (msgid, ap);
240 VA_CLOSE (ap);
243 /* Pedwarn at the location of the previously lexed token. */
244 void
245 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
247 VA_OPEN (ap, msgid);
248 VA_FIXEDARG (ap, cpp_reader *, pfile);
249 VA_FIXEDARG (ap, const char *, msgid);
251 if (_cpp_begin_message (pfile, PEDWARN, 0, 0))
252 v_message (msgid, ap);
254 VA_CLOSE (ap);
257 /* Pedwarn at a specific location. */
258 void
259 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
260 const char *msgid, ...))
262 VA_OPEN (ap, msgid);
263 VA_FIXEDARG (ap, cpp_reader *, pfile);
264 VA_FIXEDARG (ap, int, line);
265 VA_FIXEDARG (ap, int, column);
266 VA_FIXEDARG (ap, const char *, msgid);
268 if (_cpp_begin_message (pfile, PEDWARN, line, column))
269 v_message (msgid, ap);
271 VA_CLOSE (ap);
274 /* Print an error message not associated with the translation unit. */
275 void
276 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
278 VA_OPEN (ap, msgid);
279 VA_FIXEDARG (ap, cpp_reader *, pfile);
280 VA_FIXEDARG (ap, const char *, msgid);
282 if (pfile->errors < CPP_FATAL_LIMIT)
283 pfile->errors++;
285 v_message (msgid, ap);
287 VA_CLOSE (ap);
290 /* Print an error message originating from ERRNO and not associated
291 with the translation unit. */
292 void
293 cpp_notice_from_errno (pfile, name)
294 cpp_reader *pfile;
295 const char *name;
297 if (name[0] == '\0')
298 name = "stdout";
299 cpp_notice (pfile, "%s: %s", name, xstrerror (errno));