]> git.saurik.com Git - bison.git/blame_incremental - src/complain.c
warnings: organize variadic complaints call
[bison.git] / src / complain.c
... / ...
CommitLineData
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
32warnings warnings_flag =
33 Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother;
34
35warnings errors_flag;
36
37bool complaint_issued;
38static unsigned *indent_ptr = 0;
39
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",
51 "deprecated",
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 {
60 bool err = warn_flags & errors_flag;
61 fprintf (stderr, "%s-W", any ? ", " : " [");
62 fprintf (stderr, "%s%s", err ? "error=" : "" , warn_names[i]);
63 any = true;
64 }
65 if (any)
66 fprintf (stderr, "]");
67 }
68}
69
70/** Report an error message.
71 *
72 * \param loc the location, defaulting to the current file,
73 * or the program name.
74 * \param flags the category for this message.
75 * \param prefix put before the message (e.g., "warning").
76 * \param message the error message, a printf format string. Iff it
77 * ends with ": ", then no trailing newline is printed,
78 * and the caller should print the remaining
79 * newline-terminated message to stderr.
80 * \param args the arguments of the format string.
81 */
82static
83void
84error_message (const location *loc, warnings flags, const char *prefix,
85 const char *message, va_list args)
86{
87 (void) flags;
88 unsigned pos = 0;
89
90 if (loc)
91 pos += location_print (stderr, *loc);
92 else
93 pos += fprintf(stderr, "%s", current_file ? current_file : program_name);
94 pos += fprintf(stderr, ": ");
95
96 if (indent_ptr)
97 {
98 if (!*indent_ptr)
99 *indent_ptr = pos;
100 else if (*indent_ptr > pos)
101 fprintf (stderr, "%*s", *indent_ptr - pos, "");
102 indent_ptr = 0;
103 }
104
105 if (prefix)
106 fprintf (stderr, "%s: ", prefix);
107
108 vfprintf (stderr, message, args);
109 warnings_print_categories (flags);
110 {
111 size_t l = strlen (message);
112 if (l < 2 || message[l-2] != ':' || message[l-1] != ' ')
113 {
114 putc ('\n', stderr);
115 fflush (stderr);
116 }
117 }
118}
119
120/** Raise a complaint. That can be a fatal error, a complaint or just a
121 warning. */
122
123static inline void
124complains (const location *loc, warnings flags, const char *message,
125 va_list args)
126
127{
128 if (flags & fatal)
129 {
130 error_message (loc, fatal, _("fatal error"), message, args);
131 exit (EXIT_FAILURE);
132 }
133 else if (flags & (complaint | warnings_flag | silent))
134 {
135 const char* prefix =
136 flags & (errors_flag | complaint) ? _("error") : _("warning");
137 if (flags & (complaint | errors_flag))
138 complaint_issued = true;
139 error_message (loc, flags,
140 indent_ptr && *indent_ptr ? NULL : prefix,
141 message, args);
142 }
143}
144
145void
146complain (location const* loc, warnings flags, const char *message, ...)
147{
148 va_list args;
149 va_start (args, message);
150 complains (loc, flags, message, args);
151 va_end (args);
152}
153
154void
155complain_at_indent (location loc, warnings flags, unsigned *indent,
156 const char *message, ...)
157{
158 va_list args;
159 indent_ptr = indent;
160 va_start (args, message);
161 complains (&loc, flags, message, args);
162 va_end (args);
163}
164
165void
166complain_args (location const *loc, warnings w, int argc, char *argv[])
167{
168 switch (argc)
169 {
170 case 2:
171 complain (loc, w, "%s", _(argv[1]));
172 break;
173 case 3:
174 complain (loc, w, _(argv[1]), argv[2]);
175 break;
176 case 4:
177 complain (loc, w, _(argv[1]), argv[2], argv[3]);
178 break;
179 case 5:
180 complain (loc, w, _(argv[1]), argv[2], argv[3], argv[4]);
181 break;
182 case 6:
183 complain (loc, w, _(argv[1]), argv[2], argv[3], argv[4], argv[5]);
184 break;
185 default:
186 complain (loc, fatal, "too many arguments for complains");
187 break;
188 }
189}