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 /** Whether -Werror/-Wno-error was applied to a warning. */
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. */
46 /** For each warning type, its errority. */
47 static errority errority_flag
[warnings_size
];
49 /** Diagnostics severity. */
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). */
60 /** For each warning type, its severity. */
61 static severity warnings_flag
[warnings_size
];
63 static unsigned *indent_ptr
= 0;
65 /*------------------------.
66 | --warnings's handling. |
67 `------------------------*/
69 static const char * const warnings_args
[] =
86 static const int warnings_types
[] =
102 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
105 warning_argmatch (char const *arg
, size_t no
, size_t err
)
107 int value
= XARGMATCH ("--warning", arg
+ no
+ err
,
108 warnings_args
, warnings_types
);
110 /* -Wnone == -Wno-everything, and -Wno-none == -Weverything. */
118 for (b
= 0; b
< warnings_size
; ++b
)
122 /* -Wno-error=foo. */
123 errority_flag
[b
] = errority_disabled
;
126 /* -Werror=foo: enables -Wfoo. */
127 errority_flag
[b
] = errority_enabled
;
128 warnings_flag
[b
] = severity_warning
;
132 warnings_flag
[b
] = severity_disabled
;
135 warnings_flag
[b
] = severity_warning
;
139 /** Decode a comma-separated list of arguments from -W.
141 * \param args comma separated list of effective subarguments to decode.
142 * If 0, then activate all the flags.
146 warnings_argmatch (char *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;
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;
162 warning_argmatch (args
, no
, err
);
165 warning_argmatch ("all", 0, 0);
176 warnings warnings_default
=
177 Wconflicts_sr
| Wconflicts_rr
| Wdeprecated
| Wother
;
180 for (b
= 0; b
< warnings_size
; ++b
)
182 warnings_flag
[b
] = (1 << b
& warnings_default
185 errority_flag
[b
] = errority_unset
;
190 /* A diagnostic with FLAGS is about to be issued. With what severity?
191 (severity_fatal, severity_error, severity_disabled, or
192 severity_warning.) */
195 warning_severity (warnings flags
)
198 /* Diagnostics about fatal errors. */
199 return severity_fatal
;
200 else if (flags
& complaint
)
201 /* Diagnostics about errors. */
202 return severity_error
;
205 /* Diagnostics about warnings. */
206 severity res
= severity_disabled
;
208 for (b
= 0; b
< warnings_size
; ++b
)
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
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
;
226 warning_is_unset (warnings flags
)
229 for (b
= 0; b
< warnings_size
; ++b
)
230 if (flags
& 1 << b
&& warnings_flag
[b
] != severity_unset
)
235 /** Display a "[-Wyacc]" like message on \a f. */
238 warnings_print_categories (warnings warn_flags
, FILE *f
)
240 /* Display only the first match, the second is "-Wall". */
242 for (i
= 0; warnings_args
[i
]; ++i
)
243 if (warn_flags
& warnings_types
[i
])
245 severity s
= warning_severity (warnings_types
[i
]);
246 fprintf (f
, " [-W%s%s]",
247 s
== severity_error
? "error=" : "",
253 /** Report an error message.
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.
267 error_message (const location
*loc
, warnings flags
, const char *prefix
,
268 const char *message
, va_list args
)
273 pos
+= location_print (*loc
, stderr
);
275 pos
+= fprintf (stderr
, "%s", current_file
? current_file
: program_name
);
276 pos
+= fprintf (stderr
, ": ");
284 else if (*indent_ptr
> pos
)
285 fprintf (stderr
, "%*s", *indent_ptr
- pos
, "");
290 fprintf (stderr
, "%s: ", prefix
);
292 vfprintf (stderr
, message
, args
);
293 if (! (flags
& silent
))
294 warnings_print_categories (flags
, stderr
);
296 size_t l
= strlen (message
);
297 if (l
< 2 || message
[l
- 2] != ':' || message
[l
- 1] != ' ')
301 if (loc
&& feature_flag
& feature_caret
&& !(flags
& no_caret
))
302 location_caret (*loc
, stderr
);
308 /** Raise a complaint. That can be a fatal error, an error or just a
312 complains (const location
*loc
, warnings flags
, const char *message
,
315 severity s
= warning_severity (flags
);
316 if ((flags
& complaint
) && complaint_status
< status_complaint
)
317 complaint_status
= status_complaint
;
319 if (severity_warning
<= s
)
322 s
== severity_fatal
? _("fatal error")
323 : s
== severity_error
? _("error")
325 if (severity_error
<= s
&& ! complaint_status
)
326 complaint_status
= status_warning_as_error
;
327 error_message (loc
, flags
, prefix
, message
, args
);
335 complain (location
const *loc
, warnings flags
, const char *message
, ...)
338 va_start (args
, message
);
339 complains (loc
, flags
, message
, args
);
344 complain_indent (location
const *loc
, warnings flags
, unsigned *indent
,
345 const char *message
, ...)
349 va_start (args
, message
);
350 complains (loc
, flags
, message
, args
);
355 complain_args (location
const *loc
, warnings w
, unsigned *indent
,
356 int argc
, char *argv
[])
361 complain_indent (loc
, w
, indent
, "%s", _(argv
[0]));
364 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1]);
367 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1], argv
[2]);
370 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1], argv
[2], argv
[3]);
373 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1], argv
[2], argv
[3],
377 complain (loc
, fatal
, "too many arguments for complains");
383 deprecated_directive (location
const *loc
, char const *old
, char const *upd
)
385 if (feature_flag
& feature_caret
)
386 complain (loc
, Wdeprecated
,
387 _("deprecated directive, use %s"),
390 complain (loc
, Wdeprecated
,
391 _("deprecated directive: %s, use %s"),
392 quote (old
), quote_n (1, upd
));
396 duplicate_directive (char const *directive
,
397 location first
, location second
)
400 complain (&second
, complaint
, _("only one %s allowed per rule"), directive
);
402 complain_indent (&first
, complaint
, &i
, _("previous declaration"));