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