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