| 1 |
/* Default error handlers for CPP Library. |
| 2 |
Copyright (C) 1986-2025 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 |
| 6 |
|
| 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. |
| 11 |
|
| 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. |
| 16 |
|
| 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/>. |
| 20 |
|
| 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! */ |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
#include "system.h" |
| 27 |
#include "cpplib.h" |
| 28 |
#include "internal.h" |
| 29 |
|
| 30 |
/* Get a location_t for the current location in PFILE, |
| 31 |
generally that of the previously lexed token. */ |
| 32 |
|
| 33 |
location_t |
| 34 |
cpp_diagnostic_get_current_location (cpp_reader *pfile) |
| 35 |
{ |
| 36 |
if (CPP_OPTION (pfile, traditional)) |
| 37 |
{ |
| 38 |
if (pfile->state.in_directive) |
| 39 |
return pfile->directive_line; |
| 40 |
else |
| 41 |
return pfile->line_table->highest_line; |
| 42 |
} |
| 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) |
| 46 |
{ |
| 47 |
return 0; |
| 48 |
} |
| 49 |
else |
| 50 |
{ |
| 51 |
return pfile->cur_token[-1].src_loc; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/* Print a diagnostic at the given location. */ |
| 56 |
|
| 57 |
ATTRIBUTE_CPP_PPDIAG (5, 0) |
| 58 |
static bool |
| 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) |
| 62 |
{ |
| 63 |
if (!pfile->cb.diagnostic) |
| 64 |
abort (); |
| 65 |
if (pfile->diagnostic_override_loc && level != CPP_DL_NOTE) |
| 66 |
{ |
| 67 |
rich_location rc2{pfile->line_table, pfile->diagnostic_override_loc}; |
| 68 |
rc2.set_escape_on_output (richloc->escape_on_output_p ()); |
| 69 |
return pfile->cb.diagnostic (pfile, level, reason, &rc2, _(msgid), ap); |
| 70 |
} |
| 71 |
return pfile->cb.diagnostic (pfile, level, reason, richloc, _(msgid), ap); |
| 72 |
} |
| 73 |
|
| 74 |
/* Print a diagnostic at the location of the previously lexed token. */ |
| 75 |
|
| 76 |
ATTRIBUTE_CPP_PPDIAG (4, 0) |
| 77 |
static bool |
| 78 |
cpp_diagnostic (cpp_reader * pfile, enum cpp_diagnostic_level level, |
| 79 |
enum cpp_warning_reason reason, |
| 80 |
const char *msgid, va_list *ap) |
| 81 |
{ |
| 82 |
location_t src_loc = cpp_diagnostic_get_current_location (pfile); |
| 83 |
rich_location richloc (pfile->line_table, src_loc); |
| 84 |
return cpp_diagnostic_at (pfile, level, reason, &richloc, msgid, ap); |
| 85 |
} |
| 86 |
|
| 87 |
/* Print a warning or error, depending on the value of LEVEL. */ |
| 88 |
|
| 89 |
bool |
| 90 |
cpp_error (cpp_reader * pfile, enum cpp_diagnostic_level level, |
| 91 |
const char *msgid, ...) |
| 92 |
{ |
| 93 |
va_list ap; |
| 94 |
bool ret; |
| 95 |
|
| 96 |
va_start (ap, msgid); |
| 97 |
|
| 98 |
ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap); |
| 99 |
|
| 100 |
va_end (ap); |
| 101 |
return ret; |
| 102 |
} |
| 103 |
|
| 104 |
/* Print a warning. The warning reason may be given in REASON. */ |
| 105 |
|
| 106 |
bool |
| 107 |
cpp_warning (cpp_reader * pfile, enum cpp_warning_reason reason, |
| 108 |
const char *msgid, ...) |
| 109 |
{ |
| 110 |
va_list ap; |
| 111 |
bool ret; |
| 112 |
|
| 113 |
va_start (ap, msgid); |
| 114 |
|
| 115 |
ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap); |
| 116 |
|
| 117 |
va_end (ap); |
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
/* Print a pedantic warning. The warning reason may be given in REASON. */ |
| 122 |
|
| 123 |
bool |
| 124 |
cpp_pedwarning (cpp_reader * pfile, enum cpp_warning_reason reason, |
| 125 |
const char *msgid, ...) |
| 126 |
{ |
| 127 |
va_list ap; |
| 128 |
bool ret; |
| 129 |
|
| 130 |
va_start (ap, msgid); |
| 131 |
|
| 132 |
ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap); |
| 133 |
|
| 134 |
va_end (ap); |
| 135 |
return ret; |
| 136 |
} |
| 137 |
|
| 138 |
/* Print a warning, including system headers. The warning reason may be |
| 139 |
given in REASON. */ |
| 140 |
|
| 141 |
bool |
| 142 |
cpp_warning_syshdr (cpp_reader * pfile, enum cpp_warning_reason reason, |
| 143 |
const char *msgid, ...) |
| 144 |
{ |
| 145 |
va_list ap; |
| 146 |
bool ret; |
| 147 |
|
| 148 |
va_start (ap, msgid); |
| 149 |
|
| 150 |
ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap); |
| 151 |
|
| 152 |
va_end (ap); |
| 153 |
return ret; |
| 154 |
} |
| 155 |
|
| 156 |
/* As cpp_warning above, but use RICHLOC as the location of the diagnostic. */ |
| 157 |
|
| 158 |
bool cpp_warning_at (cpp_reader *pfile, enum cpp_warning_reason reason, |
| 159 |
rich_location *richloc, const char *msgid, ...) |
| 160 |
{ |
| 161 |
va_list ap; |
| 162 |
bool ret; |
| 163 |
|
| 164 |
va_start (ap, msgid); |
| 165 |
|
| 166 |
ret = cpp_diagnostic_at (pfile, CPP_DL_WARNING, reason, richloc, |
| 167 |
msgid, &ap); |
| 168 |
|
| 169 |
va_end (ap); |
| 170 |
return ret; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
/* As cpp_pedwarning above, but use RICHLOC as the location of the |
| 175 |
diagnostic. */ |
| 176 |
|
| 177 |
bool |
| 178 |
cpp_pedwarning_at (cpp_reader * pfile, enum cpp_warning_reason reason, |
| 179 |
rich_location *richloc, const char *msgid, ...) |
| 180 |
{ |
| 181 |
va_list ap; |
| 182 |
bool ret; |
| 183 |
|
| 184 |
va_start (ap, msgid); |
| 185 |
|
| 186 |
ret = cpp_diagnostic_at (pfile, CPP_DL_PEDWARN, reason, richloc, |
| 187 |
msgid, &ap); |
| 188 |
|
| 189 |
va_end (ap); |
| 190 |
return ret; |
| 191 |
} |
| 192 |
|
| 193 |
/* Print a diagnostic at a specific location. */ |
| 194 |
|
| 195 |
ATTRIBUTE_CPP_PPDIAG (6, 0) |
| 196 |
static bool |
| 197 |
cpp_diagnostic_with_line (cpp_reader * pfile, enum cpp_diagnostic_level level, |
| 198 |
enum cpp_warning_reason reason, |
| 199 |
location_t src_loc, unsigned int column, |
| 200 |
const char *msgid, va_list *ap) |
| 201 |
{ |
| 202 |
bool ret; |
| 203 |
|
| 204 |
if (!pfile->cb.diagnostic) |
| 205 |
abort (); |
| 206 |
/* Don't override note locations, which will likely make the note |
| 207 |
more confusing. */ |
| 208 |
const bool do_loc_override |
| 209 |
= pfile->diagnostic_override_loc && level != CPP_DL_NOTE; |
| 210 |
if (do_loc_override) |
| 211 |
src_loc = pfile->diagnostic_override_loc; |
| 212 |
rich_location richloc (pfile->line_table, src_loc); |
| 213 |
if (column && !do_loc_override) |
| 214 |
richloc.override_column (column); |
| 215 |
ret = pfile->cb.diagnostic (pfile, level, reason, &richloc, _(msgid), ap); |
| 216 |
|
| 217 |
return ret; |
| 218 |
} |
| 219 |
|
| 220 |
/* Print a warning or error, depending on the value of LEVEL. */ |
| 221 |
|
| 222 |
bool |
| 223 |
cpp_error_with_line (cpp_reader *pfile, enum cpp_diagnostic_level level, |
| 224 |
location_t src_loc, unsigned int column, |
| 225 |
const char *msgid, ...) |
| 226 |
{ |
| 227 |
va_list ap; |
| 228 |
bool ret; |
| 229 |
|
| 230 |
va_start (ap, msgid); |
| 231 |
|
| 232 |
ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc, |
| 233 |
column, msgid, &ap); |
| 234 |
|
| 235 |
va_end (ap); |
| 236 |
return ret; |
| 237 |
} |
| 238 |
|
| 239 |
/* Print a warning. The warning reason may be given in REASON. */ |
| 240 |
|
| 241 |
bool |
| 242 |
cpp_warning_with_line (cpp_reader *pfile, enum cpp_warning_reason reason, |
| 243 |
location_t src_loc, unsigned int column, |
| 244 |
const char *msgid, ...) |
| 245 |
{ |
| 246 |
va_list ap; |
| 247 |
bool ret; |
| 248 |
|
| 249 |
va_start (ap, msgid); |
| 250 |
|
| 251 |
ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc, |
| 252 |
column, msgid, &ap); |
| 253 |
|
| 254 |
va_end (ap); |
| 255 |
return ret; |
| 256 |
} |
| 257 |
|
| 258 |
/* Print a pedantic warning. The warning reason may be given in REASON. */ |
| 259 |
|
| 260 |
bool |
| 261 |
cpp_pedwarning_with_line (cpp_reader *pfile, enum cpp_warning_reason reason, |
| 262 |
location_t src_loc, unsigned int column, |
| 263 |
const char *msgid, ...) |
| 264 |
{ |
| 265 |
va_list ap; |
| 266 |
bool ret; |
| 267 |
|
| 268 |
va_start (ap, msgid); |
| 269 |
|
| 270 |
ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc, |
| 271 |
column, msgid, &ap); |
| 272 |
|
| 273 |
va_end (ap); |
| 274 |
return ret; |
| 275 |
} |
| 276 |
|
| 277 |
/* Print a warning, including system headers. The warning reason may be |
| 278 |
given in REASON. */ |
| 279 |
|
| 280 |
bool |
| 281 |
cpp_warning_with_line_syshdr (cpp_reader *pfile, enum cpp_warning_reason reason, |
| 282 |
location_t src_loc, unsigned int column, |
| 283 |
const char *msgid, ...) |
| 284 |
{ |
| 285 |
va_list ap; |
| 286 |
bool ret; |
| 287 |
|
| 288 |
va_start (ap, msgid); |
| 289 |
|
| 290 |
ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc, |
| 291 |
column, msgid, &ap); |
| 292 |
|
| 293 |
va_end (ap); |
| 294 |
return ret; |
| 295 |
} |
| 296 |
|
| 297 |
/* As cpp_error, but use SRC_LOC as the location of the error, without |
| 298 |
a column override. */ |
| 299 |
|
| 300 |
bool |
| 301 |
cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level, |
| 302 |
location_t src_loc, const char *msgid, ...) |
| 303 |
{ |
| 304 |
va_list ap; |
| 305 |
bool ret; |
| 306 |
|
| 307 |
va_start (ap, msgid); |
| 308 |
|
| 309 |
rich_location richloc (pfile->line_table, src_loc); |
| 310 |
ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, &richloc, |
| 311 |
msgid, &ap); |
| 312 |
|
| 313 |
va_end (ap); |
| 314 |
return ret; |
| 315 |
} |
| 316 |
|
| 317 |
/* As cpp_error, but use RICHLOC as the location of the error, without |
| 318 |
a column override. */ |
| 319 |
|
| 320 |
bool |
| 321 |
cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level, |
| 322 |
rich_location *richloc, const char *msgid, ...) |
| 323 |
{ |
| 324 |
va_list ap; |
| 325 |
bool ret; |
| 326 |
|
| 327 |
va_start (ap, msgid); |
| 328 |
|
| 329 |
ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, richloc, |
| 330 |
msgid, &ap); |
| 331 |
|
| 332 |
va_end (ap); |
| 333 |
return ret; |
| 334 |
} |
| 335 |
|
| 336 |
/* Print a warning or error, depending on the value of LEVEL. Include |
| 337 |
information from errno. */ |
| 338 |
|
| 339 |
bool |
| 340 |
cpp_errno (cpp_reader *pfile, enum cpp_diagnostic_level level, |
| 341 |
const char *msgid) |
| 342 |
{ |
| 343 |
return cpp_error (pfile, level, "%s: %s", _(msgid), xstrerror (errno)); |
| 344 |
} |
| 345 |
|
| 346 |
/* Print a warning or error, depending on the value of LEVEL. Include |
| 347 |
information from errno. Unlike cpp_errno, the argument is a filename |
| 348 |
that is not localized, but "" is replaced with localized "stdout". */ |
| 349 |
|
| 350 |
bool |
| 351 |
cpp_errno_filename (cpp_reader *pfile, enum cpp_diagnostic_level level, |
| 352 |
const char *filename, |
| 353 |
location_t loc) |
| 354 |
{ |
| 355 |
if (filename[0] == '\0') |
| 356 |
filename = _("stdout"); |
| 357 |
|
| 358 |
return cpp_error_at (pfile, level, loc, "%s: %s", filename, |
| 359 |
xstrerror (errno)); |
| 360 |
} |
| 361 |
|
| 362 |
cpp_auto_suppress_diagnostics::cpp_auto_suppress_diagnostics (cpp_reader *pfile) |
| 363 |
: m_pfile (pfile), m_cb (pfile->cb.diagnostic) |
| 364 |
{ |
| 365 |
m_pfile->cb.diagnostic |
| 366 |
= [] (cpp_reader *, cpp_diagnostic_level, cpp_warning_reason, |
| 367 |
rich_location *, const char *, va_list *) |
| 368 |
{ |
| 369 |
return true; |
| 370 |
}; |
| 371 |
} |
| 372 |
|
| 373 |
cpp_auto_suppress_diagnostics::~cpp_auto_suppress_diagnostics () |
| 374 |
{ |
| 375 |
m_pfile->cb.diagnostic = m_cb; |
| 376 |
} |