1 /* Default error handlers for CPP Library. 2 Copyright (C) 1986-2020 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 /* Print a diagnostic at the given location. */ 31 32 ATTRIBUTE_FPTR_PRINTF(5,0) 33 static bool 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) 37 { 38 bool ret; 39 40 if (!pfile->cb.diagnostic) 41 abort (); 42 ret = pfile->cb.diagnostic (pfile, level, reason, richloc, _(msgid), ap); 43 44 return ret; 45 } 46 47 /* Print a diagnostic at the location of the previously lexed token. */ 48 49 ATTRIBUTE_FPTR_PRINTF(4,0) 50 static bool 51 cpp_diagnostic (cpp_reader * pfile, enum cpp_diagnostic_level level, 52 enum cpp_warning_reason reason, 53 const char *msgid, va_list *ap) 54 { 55 location_t src_loc; 56 57 if (CPP_OPTION (pfile, traditional)) 58 { 59 if (pfile->state.in_directive) 60 src_loc = pfile->directive_line; 61 else 62 src_loc = pfile->line_table->highest_line; 63 } 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) 67 { 68 src_loc = 0; 69 } 70 else 71 { 72 src_loc = pfile->cur_token[-1].src_loc; 73 } 74 rich_location richloc (pfile->line_table, src_loc); 75 return cpp_diagnostic_at (pfile, level, reason, &richloc, msgid, ap); 76 } 77 78 /* Print a warning or error, depending on the value of LEVEL. */ 79 80 bool 81 cpp_error (cpp_reader * pfile, enum cpp_diagnostic_level level, 82 const char *msgid, ...) 83 { 84 va_list ap; 85 bool ret; 86 87 va_start (ap, msgid); 88 89 ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap); 90 91 va_end (ap); 92 return ret; 93 } 94 95 /* Print a warning. The warning reason may be given in REASON. */ 96 97 bool 98 cpp_warning (cpp_reader * pfile, enum cpp_warning_reason reason, 99 const char *msgid, ...) 100 { 101 va_list ap; 102 bool ret; 103 104 va_start (ap, msgid); 105 106 ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap); 107 108 va_end (ap); 109 return ret; 110 } 111 112 /* Print a pedantic warning. The warning reason may be given in REASON. */ 113 114 bool 115 cpp_pedwarning (cpp_reader * pfile, enum cpp_warning_reason reason, 116 const char *msgid, ...) 117 { 118 va_list ap; 119 bool ret; 120 121 va_start (ap, msgid); 122 123 ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap); 124 125 va_end (ap); 126 return ret; 127 } 128 129 /* Print a warning, including system headers. The warning reason may be 130 given in REASON. */ 131 132 bool 133 cpp_warning_syshdr (cpp_reader * pfile, enum cpp_warning_reason reason, 134 const char *msgid, ...) 135 { 136 va_list ap; 137 bool ret; 138 139 va_start (ap, msgid); 140 141 ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap); 142 143 va_end (ap); 144 return ret; 145 } 146 147 /* Print a diagnostic at a specific location. */ 148 149 ATTRIBUTE_FPTR_PRINTF(6,0) 150 static bool 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) 155 { 156 bool ret; 157 158 if (!pfile->cb.diagnostic) 159 abort (); 160 rich_location richloc (pfile->line_table, src_loc); 161 if (column) 162 richloc.override_column (column); 163 ret = pfile->cb.diagnostic (pfile, level, reason, &richloc, _(msgid), ap); 164 165 return ret; 166 } 167 168 /* Print a warning or error, depending on the value of LEVEL. */ 169 170 bool 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, ...) 174 { 175 va_list ap; 176 bool ret; 177 178 va_start (ap, msgid); 179 180 ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc, 181 column, msgid, &ap); 182 183 va_end (ap); 184 return ret; 185 } 186 187 /* Print a warning. The warning reason may be given in REASON. */ 188 189 bool 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, ...) 193 { 194 va_list ap; 195 bool ret; 196 197 va_start (ap, msgid); 198 199 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc, 200 column, msgid, &ap); 201 202 va_end (ap); 203 return ret; 204 } 205 206 /* Print a pedantic warning. The warning reason may be given in REASON. */ 207 208 bool 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, ...) 212 { 213 va_list ap; 214 bool ret; 215 216 va_start (ap, msgid); 217 218 ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc, 219 column, msgid, &ap); 220 221 va_end (ap); 222 return ret; 223 } 224 225 /* Print a warning, including system headers. The warning reason may be 226 given in REASON. */ 227 228 bool 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, ...) 232 { 233 va_list ap; 234 bool ret; 235 236 va_start (ap, msgid); 237 238 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc, 239 column, msgid, &ap); 240 241 va_end (ap); 242 return ret; 243 } 244 245 /* As cpp_error, but use SRC_LOC as the location of the error, without 246 a column override. */ 247 248 bool 249 cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level, 250 location_t src_loc, const char *msgid, ...) 251 { 252 va_list ap; 253 bool ret; 254 255 va_start (ap, msgid); 256 257 rich_location richloc (pfile->line_table, src_loc); 258 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, &richloc, 259 msgid, &ap); 260 261 va_end (ap); 262 return ret; 263 } 264 265 /* As cpp_error, but use RICHLOC as the location of the error, without 266 a column override. */ 267 268 bool 269 cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level, 270 rich_location *richloc, const char *msgid, ...) 271 { 272 va_list ap; 273 bool ret; 274 275 va_start (ap, msgid); 276 277 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, richloc, 278 msgid, &ap); 279 280 va_end (ap); 281 return ret; 282 } 283 284 /* Print a warning or error, depending on the value of LEVEL. Include 285 information from errno. */ 286 287 bool 288 cpp_errno (cpp_reader *pfile, enum cpp_diagnostic_level level, 289 const char *msgid) 290 { 291 return cpp_error (pfile, level, "%s: %s", _(msgid), xstrerror (errno)); 292 } 293 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". */ 297 298 bool 299 cpp_errno_filename (cpp_reader *pfile, enum cpp_diagnostic_level level, 300 const char *filename, 301 location_t loc) 302 { 303 if (filename[0] == '\0') 304 filename = _("stdout"); 305 306 return cpp_error_at (pfile, level, loc, "%s: %s", filename, 307 xstrerror (errno)); 308 } 309