2018-11-13 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libcpp / errors.c
blob2268fc49f0d78fcd9421e247107b07473ceaf02b
1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986-2018 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
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.
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.
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/>.
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! */
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "internal.h"
30 /* Print a diagnostic at the given location. */
32 ATTRIBUTE_FPTR_PRINTF(5,0)
33 static bool
34 cpp_diagnostic_at (cpp_reader * pfile, enum cpp_diagnostic_level level,
35 enum cpp_warning_reason reason, rich_location *richloc,
36 const char *msgid, va_list *ap)
38 bool ret;
40 if (!pfile->cb.diagnostic)
41 abort ();
42 ret = pfile->cb.diagnostic (pfile, level, reason, richloc, _(msgid), ap);
44 return ret;
47 /* Print a diagnostic at the location of the previously lexed token. */
49 ATTRIBUTE_FPTR_PRINTF(4,0)
50 static bool
51 cpp_diagnostic (cpp_reader * pfile, enum cpp_diagnostic_level level,
52 enum cpp_warning_reason reason,
53 const char *msgid, va_list *ap)
55 source_location src_loc;
57 if (CPP_OPTION (pfile, traditional))
59 if (pfile->state.in_directive)
60 src_loc = pfile->directive_line;
61 else
62 src_loc = pfile->line_table->highest_line;
64 /* We don't want to refer to a token before the beginning of the
65 current run -- that is invalid. */
66 else if (pfile->cur_token == pfile->cur_run->base)
68 src_loc = 0;
70 else
72 src_loc = pfile->cur_token[-1].src_loc;
74 rich_location richloc (pfile->line_table, src_loc);
75 return cpp_diagnostic_at (pfile, level, reason, &richloc, msgid, ap);
78 /* Print a warning or error, depending on the value of LEVEL. */
80 bool
81 cpp_error (cpp_reader * pfile, enum cpp_diagnostic_level level,
82 const char *msgid, ...)
84 va_list ap;
85 bool ret;
87 va_start (ap, msgid);
89 ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap);
91 va_end (ap);
92 return ret;
95 /* Print a warning. The warning reason may be given in REASON. */
97 bool
98 cpp_warning (cpp_reader * pfile, enum cpp_warning_reason reason,
99 const char *msgid, ...)
101 va_list ap;
102 bool ret;
104 va_start (ap, msgid);
106 ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap);
108 va_end (ap);
109 return ret;
112 /* Print a pedantic warning. The warning reason may be given in REASON. */
114 bool
115 cpp_pedwarning (cpp_reader * pfile, enum cpp_warning_reason reason,
116 const char *msgid, ...)
118 va_list ap;
119 bool ret;
121 va_start (ap, msgid);
123 ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap);
125 va_end (ap);
126 return ret;
129 /* Print a warning, including system headers. The warning reason may be
130 given in REASON. */
132 bool
133 cpp_warning_syshdr (cpp_reader * pfile, enum cpp_warning_reason reason,
134 const char *msgid, ...)
136 va_list ap;
137 bool ret;
139 va_start (ap, msgid);
141 ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap);
143 va_end (ap);
144 return ret;
147 /* Print a diagnostic at a specific location. */
149 ATTRIBUTE_FPTR_PRINTF(6,0)
150 static bool
151 cpp_diagnostic_with_line (cpp_reader * pfile, enum cpp_diagnostic_level level,
152 enum cpp_warning_reason reason,
153 source_location src_loc, unsigned int column,
154 const char *msgid, va_list *ap)
156 bool ret;
158 if (!pfile->cb.diagnostic)
159 abort ();
160 rich_location richloc (pfile->line_table, src_loc);
161 if (column)
162 richloc.override_column (column);
163 ret = pfile->cb.diagnostic (pfile, level, reason, &richloc, _(msgid), ap);
165 return ret;
168 /* Print a warning or error, depending on the value of LEVEL. */
170 bool
171 cpp_error_with_line (cpp_reader *pfile, enum cpp_diagnostic_level level,
172 source_location src_loc, unsigned int column,
173 const char *msgid, ...)
175 va_list ap;
176 bool ret;
178 va_start (ap, msgid);
180 ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc,
181 column, msgid, &ap);
183 va_end (ap);
184 return ret;
187 /* Print a warning. The warning reason may be given in REASON. */
189 bool
190 cpp_warning_with_line (cpp_reader *pfile, enum cpp_warning_reason reason,
191 source_location src_loc, unsigned int column,
192 const char *msgid, ...)
194 va_list ap;
195 bool ret;
197 va_start (ap, msgid);
199 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc,
200 column, msgid, &ap);
202 va_end (ap);
203 return ret;
206 /* Print a pedantic warning. The warning reason may be given in REASON. */
208 bool
209 cpp_pedwarning_with_line (cpp_reader *pfile, enum cpp_warning_reason reason,
210 source_location src_loc, unsigned int column,
211 const char *msgid, ...)
213 va_list ap;
214 bool ret;
216 va_start (ap, msgid);
218 ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc,
219 column, msgid, &ap);
221 va_end (ap);
222 return ret;
225 /* Print a warning, including system headers. The warning reason may be
226 given in REASON. */
228 bool
229 cpp_warning_with_line_syshdr (cpp_reader *pfile, enum cpp_warning_reason reason,
230 source_location src_loc, unsigned int column,
231 const char *msgid, ...)
233 va_list ap;
234 bool ret;
236 va_start (ap, msgid);
238 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc,
239 column, msgid, &ap);
241 va_end (ap);
242 return ret;
245 /* As cpp_error, but use SRC_LOC as the location of the error, without
246 a column override. */
248 bool
249 cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level,
250 source_location src_loc, const char *msgid, ...)
252 va_list ap;
253 bool ret;
255 va_start (ap, msgid);
257 rich_location richloc (pfile->line_table, src_loc);
258 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, &richloc,
259 msgid, &ap);
261 va_end (ap);
262 return ret;
265 /* As cpp_error, but use RICHLOC as the location of the error, without
266 a column override. */
268 bool
269 cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level,
270 rich_location *richloc, const char *msgid, ...)
272 va_list ap;
273 bool ret;
275 va_start (ap, msgid);
277 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, richloc,
278 msgid, &ap);
280 va_end (ap);
281 return ret;
284 /* Print a warning or error, depending on the value of LEVEL. Include
285 information from errno. */
287 bool
288 cpp_errno (cpp_reader *pfile, enum cpp_diagnostic_level level,
289 const char *msgid)
291 return cpp_error (pfile, level, "%s: %s", _(msgid), xstrerror (errno));
294 /* Print a warning or error, depending on the value of LEVEL. Include
295 information from errno. Unlike cpp_errno, the argument is a filename
296 that is not localized, but "" is replaced with localized "stdout". */
298 bool
299 cpp_errno_filename (cpp_reader *pfile, enum cpp_diagnostic_level level,
300 const char *filename,
301 source_location loc)
303 if (filename[0] == '\0')
304 filename = _("stdout");
306 return cpp_error_at (pfile, level, loc, "%s: %s", filename,
307 xstrerror (errno));