1169695Skan/* Default error handlers for CPP Library. 2169695Skan Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 3169695Skan 2001, 2002, 2004 Free Software Foundation, Inc. 4169695Skan Written by Per Bothner, 1994. 5169695Skan Based on CCCP program by Paul Rubin, June 1986 6169695Skan Adapted to ANSI C, Richard Stallman, Jan 1987 7169695Skan 8169695SkanThis program is free software; you can redistribute it and/or modify it 9169695Skanunder the terms of the GNU General Public License as published by the 10169695SkanFree Software Foundation; either version 2, or (at your option) any 11169695Skanlater version. 12169695Skan 13169695SkanThis program is distributed in the hope that it will be useful, 14169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 15169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16169695SkanGNU General Public License for more details. 17169695Skan 18169695SkanYou should have received a copy of the GNU General Public License 19169695Skanalong with this program; if not, write to the Free Software 20169695SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 21169695Skan 22169695Skan In other words, you are welcome to use, share and improve this program. 23169695Skan You are forbidden to forbid anyone else to use, share and improve 24169695Skan what you give them. Help stamp out software-hoarding! */ 25169695Skan 26169695Skan#include "config.h" 27169695Skan#include "system.h" 28169695Skan#include "cpplib.h" 29169695Skan#include "internal.h" 30169695Skan 31169695Skanstatic void print_location (cpp_reader *, source_location, unsigned int); 32169695Skan 33169695Skan/* Print the logical file location (LINE, COL) in preparation for a 34169695Skan diagnostic. Outputs the #include chain if it has changed. A line 35169695Skan of zero suppresses the include stack, and outputs the program name 36169695Skan instead. */ 37169695Skanstatic void 38169695Skanprint_location (cpp_reader *pfile, source_location line, unsigned int col) 39169695Skan{ 40169695Skan if (line == 0) 41169695Skan fprintf (stderr, "%s: ", progname); 42169695Skan else 43169695Skan { 44169695Skan const struct line_map *map; 45169695Skan unsigned int lin; 46169695Skan 47169695Skan map = linemap_lookup (pfile->line_table, line); 48169695Skan linemap_print_containing_files (pfile->line_table, map); 49169695Skan 50169695Skan lin = SOURCE_LINE (map, line); 51169695Skan if (col == 0) 52169695Skan { 53169695Skan col = SOURCE_COLUMN (map, line); 54169695Skan if (col == 0) 55169695Skan col = 1; 56169695Skan } 57169695Skan 58169695Skan if (lin == 0) 59169695Skan fprintf (stderr, "%s:", map->to_file); 60169695Skan else if (CPP_OPTION (pfile, show_column) == 0) 61169695Skan fprintf (stderr, "%s:%u:", map->to_file, lin); 62169695Skan else 63169695Skan fprintf (stderr, "%s:%u:%u:", map->to_file, lin, col); 64169695Skan 65169695Skan fputc (' ', stderr); 66169695Skan } 67169695Skan} 68169695Skan 69169695Skan/* Set up for a diagnostic: print the file and line, bump the error 70169695Skan counter, etc. SRC_LOC is the logical line number; zero means to print 71169695Skan at the location of the previously lexed token, which tends to be 72169695Skan the correct place by default. The column number can be specified either 73169695Skan using COLUMN or (if COLUMN==0) extracting SOURCE_COLUMN from SRC_LOC. 74169695Skan (This may seem redundant, but is useful when pre-scanning (cleaning) a line, 75169695Skan when we haven't yet verified whether the current line_map has a 76169695Skan big enough max_column_hint.) 77169695Skan 78169695Skan Returns 0 if the error has been suppressed. */ 79169695Skanint 80169695Skan_cpp_begin_message (cpp_reader *pfile, int code, 81169695Skan source_location src_loc, unsigned int column) 82169695Skan{ 83169695Skan int level = CPP_DL_EXTRACT (code); 84169695Skan 85169695Skan switch (level) 86169695Skan { 87169695Skan case CPP_DL_WARNING: 88169695Skan case CPP_DL_PEDWARN: 89169695Skan if (cpp_in_system_header (pfile) 90169695Skan && ! CPP_OPTION (pfile, warn_system_headers)) 91169695Skan return 0; 92169695Skan /* Fall through. */ 93169695Skan 94169695Skan case CPP_DL_WARNING_SYSHDR: 95169695Skan if (CPP_OPTION (pfile, warnings_are_errors) 96169695Skan || (level == CPP_DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors))) 97169695Skan { 98169695Skan if (CPP_OPTION (pfile, inhibit_errors)) 99169695Skan return 0; 100169695Skan level = CPP_DL_ERROR; 101169695Skan pfile->errors++; 102169695Skan } 103169695Skan else if (CPP_OPTION (pfile, inhibit_warnings)) 104169695Skan return 0; 105169695Skan break; 106169695Skan 107169695Skan case CPP_DL_ERROR: 108169695Skan if (CPP_OPTION (pfile, inhibit_errors)) 109169695Skan return 0; 110169695Skan /* ICEs cannot be inhibited. */ 111169695Skan case CPP_DL_ICE: 112169695Skan pfile->errors++; 113169695Skan break; 114169695Skan } 115169695Skan 116169695Skan print_location (pfile, src_loc, column); 117169695Skan if (CPP_DL_WARNING_P (level)) 118169695Skan fputs (_("warning: "), stderr); 119169695Skan else if (level == CPP_DL_ICE) 120169695Skan fputs (_("internal error: "), stderr); 121169695Skan else 122169695Skan fputs (_("error: "), stderr); 123169695Skan 124169695Skan return 1; 125169695Skan} 126169695Skan 127169695Skan/* Don't remove the blank before do, as otherwise the exgettext 128169695Skan script will mistake this as a function definition */ 129169695Skan#define v_message(msgid, ap) \ 130169695Skan do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0) 131169695Skan 132169695Skan/* Exported interface. */ 133169695Skan 134169695Skan/* Print an error at the location of the previously lexed token. */ 135169695Skanvoid 136169695Skancpp_error (cpp_reader * pfile, int level, const char *msgid, ...) 137169695Skan{ 138169695Skan source_location src_loc; 139169695Skan va_list ap; 140169695Skan 141169695Skan va_start (ap, msgid); 142169695Skan 143169695Skan if (CPP_OPTION (pfile, client_diagnostic)) 144169695Skan pfile->cb.error (pfile, level, _(msgid), &ap); 145169695Skan else 146169695Skan { 147169695Skan if (CPP_OPTION (pfile, traditional)) 148169695Skan { 149169695Skan if (pfile->state.in_directive) 150169695Skan src_loc = pfile->directive_line; 151169695Skan else 152169695Skan src_loc = pfile->line_table->highest_line; 153169695Skan } 154169695Skan else 155169695Skan { 156259243Smdf /* Find actual previous token. */ 157259243Smdf cpp_token *t; 158259243Smdf 159259243Smdf if (pfile->cur_token != pfile->cur_run->base) 160259243Smdf t = pfile->cur_token - 1; 161259243Smdf else 162259243Smdf { 163259243Smdf if (pfile->cur_run->prev != NULL) 164259243Smdf t = pfile->cur_run->prev->limit; 165259243Smdf else 166259243Smdf t = NULL; 167259243Smdf } 168259243Smdf /* Retrieve corresponding source location, unless we failed. */ 169259243Smdf src_loc = t ? t->src_loc : 0; 170169695Skan } 171169695Skan 172169695Skan if (_cpp_begin_message (pfile, level, src_loc, 0)) 173169695Skan v_message (msgid, ap); 174169695Skan } 175169695Skan 176169695Skan va_end (ap); 177169695Skan} 178169695Skan 179169695Skan/* Print an error at a specific location. */ 180169695Skanvoid 181169695Skancpp_error_with_line (cpp_reader *pfile, int level, 182169695Skan source_location src_loc, unsigned int column, 183169695Skan const char *msgid, ...) 184169695Skan{ 185169695Skan va_list ap; 186169695Skan 187169695Skan va_start (ap, msgid); 188169695Skan 189169695Skan if (_cpp_begin_message (pfile, level, src_loc, column)) 190169695Skan v_message (msgid, ap); 191169695Skan 192169695Skan va_end (ap); 193169695Skan} 194169695Skan 195169695Skanvoid 196169695Skancpp_errno (cpp_reader *pfile, int level, const char *msgid) 197169695Skan{ 198169695Skan if (msgid[0] == '\0') 199169695Skan msgid = _("stdout"); 200169695Skan 201169695Skan cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno)); 202169695Skan} 203