]> git.saurik.com Git - bison.git/blob - src/complain.c
doc: fix some invalid @ref.
[bison.git] / src / complain.c
1 /* Declaration for error-reporting function for Bison.
2
3 Copyright (C) 2000-2002, 2004-2006, 2009-2012 Free Software
4 Foundation, Inc.
5
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.
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
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 /* Based on error.c and error.h,
20 written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21
22 #include <config.h>
23 #include "system.h"
24
25 #include <stdarg.h>
26 #include <progname.h>
27
28 #include "complain.h"
29 #include "files.h"
30 #include "getargs.h"
31
32 bool complaint_issued;
33 static unsigned *indent_ptr = 0;
34
35 \f
36
37 /** Report an error message.
38 *
39 * \param loc the location, defaulting to the current file,
40 * or the program name.
41 * \param prefix put before the message (e.g., "warning").
42 * \param message the error message, a printf format string. Iff it
43 * ends with ": ", then no trailing newline is printed,
44 * and the caller should print the remaining
45 * newline-terminated message to stderr.
46 * \param args the arguments of the format string.
47 */
48 static
49 void
50 error_message (location *loc,
51 const char *prefix,
52 const char *message, va_list args)
53 {
54 unsigned pos = 0;
55
56 if (loc)
57 pos += location_print (stderr, *loc);
58 else
59 pos += fprintf(stderr, "%s", current_file ? current_file : program_name);
60 pos += fprintf(stderr, ": ");
61
62 if (indent_ptr)
63 {
64 if (!*indent_ptr)
65 *indent_ptr = pos;
66 else if (*indent_ptr > pos)
67 fprintf (stderr, "%*s", *indent_ptr - pos, "");
68 indent_ptr = 0;
69 }
70
71 if (prefix)
72 fprintf (stderr, "%s: ", prefix);
73
74 vfprintf (stderr, message, args);
75 {
76 size_t l = strlen (message);
77 if (l < 2 || message[l-2] != ':' || message[l-1] != ' ') {
78 putc ('\n', stderr);
79 fflush (stderr);
80 }
81 }
82 }
83
84 /** Wrap error_message() with varargs handling. */
85 #define ERROR_MESSAGE(Loc, Prefix, Message) \
86 { \
87 va_list args; \
88 va_start (args, Message); \
89 error_message (Loc, Prefix, Message, args); \
90 va_end (args); \
91 }
92
93
94 /*--------------------------------.
95 | Report a warning, and proceed. |
96 `--------------------------------*/
97
98 void
99 set_warning_issued (void)
100 {
101 static bool warning_issued = false;
102 if (!warning_issued && (warnings_flag & warnings_error))
103 {
104 fprintf (stderr, "%s: warnings being treated as errors\n", program_name);
105 complaint_issued = true;
106 }
107 warning_issued = true;
108 }
109
110 void
111 warn_at (location loc, const char *message, ...)
112 {
113 if (!(warnings_flag & warnings_other))
114 return;
115 set_warning_issued ();
116 ERROR_MESSAGE (&loc, _("warning"), message);
117 }
118
119 void
120 warn_at_indent (location loc, unsigned *indent,
121 const char *message, ...)
122 {
123 if (!(warnings_flag & warnings_other))
124 return;
125 set_warning_issued ();
126 indent_ptr = indent;
127 ERROR_MESSAGE (&loc, _("warning"), message);
128 }
129
130 void
131 warn (const char *message, ...)
132 {
133 if (!(warnings_flag & warnings_other))
134 return;
135 set_warning_issued ();
136 ERROR_MESSAGE (NULL, _("warning"), message);
137 }
138
139
140 /*-----------------------------------------------------------.
141 | An error has occurred, but we can proceed, and die later. |
142 `-----------------------------------------------------------*/
143
144 void
145 complain_at (location loc, const char *message, ...)
146 {
147 ERROR_MESSAGE (&loc, NULL, message);
148 complaint_issued = true;
149 }
150
151 void
152 complain_at_indent (location loc, unsigned *indent,
153 const char *message, ...)
154 {
155 indent_ptr = indent;
156 ERROR_MESSAGE (&loc, NULL, message);
157 complaint_issued = true;
158 }
159
160 void
161 complain (const char *message, ...)
162 {
163 ERROR_MESSAGE (NULL, NULL, message);
164 complaint_issued = true;
165 }
166
167
168 /*--------------------------------------------------------------.
169 | An incompatibility with POSIX Yacc: mapped either to warn* or |
170 | complain* depending on yacc_flag. |
171 `--------------------------------------------------------------*/
172
173 void
174 yacc_at (location loc, const char *message, ...)
175 {
176 if (yacc_flag)
177 {
178 ERROR_MESSAGE (&loc, NULL, message);
179 complaint_issued = true;
180 }
181 else if (warnings_flag & warnings_yacc)
182 {
183 set_warning_issued ();
184 ERROR_MESSAGE (&loc, _("warning"), message);
185 }
186 }
187
188 void
189 midrule_value_at (location loc, const char *message, ...)
190 {
191 if (!(warnings_flag & warnings_midrule_values))
192 return;
193 set_warning_issued ();
194 ERROR_MESSAGE (&loc, _("warning"), message);
195 }
196
197 /*-------------------------------------------------.
198 | A severe error has occurred, we cannot proceed. |
199 `-------------------------------------------------*/
200
201 void
202 fatal_at (location loc, const char *message, ...)
203 {
204 ERROR_MESSAGE (&loc, _("fatal error"), message);
205 exit (EXIT_FAILURE);
206 }
207
208 void
209 fatal (const char *message, ...)
210 {
211 ERROR_MESSAGE (NULL, _("fatal error"), message);
212 exit (EXIT_FAILURE);
213 }