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