1 /* Declaration for error-reporting function for Bison.
3 Copyright (C) 2000-2002, 2004-2006, 2009-2013 Free Software
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.
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.
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/>. */
19 /* Based on error.c and error.h,
20 written by David MacKenzie <djm@gnu.ai.mit.edu>. */
34 err_status complaint_status
= status_none
;
36 bool warnings_are_errors
= false;
38 /** Diagnostics severity. */
41 severity_disabled
= 0,
49 /** For each warning type, its severity. */
50 static severity warnings_flag
[warnings_size
];
52 static unsigned *indent_ptr
= 0;
54 /*------------------------.
55 | --warnings's handling. |
56 `------------------------*/
58 static const char * const warnings_args
[] =
74 static const int warnings_types
[] =
89 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
92 warning_argmatch (char const *arg
, size_t no
, size_t err
)
94 int value
= XARGMATCH ("--warning", arg
+ no
+ err
,
95 warnings_args
, warnings_types
);
97 /* -Wnone == -Wno-all, and -Wno-none == -Wall. */
107 for (b
= 0; b
< warnings_size
; ++b
)
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
;
119 warnings_flag
[b
] = severity_disabled
;
125 for (b
= 0; b
< warnings_size
; ++b
)
127 /* -Wfoo and -Werror=foo. */
128 warnings_flag
[b
] = err
? severity_error
: severity_warning
;
132 /** Decode a comma-separated list of arguments from -W.
134 * \param args comma separated list of effective subarguments to decode.
135 * If 0, then activate all the flags.
139 warnings_argmatch (char *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"))
147 warnings_are_errors
= false;
148 warning_argmatch ("no-error=all", 3, 6);
152 size_t no
= STRPREFIX_LIT ("no-", args
) ? 3 : 0;
153 size_t err
= STRPREFIX_LIT ("error=", args
+ no
) ? 6 : 0;
155 warning_argmatch (args
, no
, err
);
158 warning_argmatch ("all", 0, 0);
169 warnings warnings_default
=
170 Wconflicts_sr
| Wconflicts_rr
| Wdeprecated
| Wother
;
173 for (b
= 0; b
< warnings_size
; ++b
)
174 warnings_flag
[b
] = (1 << b
& warnings_default
180 warning_severity (warnings flags
)
183 return severity_fatal
;
184 else if (flags
& complaint
)
185 return severity_error
;
188 severity res
= severity_disabled
;
190 for (b
= 0; b
< warnings_size
; ++b
)
192 res
= res
< warnings_flag
[b
] ? warnings_flag
[b
] : res
;
193 if (res
== severity_warning
&& warnings_are_errors
)
194 res
= severity_error
;
200 warning_is_unset (warnings flags
)
203 for (b
= 0; b
< warnings_size
; ++b
)
204 if (flags
& 1 << b
&& warnings_flag
[b
] != severity_unset
)
209 /** Display a "[-Wyacc]" like message on \a f. */
212 warnings_print_categories (warnings warn_flags
, FILE *f
)
214 /* Display only the first match, the second is "-Wall". */
216 for (i
= 0; warnings_args
[i
]; ++i
)
217 if (warn_flags
& warnings_types
[i
])
219 severity s
= warning_severity (warnings_types
[i
]);
220 fprintf (f
, " [-W%s%s]",
221 s
== severity_error
? "error=" : "",
227 /** Report an error message.
229 * \param loc the location, defaulting to the current file,
230 * or the program name.
231 * \param flags the category for this message.
232 * \param prefix put before the message (e.g., "warning").
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.
237 * \param args the arguments of the format string.
241 error_message (const location
*loc
, warnings flags
, const char *prefix
,
242 const char *message
, va_list args
)
247 pos
+= location_print (*loc
, stderr
);
249 pos
+= fprintf (stderr
, "%s", current_file
? current_file
: program_name
);
250 pos
+= fprintf (stderr
, ": ");
258 else if (*indent_ptr
> pos
)
259 fprintf (stderr
, "%*s", *indent_ptr
- pos
, "");
264 fprintf (stderr
, "%s: ", prefix
);
266 vfprintf (stderr
, message
, args
);
267 if (! (flags
& silent
))
268 warnings_print_categories (flags
, stderr
);
270 size_t l
= strlen (message
);
271 if (l
< 2 || message
[l
- 2] != ':' || message
[l
- 1] != ' ')
275 if (loc
&& feature_flag
& feature_caret
&& !(flags
& no_caret
))
276 location_caret (*loc
, stderr
);
282 /** Raise a complaint. That can be a fatal error, an error or just a
286 complains (const location
*loc
, warnings flags
, const char *message
,
289 severity s
= warning_severity (flags
);
290 if ((flags
& complaint
) && complaint_status
< status_complaint
)
291 complaint_status
= status_complaint
;
293 if (severity_warning
<= s
)
296 s
== severity_fatal
? _("fatal error")
297 : s
== severity_error
? _("error")
299 if (severity_error
<= s
&& ! complaint_status
)
300 complaint_status
= status_warning_as_error
;
301 error_message (loc
, flags
, prefix
, message
, args
);
309 complain (location
const *loc
, warnings flags
, const char *message
, ...)
312 va_start (args
, message
);
313 complains (loc
, flags
, message
, args
);
318 complain_indent (location
const *loc
, warnings flags
, unsigned *indent
,
319 const char *message
, ...)
323 va_start (args
, message
);
324 complains (loc
, flags
, message
, args
);
329 complain_args (location
const *loc
, warnings w
, unsigned *indent
,
330 int argc
, char *argv
[])
335 complain_indent (loc
, w
, indent
, "%s", _(argv
[0]));
338 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1]);
341 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1], argv
[2]);
344 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1], argv
[2], argv
[3]);
347 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1], argv
[2], argv
[3],
351 complain (loc
, fatal
, "too many arguments for complains");
357 deprecated_directive (location
const *loc
, char const *old
, char const *upd
)
359 if (feature_flag
& feature_caret
)
360 complain (loc
, Wdeprecated
,
361 _("deprecated directive, use %s"),
364 complain (loc
, Wdeprecated
,
365 _("deprecated directive: %s, use %s"),
366 quote (old
), quote_n (1, upd
));