Commit 9932d281 by John Carr Committed by Jeff Law

errfn.c: Rework to avoid problems when HOST_WIDE_INT is longer than a pointer.

        * errfn.c: Rework to avoid problems when HOST_WIDE_INT is longer
        than a pointer.

Co-Authored-By: Jeffrey A Law <law@cygnus.com>

From-SVN: r19226
parent 2872409d
Wed Apr 15 13:20:06 1998 John Carr <jfc@mit.edu>
Jeff Law <law@cygnus.com>
* errfn.c: Rework to avoid problems when HOST_WIDE_INT is longer
than a pointer.
Fri Apr 10 12:16:49 1998 Benjamin Kosnik <bkoz@loony.cygnus.com> Fri Apr 10 12:16:49 1998 Benjamin Kosnik <bkoz@loony.cygnus.com>
* decl.c (duplicate_decls): Don't warn for redundant decls if * decl.c (duplicate_decls): Don't warn for redundant decls if
......
...@@ -22,13 +22,18 @@ Boston, MA 02111-1307, USA. */ ...@@ -22,13 +22,18 @@ Boston, MA 02111-1307, USA. */
#include "config.h" #include "config.h"
#include "system.h" #include "system.h"
#include "tree.h" #include "tree.h"
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
/* cp_printer is the type of a function which converts an argument into /* cp_printer is the type of a function which converts an argument into
a string for digestion by printf. The cp_printer function should deal a string for digestion by printf. The cp_printer function should deal
with all memory management; the functions in this file will not free with all memory management; the functions in this file will not free
the char*s returned. See error.c for an example use of this code. */ the char*s returned. See error.c for an example use of this code. */
typedef char* cp_printer PROTO((HOST_WIDE_INT, int)); typedef char* cp_printer PROTO((tree, int));
extern cp_printer * cp_printers[256]; extern cp_printer * cp_printers[256];
/* Whether or not we should try to be quiet for errors and warnings; this is /* Whether or not we should try to be quiet for errors and warnings; this is
...@@ -43,184 +48,221 @@ extern int cp_line_of PROTO((tree)); ...@@ -43,184 +48,221 @@ extern int cp_line_of PROTO((tree));
#define STRDUP(f) (ap = (char *) alloca (strlen (f) +1), strcpy (ap, (f)), ap) #define STRDUP(f) (ap = (char *) alloca (strlen (f) +1), strcpy (ap, (f)), ap)
#define NARGS 5 /* This function supports only `%s', `%d', and the C++ print codes. */
#define arglist a1, a2, a3, a4, a5
#define arglist_dcl HOST_WIDE_INT a1, a2, a3, a4, a5;
#define ARGSINIT \
args[0] = a1; args[1] = a2; args[2] = a3; args[3] = a4; args[4] = a5;
#define ARGSLIST args[0], args[1], args[2], args[3], args[4]
#ifdef __STDC__
static void static void
cp_thing (errfn, atarg1, format, arglist) cp_thing (errorfn *errfn, int atarg1, const char *format, va_list ap)
#else
static void
cp_thing (errfn, atarg1, format, ap)
errorfn *errfn; errorfn *errfn;
int atarg1; int atarg1;
char *format; const char *format;
arglist_dcl va_list ap;
#endif
{ {
char *fmt; static char *buf;
char *f; static long buflen;
char *ap; int nargs = 0;
int arg; long len;
HOST_WIDE_INT atarg = atarg1 ? a1 : 0; long offset;
HOST_WIDE_INT args[NARGS]; const char *f;
ARGSINIT tree atarg = 0;
fmt = STRDUP(format); len = strlen (format) + 1;
if (len > buflen)
for (f = fmt, arg = 0; *f; ++f) {
buflen = len;
buf = xmalloc (buflen);
}
offset = 0;
for (f = format; *f; ++f)
{ {
cp_printer * function; cp_printer * function;
int alternate; int alternate;
int maybe_here; int maybe_here;
/* ignore text */ /* ignore text */
if (*f != '%') continue; if (*f != '%')
{
buf[offset++] = *f;
continue;
}
++f; ++f;
alternate = 0; alternate = 0;
maybe_here = 0; maybe_here = 0;
/* ignore most flags */ /* Check for '+' and '#' (in that order). */
while (*f == ' ' || *f == '-' || *f == '+' || *f == '#') if (*f == '+')
{ {
if (*f == '+') maybe_here = 1;
maybe_here = 1;
else if (*f == '#')
alternate = 1;
++f; ++f;
} }
if (*f == '#')
/* ignore field width */
if (*f == '*')
{ {
alternate = 1;
++f; ++f;
++arg;
} }
else
while (isdigit (*f))
++f;
/* ignore precision */ /* no field width or precision */
if (*f == '.')
{
++f;
if (*f == '*')
{
++f;
++arg;
}
else
while (isdigit (*f))
++f;
}
/* ignore "long" */
if (*f == 'l')
++f;
function = cp_printers[(int)*f]; function = cp_printers[(int)*f];
if (function) if (function || *f == 's')
{ {
char *p; char *p;
int plen;
if (arg >= NARGS) abort (); if (*f == 's')
{
if (maybe_here && atarg1) p = va_arg (ap, char *);
atarg = args[arg]; nargs++;
}
else
{
tree t = va_arg (ap, tree);
nargs++;
/* Must use a temporary to avoid calling *function twice */ /* This indicates that ATARG comes from a different
p = (*function) (args[arg], alternate); location than normal. */
args[arg] = (HOST_WIDE_INT) STRDUP(p); if (maybe_here && atarg1)
*f = 's'; atarg = t;
}
++arg; /* Assume valid format string */ /* If atarg1 is set and this is the first argument, then
set ATARG appropriately. */
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 != 'd')
abort ();
len += HOST_BITS_PER_INT / 2;
if (len > buflen)
{
buflen = len;
buf = xmalloc (len);
}
sprintf (buf + offset, "%d", va_arg (ap, int));
nargs++;
offset += strlen (buf + offset);
/* With an ANSI C library one could write
out += sprintf (...); */
}
} }
buf[offset] = '\0';
/* If ATARG1 is set, but we haven't extracted any arguments, then
extract one tree argument for ATARG. */
if (nargs == 0 && atarg1)
atarg = va_arg (ap, tree);
if (atarg) if (atarg)
{ {
char *file = cp_file_of ((tree) atarg); char *file = cp_file_of (atarg);
int line = cp_line_of ((tree) atarg); int line = cp_line_of (atarg);
(*errfn) (file, line, fmt, ARGSLIST); (*errfn) (file, line, buf);
} }
else else
(*errfn) (fmt, ARGSLIST); (*errfn) (buf);
} }
void #ifdef __STDC__
cp_error (format, arglist) #define DECLARE(name) void name (const char *format, ...)
char *format; #define INIT va_start (ap, format)
arglist_dcl #else
#define DECLARE(name) void name (format, va_alist) char *format; va_dcl
#define INIT va_start (ap)
#endif
DECLARE (cp_error)
{ {
extern errorfn error; extern errorfn error;
va_list ap;
INIT;
if (! cp_silent) if (! cp_silent)
cp_thing (error, 0, format, arglist); cp_thing (error, 0, format, ap);
va_end (ap);
} }
void DECLARE (cp_warning)
cp_warning (format, arglist)
char *format;
arglist_dcl
{ {
extern errorfn warning; extern errorfn warning;
va_list ap;
INIT;
if (! cp_silent) if (! cp_silent)
cp_thing (warning, 0, format, arglist); cp_thing (warning, 0, format, ap);
va_end (ap);
} }
void DECLARE (cp_pedwarn)
cp_pedwarn (format, arglist)
char *format;
arglist_dcl
{ {
va_list ap;
INIT;
if (! cp_silent) if (! cp_silent)
cp_thing ((errorfn *) pedwarn, 0, format, arglist); cp_thing ((errorfn *) pedwarn, 0, format, ap);
va_end (ap);
} }
void DECLARE (cp_compiler_error)
cp_compiler_error (format, arglist)
char *format;
arglist_dcl
{ {
extern errorfn compiler_error; extern errorfn compiler_error;
va_list ap;
INIT;
if (! cp_silent) if (! cp_silent)
cp_thing (compiler_error, 0, format, arglist); cp_thing (compiler_error, 0, format, ap);
va_end (ap);
} }
void DECLARE (cp_sprintf)
cp_sprintf (format, arglist)
char *format;
arglist_dcl
{ {
cp_thing ((errorfn *) sprintf, 0, format, arglist); va_list ap;
INIT;
cp_thing ((errorfn *) sprintf, 0, format, ap);
va_end (ap);
} }
void DECLARE (cp_error_at)
cp_error_at (format, arglist)
char *format;
arglist_dcl
{ {
va_list ap;
INIT;
if (! cp_silent) if (! cp_silent)
cp_thing ((errorfn *) error_with_file_and_line, 1, format, arglist); cp_thing ((errorfn *) error_with_file_and_line, 1, format, ap);
va_end (ap);
} }
void DECLARE (cp_warning_at)
cp_warning_at (format, arglist)
char *format;
arglist_dcl
{ {
va_list ap;
INIT;
if (! cp_silent) if (! cp_silent)
cp_thing ((errorfn *) warning_with_file_and_line, 1, format, arglist); cp_thing ((errorfn *) warning_with_file_and_line, 1, format, ap);
va_end (ap);
} }
void DECLARE (cp_pedwarn_at)
cp_pedwarn_at (format, arglist)
char *format;
arglist_dcl
{ {
va_list ap;
INIT;
if (! cp_silent) if (! cp_silent)
cp_thing ((errorfn *) pedwarn_with_file_and_line, 1, format, arglist); cp_thing ((errorfn *) pedwarn_with_file_and_line, 1, format, ap);
va_end (ap);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment