+/*------------------------.
+| --warnings's handling. |
+`------------------------*/
+
+static const char * const warnings_args[] =
+{
+ "none",
+ "midrule-values",
+ "yacc",
+ "conflicts-sr",
+ "conflicts-rr",
+ "deprecated",
+ "empty-rule",
+ "precedence",
+ "other",
+ "all",
+ "error",
+ "everything",
+ 0
+};
+
+static const int warnings_types[] =
+{
+ Wnone,
+ Wmidrule_values,
+ Wyacc,
+ Wconflicts_sr,
+ Wconflicts_rr,
+ Wdeprecated,
+ Wempty_rule,
+ Wprecedence,
+ Wother,
+ Wall,
+ Werror,
+ Weverything
+};
+
+ARGMATCH_VERIFY (warnings_args, warnings_types);
+
+void
+warning_argmatch (char const *arg, size_t no, size_t err)
+{
+ int value = XARGMATCH ("--warning", arg + no + err,
+ warnings_args, warnings_types);
+
+ /* -Wnone == -Wno-everything, and -Wno-none == -Weverything. */
+ if (!value)
+ {
+ value = Weverything;
+ no = !no;
+ }
+
+ if (no)
+ {
+ size_t b;
+ for (b = 0; b < warnings_size; ++b)
+ if (value & 1 << b)
+ {
+ if (err)
+ {
+ /* -Wno-error=foo: if foo enabled as an error,
+ make it a warning. */
+ if (warnings_flag[b] == severity_error)
+ warnings_flag[b] = severity_warning;
+ }
+ else
+ /* -Wno-foo. */
+ warnings_flag[b] = severity_disabled;
+ }
+ }
+ else
+ {
+ size_t b;
+ for (b = 0; b < warnings_size; ++b)
+ if (value & 1 << b)
+ /* -Wfoo and -Werror=foo. */
+ warnings_flag[b] = err ? severity_error : severity_warning;
+ }
+}
+
+/** Decode a comma-separated list of arguments from -W.
+ *
+ * \param args comma separated list of effective subarguments to decode.
+ * If 0, then activate all the flags.
+ */
+
+void
+warnings_argmatch (char *args)
+{
+ if (args)
+ for (args = strtok (args, ","); args; args = strtok (NULL, ","))
+ if (STREQ (args, "error"))
+ warnings_are_errors = true;
+ else if (STREQ (args, "no-error"))
+ {
+ warnings_are_errors = false;
+ warning_argmatch ("no-error=everything", 3, 6);
+ }
+ else
+ {
+ size_t no = STRPREFIX_LIT ("no-", args) ? 3 : 0;
+ size_t err = STRPREFIX_LIT ("error=", args + no) ? 6 : 0;
+
+ warning_argmatch (args, no, err);
+ }
+ else
+ warning_argmatch ("all", 0, 0);
+}
+
+
+/*-----------.
+| complain. |
+`-----------*/
+
+void
+complain_init (void)
+{
+ warnings warnings_default =
+ Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother;
+
+ size_t b;
+ for (b = 0; b < warnings_size; ++b)
+ warnings_flag[b] = (1 << b & warnings_default
+ ? severity_warning
+ : severity_unset);
+}
+
+static severity
+warning_severity (warnings flags)
+{
+ if (flags & fatal)
+ return severity_fatal;
+ else if (flags & complaint)
+ return severity_error;
+ else
+ {
+ severity res = severity_disabled;
+ size_t b;
+ for (b = 0; b < warnings_size; ++b)
+ if (flags & 1 << b)
+ res = res < warnings_flag[b] ? warnings_flag[b] : res;
+ if (res == severity_warning && warnings_are_errors)
+ res = severity_error;
+ return res;
+ }
+}
+
+bool
+warning_is_unset (warnings flags)
+{
+ size_t b;
+ for (b = 0; b < warnings_size; ++b)
+ if (flags & 1 << b && warnings_flag[b] != severity_unset)
+ return false;
+ return true;
+}
+
+/** Display a "[-Wyacc]" like message on \a f. */
+
+static void
+warnings_print_categories (warnings warn_flags, FILE *f)
+{
+ /* Display only the first match, the second is "-Wall". */
+ size_t i;
+ for (i = 0; warnings_args[i]; ++i)
+ if (warn_flags & warnings_types[i])
+ {
+ severity s = warning_severity (warnings_types[i]);
+ fprintf (f, " [-W%s%s]",
+ s == severity_error ? "error=" : "",
+ warnings_args[i]);
+ return;
+ }
+}