1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986-2019 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
, enum cpp_diagnostic_level level
,
35 enum cpp_warning_reason reason
, rich_location
*richloc
,
36 const char *msgid
, va_list *ap
)
40 if (!pfile
->cb
.diagnostic
)
42 ret
= pfile
->cb
.diagnostic (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
, enum cpp_diagnostic_level level
,
52 enum cpp_warning_reason reason
,
53 const char *msgid
, va_list *ap
)
57 if (CPP_OPTION (pfile
, traditional
))
59 if (pfile
->state
.in_directive
)
60 src_loc
= pfile
->directive_line
;
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
)
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. */
81 cpp_error (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
82 const char *msgid
, ...)
89 ret
= cpp_diagnostic (pfile
, level
, CPP_W_NONE
, msgid
, &ap
);
95 /* Print a warning. The warning reason may be given in REASON. */
98 cpp_warning (cpp_reader
* pfile
, enum cpp_warning_reason reason
,
99 const char *msgid
, ...)
104 va_start (ap
, msgid
);
106 ret
= cpp_diagnostic (pfile
, CPP_DL_WARNING
, reason
, msgid
, &ap
);
112 /* Print a pedantic warning. The warning reason may be given in REASON. */
115 cpp_pedwarning (cpp_reader
* pfile
, enum cpp_warning_reason reason
,
116 const char *msgid
, ...)
121 va_start (ap
, msgid
);
123 ret
= cpp_diagnostic (pfile
, CPP_DL_PEDWARN
, reason
, msgid
, &ap
);
129 /* Print a warning, including system headers. The warning reason may be
133 cpp_warning_syshdr (cpp_reader
* pfile
, enum cpp_warning_reason reason
,
134 const char *msgid
, ...)
139 va_start (ap
, msgid
);
141 ret
= cpp_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, reason
, msgid
, &ap
);
147 /* Print a diagnostic at a specific location. */
149 ATTRIBUTE_FPTR_PRINTF(6,0)
151 cpp_diagnostic_with_line (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
152 enum cpp_warning_reason reason
,
153 location_t src_loc
, unsigned int column
,
154 const char *msgid
, va_list *ap
)
158 if (!pfile
->cb
.diagnostic
)
160 rich_location
richloc (pfile
->line_table
, src_loc
);
162 richloc
.override_column (column
);
163 ret
= pfile
->cb
.diagnostic (pfile
, level
, reason
, &richloc
, _(msgid
), ap
);
168 /* Print a warning or error, depending on the value of LEVEL. */
171 cpp_error_with_line (cpp_reader
*pfile
, enum cpp_diagnostic_level level
,
172 location_t src_loc
, unsigned int column
,
173 const char *msgid
, ...)
178 va_start (ap
, msgid
);
180 ret
= cpp_diagnostic_with_line (pfile
, level
, CPP_W_NONE
, src_loc
,
187 /* Print a warning. The warning reason may be given in REASON. */
190 cpp_warning_with_line (cpp_reader
*pfile
, enum cpp_warning_reason reason
,
191 location_t src_loc
, unsigned int column
,
192 const char *msgid
, ...)
197 va_start (ap
, msgid
);
199 ret
= cpp_diagnostic_with_line (pfile
, CPP_DL_WARNING
, reason
, src_loc
,
206 /* Print a pedantic warning. The warning reason may be given in REASON. */
209 cpp_pedwarning_with_line (cpp_reader
*pfile
, enum cpp_warning_reason reason
,
210 location_t src_loc
, unsigned int column
,
211 const char *msgid
, ...)
216 va_start (ap
, msgid
);
218 ret
= cpp_diagnostic_with_line (pfile
, CPP_DL_PEDWARN
, reason
, src_loc
,
225 /* Print a warning, including system headers. The warning reason may be
229 cpp_warning_with_line_syshdr (cpp_reader
*pfile
, enum cpp_warning_reason reason
,
230 location_t src_loc
, unsigned int column
,
231 const char *msgid
, ...)
236 va_start (ap
, msgid
);
238 ret
= cpp_diagnostic_with_line (pfile
, CPP_DL_WARNING_SYSHDR
, reason
, src_loc
,
245 /* As cpp_error, but use SRC_LOC as the location of the error, without
246 a column override. */
249 cpp_error_at (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
250 location_t src_loc
, const char *msgid
, ...)
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
,
265 /* As cpp_error, but use RICHLOC as the location of the error, without
266 a column override. */
269 cpp_error_at (cpp_reader
* pfile
, enum cpp_diagnostic_level level
,
270 rich_location
*richloc
, const char *msgid
, ...)
275 va_start (ap
, msgid
);
277 ret
= cpp_diagnostic_at (pfile
, level
, CPP_W_NONE
, richloc
,
284 /* Print a warning or error, depending on the value of LEVEL. Include
285 information from errno. */
288 cpp_errno (cpp_reader
*pfile
, enum cpp_diagnostic_level level
,
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". */
299 cpp_errno_filename (cpp_reader
*pfile
, enum cpp_diagnostic_level level
,
300 const char *filename
,
303 if (filename
[0] == '\0')
304 filename
= _("stdout");
306 return cpp_error_at (pfile
, level
, loc
, "%s: %s", filename
,