]>
Commit | Line | Data |
---|---|---|
7f2935c7 | 1 | /* Default error handlers for CPP Library. |
818ab71a | 2 | Copyright (C) 1986-2016 Free Software Foundation, Inc. |
7f2935c7 | 3 | Written by Per Bothner, 1994. |
38e01259 | 4 | Based on CCCP program by Paul Rubin, June 1986 |
7f2935c7 PB |
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 | |
748086b7 | 9 | Free Software Foundation; either version 3, or (at your option) any |
7f2935c7 PB |
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 | |
748086b7 JJ |
18 | along with this program; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. | |
7f2935c7 PB |
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 | ||
84ee6fd4 | 25 | #include "config.h" |
b04cd507 | 26 | #include "system.h" |
f32da1f6 | 27 | #include "cpplib.h" |
4f4e53dd | 28 | #include "internal.h" |
f32da1f6 | 29 | |
87cf0651 SB |
30 | /* Print a diagnostic at the location of the previously lexed token. */ |
31 | ||
32 | ATTRIBUTE_FPTR_PRINTF(4,0) | |
33 | static bool | |
34 | cpp_diagnostic (cpp_reader * pfile, int level, int reason, | |
35 | const char *msgid, va_list *ap) | |
c1212d2f | 36 | { |
12f9df4e | 37 | source_location src_loc; |
148e4216 JM |
38 | bool ret; |
39 | ||
148e4216 | 40 | if (CPP_OPTION (pfile, traditional)) |
75ee800b | 41 | { |
148e4216 JM |
42 | if (pfile->state.in_directive) |
43 | src_loc = pfile->directive_line; | |
178b58b5 | 44 | else |
148e4216 JM |
45 | src_loc = pfile->line_table->highest_line; |
46 | } | |
47 | /* We don't want to refer to a token before the beginning of the | |
48 | current run -- that is invalid. */ | |
49 | else if (pfile->cur_token == pfile->cur_run->base) | |
50 | { | |
cc811a8a | 51 | src_loc = 0; |
178b58b5 | 52 | } |
148e4216 JM |
53 | else |
54 | { | |
55 | src_loc = pfile->cur_token[-1].src_loc; | |
56 | } | |
57 | ||
58 | if (!pfile->cb.error) | |
59 | abort (); | |
ebedc9a3 | 60 | rich_location richloc (pfile->line_table, src_loc); |
8a645150 | 61 | ret = pfile->cb.error (pfile, level, reason, &richloc, _(msgid), ap); |
87cf0651 SB |
62 | |
63 | return ret; | |
64 | } | |
65 | ||
66 | /* Print a warning or error, depending on the value of LEVEL. */ | |
67 | ||
68 | bool | |
69 | cpp_error (cpp_reader * pfile, int level, const char *msgid, ...) | |
70 | { | |
71 | va_list ap; | |
72 | bool ret; | |
73 | ||
74 | va_start (ap, msgid); | |
75 | ||
76 | ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap); | |
b649398a | 77 | |
e34d07f2 | 78 | va_end (ap); |
148e4216 | 79 | return ret; |
c1212d2f ZW |
80 | } |
81 | ||
87cf0651 SB |
82 | /* Print a warning. The warning reason may be given in REASON. */ |
83 | ||
84 | bool | |
85 | cpp_warning (cpp_reader * pfile, int reason, const char *msgid, ...) | |
86 | { | |
87 | va_list ap; | |
88 | bool ret; | |
89 | ||
90 | va_start (ap, msgid); | |
91 | ||
92 | ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap); | |
93 | ||
94 | va_end (ap); | |
95 | return ret; | |
96 | } | |
97 | ||
98 | /* Print a pedantic warning. The warning reason may be given in REASON. */ | |
99 | ||
100 | bool | |
101 | cpp_pedwarning (cpp_reader * pfile, int reason, const char *msgid, ...) | |
102 | { | |
103 | va_list ap; | |
104 | bool ret; | |
105 | ||
106 | va_start (ap, msgid); | |
107 | ||
108 | ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap); | |
109 | ||
110 | va_end (ap); | |
111 | return ret; | |
112 | } | |
113 | ||
114 | /* Print a warning, including system headers. The warning reason may be | |
115 | given in REASON. */ | |
116 | ||
117 | bool | |
118 | cpp_warning_syshdr (cpp_reader * pfile, int reason, const char *msgid, ...) | |
119 | { | |
120 | va_list ap; | |
121 | bool ret; | |
122 | ||
123 | va_start (ap, msgid); | |
124 | ||
125 | ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap); | |
126 | ||
127 | va_end (ap); | |
128 | return ret; | |
129 | } | |
130 | ||
131 | /* Print a diagnostic at a specific location. */ | |
132 | ||
133 | ATTRIBUTE_FPTR_PRINTF(6,0) | |
134 | static bool | |
135 | cpp_diagnostic_with_line (cpp_reader * pfile, int level, int reason, | |
136 | source_location src_loc, unsigned int column, | |
137 | const char *msgid, va_list *ap) | |
138 | { | |
139 | bool ret; | |
140 | ||
141 | if (!pfile->cb.error) | |
142 | abort (); | |
ebedc9a3 | 143 | rich_location richloc (pfile->line_table, src_loc); |
8a645150 DM |
144 | richloc.override_column (column); |
145 | ret = pfile->cb.error (pfile, level, reason, &richloc, _(msgid), ap); | |
87cf0651 SB |
146 | |
147 | return ret; | |
148 | } | |
149 | ||
150 | /* Print a warning or error, depending on the value of LEVEL. */ | |
151 | ||
148e4216 | 152 | bool |
e34d07f2 | 153 | cpp_error_with_line (cpp_reader *pfile, int level, |
12f9df4e | 154 | source_location src_loc, unsigned int column, |
e34d07f2 | 155 | const char *msgid, ...) |
c1212d2f | 156 | { |
e34d07f2 | 157 | va_list ap; |
148e4216 | 158 | bool ret; |
87cf0651 | 159 | |
e34d07f2 | 160 | va_start (ap, msgid); |
ab87f8c8 | 161 | |
87cf0651 SB |
162 | ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc, |
163 | column, msgid, &ap); | |
164 | ||
165 | va_end (ap); | |
166 | return ret; | |
167 | } | |
168 | ||
169 | /* Print a warning. The warning reason may be given in REASON. */ | |
170 | ||
171 | bool | |
172 | cpp_warning_with_line (cpp_reader *pfile, int reason, | |
173 | source_location src_loc, unsigned int column, | |
174 | const char *msgid, ...) | |
175 | { | |
176 | va_list ap; | |
177 | bool ret; | |
178 | ||
179 | va_start (ap, msgid); | |
180 | ||
181 | ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc, | |
182 | column, msgid, &ap); | |
183 | ||
184 | va_end (ap); | |
185 | return ret; | |
186 | } | |
187 | ||
188 | /* Print a pedantic warning. The warning reason may be given in REASON. */ | |
189 | ||
190 | bool | |
191 | cpp_pedwarning_with_line (cpp_reader *pfile, int reason, | |
192 | source_location src_loc, unsigned int column, | |
193 | const char *msgid, ...) | |
194 | { | |
195 | va_list ap; | |
196 | bool ret; | |
197 | ||
198 | va_start (ap, msgid); | |
199 | ||
200 | ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc, | |
201 | column, msgid, &ap); | |
b649398a | 202 | |
e34d07f2 | 203 | va_end (ap); |
148e4216 | 204 | return ret; |
c1212d2f ZW |
205 | } |
206 | ||
87cf0651 SB |
207 | /* Print a warning, including system headers. The warning reason may be |
208 | given in REASON. */ | |
209 | ||
210 | bool | |
211 | cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason, | |
212 | source_location src_loc, unsigned int column, | |
213 | const char *msgid, ...) | |
214 | { | |
215 | va_list ap; | |
216 | bool ret; | |
217 | ||
218 | va_start (ap, msgid); | |
219 | ||
220 | ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc, | |
221 | column, msgid, &ap); | |
222 | ||
223 | va_end (ap); | |
224 | return ret; | |
225 | } | |
226 | ||
227 | /* Print a warning or error, depending on the value of LEVEL. Include | |
228 | information from errno. */ | |
229 | ||
148e4216 | 230 | bool |
6cf87ca4 | 231 | cpp_errno (cpp_reader *pfile, int level, const char *msgid) |
c1212d2f | 232 | { |
46ce03de JJ |
233 | return cpp_error (pfile, level, "%s: %s", _(msgid), xstrerror (errno)); |
234 | } | |
235 | ||
236 | /* Print a warning or error, depending on the value of LEVEL. Include | |
237 | information from errno. Unlike cpp_errno, the argument is a filename | |
238 | that is not localized, but "" is replaced with localized "stdout". */ | |
239 | ||
240 | bool | |
241 | cpp_errno_filename (cpp_reader *pfile, int level, const char *filename) | |
242 | { | |
243 | if (filename[0] == '\0') | |
244 | filename = _("stdout"); | |
ebef4e8c | 245 | |
46ce03de | 246 | return cpp_error (pfile, level, "%s: %s", filename, xstrerror (errno)); |
c1212d2f | 247 | } |