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
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 /* Print a diagnostic at the given location. */
32 ATTRIBUTE_FPTR_PRINTF(5,0)
34 cpp_diagnostic_at (cpp_reader
* pfile
, int level
, int reason
,
35 rich_location
*richloc
,
36 const char *msgid
, va_list *ap
)
42 ret
= pfile
->cb
.error (pfile
, level
, reason
, richloc
, _(msgid
), ap
);
47 /* Print a diagnostic at the location of the previously lexed token. */
49 ATTRIBUTE_FPTR_PRINTF(4,0)
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
;
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
)
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. */
80 cpp_error (cpp_reader
* pfile
, int level
, const char *msgid
, ...)
87 ret
= cpp_diagnostic (pfile
, level
, CPP_W_NONE
, msgid
, &ap
);
93 /* Print a warning. The warning reason may be given in REASON. */
96 cpp_warning (cpp_reader
* pfile
, int reason
, const char *msgid
, ...)
101 va_start (ap
, msgid
);
103 ret
= cpp_diagnostic (pfile
, CPP_DL_WARNING
, reason
, msgid
, &ap
);
109 /* Print a pedantic warning. The warning reason may be given in REASON. */
112 cpp_pedwarning (cpp_reader
* pfile
, int reason
, const char *msgid
, ...)
117 va_start (ap
, msgid
);
119 ret
= cpp_diagnostic (pfile
, CPP_DL_PEDWARN
, reason
, msgid
, &ap
);
125 /* Print a warning, including system headers. The warning reason may be
129 cpp_warning_syshdr (cpp_reader
* pfile
, int reason
, const char *msgid
, ...)
134 va_start (ap
, msgid
);
136 ret
= cpp_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, reason
, msgid
, &ap
);
142 /* Print a diagnostic at a specific location. */
144 ATTRIBUTE_FPTR_PRINTF(6,0)
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
)
152 if (!pfile
->cb
.error
)
154 rich_location
richloc (pfile
->line_table
, src_loc
);
156 richloc
.override_column (column
);
157 ret
= pfile
->cb
.error (pfile
, level
, reason
, &richloc
, _(msgid
), ap
);
162 /* Print a warning or error, depending on the value of LEVEL. */
165 cpp_error_with_line (cpp_reader
*pfile
, int level
,
166 source_location src_loc
, unsigned int column
,
167 const char *msgid
, ...)
172 va_start (ap
, msgid
);
174 ret
= cpp_diagnostic_with_line (pfile
, level
, CPP_W_NONE
, src_loc
,
181 /* Print a warning. The warning reason may be given in REASON. */
184 cpp_warning_with_line (cpp_reader
*pfile
, int reason
,
185 source_location src_loc
, unsigned int column
,
186 const char *msgid
, ...)
191 va_start (ap
, msgid
);
193 ret
= cpp_diagnostic_with_line (pfile
, CPP_DL_WARNING
, reason
, src_loc
,
200 /* Print a pedantic warning. The warning reason may be given in REASON. */
203 cpp_pedwarning_with_line (cpp_reader
*pfile
, int reason
,
204 source_location src_loc
, unsigned int column
,
205 const char *msgid
, ...)
210 va_start (ap
, msgid
);
212 ret
= cpp_diagnostic_with_line (pfile
, CPP_DL_PEDWARN
, reason
, src_loc
,
219 /* Print a warning, including system headers. The warning reason may be
223 cpp_warning_with_line_syshdr (cpp_reader
*pfile
, int reason
,
224 source_location src_loc
, unsigned int column
,
225 const char *msgid
, ...)
230 va_start (ap
, msgid
);
232 ret
= cpp_diagnostic_with_line (pfile
, CPP_DL_WARNING_SYSHDR
, reason
, src_loc
,
239 /* As cpp_error, but use SRC_LOC as the location of the error, without
240 a column override. */
243 cpp_error_at (cpp_reader
* pfile
, int level
, source_location src_loc
,
244 const char *msgid
, ...)
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
,
259 /* As cpp_error, but use RICHLOC as the location of the error, without
260 a column override. */
263 cpp_error_at (cpp_reader
* pfile
, int level
, rich_location
*richloc
,
264 const char *msgid
, ...)
269 va_start (ap
, msgid
);
271 ret
= cpp_diagnostic_at (pfile
, level
, CPP_W_NONE
, richloc
,
278 /* Print a warning or error, depending on the value of LEVEL. Include
279 information from errno. */
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". */
292 cpp_errno_filename (cpp_reader
*pfile
, int level
, const char *filename
,
295 if (filename
[0] == '\0')
296 filename
= _("stdout");
298 return cpp_error_at (pfile
, level
, loc
, "%s: %s", filename
,