]> git.saurik.com Git - bison.git/blame - src/complain.c
errors: pointerize complain_at_indent
[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;
d0f11c1b 34
9503b0a4
TR
35warnings errors_flag;
36
0ae6073a 37bool complaint_issued;
66381412 38static unsigned *indent_ptr = 0;
a0f6b076 39
b6403170
VS
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",
518e8830 51 "deprecated",
b6403170
VS
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 {
1048a1c9
AD
60 bool err = warn_flags & errors_flag;
61 fprintf (stderr, "%s-W", any ? ", " : " [");
62 fprintf (stderr, "%s%s", err ? "error=" : "" , warn_names[i]);
b6403170
VS
63 any = true;
64 }
65 if (any)
66 fprintf (stderr, "]");
67 }
68}
a0f6b076 69
23eb2a69
AD
70/** Report an error message.
71 *
72 * \param loc the location, defaulting to the current file,
73 * or the program name.
6fb8b256 74 * \param flags the category for this message.
23eb2a69 75 * \param prefix put before the message (e.g., "warning").
2bfcac9a
JD
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.
23eb2a69
AD
80 * \param args the arguments of the format string.
81 */
82static
e9955c83 83void
6fb8b256 84error_message (const location *loc, warnings flags, const char *prefix,
e9690142 85 const char *message, va_list args)
e9955c83 86{
66381412
AR
87 unsigned pos = 0;
88
23eb2a69 89 if (loc)
66381412 90 pos += location_print (stderr, *loc);
23eb2a69 91 else
46b7d74c
TR
92 pos += fprintf (stderr, "%s", current_file ? current_file : program_name);
93 pos += fprintf (stderr, ": ");
66381412
AR
94
95 if (indent_ptr)
96 {
a686f6cd
TR
97 if (*indent_ptr)
98 prefix = NULL;
66381412
AR
99 if (!*indent_ptr)
100 *indent_ptr = pos;
101 else if (*indent_ptr > pos)
102 fprintf (stderr, "%*s", *indent_ptr - pos, "");
103 indent_ptr = 0;
104 }
e9955c83 105
23eb2a69
AD
106 if (prefix)
107 fprintf (stderr, "%s: ", prefix);
108
52489d44 109 vfprintf (stderr, message, args);
73370a9d 110 warnings_print_categories (flags);
2bfcac9a
JD
111 {
112 size_t l = strlen (message);
aed41cf9
AD
113 if (l < 2 || message[l-2] != ':' || message[l-1] != ' ')
114 {
115 putc ('\n', stderr);
116 fflush (stderr);
117 }
2bfcac9a 118 }
23eb2a69
AD
119}
120
6fb8b256
VS
121/** Raise a complaint. That can be a fatal error, a complaint or just a
122 warning. */
23eb2a69 123
6fb8b256
VS
124static inline void
125complains (const location *loc, warnings flags, const char *message,
126 va_list args)
1048a1c9 127
89eb3c76 128{
a686f6cd
TR
129 const char* prefix =
130 flags & fatal ? _("fatal error")
131 : flags & (errors_flag | complaint) ? _("error")
132 : _("warning");
133
134 complaint_issued |= flags & (complaint | errors_flag);
135 if (flags & (warnings_flag | silent | fatal | complaint))
136 error_message (loc, flags, prefix, message, args);
1048a1c9 137 if (flags & fatal)
a686f6cd 138 exit (EXIT_FAILURE);
66381412
AR
139}
140
52489d44 141void
b999409e 142complain (location const *loc, warnings flags, const char *message, ...)
52489d44 143{
6fb8b256
VS
144 va_list args;
145 va_start (args, message);
bb8e56ff 146 complains (loc, flags, message, args);
6fb8b256 147 va_end (args);
23eb2a69 148}
52489d44 149
e9955c83 150void
b999409e
TR
151complain_indent (location const *loc, warnings flags, unsigned *indent,
152 const char *message, ...)
e9955c83 153{
6fb8b256 154 va_list args;
66381412 155 indent_ptr = indent;
6fb8b256 156 va_start (args, message);
b999409e 157 complains (loc, flags, message, args);
6fb8b256 158 va_end (args);
e9955c83 159}
782e8187
TR
160
161void
162complain_args (location const *loc, warnings w, int argc, char *argv[])
163{
164 switch (argc)
165 {
166 case 2:
167 complain (loc, w, "%s", _(argv[1]));
168 break;
169 case 3:
170 complain (loc, w, _(argv[1]), argv[2]);
171 break;
172 case 4:
173 complain (loc, w, _(argv[1]), argv[2], argv[3]);
174 break;
175 case 5:
176 complain (loc, w, _(argv[1]), argv[2], argv[3], argv[4]);
177 break;
178 case 6:
179 complain (loc, w, _(argv[1]), argv[2], argv[3], argv[4], argv[5]);
180 break;
181 default:
182 complain (loc, fatal, "too many arguments for complains");
183 break;
184 }
185}