]>
Commit | Line | Data |
---|---|---|
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 | |
0ae6073a | 32 | bool complaint_issued; |
66381412 | 33 | static unsigned *indent_ptr = 0; |
a0f6b076 | 34 | |
a0f6b076 | 35 | \f |
a0f6b076 | 36 | |
23eb2a69 AD |
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"). | |
2bfcac9a JD |
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. | |
23eb2a69 AD |
46 | * \param args the arguments of the format string. |
47 | */ | |
48 | static | |
e9955c83 | 49 | void |
23eb2a69 | 50 | error_message (location *loc, |
e9690142 JD |
51 | const char *prefix, |
52 | const char *message, va_list args) | |
e9955c83 | 53 | { |
66381412 AR |
54 | unsigned pos = 0; |
55 | ||
23eb2a69 | 56 | if (loc) |
66381412 | 57 | pos += location_print (stderr, *loc); |
23eb2a69 | 58 | else |
66381412 AR |
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 | } | |
e9955c83 | 70 | |
23eb2a69 AD |
71 | if (prefix) |
72 | fprintf (stderr, "%s: ", prefix); | |
73 | ||
52489d44 | 74 | vfprintf (stderr, message, args); |
2bfcac9a JD |
75 | { |
76 | size_t l = strlen (message); | |
aed41cf9 AD |
77 | if (l < 2 || message[l-2] != ':' || message[l-1] != ' ') |
78 | { | |
79 | putc ('\n', stderr); | |
80 | fflush (stderr); | |
81 | } | |
2bfcac9a | 82 | } |
23eb2a69 AD |
83 | } |
84 | ||
85 | /** Wrap error_message() with varargs handling. */ | |
e9690142 JD |
86 | #define ERROR_MESSAGE(Loc, Prefix, Message) \ |
87 | { \ | |
88 | va_list args; \ | |
89 | va_start (args, Message); \ | |
90 | error_message (Loc, Prefix, Message, args); \ | |
91 | va_end (args); \ | |
23eb2a69 AD |
92 | } |
93 | ||
94 | ||
95 | /*--------------------------------. | |
96 | | Report a warning, and proceed. | | |
97 | `--------------------------------*/ | |
98 | ||
786743d5 | 99 | void |
89eb3c76 JD |
100 | set_warning_issued (void) |
101 | { | |
102 | static bool warning_issued = false; | |
103 | if (!warning_issued && (warnings_flag & warnings_error)) | |
104 | { | |
105 | fprintf (stderr, "%s: warnings being treated as errors\n", program_name); | |
106 | complaint_issued = true; | |
107 | } | |
108 | warning_issued = true; | |
109 | } | |
110 | ||
23eb2a69 AD |
111 | void |
112 | warn_at (location loc, const char *message, ...) | |
113 | { | |
c39014ae JD |
114 | if (!(warnings_flag & warnings_other)) |
115 | return; | |
89eb3c76 | 116 | set_warning_issued (); |
23eb2a69 | 117 | ERROR_MESSAGE (&loc, _("warning"), message); |
52489d44 AD |
118 | } |
119 | ||
66381412 AR |
120 | void |
121 | warn_at_indent (location loc, unsigned *indent, | |
122 | const char *message, ...) | |
123 | { | |
c39014ae JD |
124 | if (!(warnings_flag & warnings_other)) |
125 | return; | |
66381412 AR |
126 | set_warning_issued (); |
127 | indent_ptr = indent; | |
128 | ERROR_MESSAGE (&loc, _("warning"), message); | |
129 | } | |
130 | ||
52489d44 | 131 | void |
52489d44 | 132 | warn (const char *message, ...) |
52489d44 | 133 | { |
c39014ae JD |
134 | if (!(warnings_flag & warnings_other)) |
135 | return; | |
89eb3c76 | 136 | set_warning_issued (); |
23eb2a69 AD |
137 | ERROR_MESSAGE (NULL, _("warning"), message); |
138 | } | |
52489d44 | 139 | |
e9955c83 | 140 | |
a0f6b076 AD |
141 | /*-----------------------------------------------------------. |
142 | | An error has occurred, but we can proceed, and die later. | | |
143 | `-----------------------------------------------------------*/ | |
144 | ||
e9955c83 | 145 | void |
41f83caf | 146 | complain_at (location loc, const char *message, ...) |
e9955c83 | 147 | { |
23eb2a69 | 148 | ERROR_MESSAGE (&loc, NULL, message); |
0ae6073a | 149 | complaint_issued = true; |
52489d44 AD |
150 | } |
151 | ||
66381412 AR |
152 | void |
153 | complain_at_indent (location loc, unsigned *indent, | |
154 | const char *message, ...) | |
155 | { | |
156 | indent_ptr = indent; | |
157 | ERROR_MESSAGE (&loc, NULL, message); | |
158 | complaint_issued = true; | |
159 | } | |
160 | ||
52489d44 | 161 | void |
52489d44 | 162 | complain (const char *message, ...) |
52489d44 | 163 | { |
23eb2a69 | 164 | ERROR_MESSAGE (NULL, NULL, message); |
0ae6073a | 165 | complaint_issued = true; |
e9955c83 | 166 | } |
23eb2a69 AD |
167 | |
168 | ||
4f646c37 AD |
169 | /*--------------------------------------------------------------. |
170 | | An incompatibility with POSIX Yacc: mapped either to warn* or | | |
171 | | complain* depending on yacc_flag. | | |
172 | `--------------------------------------------------------------*/ | |
173 | ||
174 | void | |
175 | yacc_at (location loc, const char *message, ...) | |
176 | { | |
177 | if (yacc_flag) | |
178 | { | |
179 | ERROR_MESSAGE (&loc, NULL, message); | |
180 | complaint_issued = true; | |
181 | } | |
182 | else if (warnings_flag & warnings_yacc) | |
183 | { | |
184 | set_warning_issued (); | |
185 | ERROR_MESSAGE (&loc, _("warning"), message); | |
186 | } | |
187 | } | |
188 | ||
c39014ae JD |
189 | void |
190 | midrule_value_at (location loc, const char *message, ...) | |
191 | { | |
192 | if (!(warnings_flag & warnings_midrule_values)) | |
193 | return; | |
194 | set_warning_issued (); | |
195 | ERROR_MESSAGE (&loc, _("warning"), message); | |
196 | } | |
4f646c37 | 197 | |
a0f6b076 AD |
198 | /*-------------------------------------------------. |
199 | | A severe error has occurred, we cannot proceed. | | |
200 | `-------------------------------------------------*/ | |
201 | ||
e9955c83 | 202 | void |
41f83caf | 203 | fatal_at (location loc, const char *message, ...) |
e9955c83 | 204 | { |
23eb2a69 | 205 | ERROR_MESSAGE (&loc, _("fatal error"), message); |
0ae6073a | 206 | exit (EXIT_FAILURE); |
e9955c83 AD |
207 | } |
208 | ||
a0f6b076 | 209 | void |
a0f6b076 | 210 | fatal (const char *message, ...) |
a0f6b076 | 211 | { |
23eb2a69 | 212 | ERROR_MESSAGE (NULL, _("fatal error"), message); |
0ae6073a | 213 | exit (EXIT_FAILURE); |
a0f6b076 | 214 | } |