]> git.saurik.com Git - bison.git/blame - src/complain.c
tests: don't abuse AT_BISON_CHECK.
[bison.git] / src / complain.c
CommitLineData
a0f6b076 1/* Declaration for error-reporting function for Bison.
2cec9080 2
663ce7bb 3 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2009
23eb2a69 4 Free Software 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").
41 * \param message the error message, a printf format string.
42 * \param args the arguments of the format string.
43 */
44static
e9955c83 45void
23eb2a69
AD
46error_message (location *loc,
47 const char *prefix,
48 const char *message, va_list args)
e9955c83 49{
348f5608
AR
50 unsigned pos = 0;
51
23eb2a69 52 if (loc)
348f5608 53 pos += location_print (stderr, *loc);
23eb2a69 54 else
348f5608
AR
55 pos += fprintf(stderr, "%s", current_file ? current_file : program_name);
56 pos += fprintf(stderr, ": ");
57
58 if (indent_ptr)
59 {
60 if (!*indent_ptr)
61 *indent_ptr = pos;
62 else if (*indent_ptr > pos)
63 fprintf (stderr, "%*s", *indent_ptr - pos, "");
64 indent_ptr = 0;
65 }
e9955c83 66
23eb2a69
AD
67 if (prefix)
68 fprintf (stderr, "%s: ", prefix);
69
52489d44
AD
70 vfprintf (stderr, message, args);
71 va_end (args);
52489d44 72 putc ('\n', stderr);
23eb2a69
AD
73 fflush (stderr);
74}
75
76/** Wrap error_message() with varargs handling. */
77#define ERROR_MESSAGE(Loc, Prefix, Message) \
78{ \
79 va_list args; \
80 va_start (args, Message); \
81 error_message (Loc, Prefix, Message, args); \
82}
83
84
85/*--------------------------------.
86| Report a warning, and proceed. |
87`--------------------------------*/
88
89eb3c76
JD
89static void
90set_warning_issued (void)
91{
92 static bool warning_issued = false;
93 if (!warning_issued && (warnings_flag & warnings_error))
94 {
95 fprintf (stderr, "%s: warnings being treated as errors\n", program_name);
96 complaint_issued = true;
97 }
98 warning_issued = true;
99}
100
23eb2a69
AD
101void
102warn_at (location loc, const char *message, ...)
103{
89eb3c76 104 set_warning_issued ();
23eb2a69 105 ERROR_MESSAGE (&loc, _("warning"), message);
52489d44
AD
106}
107
348f5608
AR
108void
109warn_at_indent (location loc, unsigned *indent,
110 const char *message, ...)
111{
112 set_warning_issued ();
113 indent_ptr = indent;
114 ERROR_MESSAGE (&loc, _("warning"), message);
115}
116
52489d44 117void
52489d44 118warn (const char *message, ...)
52489d44 119{
89eb3c76 120 set_warning_issued ();
23eb2a69
AD
121 ERROR_MESSAGE (NULL, _("warning"), message);
122}
52489d44 123
e9955c83 124
a0f6b076
AD
125/*-----------------------------------------------------------.
126| An error has occurred, but we can proceed, and die later. |
127`-----------------------------------------------------------*/
128
e9955c83 129void
41f83caf 130complain_at (location loc, const char *message, ...)
e9955c83 131{
23eb2a69 132 ERROR_MESSAGE (&loc, NULL, message);
0ae6073a 133 complaint_issued = true;
52489d44
AD
134}
135
348f5608
AR
136void
137complain_at_indent (location loc, unsigned *indent,
138 const char *message, ...)
139{
140 indent_ptr = indent;
141 ERROR_MESSAGE (&loc, NULL, message);
142 complaint_issued = true;
143}
144
52489d44 145void
52489d44 146complain (const char *message, ...)
52489d44 147{
23eb2a69 148 ERROR_MESSAGE (NULL, NULL, message);
0ae6073a 149 complaint_issued = true;
e9955c83 150}
23eb2a69
AD
151
152
663ce7bb
AD
153/*--------------------------------------------------------------.
154| An incompatibility with POSIX Yacc: mapped either to warn* or |
155| complain* depending on yacc_flag. |
156`--------------------------------------------------------------*/
157
158void
159yacc_at (location loc, const char *message, ...)
160{
161 if (yacc_flag)
162 {
163 ERROR_MESSAGE (&loc, NULL, message);
164 complaint_issued = true;
165 }
166 else if (warnings_flag & warnings_yacc)
167 {
168 set_warning_issued ();
169 ERROR_MESSAGE (&loc, _("warning"), message);
170 }
171}
172
173
a0f6b076
AD
174/*-------------------------------------------------.
175| A severe error has occurred, we cannot proceed. |
176`-------------------------------------------------*/
177
e9955c83 178void
41f83caf 179fatal_at (location loc, const char *message, ...)
e9955c83 180{
23eb2a69 181 ERROR_MESSAGE (&loc, _("fatal error"), message);
0ae6073a 182 exit (EXIT_FAILURE);
e9955c83
AD
183}
184
a0f6b076 185void
a0f6b076 186fatal (const char *message, ...)
a0f6b076 187{
23eb2a69 188 ERROR_MESSAGE (NULL, _("fatal error"), message);
0ae6073a 189 exit (EXIT_FAILURE);
a0f6b076 190}