#include "config.h"
#include "system.h"
#include "tree.h"
#include "cp-tree.h"
#include "toplev.h"
typedef char* cp_printer PROTO((tree, int));
extern cp_printer * cp_printers[256];
int cp_silent = 0;
typedef void errorfn ();
static void cp_thing PROTO ((errorfn *, int, const char *, va_list));
#define STRDUP(f) (ap = (char *) alloca (strlen (f) +1), strcpy (ap, (f)), ap)
static void
cp_thing (errfn, atarg1, format, ap)
errorfn *errfn;
int atarg1;
const char *format;
va_list ap;
{
static char *buf;
static long buflen;
int nargs = 0;
long len;
long offset;
const char *f;
tree atarg = 0;
len = strlen (format) + 1;
if (len > buflen)
{
buflen = len;
buf = xrealloc (buf, buflen);
}
offset = 0;
for (f = format; *f; ++f)
{
cp_printer * function;
int alternate;
int maybe_here;
if (*f != '%')
{
buf[offset++] = *f;
continue;
}
++f;
alternate = 0;
maybe_here = 0;
if (*f == '+')
{
maybe_here = 1;
++f;
}
if (*f == '#')
{
alternate = 1;
++f;
}
function = cp_printers[(int)*f];
if (function || *f == 's')
{
char *p;
int plen;
if (*f == 's')
{
p = va_arg (ap, char *);
nargs++;
}
else
{
tree t = va_arg (ap, tree);
nargs++;
if (maybe_here && atarg1)
atarg = t;
if (atarg1 && nargs == 1)
atarg = t;
p = (*function) (t, alternate);
}
plen = strlen (p);
len += plen;
if (len > buflen)
{
buflen = len;
buf = xrealloc (buf, len);
}
strcpy (buf + offset, p);
offset += plen;
}
else if (*f == '%')
{
if (++len > buflen)
{
buflen = len;
buf = xrealloc (buf, len);
}
buf[offset++] = '%';
}
else
{
if (*f != 'd')
abort ();
len += HOST_BITS_PER_INT / 2;
if (len > buflen)
{
buflen = len;
buf = xrealloc (buf, len);
}
sprintf (buf + offset, "%d", va_arg (ap, int));
nargs++;
offset += strlen (buf + offset);
}
}
buf[offset] = '\0';
if (nargs == 0 && atarg1)
atarg = va_arg (ap, tree);
if (atarg)
{
char *file = cp_file_of (atarg);
int line = cp_line_of (atarg);
(*errfn) (file, line, "%s", buf);
}
else
(*errfn) ("%s", buf);
}
void
cp_error VPROTO((const char *format, ...))
{
#ifndef ANSI_PROTOTYPES
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef ANSI_PROTOTYPES
format = va_arg (ap, char *);
#endif
if (! cp_silent)
cp_thing ((errorfn *) error, 0, format, ap);
va_end (ap);
}
void
cp_warning VPROTO((const char *format, ...))
{
#ifndef ANSI_PROTOTYPES
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef ANSI_PROTOTYPES
format = va_arg (ap, char *);
#endif
if (! cp_silent)
cp_thing ((errorfn *) warning, 0, format, ap);
va_end (ap);
}
void
cp_pedwarn VPROTO((const char *format, ...))
{
#ifndef ANSI_PROTOTYPES
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef ANSI_PROTOTYPES
format = va_arg (ap, char *);
#endif
if (! cp_silent)
cp_thing ((errorfn *) pedwarn, 0, format, ap);
va_end (ap);
}
void
cp_compiler_error VPROTO((const char *format, ...))
{
#ifndef ANSI_PROTOTYPES
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef ANSI_PROTOTYPES
format = va_arg (ap, char *);
#endif
if (! cp_silent)
cp_thing ((errorfn *) compiler_error, 0, format, ap);
va_end (ap);
}
void
cp_deprecated (msg)
const char *msg;
{
extern int warn_deprecated;
if (!warn_deprecated)
return;
cp_warning ("%s is deprecated.", msg);
cp_warning ("Please see the documentation for details.");
}
void
cp_sprintf VPROTO((const char *format, ...))
{
#ifndef ANSI_PROTOTYPES
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef ANSI_PROTOTYPES
format = va_arg (ap, char *);
#endif
cp_thing ((errorfn *) sprintf, 0, format, ap);
va_end (ap);
}
void
cp_error_at VPROTO((const char *format, ...))
{
#ifndef ANSI_PROTOTYPES
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef ANSI_PROTOTYPES
format = va_arg (ap, char *);
#endif
if (! cp_silent)
cp_thing ((errorfn *) error_with_file_and_line, 1, format, ap);
va_end (ap);
}
void
cp_warning_at VPROTO((const char *format, ...))
{
#ifndef ANSI_PROTOTYPES
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef ANSI_PROTOTYPES
format = va_arg (ap, char *);
#endif
if (! cp_silent)
cp_thing ((errorfn *) warning_with_file_and_line, 1, format, ap);
va_end (ap);
}
void
cp_pedwarn_at VPROTO((const char *format, ...))
{
#ifndef ANSI_PROTOTYPES
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef ANSI_PROTOTYPES
format = va_arg (ap, char *);
#endif
if (! cp_silent)
cp_thing ((errorfn *) pedwarn_with_file_and_line, 1, format, ap);
va_end (ap);
}