Fix ICE for maps with zero components.
[gcc.git] / libcpp / errors.c
1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986-2016 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
20
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
24
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "internal.h"
29
30 /* Print a diagnostic at the given location. */
31
32 ATTRIBUTE_FPTR_PRINTF(5,0)
33 static bool
34 cpp_diagnostic_at_richloc (cpp_reader * pfile, int level, int reason,
35 rich_location *richloc,
36 const char *msgid, va_list *ap)
37 {
38 bool ret;
39
40 if (!pfile->cb.error)
41 abort ();
42 ret = pfile->cb.error (pfile, level, reason, richloc, _(msgid), ap);
43
44 return ret;
45 }
46
47 /* Print a diagnostic at the given location. */
48
49 ATTRIBUTE_FPTR_PRINTF(5,0)
50 static bool
51 cpp_diagnostic_at (cpp_reader * pfile, int level, int reason,
52 source_location src_loc,
53 const char *msgid, va_list *ap)
54 {
55 bool ret;
56
57 if (!pfile->cb.error)
58 abort ();
59 rich_location richloc (pfile->line_table, src_loc);
60 ret = pfile->cb.error (pfile, level, reason, &richloc, _(msgid), ap);
61
62 return ret;
63 }
64
65 /* Print a diagnostic at the location of the previously lexed token. */
66
67 ATTRIBUTE_FPTR_PRINTF(4,0)
68 static bool
69 cpp_diagnostic (cpp_reader * pfile, int level, int reason,
70 const char *msgid, va_list *ap)
71 {
72 source_location src_loc;
73
74 if (CPP_OPTION (pfile, traditional))
75 {
76 if (pfile->state.in_directive)
77 src_loc = pfile->directive_line;
78 else
79 src_loc = pfile->line_table->highest_line;
80 }
81 /* We don't want to refer to a token before the beginning of the
82 current run -- that is invalid. */
83 else if (pfile->cur_token == pfile->cur_run->base)
84 {
85 src_loc = 0;
86 }
87 else
88 {
89 src_loc = pfile->cur_token[-1].src_loc;
90 }
91 return cpp_diagnostic_at (pfile, level, reason, src_loc, msgid, ap);
92 }
93
94 /* Print a warning or error, depending on the value of LEVEL. */
95
96 bool
97 cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
98 {
99 va_list ap;
100 bool ret;
101
102 va_start (ap, msgid);
103
104 ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap);
105
106 va_end (ap);
107 return ret;
108 }
109
110 /* Print a warning. The warning reason may be given in REASON. */
111
112 bool
113 cpp_warning (cpp_reader * pfile, int reason, const char *msgid, ...)
114 {
115 va_list ap;
116 bool ret;
117
118 va_start (ap, msgid);
119
120 ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap);
121
122 va_end (ap);
123 return ret;
124 }
125
126 /* Print a pedantic warning. The warning reason may be given in REASON. */
127
128 bool
129 cpp_pedwarning (cpp_reader * pfile, int reason, const char *msgid, ...)
130 {
131 va_list ap;
132 bool ret;
133
134 va_start (ap, msgid);
135
136 ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap);
137
138 va_end (ap);
139 return ret;
140 }
141
142 /* Print a warning, including system headers. The warning reason may be
143 given in REASON. */
144
145 bool
146 cpp_warning_syshdr (cpp_reader * pfile, int reason, const char *msgid, ...)
147 {
148 va_list ap;
149 bool ret;
150
151 va_start (ap, msgid);
152
153 ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap);
154
155 va_end (ap);
156 return ret;
157 }
158
159 /* Print a diagnostic at a specific location. */
160
161 ATTRIBUTE_FPTR_PRINTF(6,0)
162 static bool
163 cpp_diagnostic_with_line (cpp_reader * pfile, int level, int reason,
164 source_location src_loc, unsigned int column,
165 const char *msgid, va_list *ap)
166 {
167 bool ret;
168
169 if (!pfile->cb.error)
170 abort ();
171 rich_location richloc (pfile->line_table, src_loc);
172 if (column)
173 richloc.override_column (column);
174 ret = pfile->cb.error (pfile, level, reason, &richloc, _(msgid), ap);
175
176 return ret;
177 }
178
179 /* Print a warning or error, depending on the value of LEVEL. */
180
181 bool
182 cpp_error_with_line (cpp_reader *pfile, int level,
183 source_location src_loc, unsigned int column,
184 const char *msgid, ...)
185 {
186 va_list ap;
187 bool ret;
188
189 va_start (ap, msgid);
190
191 ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc,
192 column, msgid, &ap);
193
194 va_end (ap);
195 return ret;
196 }
197
198 /* Print a warning. The warning reason may be given in REASON. */
199
200 bool
201 cpp_warning_with_line (cpp_reader *pfile, int reason,
202 source_location src_loc, unsigned int column,
203 const char *msgid, ...)
204 {
205 va_list ap;
206 bool ret;
207
208 va_start (ap, msgid);
209
210 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc,
211 column, msgid, &ap);
212
213 va_end (ap);
214 return ret;
215 }
216
217 /* Print a pedantic warning. The warning reason may be given in REASON. */
218
219 bool
220 cpp_pedwarning_with_line (cpp_reader *pfile, int reason,
221 source_location src_loc, unsigned int column,
222 const char *msgid, ...)
223 {
224 va_list ap;
225 bool ret;
226
227 va_start (ap, msgid);
228
229 ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc,
230 column, msgid, &ap);
231
232 va_end (ap);
233 return ret;
234 }
235
236 /* Print a warning, including system headers. The warning reason may be
237 given in REASON. */
238
239 bool
240 cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason,
241 source_location src_loc, unsigned int column,
242 const char *msgid, ...)
243 {
244 va_list ap;
245 bool ret;
246
247 va_start (ap, msgid);
248
249 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc,
250 column, msgid, &ap);
251
252 va_end (ap);
253 return ret;
254 }
255
256 /* As cpp_error, but use SRC_LOC as the location of the error, without
257 a column override. */
258
259 bool
260 cpp_error_at (cpp_reader * pfile, int level, source_location src_loc,
261 const char *msgid, ...)
262 {
263 va_list ap;
264 bool ret;
265
266 va_start (ap, msgid);
267
268 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, src_loc,
269 msgid, &ap);
270
271 va_end (ap);
272 return ret;
273 }
274
275 /* As cpp_error, but use RICHLOC as the location of the error, without
276 a column override. */
277
278 bool
279 cpp_error_at_richloc (cpp_reader * pfile, int level, rich_location *richloc,
280 const char *msgid, ...)
281 {
282 va_list ap;
283 bool ret;
284
285 va_start (ap, msgid);
286
287 ret = cpp_diagnostic_at_richloc (pfile, level, CPP_W_NONE, richloc,
288 msgid, &ap);
289
290 va_end (ap);
291 return ret;
292 }
293
294 /* Print a warning or error, depending on the value of LEVEL. Include
295 information from errno. */
296
297 bool
298 cpp_errno (cpp_reader *pfile, int level, const char *msgid)
299 {
300 return cpp_error (pfile, level, "%s: %s", _(msgid), xstrerror (errno));
301 }
302
303 /* Print a warning or error, depending on the value of LEVEL. Include
304 information from errno. Unlike cpp_errno, the argument is a filename
305 that is not localized, but "" is replaced with localized "stdout". */
306
307 bool
308 cpp_errno_filename (cpp_reader *pfile, int level, const char *filename,
309 source_location loc)
310 {
311 if (filename[0] == '\0')
312 filename = _("stdout");
313
314 return cpp_error_at (pfile, level, loc, "%s: %s", filename,
315 xstrerror (errno));
316 }