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