]> git.saurik.com Git - bison.git/blame - src/complain.c
errors: introduce the -Werror=CATEGORY option
[bison.git] / src / complain.c
CommitLineData
a0f6b076 1/* Declaration for error-reporting function for Bison.
2cec9080 2
34136e65 3 Copyright (C) 2000-2002, 2004-2006, 2009-2012 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
21184140 25#include <stdarg.h>
4d699f44 26#include <progname.h>
a0f6b076 27
a0f6b076 28#include "complain.h"
4b68955b 29#include "files.h"
89eb3c76 30#include "getargs.h"
a0f6b076 31
518e8830
AD
32warnings warnings_flag =
33 Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother;
d0f11c1b 34
9503b0a4
TR
35warnings errors_flag;
36
0ae6073a 37bool complaint_issued;
66381412 38static unsigned *indent_ptr = 0;
a0f6b076 39
b6403170
VS
40void
41warnings_print_categories (warnings warn_flags)
42{
43 if (! (warn_flags & silent))
44 {
45 char const *warn_names[] =
46 {
47 "midrule-values",
48 "yacc",
49 "conflicts-sr",
50 "conflicts-rr",
518e8830 51 "deprecated",
b6403170
VS
52 "other"
53 };
54
55 bool any = false;
56 int i;
57 for (i = 0; i < ARRAY_CARDINALITY (warn_names); ++i)
58 if (warn_flags & 1 << i)
59 {
0d43e605 60 fprintf (stderr, "%s-W%s", any ? ", " : " [", warn_names[i]);
b6403170
VS
61 any = true;
62 }
63 if (any)
64 fprintf (stderr, "]");
65 }
66}
a0f6b076 67
23eb2a69
AD
68/** Report an error message.
69 *
70 * \param loc the location, defaulting to the current file,
71 * or the program name.
6fb8b256 72 * \param flags the category for this message.
23eb2a69 73 * \param prefix put before the message (e.g., "warning").
2bfcac9a
JD
74 * \param message the error message, a printf format string. Iff it
75 * ends with ": ", then no trailing newline is printed,
76 * and the caller should print the remaining
77 * newline-terminated message to stderr.
23eb2a69
AD
78 * \param args the arguments of the format string.
79 */
80static
e9955c83 81void
6fb8b256 82error_message (const location *loc, warnings flags, const char *prefix,
e9690142 83 const char *message, va_list args)
e9955c83 84{
6fb8b256 85 (void) flags;
66381412
AR
86 unsigned pos = 0;
87
23eb2a69 88 if (loc)
66381412 89 pos += location_print (stderr, *loc);
23eb2a69 90 else
66381412
AR
91 pos += fprintf(stderr, "%s", current_file ? current_file : program_name);
92 pos += fprintf(stderr, ": ");
93
94 if (indent_ptr)
95 {
96 if (!*indent_ptr)
97 *indent_ptr = pos;
98 else if (*indent_ptr > pos)
99 fprintf (stderr, "%*s", *indent_ptr - pos, "");
100 indent_ptr = 0;
101 }
e9955c83 102
23eb2a69
AD
103 if (prefix)
104 fprintf (stderr, "%s: ", prefix);
105
52489d44 106 vfprintf (stderr, message, args);
73370a9d 107 warnings_print_categories (flags);
2bfcac9a
JD
108 {
109 size_t l = strlen (message);
aed41cf9
AD
110 if (l < 2 || message[l-2] != ':' || message[l-1] != ' ')
111 {
112 putc ('\n', stderr);
113 fflush (stderr);
114 }
2bfcac9a 115 }
23eb2a69
AD
116}
117
6fb8b256
VS
118/** Raise a complaint. That can be a fatal error, a complaint or just a
119 warning. */
23eb2a69 120
6fb8b256
VS
121static inline void
122complains (const location *loc, warnings flags, const char *message,
123 va_list args)
89eb3c76 124{
6fb8b256 125 if (flags & complaint)
89eb3c76 126 {
11b19212
AD
127 error_message (loc, complaint,
128 indent_ptr && *indent_ptr ? NULL : _("error"),
129 message, args);
89eb3c76
JD
130 complaint_issued = true;
131 }
6fb8b256
VS
132 else if (flags & fatal)
133 {
134 error_message (loc, fatal, _("fatal error"), message, args);
135 exit (EXIT_FAILURE);
136 }
137 else if (flags & Wyacc)
138 {
139 if (yacc_flag)
140 {
141 error_message (loc, flags, NULL, message, args);
142 complaint_issued = true;
143 }
144 else if (warnings_flag & Wyacc)
145 {
9503b0a4 146 set_warning_issued (Wyacc);
11b19212
AD
147 error_message (loc, flags,
148 indent_ptr && *indent_ptr ? NULL : _("warning"),
149 message, args);
6fb8b256
VS
150 }
151 }
5ff5cf67 152 else if (warnings_flag & flags)
6fb8b256 153 {
9503b0a4 154 set_warning_issued (flags);
11b19212
AD
155 error_message (loc, flags,
156 indent_ptr && *indent_ptr ? NULL : _("warning"),
157 message, args);
6fb8b256 158 }
66381412
AR
159}
160
52489d44 161void
6fb8b256 162complain (warnings flags, const char *message, ...)
52489d44 163{
6fb8b256
VS
164 va_list args;
165 va_start (args, message);
166 complains (NULL, flags, message, args);
167 va_end (args);
23eb2a69 168}
52489d44 169
e9955c83 170void
6fb8b256 171complain_at (location loc, warnings flags, const char *message, ...)
e9955c83 172{
6fb8b256
VS
173 va_list args;
174 va_start (args, message);
175 complains (&loc, flags, message, args);
176 va_end (args);
52489d44
AD
177}
178
6fb8b256
VS
179void complain_at_indent (location loc, warnings flags, unsigned *indent,
180 const char *message, ...)
66381412
AR
181{
182 indent_ptr = indent;
66381412 183
6fb8b256
VS
184 va_list args;
185 va_start (args, message);
186 complains (&loc, flags, message, args);
187 va_end (args);
e9955c83 188}
23eb2a69
AD
189
190
6fb8b256
VS
191/*--------------------------------.
192| Report a warning, and proceed. |
193`--------------------------------*/
4f646c37
AD
194
195void
9503b0a4 196set_warning_issued (warnings warning)
4f646c37 197{
6fb8b256 198 static bool warning_issued = false;
9503b0a4 199 if (!warning_issued && (warning & warnings_flag & errors_flag))
4f646c37 200 {
6fb8b256 201 fprintf (stderr, "%s: warnings being treated as errors\n", program_name);
4f646c37
AD
202 complaint_issued = true;
203 }
6fb8b256 204 warning_issued = true;
a0f6b076 205}