]>
Commit | Line | Data |
---|---|---|
a0f6b076 | 1 | /* Declaration for error-reporting function for Bison. |
2cec9080 | 2 | |
7d6bad19 | 3 | Copyright (C) 2000-2002, 2004-2006, 2009-2013 Free Software |
575619af | 4 | Foundation, Inc. |
a0f6b076 | 5 | |
f16b0819 PE |
6 | This program is free software: you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation, either version 3 of the License, or | |
9 | (at your option) any later version. | |
a0f6b076 AD |
10 | |
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
f16b0819 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
a0f6b076 AD |
18 | |
19 | /* Based on error.c and error.h, | |
20 | written by David MacKenzie <djm@gnu.ai.mit.edu>. */ | |
21 | ||
2cec9080 | 22 | #include <config.h> |
342b8b6e | 23 | #include "system.h" |
a0f6b076 | 24 | |
12bc1c9e | 25 | #include <argmatch.h> |
21184140 | 26 | #include <stdarg.h> |
4d699f44 | 27 | #include <progname.h> |
a0f6b076 | 28 | |
a0f6b076 | 29 | #include "complain.h" |
4b68955b | 30 | #include "files.h" |
89eb3c76 | 31 | #include "getargs.h" |
1dc927a7 | 32 | #include "quote.h" |
a0f6b076 | 33 | |
808e523d | 34 | err_status complaint_status = status_none; |
d0f11c1b | 35 | |
808e523d | 36 | bool warnings_are_errors = false; |
12bc1c9e AD |
37 | |
38 | /** Diagnostics severity. */ | |
39 | typedef enum | |
40 | { | |
41 | severity_disabled = 0, | |
42 | severity_unset = 1, | |
43 | severity_warning = 2, | |
44 | severity_error = 3, | |
45 | severity_fatal = 4 | |
46 | } severity; | |
47 | ||
48 | ||
49 | /** For each warning type, its severity. */ | |
50 | static severity warnings_flag[warnings_size]; | |
9503b0a4 | 51 | |
66381412 | 52 | static unsigned *indent_ptr = 0; |
12bc1c9e AD |
53 | |
54 | /*------------------------. | |
55 | | --warnings's handling. | | |
56 | `------------------------*/ | |
57 | ||
58 | static const char * const warnings_args[] = | |
59 | { | |
60 | "none", | |
61 | "midrule-values", | |
62 | "yacc", | |
63 | "conflicts-sr", | |
64 | "conflicts-rr", | |
65 | "deprecated", | |
f68a49ed | 66 | "empty-rule", |
12bc1c9e AD |
67 | "precedence", |
68 | "other", | |
69 | "all", | |
70 | "error", | |
71 | 0 | |
72 | }; | |
73 | ||
74 | static const int warnings_types[] = | |
75 | { | |
76 | Wnone, | |
77 | Wmidrule_values, | |
78 | Wyacc, | |
79 | Wconflicts_sr, | |
80 | Wconflicts_rr, | |
81 | Wdeprecated, | |
f68a49ed | 82 | Wempty_rule, |
12bc1c9e AD |
83 | Wprecedence, |
84 | Wother, | |
85 | Wall, | |
86 | Werror | |
87 | }; | |
88 | ||
89 | ARGMATCH_VERIFY (warnings_args, warnings_types); | |
90 | ||
91 | void | |
92 | warning_argmatch (char const *arg, size_t no, size_t err) | |
93 | { | |
94 | int value = XARGMATCH ("--warning", arg + no + err, | |
95 | warnings_args, warnings_types); | |
96 | ||
97 | /* -Wnone == -Wno-all, and -Wno-none == -Wall. */ | |
98 | if (!value) | |
99 | { | |
100 | value = Wall; | |
101 | no = !no; | |
102 | } | |
103 | ||
104 | if (no) | |
105 | { | |
106 | size_t b; | |
107 | for (b = 0; b < warnings_size; ++b) | |
108 | if (value & 1 << b) | |
109 | { | |
110 | if (err) | |
111 | { | |
112 | /* -Wno-error=foo: if foo enabled as an error, | |
113 | make it a warning. */ | |
114 | if (warnings_flag[b] == severity_error) | |
115 | warnings_flag[b] = severity_warning; | |
116 | } | |
117 | else | |
118 | /* -Wno-foo. */ | |
119 | warnings_flag[b] = severity_disabled; | |
120 | } | |
121 | } | |
122 | else | |
123 | { | |
124 | size_t b; | |
125 | for (b = 0; b < warnings_size; ++b) | |
126 | if (value & 1 << b) | |
127 | /* -Wfoo and -Werror=foo. */ | |
128 | warnings_flag[b] = err ? severity_error : severity_warning; | |
129 | } | |
130 | } | |
131 | ||
132 | /** Decode a comma-separated list of arguments from -W. | |
133 | * | |
134 | * \param args comma separated list of effective subarguments to decode. | |
135 | * If 0, then activate all the flags. | |
136 | */ | |
137 | ||
138 | void | |
139 | warnings_argmatch (char *args) | |
140 | { | |
141 | if (args) | |
142 | for (args = strtok (args, ","); args; args = strtok (NULL, ",")) | |
143 | if (STREQ (args, "error")) | |
144 | warnings_are_errors = true; | |
145 | else if (STREQ (args, "no-error")) | |
146 | { | |
147 | warnings_are_errors = false; | |
148 | warning_argmatch ("no-error=all", 3, 6); | |
149 | } | |
150 | else | |
151 | { | |
152 | size_t no = STRPREFIX_LIT ("no-", args) ? 3 : 0; | |
153 | size_t err = STRPREFIX_LIT ("error=", args + no) ? 6 : 0; | |
154 | ||
155 | warning_argmatch (args, no, err); | |
156 | } | |
157 | else | |
158 | warning_argmatch ("all", 0, 0); | |
159 | } | |
160 | ||
161 | ||
162 | /*-----------. | |
163 | | complain. | | |
164 | `-----------*/ | |
a0f6b076 | 165 | |
808e523d AD |
166 | void |
167 | complain_init (void) | |
168 | { | |
169 | warnings warnings_default = | |
170 | Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother; | |
171 | ||
172 | size_t b; | |
173 | for (b = 0; b < warnings_size; ++b) | |
174 | warnings_flag[b] = (1 << b & warnings_default | |
175 | ? severity_warning | |
176 | : severity_unset); | |
177 | } | |
178 | ||
179 | static severity | |
180 | warning_severity (warnings flags) | |
181 | { | |
182 | if (flags & fatal) | |
183 | return severity_fatal; | |
184 | else if (flags & complaint) | |
185 | return severity_error; | |
186 | else | |
187 | { | |
188 | severity res = severity_disabled; | |
189 | size_t b; | |
190 | for (b = 0; b < warnings_size; ++b) | |
191 | if (flags & 1 << b) | |
192 | res = res < warnings_flag[b] ? warnings_flag[b] : res; | |
193 | if (res == severity_warning && warnings_are_errors) | |
194 | res = severity_error; | |
195 | return res; | |
196 | } | |
197 | } | |
198 | ||
199 | ||
219458e2 AD |
200 | /** Display a "[-Wyacc]" like message on \a f. */ |
201 | ||
202 | static void | |
203 | warnings_print_categories (warnings warn_flags, FILE *f) | |
b6403170 | 204 | { |
219458e2 | 205 | /* Display only the first match, the second is "-Wall". */ |
808e523d | 206 | size_t i; |
219458e2 AD |
207 | for (i = 0; warnings_args[i]; ++i) |
208 | if (warn_flags & warnings_types[i]) | |
209 | { | |
808e523d AD |
210 | severity s = warning_severity (warnings_types[i]); |
211 | fprintf (f, " [-W%s%s]", | |
212 | s == severity_error ? "error=" : "", | |
213 | warnings_args[i]); | |
214 | return; | |
219458e2 | 215 | } |
b6403170 | 216 | } |
a0f6b076 | 217 | |
23eb2a69 AD |
218 | /** Report an error message. |
219 | * | |
220 | * \param loc the location, defaulting to the current file, | |
221 | * or the program name. | |
6fb8b256 | 222 | * \param flags the category for this message. |
23eb2a69 | 223 | * \param prefix put before the message (e.g., "warning"). |
2bfcac9a JD |
224 | * \param message the error message, a printf format string. Iff it |
225 | * ends with ": ", then no trailing newline is printed, | |
226 | * and the caller should print the remaining | |
227 | * newline-terminated message to stderr. | |
23eb2a69 AD |
228 | * \param args the arguments of the format string. |
229 | */ | |
230 | static | |
e9955c83 | 231 | void |
6fb8b256 | 232 | error_message (const location *loc, warnings flags, const char *prefix, |
e9690142 | 233 | const char *message, va_list args) |
e9955c83 | 234 | { |
66381412 AR |
235 | unsigned pos = 0; |
236 | ||
23eb2a69 | 237 | if (loc) |
b805eca7 | 238 | pos += location_print (*loc, stderr); |
23eb2a69 | 239 | else |
46b7d74c TR |
240 | pos += fprintf (stderr, "%s", current_file ? current_file : program_name); |
241 | pos += fprintf (stderr, ": "); | |
66381412 AR |
242 | |
243 | if (indent_ptr) | |
244 | { | |
a686f6cd TR |
245 | if (*indent_ptr) |
246 | prefix = NULL; | |
66381412 AR |
247 | if (!*indent_ptr) |
248 | *indent_ptr = pos; | |
249 | else if (*indent_ptr > pos) | |
250 | fprintf (stderr, "%*s", *indent_ptr - pos, ""); | |
251 | indent_ptr = 0; | |
252 | } | |
e9955c83 | 253 | |
23eb2a69 AD |
254 | if (prefix) |
255 | fprintf (stderr, "%s: ", prefix); | |
256 | ||
52489d44 | 257 | vfprintf (stderr, message, args); |
219458e2 AD |
258 | if (! (flags & silent)) |
259 | warnings_print_categories (flags, stderr); | |
2bfcac9a JD |
260 | { |
261 | size_t l = strlen (message); | |
3f5d1b2c | 262 | if (l < 2 || message[l - 2] != ':' || message[l - 1] != ' ') |
aed41cf9 AD |
263 | { |
264 | putc ('\n', stderr); | |
265 | fflush (stderr); | |
ea9e670d | 266 | if (loc && feature_flag & feature_caret && !(flags & no_caret)) |
b805eca7 | 267 | location_caret (*loc, stderr); |
aed41cf9 | 268 | } |
2bfcac9a | 269 | } |
3f5d1b2c | 270 | fflush (stderr); |
23eb2a69 AD |
271 | } |
272 | ||
808e523d | 273 | /** Raise a complaint. That can be a fatal error, an error or just a |
6fb8b256 | 274 | warning. */ |
808e523d AD |
275 | |
276 | static void | |
6fb8b256 VS |
277 | complains (const location *loc, warnings flags, const char *message, |
278 | va_list args) | |
89eb3c76 | 279 | { |
808e523d | 280 | severity s = warning_severity (flags); |
697a8022 TR |
281 | if ((flags & complaint) && complaint_status < status_complaint) |
282 | complaint_status = status_complaint; | |
808e523d AD |
283 | |
284 | if (severity_warning <= s) | |
285 | { | |
286 | const char* prefix = | |
287 | s == severity_fatal ? _("fatal error") | |
288 | : s == severity_error ? _("error") | |
289 | : _("warning"); | |
290 | if (severity_error <= s && ! complaint_status) | |
291 | complaint_status = status_warning_as_error; | |
292 | error_message (loc, flags, prefix, message, args); | |
293 | } | |
294 | ||
1048a1c9 | 295 | if (flags & fatal) |
a686f6cd | 296 | exit (EXIT_FAILURE); |
66381412 AR |
297 | } |
298 | ||
52489d44 | 299 | void |
b999409e | 300 | complain (location const *loc, warnings flags, const char *message, ...) |
52489d44 | 301 | { |
6fb8b256 VS |
302 | va_list args; |
303 | va_start (args, message); | |
bb8e56ff | 304 | complains (loc, flags, message, args); |
6fb8b256 | 305 | va_end (args); |
23eb2a69 | 306 | } |
52489d44 | 307 | |
e9955c83 | 308 | void |
b999409e TR |
309 | complain_indent (location const *loc, warnings flags, unsigned *indent, |
310 | const char *message, ...) | |
e9955c83 | 311 | { |
6fb8b256 | 312 | va_list args; |
66381412 | 313 | indent_ptr = indent; |
6fb8b256 | 314 | va_start (args, message); |
b999409e | 315 | complains (loc, flags, message, args); |
6fb8b256 | 316 | va_end (args); |
e9955c83 | 317 | } |
782e8187 TR |
318 | |
319 | void | |
c6c8de16 TR |
320 | complain_args (location const *loc, warnings w, unsigned *indent, |
321 | int argc, char *argv[]) | |
782e8187 TR |
322 | { |
323 | switch (argc) | |
324 | { | |
f60321dc TR |
325 | case 1: |
326 | complain_indent (loc, w, indent, "%s", _(argv[0])); | |
327 | break; | |
782e8187 | 328 | case 2: |
f60321dc | 329 | complain_indent (loc, w, indent, _(argv[0]), argv[1]); |
782e8187 TR |
330 | break; |
331 | case 3: | |
f60321dc | 332 | complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2]); |
782e8187 TR |
333 | break; |
334 | case 4: | |
f60321dc | 335 | complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3]); |
782e8187 TR |
336 | break; |
337 | case 5: | |
f60321dc TR |
338 | complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3], |
339 | argv[4]); | |
782e8187 TR |
340 | break; |
341 | default: | |
342 | complain (loc, fatal, "too many arguments for complains"); | |
343 | break; | |
344 | } | |
1dc927a7 AD |
345 | } |
346 | ||
347 | void | |
348 | deprecated_directive (location const *loc, char const *old, char const *upd) | |
349 | { | |
350 | if (feature_flag & feature_caret) | |
351 | complain (loc, Wdeprecated, | |
352 | _("deprecated directive, use %s"), | |
353 | quote_n (1, upd)); | |
354 | else | |
355 | complain (loc, Wdeprecated, | |
356 | _("deprecated directive: %s, use %s"), | |
357 | quote (old), quote_n (1, upd)); | |
782e8187 | 358 | } |