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