Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1 | /* Default error handlers for CPP Library. |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000, |
Jakub Jelinek | d652f22 | 2011-01-03 21:52:22 +0100 | [diff] [blame] | 3 | 2001, 2002, 2004, 2008, 2009, 2010 Free Software Foundation, Inc. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 4 | Written by Per Bothner, 1994. |
Jeff Law | 38e0125 | 1998-05-06 15:09:07 -0600 | [diff] [blame] | 5 | Based on CCCP program by Paul Rubin, June 1986 |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 6 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify it |
| 9 | under the terms of the GNU General Public License as published by the |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 10 | Free Software Foundation; either version 3, or (at your option) any |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 11 | later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 19 | along with this program; see the file COPYING3. If not see |
| 20 | <http://www.gnu.org/licenses/>. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 21 | |
| 22 | In other words, you are welcome to use, share and improve this program. |
| 23 | You are forbidden to forbid anyone else to use, share and improve |
| 24 | what you give them. Help stamp out software-hoarding! */ |
| 25 | |
Richard Kenner | 84ee6fd | 1995-05-16 18:02:38 -0400 | [diff] [blame] | 26 | #include "config.h" |
Kaveh R. Ghazi | b04cd507 | 1998-03-30 12:05:54 +0000 | [diff] [blame] | 27 | #include "system.h" |
Richard Kenner | f32da1f | 1995-05-20 06:51:33 -0400 | [diff] [blame] | 28 | #include "cpplib.h" |
Paolo Bonzini | 4f4e53dd | 2004-05-24 10:50:45 +0000 | [diff] [blame] | 29 | #include "internal.h" |
Richard Kenner | f32da1f | 1995-05-20 06:51:33 -0400 | [diff] [blame] | 30 | |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 31 | /* Print a diagnostic at the location of the previously lexed token. */ |
| 32 | |
| 33 | ATTRIBUTE_FPTR_PRINTF(4,0) |
| 34 | static bool |
| 35 | cpp_diagnostic (cpp_reader * pfile, int level, int reason, |
| 36 | const char *msgid, va_list *ap) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 37 | { |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 38 | source_location src_loc; |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 39 | bool ret; |
| 40 | |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 41 | if (CPP_OPTION (pfile, traditional)) |
| 42 | { |
| 43 | if (pfile->state.in_directive) |
| 44 | src_loc = pfile->directive_line; |
| 45 | else |
| 46 | src_loc = pfile->line_table->highest_line; |
| 47 | } |
| 48 | /* We don't want to refer to a token before the beginning of the |
| 49 | current run -- that is invalid. */ |
| 50 | else if (pfile->cur_token == pfile->cur_run->base) |
| 51 | { |
| 52 | if (pfile->cur_run->prev != NULL) |
| 53 | src_loc = pfile->cur_run->prev->limit->src_loc; |
| 54 | else |
| 55 | src_loc = 0; |
| 56 | } |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 57 | else |
Per Bothner | 75ee800 | 2003-08-28 00:21:34 +0000 | [diff] [blame] | 58 | { |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 59 | src_loc = pfile->cur_token[-1].src_loc; |
Joseph Myers | 178b58b | 2005-11-03 23:08:18 +0000 | [diff] [blame] | 60 | } |
Zack Weinberg | b649398 | 2001-08-18 20:46:45 +0000 | [diff] [blame] | 61 | |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 62 | if (!pfile->cb.error) |
| 63 | abort (); |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 64 | ret = pfile->cb.error (pfile, level, reason, src_loc, 0, _(msgid), ap); |
| 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | /* Print a warning or error, depending on the value of LEVEL. */ |
| 70 | |
| 71 | bool |
| 72 | cpp_error (cpp_reader * pfile, int level, const char *msgid, ...) |
| 73 | { |
| 74 | va_list ap; |
| 75 | bool ret; |
| 76 | |
| 77 | va_start (ap, msgid); |
| 78 | |
| 79 | ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap); |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 80 | |
Kaveh R. Ghazi | e34d07f | 2003-05-17 22:21:35 +0000 | [diff] [blame] | 81 | va_end (ap); |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 82 | return ret; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 83 | } |
Jeffrey A Law | ab87f8c | 1999-01-27 01:43:17 +0000 | [diff] [blame] | 84 | |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 85 | /* Print a warning. The warning reason may be given in REASON. */ |
| 86 | |
| 87 | bool |
| 88 | cpp_warning (cpp_reader * pfile, int reason, const char *msgid, ...) |
| 89 | { |
| 90 | va_list ap; |
| 91 | bool ret; |
| 92 | |
| 93 | va_start (ap, msgid); |
| 94 | |
| 95 | ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap); |
| 96 | |
| 97 | va_end (ap); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | /* Print a pedantic warning. The warning reason may be given in REASON. */ |
| 102 | |
| 103 | bool |
| 104 | cpp_pedwarning (cpp_reader * pfile, int reason, const char *msgid, ...) |
| 105 | { |
| 106 | va_list ap; |
| 107 | bool ret; |
| 108 | |
| 109 | va_start (ap, msgid); |
| 110 | |
| 111 | ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap); |
| 112 | |
| 113 | va_end (ap); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | /* Print a warning, including system headers. The warning reason may be |
| 118 | given in REASON. */ |
| 119 | |
| 120 | bool |
| 121 | cpp_warning_syshdr (cpp_reader * pfile, int reason, const char *msgid, ...) |
| 122 | { |
| 123 | va_list ap; |
| 124 | bool ret; |
| 125 | |
| 126 | va_start (ap, msgid); |
| 127 | |
| 128 | ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap); |
| 129 | |
| 130 | va_end (ap); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | /* Print a diagnostic at a specific location. */ |
| 135 | |
| 136 | ATTRIBUTE_FPTR_PRINTF(6,0) |
| 137 | static bool |
| 138 | cpp_diagnostic_with_line (cpp_reader * pfile, int level, int reason, |
| 139 | source_location src_loc, unsigned int column, |
| 140 | const char *msgid, va_list *ap) |
| 141 | { |
| 142 | bool ret; |
| 143 | |
| 144 | if (!pfile->cb.error) |
| 145 | abort (); |
| 146 | ret = pfile->cb.error (pfile, level, reason, src_loc, column, _(msgid), ap); |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | /* Print a warning or error, depending on the value of LEVEL. */ |
| 152 | |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 153 | bool |
Kaveh R. Ghazi | e34d07f | 2003-05-17 22:21:35 +0000 | [diff] [blame] | 154 | cpp_error_with_line (cpp_reader *pfile, int level, |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 155 | source_location src_loc, unsigned int column, |
Kaveh R. Ghazi | e34d07f | 2003-05-17 22:21:35 +0000 | [diff] [blame] | 156 | const char *msgid, ...) |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 157 | { |
Kaveh R. Ghazi | e34d07f | 2003-05-17 22:21:35 +0000 | [diff] [blame] | 158 | va_list ap; |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 159 | bool ret; |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 160 | |
Kaveh R. Ghazi | e34d07f | 2003-05-17 22:21:35 +0000 | [diff] [blame] | 161 | va_start (ap, msgid); |
Jeffrey A Law | ab87f8c | 1999-01-27 01:43:17 +0000 | [diff] [blame] | 162 | |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 163 | ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc, |
| 164 | column, msgid, &ap); |
Zack Weinberg | b649398 | 2001-08-18 20:46:45 +0000 | [diff] [blame] | 165 | |
Kaveh R. Ghazi | e34d07f | 2003-05-17 22:21:35 +0000 | [diff] [blame] | 166 | va_end (ap); |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 167 | return ret; |
Jeffrey A Law | ab87f8c | 1999-01-27 01:43:17 +0000 | [diff] [blame] | 168 | } |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 169 | |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 170 | /* Print a warning. The warning reason may be given in REASON. */ |
| 171 | |
| 172 | bool |
| 173 | cpp_warning_with_line (cpp_reader *pfile, int reason, |
| 174 | source_location src_loc, unsigned int column, |
| 175 | const char *msgid, ...) |
| 176 | { |
| 177 | va_list ap; |
| 178 | bool ret; |
| 179 | |
| 180 | va_start (ap, msgid); |
| 181 | |
| 182 | ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc, |
| 183 | column, msgid, &ap); |
| 184 | |
| 185 | va_end (ap); |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | /* Print a pedantic warning. The warning reason may be given in REASON. */ |
| 190 | |
| 191 | bool |
| 192 | cpp_pedwarning_with_line (cpp_reader *pfile, int reason, |
| 193 | source_location src_loc, unsigned int column, |
| 194 | const char *msgid, ...) |
| 195 | { |
| 196 | va_list ap; |
| 197 | bool ret; |
| 198 | |
| 199 | va_start (ap, msgid); |
| 200 | |
| 201 | ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc, |
| 202 | column, msgid, &ap); |
| 203 | |
| 204 | va_end (ap); |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | /* Print a warning, including system headers. The warning reason may be |
| 209 | given in REASON. */ |
| 210 | |
| 211 | bool |
| 212 | cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason, |
| 213 | source_location src_loc, unsigned int column, |
| 214 | const char *msgid, ...) |
| 215 | { |
| 216 | va_list ap; |
| 217 | bool ret; |
| 218 | |
| 219 | va_start (ap, msgid); |
| 220 | |
| 221 | ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc, |
| 222 | column, msgid, &ap); |
| 223 | |
| 224 | va_end (ap); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | /* Print a warning or error, depending on the value of LEVEL. Include |
| 229 | information from errno. */ |
| 230 | |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 231 | bool |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 232 | cpp_errno (cpp_reader *pfile, int level, const char *msgid) |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 233 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 234 | if (msgid[0] == '\0') |
| 235 | msgid = _("stdout"); |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 236 | |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 237 | return cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno)); |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 238 | } |