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