]> git.saurik.com Git - bison.git/blame - src/complain.c
diagnostics: factor the list of warning names
[bison.git] / src / complain.c
CommitLineData
a0f6b076 1/* Declaration for error-reporting function for Bison.
2cec9080 2
7d6bad19 3 Copyright (C) 2000-2002, 2004-2006, 2009-2013 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"
1dc927a7 31#include "quote.h"
a0f6b076 32
518e8830 33warnings warnings_flag =
ec6dc437 34 Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother;
d0f11c1b 35
9503b0a4
TR
36warnings errors_flag;
37
697a8022 38err_status complaint_status = status_none;
66381412 39static unsigned *indent_ptr = 0;
a0f6b076 40
219458e2
AD
41/** Display a "[-Wyacc]" like message on \a f. */
42
43static void
44warnings_print_categories (warnings warn_flags, FILE *f)
b6403170 45{
219458e2
AD
46 /* Display only the first match, the second is "-Wall". */
47 int i;
48 for (i = 0; warnings_args[i]; ++i)
49 if (warn_flags & warnings_types[i])
50 {
51 bool err = warn_flags & errors_flag;
52 fprintf (f, " [-W%s%s]", err ? "error=" : "" , warnings_args[i]);
53 break;
54 }
b6403170 55}
a0f6b076 56
23eb2a69
AD
57/** Report an error message.
58 *
59 * \param loc the location, defaulting to the current file,
60 * or the program name.
6fb8b256 61 * \param flags the category for this message.
23eb2a69 62 * \param prefix put before the message (e.g., "warning").
2bfcac9a
JD
63 * \param message the error message, a printf format string. Iff it
64 * ends with ": ", then no trailing newline is printed,
65 * and the caller should print the remaining
66 * newline-terminated message to stderr.
23eb2a69
AD
67 * \param args the arguments of the format string.
68 */
69static
e9955c83 70void
6fb8b256 71error_message (const location *loc, warnings flags, const char *prefix,
e9690142 72 const char *message, va_list args)
e9955c83 73{
66381412
AR
74 unsigned pos = 0;
75
23eb2a69 76 if (loc)
b805eca7 77 pos += location_print (*loc, stderr);
23eb2a69 78 else
46b7d74c
TR
79 pos += fprintf (stderr, "%s", current_file ? current_file : program_name);
80 pos += fprintf (stderr, ": ");
66381412
AR
81
82 if (indent_ptr)
83 {
a686f6cd
TR
84 if (*indent_ptr)
85 prefix = NULL;
66381412
AR
86 if (!*indent_ptr)
87 *indent_ptr = pos;
88 else if (*indent_ptr > pos)
89 fprintf (stderr, "%*s", *indent_ptr - pos, "");
90 indent_ptr = 0;
91 }
e9955c83 92
23eb2a69
AD
93 if (prefix)
94 fprintf (stderr, "%s: ", prefix);
95
52489d44 96 vfprintf (stderr, message, args);
219458e2
AD
97 if (! (flags & silent))
98 warnings_print_categories (flags, stderr);
2bfcac9a
JD
99 {
100 size_t l = strlen (message);
3f5d1b2c 101 if (l < 2 || message[l - 2] != ':' || message[l - 1] != ' ')
aed41cf9
AD
102 {
103 putc ('\n', stderr);
104 fflush (stderr);
ea9e670d 105 if (loc && feature_flag & feature_caret && !(flags & no_caret))
b805eca7 106 location_caret (*loc, stderr);
aed41cf9 107 }
2bfcac9a 108 }
3f5d1b2c 109 fflush (stderr);
23eb2a69
AD
110}
111
6fb8b256
VS
112/** Raise a complaint. That can be a fatal error, a complaint or just a
113 warning. */
6fb8b256
VS
114static inline void
115complains (const location *loc, warnings flags, const char *message,
116 va_list args)
89eb3c76 117{
a686f6cd
TR
118 const char* prefix =
119 flags & fatal ? _("fatal error")
120 : flags & (errors_flag | complaint) ? _("error")
121 : _("warning");
122
697a8022
TR
123 if ((flags & complaint) && complaint_status < status_complaint)
124 complaint_status = status_complaint;
125 else if ((flags & (warnings_flag & errors_flag)) && ! complaint_status)
126 complaint_status = status_warning_as_error;
127 if (flags & (warnings_flag | fatal | complaint))
a686f6cd 128 error_message (loc, flags, prefix, message, args);
1048a1c9 129 if (flags & fatal)
a686f6cd 130 exit (EXIT_FAILURE);
66381412
AR
131}
132
52489d44 133void
b999409e 134complain (location const *loc, warnings flags, const char *message, ...)
52489d44 135{
6fb8b256
VS
136 va_list args;
137 va_start (args, message);
bb8e56ff 138 complains (loc, flags, message, args);
6fb8b256 139 va_end (args);
23eb2a69 140}
52489d44 141
e9955c83 142void
b999409e
TR
143complain_indent (location const *loc, warnings flags, unsigned *indent,
144 const char *message, ...)
e9955c83 145{
6fb8b256 146 va_list args;
66381412 147 indent_ptr = indent;
6fb8b256 148 va_start (args, message);
b999409e 149 complains (loc, flags, message, args);
6fb8b256 150 va_end (args);
e9955c83 151}
782e8187
TR
152
153void
c6c8de16
TR
154complain_args (location const *loc, warnings w, unsigned *indent,
155 int argc, char *argv[])
782e8187
TR
156{
157 switch (argc)
158 {
f60321dc
TR
159 case 1:
160 complain_indent (loc, w, indent, "%s", _(argv[0]));
161 break;
782e8187 162 case 2:
f60321dc 163 complain_indent (loc, w, indent, _(argv[0]), argv[1]);
782e8187
TR
164 break;
165 case 3:
f60321dc 166 complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2]);
782e8187
TR
167 break;
168 case 4:
f60321dc 169 complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3]);
782e8187
TR
170 break;
171 case 5:
f60321dc
TR
172 complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3],
173 argv[4]);
782e8187
TR
174 break;
175 default:
176 complain (loc, fatal, "too many arguments for complains");
177 break;
178 }
1dc927a7
AD
179
180}
181
182void
183deprecated_directive (location const *loc, char const *old, char const *upd)
184{
185 if (feature_flag & feature_caret)
186 complain (loc, Wdeprecated,
187 _("deprecated directive, use %s"),
188 quote_n (1, upd));
189 else
190 complain (loc, Wdeprecated,
191 _("deprecated directive: %s, use %s"),
192 quote (old), quote_n (1, upd));
782e8187 193}