Update version
[official-gcc.git] / gcc / cp / errfn.c
blob351dba958884b2320dbd9d47d9f4116275cab951
1 /* Provide a call-back mechanism for handling error output.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4 Contributed by Jason Merrill (jason@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "tree.h"
26 #include "cp-tree.h"
27 #include "toplev.h"
29 /* Whether or not we should try to be quiet for errors and warnings; this is
30 used to avoid being too talkative about problems with tentative choices
31 when we're computing the conversion costs for a method call. */
32 int cp_silent = 0;
34 typedef void errorfn (); /* deliberately vague */
36 static void cp_thing PARAMS ((errorfn *, int, const char *, va_list));
38 #define STRDUP(f) (ap = (char *) alloca (strlen (f) +1), strcpy (ap, (f)), ap)
40 /* This function supports only `%s', `%d', `%%', and the C++ print
41 codes. */
43 static void
44 cp_thing (errfn, atarg1, format, ap)
45 errorfn *errfn;
46 int atarg1;
47 const char *format;
48 va_list ap;
50 static char *buf;
51 static long buflen;
52 int nargs = 0;
53 long len;
54 long offset;
55 const char *f;
56 tree atarg = 0;
58 len = strlen (format) + 1;
59 if (len > buflen)
61 buflen = len;
62 buf = xrealloc (buf, buflen);
64 offset = 0;
66 for (f = format; *f; ++f)
68 cp_printer * function;
69 int alternate;
70 int maybe_here;
72 /* ignore text */
73 if (*f != '%')
75 buf[offset++] = *f;
76 continue;
79 ++f;
81 alternate = 0;
82 maybe_here = 0;
84 /* Check for '+' and '#' (in that order). */
85 if (*f == '+')
87 maybe_here = 1;
88 ++f;
90 if (*f == '#')
92 alternate = 1;
93 ++f;
96 /* no field width or precision */
98 function = cp_printers[(int)*f];
100 if (function || *f == 's')
102 const char *p;
103 int plen;
105 if (*f == 's')
107 p = va_arg (ap, char *);
108 nargs++;
110 else
112 tree t = va_arg (ap, tree);
113 nargs++;
115 /* This indicates that ATARG comes from a different
116 location than normal. */
117 if (maybe_here && atarg1)
118 atarg = t;
120 /* If atarg1 is set and this is the first argument, then
121 set ATARG appropriately. */
122 if (atarg1 && nargs == 1)
123 atarg = t;
125 p = (*function) (t, alternate);
128 plen = strlen (p);
129 len += plen;
130 if (len > buflen)
132 buflen = len;
133 buf = xrealloc (buf, len);
135 strcpy (buf + offset, p);
136 offset += plen;
138 else if (*f == '%')
140 /* A `%%' has occurred in the input string. Replace it with
141 a `%' in the formatted message buf. */
143 if (++len > buflen)
145 buflen = len;
146 buf = xrealloc (buf, len);
148 buf[offset++] = '%';
150 else
152 if (*f != 'd')
153 abort ();
154 len += HOST_BITS_PER_INT / 2;
155 if (len > buflen)
157 buflen = len;
158 buf = xrealloc (buf, len);
160 sprintf (buf + offset, "%d", va_arg (ap, int));
161 nargs++;
162 offset += strlen (buf + offset);
163 /* With an ANSI C library one could write
164 out += sprintf (...); */
167 buf[offset] = '\0';
169 /* If ATARG1 is set, but we haven't extracted any arguments, then
170 extract one tree argument for ATARG. */
171 if (nargs == 0 && atarg1)
172 atarg = va_arg (ap, tree);
174 if (atarg)
176 const char *file = cp_file_of (atarg);
177 int line = cp_line_of (atarg);
178 (*errfn) (file, line, "%s", buf);
180 else
181 (*errfn) ("%s", buf);
185 void
186 cp_error VPARAMS ((const char *format, ...))
188 #ifndef ANSI_PROTOTYPES
189 char *format;
190 #endif
191 va_list ap;
193 VA_START (ap, format);
195 #ifndef ANSI_PROTOTYPES
196 format = va_arg (ap, char *);
197 #endif
199 if (! cp_silent)
200 cp_thing ((errorfn *) error, 0, format, ap);
201 va_end (ap);
204 void
205 cp_warning VPARAMS ((const char *format, ...))
207 #ifndef ANSI_PROTOTYPES
208 char *format;
209 #endif
210 va_list ap;
212 VA_START (ap, format);
214 #ifndef ANSI_PROTOTYPES
215 format = va_arg (ap, char *);
216 #endif
218 if (! cp_silent)
219 cp_thing ((errorfn *) warning, 0, format, ap);
220 va_end (ap);
223 void
224 cp_pedwarn VPARAMS ((const char *format, ...))
226 #ifndef ANSI_PROTOTYPES
227 char *format;
228 #endif
229 va_list ap;
231 VA_START (ap, format);
233 #ifndef ANSI_PROTOTYPES
234 format = va_arg (ap, char *);
235 #endif
237 if (! cp_silent)
238 cp_thing ((errorfn *) pedwarn, 0, format, ap);
239 va_end (ap);
242 void
243 cp_compiler_error VPARAMS ((const char *format, ...))
245 #ifndef ANSI_PROTOTYPES
246 char *format;
247 #endif
248 va_list ap;
250 VA_START (ap, format);
252 #ifndef ANSI_PROTOTYPES
253 format = va_arg (ap, char *);
254 #endif
256 if (! cp_silent)
257 cp_thing ((errorfn *) compiler_error, 0, format, ap);
258 va_end (ap);
261 void
262 cp_deprecated (msg)
263 const char *msg;
265 extern int warn_deprecated;
266 if (!warn_deprecated)
267 return;
268 cp_warning ("%s is deprecated, please see the documentation for details", msg);
271 void
272 cp_sprintf VPARAMS ((const char *format, ...))
274 #ifndef ANSI_PROTOTYPES
275 char *format;
276 #endif
277 va_list ap;
279 VA_START (ap, format);
281 #ifndef ANSI_PROTOTYPES
282 format = va_arg (ap, char *);
283 #endif
285 cp_thing ((errorfn *) sprintf, 0, format, ap);
286 va_end (ap);
289 void
290 cp_error_at VPARAMS ((const char *format, ...))
292 #ifndef ANSI_PROTOTYPES
293 char *format;
294 #endif
295 va_list ap;
297 VA_START (ap, format);
299 #ifndef ANSI_PROTOTYPES
300 format = va_arg (ap, char *);
301 #endif
303 if (! cp_silent)
304 cp_thing ((errorfn *) error_with_file_and_line, 1, format, ap);
305 va_end (ap);
308 void
309 cp_warning_at VPARAMS ((const char *format, ...))
311 #ifndef ANSI_PROTOTYPES
312 char *format;
313 #endif
314 va_list ap;
316 VA_START (ap, format);
318 #ifndef ANSI_PROTOTYPES
319 format = va_arg (ap, char *);
320 #endif
322 if (! cp_silent)
323 cp_thing ((errorfn *) warning_with_file_and_line, 1, format, ap);
324 va_end (ap);
327 void
328 cp_pedwarn_at VPARAMS ((const char *format, ...))
330 #ifndef ANSI_PROTOTYPES
331 char *format;
332 #endif
333 va_list ap;
335 VA_START (ap, format);
337 #ifndef ANSI_PROTOTYPES
338 format = va_arg (ap, char *);
339 #endif
341 if (! cp_silent)
342 cp_thing ((errorfn *) pedwarn_with_file_and_line, 1, format, ap);
343 va_end (ap);