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