]> git.saurik.com Git - bison.git/blob - src/complain.c
2bc276e95a4717fc6ff34e32d07ca289148432dc
[bison.git] / src / complain.c
1 /* Declaration for error-reporting function for Bison.
2
3 Copyright (C) 2000-2002, 2004-2006, 2009-2013 Free Software
4 Foundation, Inc.
5
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.
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
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 /* Based on error.c and error.h,
20 written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21
22 #include <config.h>
23 #include "system.h"
24
25 #include <stdarg.h>
26 #include <progname.h>
27
28 #include "complain.h"
29 #include "files.h"
30 #include "getargs.h"
31 #include "quote.h"
32
33 err_status complaint_status = status_none;
34
35 bool warnings_are_errors = false;
36 severity warnings_flag[warnings_size];
37
38 static unsigned *indent_ptr = 0;
39
40 void
41 complain_init (void)
42 {
43 warnings warnings_default =
44 Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother;
45
46 size_t b;
47 for (b = 0; b < warnings_size; ++b)
48 warnings_flag[b] = (1 << b & warnings_default
49 ? severity_warning
50 : severity_unset);
51 }
52
53 static severity
54 warning_severity (warnings flags)
55 {
56 if (flags & fatal)
57 return severity_fatal;
58 else if (flags & complaint)
59 return severity_error;
60 else
61 {
62 severity res = severity_disabled;
63 size_t b;
64 for (b = 0; b < warnings_size; ++b)
65 if (flags & 1 << b)
66 res = res < warnings_flag[b] ? warnings_flag[b] : res;
67 if (res == severity_warning && warnings_are_errors)
68 res = severity_error;
69 return res;
70 }
71 }
72
73
74 /** Display a "[-Wyacc]" like message on \a f. */
75
76 static void
77 warnings_print_categories (warnings warn_flags, FILE *f)
78 {
79 /* Display only the first match, the second is "-Wall". */
80 size_t i;
81 for (i = 0; warnings_args[i]; ++i)
82 if (warn_flags & warnings_types[i])
83 {
84 severity s = warning_severity (warnings_types[i]);
85 fprintf (f, " [-W%s%s]",
86 s == severity_error ? "error=" : "",
87 warnings_args[i]);
88 return;
89 }
90 }
91
92 /** Report an error message.
93 *
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.
103 */
104 static
105 void
106 error_message (const location *loc, warnings flags, const char *prefix,
107 const char *message, va_list args)
108 {
109 unsigned pos = 0;
110
111 if (loc)
112 pos += location_print (*loc, stderr);
113 else
114 pos += fprintf (stderr, "%s", current_file ? current_file : program_name);
115 pos += fprintf (stderr, ": ");
116
117 if (indent_ptr)
118 {
119 if (*indent_ptr)
120 prefix = NULL;
121 if (!*indent_ptr)
122 *indent_ptr = pos;
123 else if (*indent_ptr > pos)
124 fprintf (stderr, "%*s", *indent_ptr - pos, "");
125 indent_ptr = 0;
126 }
127
128 if (prefix)
129 fprintf (stderr, "%s: ", prefix);
130
131 vfprintf (stderr, message, args);
132 if (! (flags & silent))
133 warnings_print_categories (flags, stderr);
134 {
135 size_t l = strlen (message);
136 if (l < 2 || message[l - 2] != ':' || message[l - 1] != ' ')
137 {
138 putc ('\n', stderr);
139 fflush (stderr);
140 if (loc && feature_flag & feature_caret && !(flags & no_caret))
141 location_caret (*loc, stderr);
142 }
143 }
144 fflush (stderr);
145 }
146
147 /** Raise a complaint. That can be a fatal error, an error or just a
148 warning. */
149
150 static void
151 complains (const location *loc, warnings flags, const char *message,
152 va_list args)
153 {
154 severity s = warning_severity (flags);
155 if ((flags & complaint) && complaint_status < status_complaint)
156 complaint_status = status_complaint;
157
158 if (severity_warning <= s)
159 {
160 const char* prefix =
161 s == severity_fatal ? _("fatal error")
162 : s == severity_error ? _("error")
163 : _("warning");
164 if (severity_error <= s && ! complaint_status)
165 complaint_status = status_warning_as_error;
166 error_message (loc, flags, prefix, message, args);
167 }
168
169 if (flags & fatal)
170 exit (EXIT_FAILURE);
171 }
172
173 void
174 complain (location const *loc, warnings flags, const char *message, ...)
175 {
176 va_list args;
177 va_start (args, message);
178 complains (loc, flags, message, args);
179 va_end (args);
180 }
181
182 void
183 complain_indent (location const *loc, warnings flags, unsigned *indent,
184 const char *message, ...)
185 {
186 va_list args;
187 indent_ptr = indent;
188 va_start (args, message);
189 complains (loc, flags, message, args);
190 va_end (args);
191 }
192
193 void
194 complain_args (location const *loc, warnings w, unsigned *indent,
195 int argc, char *argv[])
196 {
197 switch (argc)
198 {
199 case 1:
200 complain_indent (loc, w, indent, "%s", _(argv[0]));
201 break;
202 case 2:
203 complain_indent (loc, w, indent, _(argv[0]), argv[1]);
204 break;
205 case 3:
206 complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2]);
207 break;
208 case 4:
209 complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3]);
210 break;
211 case 5:
212 complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3],
213 argv[4]);
214 break;
215 default:
216 complain (loc, fatal, "too many arguments for complains");
217 break;
218 }
219 }
220
221 void
222 deprecated_directive (location const *loc, char const *old, char const *upd)
223 {
224 if (feature_flag & feature_caret)
225 complain (loc, Wdeprecated,
226 _("deprecated directive, use %s"),
227 quote_n (1, upd));
228 else
229 complain (loc, Wdeprecated,
230 _("deprecated directive: %s, use %s"),
231 quote (old), quote_n (1, upd));
232 }