]> git.saurik.com Git - bison.git/blob - src/complain.c
a84718bef001b66c77b6ac8c1c580eec4dd1201a
[bison.git] / src / complain.c
1 /* Declaration for error-reporting function for Bison.
2
3 Copyright (C) 2000-2002, 2004-2006, 2009-2012 Free Software
4 Foundation, Inc.
5
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.
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
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 /* Based on error.c and error.h,
20 written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21
22 #include <config.h>
23 #include "system.h"
24
25 #include <stdarg.h>
26 #include <progname.h>
27
28 #include "complain.h"
29 #include "files.h"
30 #include "getargs.h"
31
32 warnings warnings_flag = Wconflicts_sr | Wconflicts_rr | Wother;
33
34 bool complaint_issued;
35 static unsigned *indent_ptr = 0;
36
37 \f
38
39 /** Report an error message.
40 *
41 * \param loc the location, defaulting to the current file,
42 * or the program name.
43 * \param flags the category for this message.
44 * \param prefix put before the message (e.g., "warning").
45 * \param message the error message, a printf format string. Iff it
46 * ends with ": ", then no trailing newline is printed,
47 * and the caller should print the remaining
48 * newline-terminated message to stderr.
49 * \param args the arguments of the format string.
50 */
51 static
52 void
53 error_message (const location *loc, warnings flags, const char *prefix,
54 const char *message, va_list args)
55 {
56 (void) flags;
57 unsigned pos = 0;
58
59 if (loc)
60 pos += location_print (stderr, *loc);
61 else
62 pos += fprintf(stderr, "%s", current_file ? current_file : program_name);
63 pos += fprintf(stderr, ": ");
64
65 if (indent_ptr)
66 {
67 if (!*indent_ptr)
68 *indent_ptr = pos;
69 else if (*indent_ptr > pos)
70 fprintf (stderr, "%*s", *indent_ptr - pos, "");
71 indent_ptr = 0;
72 }
73
74 if (prefix)
75 fprintf (stderr, "%s: ", prefix);
76
77 vfprintf (stderr, message, args);
78 {
79 size_t l = strlen (message);
80 if (l < 2 || message[l-2] != ':' || message[l-1] != ' ')
81 {
82 putc ('\n', stderr);
83 fflush (stderr);
84 }
85 }
86 }
87
88 /** Raise a complaint. That can be a fatal error, a complaint or just a
89 warning. */
90
91 static inline void
92 complains (const location *loc, warnings flags, const char *message,
93 va_list args)
94 {
95 if (flags & complaint)
96 {
97 error_message (loc, complaint, NULL, message, args);
98 complaint_issued = true;
99 }
100 else if (flags & fatal)
101 {
102 error_message (loc, fatal, _("fatal error"), message, args);
103 exit (EXIT_FAILURE);
104 }
105 else if (flags & Wyacc)
106 {
107 if (yacc_flag)
108 {
109 error_message (loc, flags, NULL, message, args);
110 complaint_issued = true;
111 }
112 else if (warnings_flag & Wyacc)
113 {
114 set_warning_issued ();
115 error_message (loc, flags, _("warning"), message, args);
116 }
117 }
118 else if (warnings_flag & flags)
119 {
120 set_warning_issued ();
121 error_message (loc, flags, _("warning"), message, args);
122 }
123 }
124
125 void
126 complain (warnings flags, const char *message, ...)
127 {
128 va_list args;
129 va_start (args, message);
130 complains (NULL, flags, message, args);
131 va_end (args);
132 }
133
134 void
135 complain_at (location loc, warnings flags, const char *message, ...)
136 {
137 va_list args;
138 va_start (args, message);
139 complains (&loc, flags, message, args);
140 va_end (args);
141 }
142
143 void complain_at_indent (location loc, warnings flags, unsigned *indent,
144 const char *message, ...)
145 {
146 indent_ptr = indent;
147
148 va_list args;
149 va_start (args, message);
150 complains (&loc, flags, message, args);
151 va_end (args);
152 }
153
154 /*--------------------------------.
155 | Report a warning, and proceed. |
156 `--------------------------------*/
157
158 void
159 set_warning_issued (void)
160 {
161 static bool warning_issued = false;
162 if (!warning_issued && (warnings_flag & Werror))
163 {
164 fprintf (stderr, "%s: warnings being treated as errors\n", program_name);
165 complaint_issued = true;
166 }
167 warning_issued = true;
168 }