1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986-2021 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
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! */
30 /* Get a location_t for the current location in PFILE,
31 generally that of the previously lexed token. */
34 cpp_diagnostic_get_current_location (cpp_reader
*pfile
)
36 if (CPP_OPTION (pfile
, traditional
))
38 if (pfile
->state
.in_directive
)
39 return pfile
->directive_line
;
41 return pfile
->line_table
->highest_line
;
43 /* We don't want to refer to a token before the beginning of the
44 current run -- that is invalid. */
45 else if (pfile
->cur_token
== pfile
->cur_run
->base
)
51 return pfile
->cur_token
[-1].src_loc
;
55 /* Print a diagnostic at the given location. */
57 ATTRIBUTE_FPTR_PRINTF(5,0)
59 cpp_diagnostic_at (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
60 enum cpp_warning_reason reason
, rich_location
*richloc
,
61 const char *msgid
, va_list *ap
)
65 if (!pfile
->cb
.diagnostic
)
67 ret
= pfile
->cb
.diagnostic (pfile
, level
, reason
, richloc
, _(msgid
), ap
);
72 /* Print a diagnostic at the location of the previously lexed token. */
74 ATTRIBUTE_FPTR_PRINTF(4,0)
76 cpp_diagnostic (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
77 enum cpp_warning_reason reason
,
78 const char *msgid
, va_list *ap
)
80 location_t src_loc
= cpp_diagnostic_get_current_location (pfile
);
81 rich_location
richloc (pfile
->line_table
, src_loc
);
82 return cpp_diagnostic_at (pfile
, level
, reason
, &richloc
, msgid
, ap
);
85 /* Print a warning or error, depending on the value of LEVEL. */
88 cpp_error (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
89 const char *msgid
, ...)
96 ret
= cpp_diagnostic (pfile
, level
, CPP_W_NONE
, msgid
, &ap
);
102 /* Print a warning. The warning reason may be given in REASON. */
105 cpp_warning (cpp_reader
* pfile
, enum cpp_warning_reason reason
,
106 const char *msgid
, ...)
111 va_start (ap
, msgid
);
113 ret
= cpp_diagnostic (pfile
, CPP_DL_WARNING
, reason
, msgid
, &ap
);
119 /* Print a pedantic warning. The warning reason may be given in REASON. */
122 cpp_pedwarning (cpp_reader
* pfile
, enum cpp_warning_reason reason
,
123 const char *msgid
, ...)
128 va_start (ap
, msgid
);
130 ret
= cpp_diagnostic (pfile
, CPP_DL_PEDWARN
, reason
, msgid
, &ap
);
136 /* Print a warning, including system headers. The warning reason may be
140 cpp_warning_syshdr (cpp_reader
* pfile
, enum cpp_warning_reason reason
,
141 const char *msgid
, ...)
146 va_start (ap
, msgid
);
148 ret
= cpp_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, reason
, msgid
, &ap
);
154 /* As cpp_warning above, but use RICHLOC as the location of the diagnostic. */
156 bool cpp_warning_at (cpp_reader
*pfile
, enum cpp_warning_reason reason
,
157 rich_location
*richloc
, const char *msgid
, ...)
162 va_start (ap
, msgid
);
164 ret
= cpp_diagnostic_at (pfile
, CPP_DL_WARNING
, reason
, richloc
,
172 /* As cpp_pedwarning above, but use RICHLOC as the location of the
176 cpp_pedwarning_at (cpp_reader
* pfile
, enum cpp_warning_reason reason
,
177 rich_location
*richloc
, const char *msgid
, ...)
182 va_start (ap
, msgid
);
184 ret
= cpp_diagnostic_at (pfile
, CPP_DL_PEDWARN
, reason
, richloc
,
191 /* Print a diagnostic at a specific location. */
193 ATTRIBUTE_FPTR_PRINTF(6,0)
195 cpp_diagnostic_with_line (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
196 enum cpp_warning_reason reason
,
197 location_t src_loc
, unsigned int column
,
198 const char *msgid
, va_list *ap
)
202 if (!pfile
->cb
.diagnostic
)
204 rich_location
richloc (pfile
->line_table
, src_loc
);
206 richloc
.override_column (column
);
207 ret
= pfile
->cb
.diagnostic (pfile
, level
, reason
, &richloc
, _(msgid
), ap
);
212 /* Print a warning or error, depending on the value of LEVEL. */
215 cpp_error_with_line (cpp_reader
*pfile
, enum cpp_diagnostic_level level
,
216 location_t src_loc
, unsigned int column
,
217 const char *msgid
, ...)
222 va_start (ap
, msgid
);
224 ret
= cpp_diagnostic_with_line (pfile
, level
, CPP_W_NONE
, src_loc
,
231 /* Print a warning. The warning reason may be given in REASON. */
234 cpp_warning_with_line (cpp_reader
*pfile
, enum cpp_warning_reason reason
,
235 location_t src_loc
, unsigned int column
,
236 const char *msgid
, ...)
241 va_start (ap
, msgid
);
243 ret
= cpp_diagnostic_with_line (pfile
, CPP_DL_WARNING
, reason
, src_loc
,
250 /* Print a pedantic warning. The warning reason may be given in REASON. */
253 cpp_pedwarning_with_line (cpp_reader
*pfile
, enum cpp_warning_reason reason
,
254 location_t src_loc
, unsigned int column
,
255 const char *msgid
, ...)
260 va_start (ap
, msgid
);
262 ret
= cpp_diagnostic_with_line (pfile
, CPP_DL_PEDWARN
, reason
, src_loc
,
269 /* Print a warning, including system headers. The warning reason may be
273 cpp_warning_with_line_syshdr (cpp_reader
*pfile
, enum cpp_warning_reason reason
,
274 location_t src_loc
, unsigned int column
,
275 const char *msgid
, ...)
280 va_start (ap
, msgid
);
282 ret
= cpp_diagnostic_with_line (pfile
, CPP_DL_WARNING_SYSHDR
, reason
, src_loc
,
289 /* As cpp_error, but use SRC_LOC as the location of the error, without
290 a column override. */
293 cpp_error_at (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
294 location_t src_loc
, const char *msgid
, ...)
299 va_start (ap
, msgid
);
301 rich_location
richloc (pfile
->line_table
, src_loc
);
302 ret
= cpp_diagnostic_at (pfile
, level
, CPP_W_NONE
, &richloc
,
309 /* As cpp_error, but use RICHLOC as the location of the error, without
310 a column override. */
313 cpp_error_at (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
314 rich_location
*richloc
, const char *msgid
, ...)
319 va_start (ap
, msgid
);
321 ret
= cpp_diagnostic_at (pfile
, level
, CPP_W_NONE
, richloc
,
328 /* Print a warning or error, depending on the value of LEVEL. Include
329 information from errno. */
332 cpp_errno (cpp_reader
*pfile
, enum cpp_diagnostic_level level
,
335 return cpp_error (pfile
, level
, "%s: %s", _(msgid
), xstrerror (errno
));
338 /* Print a warning or error, depending on the value of LEVEL. Include
339 information from errno. Unlike cpp_errno, the argument is a filename
340 that is not localized, but "" is replaced with localized "stdout". */
343 cpp_errno_filename (cpp_reader
*pfile
, enum cpp_diagnostic_level level
,
344 const char *filename
,
347 if (filename
[0] == '\0')
348 filename
= _("stdout");
350 return cpp_error_at (pfile
, level
, loc
, "%s: %s", filename
,