]>
Commit | Line | Data |
---|---|---|
1 | /* Declaration for error-reporting function for Bison. | |
2 | ||
3 | Copyright (C) 2000-2002, 2004-2006, 2009-2013 Free Software | |
4 | Foundation, Inc. | |
5 | ||
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. | |
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 | |
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | /* Based on error.c and error.h, | |
20 | written by David MacKenzie <djm@gnu.ai.mit.edu>. */ | |
21 | ||
22 | #include <config.h> | |
23 | #include "system.h" | |
24 | ||
25 | #include <argmatch.h> | |
26 | #include <stdarg.h> | |
27 | #include <progname.h> | |
28 | ||
29 | #include "complain.h" | |
30 | #include "files.h" | |
31 | #include "getargs.h" | |
32 | #include "quote.h" | |
33 | ||
34 | err_status complaint_status = status_none; | |
35 | ||
36 | bool warnings_are_errors = false; | |
37 | ||
38 | /** Whether -Werror/-Wno-error was applied to a warning. */ | |
39 | typedef enum | |
40 | { | |
41 | errority_unset = 0, /** No explict status. */ | |
42 | errority_disabled = 1, /** Explictly disabled with -Wno-error=foo. */ | |
43 | errority_enabled = 2 /** Explictly enabled with -Werror=foo. */ | |
44 | } errority; | |
45 | ||
46 | /** For each warning type, its errority. */ | |
47 | static errority errority_flag[warnings_size]; | |
48 | ||
49 | /** Diagnostics severity. */ | |
50 | typedef enum | |
51 | { | |
52 | severity_disabled = 0, /**< Explicitly disabled via -Wno-foo. */ | |
53 | severity_unset = 1, /**< Unspecified status. */ | |
54 | severity_warning = 2, /**< A warning. */ | |
55 | severity_error = 3, /**< An error (continue, but die soon). */ | |
56 | severity_fatal = 4 /**< Fatal error (die now). */ | |
57 | } severity; | |
58 | ||
59 | ||
60 | /** For each warning type, its severity. */ | |
61 | static severity warnings_flag[warnings_size]; | |
62 | ||
63 | static unsigned *indent_ptr = 0; | |
64 | ||
65 | /*------------------------. | |
66 | | --warnings's handling. | | |
67 | `------------------------*/ | |
68 | ||
69 | static const char * const warnings_args[] = | |
70 | { | |
71 | "none", | |
72 | "midrule-values", | |
73 | "yacc", | |
74 | "conflicts-sr", | |
75 | "conflicts-rr", | |
76 | "deprecated", | |
77 | "empty-rule", | |
78 | "precedence", | |
79 | "other", | |
80 | "all", | |
81 | "error", | |
82 | "everything", | |
83 | 0 | |
84 | }; | |
85 | ||
86 | static const int warnings_types[] = | |
87 | { | |
88 | Wnone, | |
89 | Wmidrule_values, | |
90 | Wyacc, | |
91 | Wconflicts_sr, | |
92 | Wconflicts_rr, | |
93 | Wdeprecated, | |
94 | Wempty_rule, | |
95 | Wprecedence, | |
96 | Wother, | |
97 | Wall, | |
98 | Werror, | |
99 | Weverything | |
100 | }; | |
101 | ||
102 | ARGMATCH_VERIFY (warnings_args, warnings_types); | |
103 | ||
104 | void | |
105 | warning_argmatch (char const *arg, size_t no, size_t err) | |
106 | { | |
107 | int value = XARGMATCH ("--warning", arg + no + err, | |
108 | warnings_args, warnings_types); | |
109 | ||
110 | /* -Wnone == -Wno-everything, and -Wno-none == -Weverything. */ | |
111 | if (!value) | |
112 | { | |
113 | value = Weverything; | |
114 | no = !no; | |
115 | } | |
116 | ||
117 | size_t b; | |
118 | for (b = 0; b < warnings_size; ++b) | |
119 | if (value & 1 << b) | |
120 | { | |
121 | if (err && no) | |
122 | /* -Wno-error=foo. */ | |
123 | errority_flag[b] = errority_disabled; | |
124 | else if (err && !no) | |
125 | { | |
126 | /* -Werror=foo: enables -Wfoo. */ | |
127 | errority_flag[b] = errority_enabled; | |
128 | warnings_flag[b] = severity_warning; | |
129 | } | |
130 | else if (no) | |
131 | /* -Wno-foo. */ | |
132 | warnings_flag[b] = severity_disabled; | |
133 | else | |
134 | /* -Wfoo. */ | |
135 | warnings_flag[b] = severity_warning; | |
136 | } | |
137 | } | |
138 | ||
139 | /** Decode a comma-separated list of arguments from -W. | |
140 | * | |
141 | * \param args comma separated list of effective subarguments to decode. | |
142 | * If 0, then activate all the flags. | |
143 | */ | |
144 | ||
145 | void | |
146 | warnings_argmatch (char *args) | |
147 | { | |
148 | if (args) | |
149 | for (args = strtok (args, ","); args; args = strtok (NULL, ",")) | |
150 | if (STREQ (args, "error")) | |
151 | warnings_are_errors = true; | |
152 | else if (STREQ (args, "no-error")) | |
153 | warnings_are_errors = false; | |
154 | else | |
155 | { | |
156 | // The length of the possible 'no-' prefix: 3, or 0. | |
157 | size_t no = STRPREFIX_LIT ("no-", args) ? 3 : 0; | |
158 | // The length of the possible 'error=' (possibly after | |
159 | // 'no-') prefix: 6, or 0. | |
160 | size_t err = STRPREFIX_LIT ("error=", args + no) ? 6 : 0; | |
161 | ||
162 | warning_argmatch (args, no, err); | |
163 | } | |
164 | else | |
165 | warning_argmatch ("all", 0, 0); | |
166 | } | |
167 | ||
168 | ||
169 | /*-----------. | |
170 | | complain. | | |
171 | `-----------*/ | |
172 | ||
173 | void | |
174 | complain_init (void) | |
175 | { | |
176 | warnings warnings_default = | |
177 | Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother; | |
178 | ||
179 | size_t b; | |
180 | for (b = 0; b < warnings_size; ++b) | |
181 | { | |
182 | warnings_flag[b] = (1 << b & warnings_default | |
183 | ? severity_warning | |
184 | : severity_unset); | |
185 | errority_flag[b] = errority_unset; | |
186 | } | |
187 | } | |
188 | ||
189 | ||
190 | /* A diagnostic with FLAGS is about to be issued. With what severity? | |
191 | (severity_fatal, severity_error, severity_disabled, or | |
192 | severity_warning.) */ | |
193 | ||
194 | static severity | |
195 | warning_severity (warnings flags) | |
196 | { | |
197 | if (flags & fatal) | |
198 | /* Diagnostics about fatal errors. */ | |
199 | return severity_fatal; | |
200 | else if (flags & complaint) | |
201 | /* Diagnostics about errors. */ | |
202 | return severity_error; | |
203 | else | |
204 | { | |
205 | /* Diagnostics about warnings. */ | |
206 | severity res = severity_disabled; | |
207 | size_t b; | |
208 | for (b = 0; b < warnings_size; ++b) | |
209 | if (flags & 1 << b) | |
210 | { | |
211 | res = res < warnings_flag[b] ? warnings_flag[b] : res; | |
212 | /* If the diagnostic is enabled, and -Werror is enabled, | |
213 | and -Wno-error=foo was not explicitly requested, this | |
214 | is an error. */ | |
215 | if (res == severity_warning | |
216 | && (errority_flag[b] == errority_enabled | |
217 | || (warnings_are_errors | |
218 | && errority_flag[b] != errority_disabled))) | |
219 | res = severity_error; | |
220 | } | |
221 | return res; | |
222 | } | |
223 | } | |
224 | ||
225 | bool | |
226 | warning_is_unset (warnings flags) | |
227 | { | |
228 | size_t b; | |
229 | for (b = 0; b < warnings_size; ++b) | |
230 | if (flags & 1 << b && warnings_flag[b] != severity_unset) | |
231 | return false; | |
232 | return true; | |
233 | } | |
234 | ||
235 | /** Display a "[-Wyacc]" like message on \a f. */ | |
236 | ||
237 | static void | |
238 | warnings_print_categories (warnings warn_flags, FILE *f) | |
239 | { | |
240 | /* Display only the first match, the second is "-Wall". */ | |
241 | size_t i; | |
242 | for (i = 0; warnings_args[i]; ++i) | |
243 | if (warn_flags & warnings_types[i]) | |
244 | { | |
245 | severity s = warning_severity (warnings_types[i]); | |
246 | fprintf (f, " [-W%s%s]", | |
247 | s == severity_error ? "error=" : "", | |
248 | warnings_args[i]); | |
249 | return; | |
250 | } | |
251 | } | |
252 | ||
253 | /** Report an error message. | |
254 | * | |
255 | * \param loc the location, defaulting to the current file, | |
256 | * or the program name. | |
257 | * \param flags the category for this message. | |
258 | * \param prefix put before the message (e.g., "warning"). | |
259 | * \param message the error message, a printf format string. Iff it | |
260 | * ends with ": ", then no trailing newline is printed, | |
261 | * and the caller should print the remaining | |
262 | * newline-terminated message to stderr. | |
263 | * \param args the arguments of the format string. | |
264 | */ | |
265 | static | |
266 | void | |
267 | error_message (const location *loc, warnings flags, const char *prefix, | |
268 | const char *message, va_list args) | |
269 | { | |
270 | unsigned pos = 0; | |
271 | ||
272 | if (loc) | |
273 | pos += location_print (*loc, stderr); | |
274 | else | |
275 | pos += fprintf (stderr, "%s", current_file ? current_file : program_name); | |
276 | pos += fprintf (stderr, ": "); | |
277 | ||
278 | if (indent_ptr) | |
279 | { | |
280 | if (*indent_ptr) | |
281 | prefix = NULL; | |
282 | if (!*indent_ptr) | |
283 | *indent_ptr = pos; | |
284 | else if (*indent_ptr > pos) | |
285 | fprintf (stderr, "%*s", *indent_ptr - pos, ""); | |
286 | indent_ptr = 0; | |
287 | } | |
288 | ||
289 | if (prefix) | |
290 | fprintf (stderr, "%s: ", prefix); | |
291 | ||
292 | vfprintf (stderr, message, args); | |
293 | if (! (flags & silent)) | |
294 | warnings_print_categories (flags, stderr); | |
295 | { | |
296 | size_t l = strlen (message); | |
297 | if (l < 2 || message[l - 2] != ':' || message[l - 1] != ' ') | |
298 | { | |
299 | putc ('\n', stderr); | |
300 | fflush (stderr); | |
301 | if (loc && feature_flag & feature_caret && !(flags & no_caret)) | |
302 | location_caret (*loc, stderr); | |
303 | } | |
304 | } | |
305 | fflush (stderr); | |
306 | } | |
307 | ||
308 | /** Raise a complaint. That can be a fatal error, an error or just a | |
309 | warning. */ | |
310 | ||
311 | static void | |
312 | complains (const location *loc, warnings flags, const char *message, | |
313 | va_list args) | |
314 | { | |
315 | severity s = warning_severity (flags); | |
316 | if ((flags & complaint) && complaint_status < status_complaint) | |
317 | complaint_status = status_complaint; | |
318 | ||
319 | if (severity_warning <= s) | |
320 | { | |
321 | const char* prefix = | |
322 | s == severity_fatal ? _("fatal error") | |
323 | : s == severity_error ? _("error") | |
324 | : _("warning"); | |
325 | if (severity_error <= s && ! complaint_status) | |
326 | complaint_status = status_warning_as_error; | |
327 | error_message (loc, flags, prefix, message, args); | |
328 | } | |
329 | ||
330 | if (flags & fatal) | |
331 | exit (EXIT_FAILURE); | |
332 | } | |
333 | ||
334 | void | |
335 | complain (location const *loc, warnings flags, const char *message, ...) | |
336 | { | |
337 | va_list args; | |
338 | va_start (args, message); | |
339 | complains (loc, flags, message, args); | |
340 | va_end (args); | |
341 | } | |
342 | ||
343 | void | |
344 | complain_indent (location const *loc, warnings flags, unsigned *indent, | |
345 | const char *message, ...) | |
346 | { | |
347 | va_list args; | |
348 | indent_ptr = indent; | |
349 | va_start (args, message); | |
350 | complains (loc, flags, message, args); | |
351 | va_end (args); | |
352 | } | |
353 | ||
354 | void | |
355 | complain_args (location const *loc, warnings w, unsigned *indent, | |
356 | int argc, char *argv[]) | |
357 | { | |
358 | switch (argc) | |
359 | { | |
360 | case 1: | |
361 | complain_indent (loc, w, indent, "%s", _(argv[0])); | |
362 | break; | |
363 | case 2: | |
364 | complain_indent (loc, w, indent, _(argv[0]), argv[1]); | |
365 | break; | |
366 | case 3: | |
367 | complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2]); | |
368 | break; | |
369 | case 4: | |
370 | complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3]); | |
371 | break; | |
372 | case 5: | |
373 | complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3], | |
374 | argv[4]); | |
375 | break; | |
376 | default: | |
377 | complain (loc, fatal, "too many arguments for complains"); | |
378 | break; | |
379 | } | |
380 | } | |
381 | ||
382 | void | |
383 | deprecated_directive (location const *loc, char const *old, char const *upd) | |
384 | { | |
385 | if (feature_flag & feature_caret) | |
386 | complain (loc, Wdeprecated, | |
387 | _("deprecated directive, use %s"), | |
388 | quote_n (1, upd)); | |
389 | else | |
390 | complain (loc, Wdeprecated, | |
391 | _("deprecated directive: %s, use %s"), | |
392 | quote (old), quote_n (1, upd)); | |
393 | } | |
394 | ||
395 | void | |
396 | duplicate_directive (char const *directive, | |
397 | location first, location second) | |
398 | { | |
399 | unsigned i = 0; | |
400 | complain (&second, complaint, _("only one %s allowed per rule"), directive); | |
401 | i += SUB_INDENT; | |
402 | complain_indent (&first, complaint, &i, _("previous declaration")); | |
403 | } |