]>
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 | |
518e8830 AD |
32 | warnings warnings_flag = |
33 | Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother; | |
d0f11c1b | 34 | |
0ae6073a | 35 | bool complaint_issued; |
66381412 | 36 | static unsigned *indent_ptr = 0; |
a0f6b076 | 37 | |
b6403170 VS |
38 | void |
39 | warnings_print_categories (warnings warn_flags) | |
40 | { | |
41 | if (! (warn_flags & silent)) | |
42 | { | |
43 | char const *warn_names[] = | |
44 | { | |
45 | "midrule-values", | |
46 | "yacc", | |
47 | "conflicts-sr", | |
48 | "conflicts-rr", | |
518e8830 | 49 | "deprecated", |
b6403170 VS |
50 | "other" |
51 | }; | |
52 | ||
53 | bool any = false; | |
54 | int i; | |
55 | for (i = 0; i < ARRAY_CARDINALITY (warn_names); ++i) | |
56 | if (warn_flags & 1 << i) | |
57 | { | |
58 | fprintf (stderr, "%s-W%s", any ? ", " : " [", warn_names[i]); | |
59 | any = true; | |
60 | } | |
61 | if (any) | |
62 | fprintf (stderr, "]"); | |
63 | } | |
64 | } | |
a0f6b076 | 65 | |
23eb2a69 AD |
66 | /** Report an error message. |
67 | * | |
68 | * \param loc the location, defaulting to the current file, | |
69 | * or the program name. | |
6fb8b256 | 70 | * \param flags the category for this message. |
23eb2a69 | 71 | * \param prefix put before the message (e.g., "warning"). |
2bfcac9a JD |
72 | * \param message the error message, a printf format string. Iff it |
73 | * ends with ": ", then no trailing newline is printed, | |
74 | * and the caller should print the remaining | |
75 | * newline-terminated message to stderr. | |
23eb2a69 AD |
76 | * \param args the arguments of the format string. |
77 | */ | |
78 | static | |
e9955c83 | 79 | void |
6fb8b256 | 80 | error_message (const location *loc, warnings flags, const char *prefix, |
e9690142 | 81 | const char *message, va_list args) |
e9955c83 | 82 | { |
6fb8b256 | 83 | (void) flags; |
66381412 AR |
84 | unsigned pos = 0; |
85 | ||
23eb2a69 | 86 | if (loc) |
66381412 | 87 | pos += location_print (stderr, *loc); |
23eb2a69 | 88 | else |
66381412 AR |
89 | pos += fprintf(stderr, "%s", current_file ? current_file : program_name); |
90 | pos += fprintf(stderr, ": "); | |
91 | ||
92 | if (indent_ptr) | |
93 | { | |
94 | if (!*indent_ptr) | |
95 | *indent_ptr = pos; | |
96 | else if (*indent_ptr > pos) | |
97 | fprintf (stderr, "%*s", *indent_ptr - pos, ""); | |
98 | indent_ptr = 0; | |
99 | } | |
e9955c83 | 100 | |
23eb2a69 AD |
101 | if (prefix) |
102 | fprintf (stderr, "%s: ", prefix); | |
103 | ||
52489d44 | 104 | vfprintf (stderr, message, args); |
73370a9d | 105 | warnings_print_categories (flags); |
2bfcac9a JD |
106 | { |
107 | size_t l = strlen (message); | |
aed41cf9 AD |
108 | if (l < 2 || message[l-2] != ':' || message[l-1] != ' ') |
109 | { | |
110 | putc ('\n', stderr); | |
111 | fflush (stderr); | |
112 | } | |
2bfcac9a | 113 | } |
23eb2a69 AD |
114 | } |
115 | ||
6fb8b256 VS |
116 | /** Raise a complaint. That can be a fatal error, a complaint or just a |
117 | warning. */ | |
23eb2a69 | 118 | |
6fb8b256 VS |
119 | static inline void |
120 | complains (const location *loc, warnings flags, const char *message, | |
121 | va_list args) | |
89eb3c76 | 122 | { |
6fb8b256 | 123 | if (flags & complaint) |
89eb3c76 | 124 | { |
6fb8b256 | 125 | error_message (loc, complaint, NULL, message, args); |
89eb3c76 JD |
126 | complaint_issued = true; |
127 | } | |
6fb8b256 VS |
128 | else if (flags & fatal) |
129 | { | |
130 | error_message (loc, fatal, _("fatal error"), message, args); | |
131 | exit (EXIT_FAILURE); | |
132 | } | |
133 | else if (flags & Wyacc) | |
134 | { | |
135 | if (yacc_flag) | |
136 | { | |
137 | error_message (loc, flags, NULL, message, args); | |
138 | complaint_issued = true; | |
139 | } | |
140 | else if (warnings_flag & Wyacc) | |
141 | { | |
142 | set_warning_issued (); | |
143 | error_message (loc, flags, _("warning"), message, args); | |
144 | } | |
145 | } | |
5ff5cf67 | 146 | else if (warnings_flag & flags) |
6fb8b256 | 147 | { |
6fb8b256 VS |
148 | set_warning_issued (); |
149 | error_message (loc, flags, _("warning"), message, args); | |
150 | } | |
66381412 AR |
151 | } |
152 | ||
52489d44 | 153 | void |
6fb8b256 | 154 | complain (warnings flags, const char *message, ...) |
52489d44 | 155 | { |
6fb8b256 VS |
156 | va_list args; |
157 | va_start (args, message); | |
158 | complains (NULL, flags, message, args); | |
159 | va_end (args); | |
23eb2a69 | 160 | } |
52489d44 | 161 | |
e9955c83 | 162 | void |
6fb8b256 | 163 | complain_at (location loc, warnings flags, const char *message, ...) |
e9955c83 | 164 | { |
6fb8b256 VS |
165 | va_list args; |
166 | va_start (args, message); | |
167 | complains (&loc, flags, message, args); | |
168 | va_end (args); | |
52489d44 AD |
169 | } |
170 | ||
6fb8b256 VS |
171 | void complain_at_indent (location loc, warnings flags, unsigned *indent, |
172 | const char *message, ...) | |
66381412 AR |
173 | { |
174 | indent_ptr = indent; | |
66381412 | 175 | |
6fb8b256 VS |
176 | va_list args; |
177 | va_start (args, message); | |
178 | complains (&loc, flags, message, args); | |
179 | va_end (args); | |
e9955c83 | 180 | } |
23eb2a69 | 181 | |
6fb8b256 VS |
182 | /*--------------------------------. |
183 | | Report a warning, and proceed. | | |
184 | `--------------------------------*/ | |
4f646c37 AD |
185 | |
186 | void | |
6fb8b256 | 187 | set_warning_issued (void) |
4f646c37 | 188 | { |
6fb8b256 VS |
189 | static bool warning_issued = false; |
190 | if (!warning_issued && (warnings_flag & Werror)) | |
4f646c37 | 191 | { |
6fb8b256 | 192 | fprintf (stderr, "%s: warnings being treated as errors\n", program_name); |
4f646c37 AD |
193 | complaint_issued = true; |
194 | } | |
6fb8b256 | 195 | warning_issued = true; |
a0f6b076 | 196 | } |