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>. */
33 err_status complaint_status
= status_none
;
35 bool warnings_are_errors
= false;
36 severity warnings_flag
[warnings_size
];
38 static unsigned *indent_ptr
= 0;
43 warnings warnings_default
=
44 Wconflicts_sr
| Wconflicts_rr
| Wdeprecated
| Wother
;
47 for (b
= 0; b
< warnings_size
; ++b
)
48 warnings_flag
[b
] = (1 << b
& warnings_default
54 warning_severity (warnings flags
)
57 return severity_fatal
;
58 else if (flags
& complaint
)
59 return severity_error
;
62 severity res
= severity_disabled
;
64 for (b
= 0; b
< warnings_size
; ++b
)
66 res
= res
< warnings_flag
[b
] ? warnings_flag
[b
] : res
;
67 if (res
== severity_warning
&& warnings_are_errors
)
74 /** Display a "[-Wyacc]" like message on \a f. */
77 warnings_print_categories (warnings warn_flags
, FILE *f
)
79 /* Display only the first match, the second is "-Wall". */
81 for (i
= 0; warnings_args
[i
]; ++i
)
82 if (warn_flags
& warnings_types
[i
])
84 severity s
= warning_severity (warnings_types
[i
]);
85 fprintf (f
, " [-W%s%s]",
86 s
== severity_error
? "error=" : "",
92 /** Report an error message.
94 * \param loc the location, defaulting to the current file,
95 * or the program name.
96 * \param flags the category for this message.
97 * \param prefix put before the message (e.g., "warning").
98 * \param message the error message, a printf format string. Iff it
99 * ends with ": ", then no trailing newline is printed,
100 * and the caller should print the remaining
101 * newline-terminated message to stderr.
102 * \param args the arguments of the format string.
106 error_message (const location
*loc
, warnings flags
, const char *prefix
,
107 const char *message
, va_list args
)
112 pos
+= location_print (*loc
, stderr
);
114 pos
+= fprintf (stderr
, "%s", current_file
? current_file
: program_name
);
115 pos
+= fprintf (stderr
, ": ");
123 else if (*indent_ptr
> pos
)
124 fprintf (stderr
, "%*s", *indent_ptr
- pos
, "");
129 fprintf (stderr
, "%s: ", prefix
);
131 vfprintf (stderr
, message
, args
);
132 if (! (flags
& silent
))
133 warnings_print_categories (flags
, stderr
);
135 size_t l
= strlen (message
);
136 if (l
< 2 || message
[l
- 2] != ':' || message
[l
- 1] != ' ')
140 if (loc
&& feature_flag
& feature_caret
&& !(flags
& no_caret
))
141 location_caret (*loc
, stderr
);
147 /** Raise a complaint. That can be a fatal error, an error or just a
151 complains (const location
*loc
, warnings flags
, const char *message
,
154 severity s
= warning_severity (flags
);
155 if ((flags
& complaint
) && complaint_status
< status_complaint
)
156 complaint_status
= status_complaint
;
158 if (severity_warning
<= s
)
161 s
== severity_fatal
? _("fatal error")
162 : s
== severity_error
? _("error")
164 if (severity_error
<= s
&& ! complaint_status
)
165 complaint_status
= status_warning_as_error
;
166 error_message (loc
, flags
, prefix
, message
, args
);
174 complain (location
const *loc
, warnings flags
, const char *message
, ...)
177 va_start (args
, message
);
178 complains (loc
, flags
, message
, args
);
183 complain_indent (location
const *loc
, warnings flags
, unsigned *indent
,
184 const char *message
, ...)
188 va_start (args
, message
);
189 complains (loc
, flags
, message
, args
);
194 complain_args (location
const *loc
, warnings w
, unsigned *indent
,
195 int argc
, char *argv
[])
200 complain_indent (loc
, w
, indent
, "%s", _(argv
[0]));
203 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1]);
206 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1], argv
[2]);
209 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1], argv
[2], argv
[3]);
212 complain_indent (loc
, w
, indent
, _(argv
[0]), argv
[1], argv
[2], argv
[3],
216 complain (loc
, fatal
, "too many arguments for complains");
222 deprecated_directive (location
const *loc
, char const *old
, char const *upd
)
224 if (feature_flag
& feature_caret
)
225 complain (loc
, Wdeprecated
,
226 _("deprecated directive, use %s"),
229 complain (loc
, Wdeprecated
,
230 _("deprecated directive: %s, use %s"),
231 quote (old
), quote_n (1, upd
));