[gcc]
[official-gcc.git] / libcpp / errors.c
blobd49c85324d8358fe670d2df6bf87dee71eb39a2a
1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986-2017 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, int level, int reason,
35 rich_location *richloc,
36 const char *msgid, va_list *ap)
38 bool ret;
40 if (!pfile->cb.error)
41 abort ();
42 ret = pfile->cb.error (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, int level, int reason,
52 const char *msgid, va_list *ap)
54 source_location src_loc;
56 if (CPP_OPTION (pfile, traditional))
58 if (pfile->state.in_directive)
59 src_loc = pfile->directive_line;
60 else
61 src_loc = pfile->line_table->highest_line;
63 /* We don't want to refer to a token before the beginning of the
64 current run -- that is invalid. */
65 else if (pfile->cur_token == pfile->cur_run->base)
67 src_loc = 0;
69 else
71 src_loc = pfile->cur_token[-1].src_loc;
73 rich_location richloc (pfile->line_table, src_loc);
74 return cpp_diagnostic_at (pfile, level, reason, &richloc, msgid, ap);
77 /* Print a warning or error, depending on the value of LEVEL. */
79 bool
80 cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
82 va_list ap;
83 bool ret;
85 va_start (ap, msgid);
87 ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap);
89 va_end (ap);
90 return ret;
93 /* Print a warning. The warning reason may be given in REASON. */
95 bool
96 cpp_warning (cpp_reader * pfile, int reason, const char *msgid, ...)
98 va_list ap;
99 bool ret;
101 va_start (ap, msgid);
103 ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap);
105 va_end (ap);
106 return ret;
109 /* Print a pedantic warning. The warning reason may be given in REASON. */
111 bool
112 cpp_pedwarning (cpp_reader * pfile, int reason, const char *msgid, ...)
114 va_list ap;
115 bool ret;
117 va_start (ap, msgid);
119 ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap);
121 va_end (ap);
122 return ret;
125 /* Print a warning, including system headers. The warning reason may be
126 given in REASON. */
128 bool
129 cpp_warning_syshdr (cpp_reader * pfile, int reason, const char *msgid, ...)
131 va_list ap;
132 bool ret;
134 va_start (ap, msgid);
136 ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap);
138 va_end (ap);
139 return ret;
142 /* Print a diagnostic at a specific location. */
144 ATTRIBUTE_FPTR_PRINTF(6,0)
145 static bool
146 cpp_diagnostic_with_line (cpp_reader * pfile, int level, int reason,
147 source_location src_loc, unsigned int column,
148 const char *msgid, va_list *ap)
150 bool ret;
152 if (!pfile->cb.error)
153 abort ();
154 rich_location richloc (pfile->line_table, src_loc);
155 if (column)
156 richloc.override_column (column);
157 ret = pfile->cb.error (pfile, level, reason, &richloc, _(msgid), ap);
159 return ret;
162 /* Print a warning or error, depending on the value of LEVEL. */
164 bool
165 cpp_error_with_line (cpp_reader *pfile, int level,
166 source_location src_loc, unsigned int column,
167 const char *msgid, ...)
169 va_list ap;
170 bool ret;
172 va_start (ap, msgid);
174 ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc,
175 column, msgid, &ap);
177 va_end (ap);
178 return ret;
181 /* Print a warning. The warning reason may be given in REASON. */
183 bool
184 cpp_warning_with_line (cpp_reader *pfile, int reason,
185 source_location src_loc, unsigned int column,
186 const char *msgid, ...)
188 va_list ap;
189 bool ret;
191 va_start (ap, msgid);
193 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc,
194 column, msgid, &ap);
196 va_end (ap);
197 return ret;
200 /* Print a pedantic warning. The warning reason may be given in REASON. */
202 bool
203 cpp_pedwarning_with_line (cpp_reader *pfile, int reason,
204 source_location src_loc, unsigned int column,
205 const char *msgid, ...)
207 va_list ap;
208 bool ret;
210 va_start (ap, msgid);
212 ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc,
213 column, msgid, &ap);
215 va_end (ap);
216 return ret;
219 /* Print a warning, including system headers. The warning reason may be
220 given in REASON. */
222 bool
223 cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason,
224 source_location src_loc, unsigned int column,
225 const char *msgid, ...)
227 va_list ap;
228 bool ret;
230 va_start (ap, msgid);
232 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc,
233 column, msgid, &ap);
235 va_end (ap);
236 return ret;
239 /* As cpp_error, but use SRC_LOC as the location of the error, without
240 a column override. */
242 bool
243 cpp_error_at (cpp_reader * pfile, int level, source_location src_loc,
244 const char *msgid, ...)
246 va_list ap;
247 bool ret;
249 va_start (ap, msgid);
251 rich_location richloc (pfile->line_table, src_loc);
252 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, &richloc,
253 msgid, &ap);
255 va_end (ap);
256 return ret;
259 /* As cpp_error, but use RICHLOC as the location of the error, without
260 a column override. */
262 bool
263 cpp_error_at (cpp_reader * pfile, int level, rich_location *richloc,
264 const char *msgid, ...)
266 va_list ap;
267 bool ret;
269 va_start (ap, msgid);
271 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, richloc,
272 msgid, &ap);
274 va_end (ap);
275 return ret;
278 /* Print a warning or error, depending on the value of LEVEL. Include
279 information from errno. */
281 bool
282 cpp_errno (cpp_reader *pfile, int level, const char *msgid)
284 return cpp_error (pfile, level, "%s: %s", _(msgid), xstrerror (errno));
287 /* Print a warning or error, depending on the value of LEVEL. Include
288 information from errno. Unlike cpp_errno, the argument is a filename
289 that is not localized, but "" is replaced with localized "stdout". */
291 bool
292 cpp_errno_filename (cpp_reader *pfile, int level, const char *filename,
293 source_location loc)
295 if (filename[0] == '\0')
296 filename = _("stdout");
298 return cpp_error_at (pfile, level, loc, "%s: %s", filename,
299 xstrerror (errno));