]> git.saurik.com Git - bison.git/blame - src/complain.c
diagnostics: factor and enhance messages about duplicate rule directives
[bison.git] / src / complain.c
CommitLineData
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 34err_status complaint_status = status_none;
d0f11c1b 35
808e523d 36bool warnings_are_errors = false;
12bc1c9e
AD
37
38/** Diagnostics severity. */
39typedef 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. */
50static severity warnings_flag[warnings_size];
9503b0a4 51
66381412 52static unsigned *indent_ptr = 0;
12bc1c9e
AD
53
54/*------------------------.
55| --warnings's handling. |
56`------------------------*/
57
58static 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
74static 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
89ARGMATCH_VERIFY (warnings_args, warnings_types);
90
91void
92warning_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
138void
139warnings_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
166void
167complain_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
179static severity
180warning_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
42d101da
AD
199bool
200warning_is_unset (warnings flags)
201{
202 size_t b;
203 for (b = 0; b < warnings_size; ++b)
204 if (flags & 1 << b && warnings_flag[b] != severity_unset)
205 return false;
206 return true;
207}
808e523d 208
219458e2
AD
209/** Display a "[-Wyacc]" like message on \a f. */
210
211static void
212warnings_print_categories (warnings warn_flags, FILE *f)
b6403170 213{
219458e2 214 /* Display only the first match, the second is "-Wall". */
808e523d 215 size_t i;
219458e2
AD
216 for (i = 0; warnings_args[i]; ++i)
217 if (warn_flags & warnings_types[i])
218 {
808e523d
AD
219 severity s = warning_severity (warnings_types[i]);
220 fprintf (f, " [-W%s%s]",
221 s == severity_error ? "error=" : "",
222 warnings_args[i]);
223 return;
219458e2 224 }
b6403170 225}
a0f6b076 226
23eb2a69
AD
227/** Report an error message.
228 *
229 * \param loc the location, defaulting to the current file,
230 * or the program name.
6fb8b256 231 * \param flags the category for this message.
23eb2a69 232 * \param prefix put before the message (e.g., "warning").
2bfcac9a
JD
233 * \param message the error message, a printf format string. Iff it
234 * ends with ": ", then no trailing newline is printed,
235 * and the caller should print the remaining
236 * newline-terminated message to stderr.
23eb2a69
AD
237 * \param args the arguments of the format string.
238 */
239static
e9955c83 240void
6fb8b256 241error_message (const location *loc, warnings flags, const char *prefix,
e9690142 242 const char *message, va_list args)
e9955c83 243{
66381412
AR
244 unsigned pos = 0;
245
23eb2a69 246 if (loc)
b805eca7 247 pos += location_print (*loc, stderr);
23eb2a69 248 else
46b7d74c
TR
249 pos += fprintf (stderr, "%s", current_file ? current_file : program_name);
250 pos += fprintf (stderr, ": ");
66381412
AR
251
252 if (indent_ptr)
253 {
a686f6cd
TR
254 if (*indent_ptr)
255 prefix = NULL;
66381412
AR
256 if (!*indent_ptr)
257 *indent_ptr = pos;
258 else if (*indent_ptr > pos)
259 fprintf (stderr, "%*s", *indent_ptr - pos, "");
260 indent_ptr = 0;
261 }
e9955c83 262
23eb2a69
AD
263 if (prefix)
264 fprintf (stderr, "%s: ", prefix);
265
52489d44 266 vfprintf (stderr, message, args);
219458e2
AD
267 if (! (flags & silent))
268 warnings_print_categories (flags, stderr);
2bfcac9a
JD
269 {
270 size_t l = strlen (message);
3f5d1b2c 271 if (l < 2 || message[l - 2] != ':' || message[l - 1] != ' ')
aed41cf9
AD
272 {
273 putc ('\n', stderr);
274 fflush (stderr);
ea9e670d 275 if (loc && feature_flag & feature_caret && !(flags & no_caret))
b805eca7 276 location_caret (*loc, stderr);
aed41cf9 277 }
2bfcac9a 278 }
3f5d1b2c 279 fflush (stderr);
23eb2a69
AD
280}
281
808e523d 282/** Raise a complaint. That can be a fatal error, an error or just a
6fb8b256 283 warning. */
808e523d
AD
284
285static void
6fb8b256
VS
286complains (const location *loc, warnings flags, const char *message,
287 va_list args)
89eb3c76 288{
808e523d 289 severity s = warning_severity (flags);
697a8022
TR
290 if ((flags & complaint) && complaint_status < status_complaint)
291 complaint_status = status_complaint;
808e523d
AD
292
293 if (severity_warning <= s)
294 {
295 const char* prefix =
296 s == severity_fatal ? _("fatal error")
297 : s == severity_error ? _("error")
298 : _("warning");
299 if (severity_error <= s && ! complaint_status)
300 complaint_status = status_warning_as_error;
301 error_message (loc, flags, prefix, message, args);
302 }
303
1048a1c9 304 if (flags & fatal)
a686f6cd 305 exit (EXIT_FAILURE);
66381412
AR
306}
307
52489d44 308void
b999409e 309complain (location const *loc, warnings flags, const char *message, ...)
52489d44 310{
6fb8b256
VS
311 va_list args;
312 va_start (args, message);
bb8e56ff 313 complains (loc, flags, message, args);
6fb8b256 314 va_end (args);
23eb2a69 315}
52489d44 316
e9955c83 317void
b999409e
TR
318complain_indent (location const *loc, warnings flags, unsigned *indent,
319 const char *message, ...)
e9955c83 320{
6fb8b256 321 va_list args;
66381412 322 indent_ptr = indent;
6fb8b256 323 va_start (args, message);
b999409e 324 complains (loc, flags, message, args);
6fb8b256 325 va_end (args);
e9955c83 326}
782e8187
TR
327
328void
c6c8de16
TR
329complain_args (location const *loc, warnings w, unsigned *indent,
330 int argc, char *argv[])
782e8187
TR
331{
332 switch (argc)
333 {
f60321dc
TR
334 case 1:
335 complain_indent (loc, w, indent, "%s", _(argv[0]));
336 break;
782e8187 337 case 2:
f60321dc 338 complain_indent (loc, w, indent, _(argv[0]), argv[1]);
782e8187
TR
339 break;
340 case 3:
f60321dc 341 complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2]);
782e8187
TR
342 break;
343 case 4:
f60321dc 344 complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3]);
782e8187
TR
345 break;
346 case 5:
f60321dc
TR
347 complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3],
348 argv[4]);
782e8187
TR
349 break;
350 default:
351 complain (loc, fatal, "too many arguments for complains");
352 break;
353 }
1dc927a7
AD
354}
355
356void
357deprecated_directive (location const *loc, char const *old, char const *upd)
358{
359 if (feature_flag & feature_caret)
360 complain (loc, Wdeprecated,
361 _("deprecated directive, use %s"),
362 quote_n (1, upd));
363 else
364 complain (loc, Wdeprecated,
365 _("deprecated directive: %s, use %s"),
366 quote (old), quote_n (1, upd));
782e8187 367}
0fe5a72a
AD
368
369void
370duplicate_directive (char const *directive,
371 location first, location second)
372{
373 unsigned i = 0;
374 complain (&second, complaint, _("only one %s allowed per rule"), directive);
375 i += SUB_INDENT;
376 complain_indent (&first, complaint, &i, _("previous declaration"));
377}