binutils-gdb/libiberty/_doprnt.c

297 lines
7.3 KiB
C
Raw Normal View History

2001-11-28 10:11:49 +08:00
/* Provide a version of _doprnt in terms of fprintf.
Update the libiberty sources with the latest patches found in the master sources. 2017-01-02 Richard Biener <rguenther@suse.de> PR lto/83452 * simple-object-elf.c (simple_object_elf_copy_lto_debug_section): Do not use UNDEF locals for removed symbols but instead just define them in the first prevailing section and with no name. Use the same gnu_lto_v1 name for all removed globals we promote to WEAK UNDEFs so hpux can use a stub to provide this symbol. Clear sh_info and sh_link in removed sections. 2017-10-30 Richard Biener <rguenther@suse.de> PR lto/82757 * simple-object-elf.c (simple_object_elf_copy_lto_debug_sections): Strip two leading _s from the __gnu_lto_* symbols. 2017-10-24 Alan Modra <amodra@gmail.com> PR lto/82687 PR lto/82575 * simple-object-elf.c (simple_object_elf_copy_lto_debug_sections): Only make __gnu_lto symbols hidden. 2017-10-20 Alan Modra <amodra@gmail.com> PR lto/82575 * simple-object-elf.c (simple_object_elf_copy_lto_debug_sections): Make discarded non-local symbols weak and hidden. 2017-10-18 Jakub Jelinek <jakub@redhat.com> PR lto/82598 * simple-object.c (handle_lto_debug_sections): Copy over also .note.GNU-stack section with unchanged name. * simple-object-elf.c (SHF_EXECINSTR): Define. (simple_object_elf_copy_lto_debug_section): Drop SHF_EXECINSTR bit on .note.GNU-stack section. 2017-09-25 Nathan Sidwell <nathan@acm.org> PR demangler/82195 * cp-demangle.c (d_encoding): Strip return type when name is a LOCAL_NAME. (d_local_name): Strip return type of enclosing TYPED_NAME. * testsuite/demangle-expected: Add and adjust tests. 2017-09-21 Nathan Sidwell <nathan@acm.org> PR demangler/82195 * cp-demangle.c (d_name): Revert addition of 'toplevel' parm. (has_return_type): Recurse for DEMANGLE_COMPONENT_LOCAL_NAME. (d_encoding): Revert d_name change. Use is_fnqual_component_type to strip modifiers that do not belong. (d_special_name, d_class_enum_type): Revert d_name call change. (d_expresion_1): Commonize DEMANGLE_COMPONENT_UNARY building. (d_local_name): Revert parsing of a function type. (d_print_comp_inner): An inner LOCAL_NAME might contain a TEMPLATE. * testsuite/demangle-expected: Add & adjust tests
2018-01-10 21:57:48 +08:00
Copyright (C) 1998-2018 Free Software Foundation, Inc.
2001-11-28 10:11:49 +08:00
Contributed by Kaveh Ghazi (ghazi@caip.rutgers.edu) 3/29/98
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
2001-11-28 10:11:49 +08:00
#include "config.h"
#include "ansidecl.h"
#include "safe-ctype.h"
#include <stdio.h>
#include <stdarg.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
2002-01-23 04:14:53 +08:00
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
2001-11-28 10:11:49 +08:00
#undef _doprnt
2002-01-23 04:14:53 +08:00
#ifdef HAVE__DOPRNT
#define TEST
#endif
2001-11-28 10:11:49 +08:00
#ifdef TEST /* Make sure to use the internal one. */
#define _doprnt my_doprnt
#endif
#define COPY_VA_INT \
do { \
const int value = abs (va_arg (ap, int)); \
char buf[32]; \
ptr++; /* Go past the asterisk. */ \
*sptr = '\0'; /* NULL terminate sptr. */ \
sprintf(buf, "%d", value); \
strcat(sptr, buf); \
while (*sptr) sptr++; \
} while (0)
#define PRINT_CHAR(CHAR) \
do { \
putc(CHAR, stream); \
ptr++; \
total_printed++; \
continue; \
} while (0)
#define PRINT_TYPE(TYPE) \
do { \
int result; \
TYPE value = va_arg (ap, TYPE); \
*sptr++ = *ptr++; /* Copy the type specifier. */ \
*sptr = '\0'; /* NULL terminate sptr. */ \
result = fprintf(stream, specifier, value); \
if (result == -1) \
return -1; \
else \
{ \
total_printed += result; \
continue; \
} \
} while (0)
int
2005-03-27 13:28:42 +08:00
_doprnt (const char *format, va_list ap, FILE *stream)
2001-11-28 10:11:49 +08:00
{
const char * ptr = format;
char specifier[128];
int total_printed = 0;
while (*ptr != '\0')
{
if (*ptr != '%') /* While we have regular characters, print them. */
PRINT_CHAR(*ptr);
else /* We got a format specifier! */
{
char * sptr = specifier;
int wide_width = 0, short_width = 0;
*sptr++ = *ptr++; /* Copy the % and move forward. */
while (strchr ("-+ #0", *ptr)) /* Move past flags. */
*sptr++ = *ptr++;
if (*ptr == '*')
COPY_VA_INT;
else
while (ISDIGIT(*ptr)) /* Handle explicit numeric value. */
*sptr++ = *ptr++;
if (*ptr == '.')
{
*sptr++ = *ptr++; /* Copy and go past the period. */
if (*ptr == '*')
COPY_VA_INT;
else
while (ISDIGIT(*ptr)) /* Handle explicit numeric value. */
*sptr++ = *ptr++;
}
while (strchr ("hlL", *ptr))
{
switch (*ptr)
{
case 'h':
short_width = 1;
break;
case 'l':
wide_width++;
break;
case 'L':
wide_width = 2;
break;
default:
abort();
}
*sptr++ = *ptr++;
}
switch (*ptr)
{
case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
case 'c':
{
/* Short values are promoted to int, so just copy it
as an int and trust the C library printf to cast it
to the right width. */
if (short_width)
PRINT_TYPE(int);
else
{
switch (wide_width)
{
case 0:
PRINT_TYPE(int);
break;
case 1:
PRINT_TYPE(long);
break;
case 2:
default:
#if defined(__GNUC__) || defined(HAVE_LONG_LONG)
PRINT_TYPE(long long);
#else
PRINT_TYPE(long); /* Fake it and hope for the best. */
#endif
break;
} /* End of switch (wide_width) */
} /* End of else statement */
} /* End of integer case */
break;
case 'f':
case 'e':
case 'E':
case 'g':
case 'G':
{
if (wide_width == 0)
PRINT_TYPE(double);
else
{
#if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
PRINT_TYPE(long double);
#else
PRINT_TYPE(double); /* Fake it and hope for the best. */
#endif
}
}
break;
case 's':
PRINT_TYPE(char *);
break;
case 'p':
PRINT_TYPE(void *);
break;
case '%':
PRINT_CHAR('%');
break;
default:
abort();
} /* End of switch (*ptr) */
} /* End of else statement */
}
return total_printed;
}
#ifdef TEST
#include <math.h>
#ifndef M_PI
#define M_PI (3.1415926535897932385)
#endif
#define RESULT(x) do \
{ \
int i = (x); \
printf ("printed %d characters\n", i); \
fflush(stdin); \
} while (0)
2005-03-27 13:28:42 +08:00
static int checkit (const char * format, ...) ATTRIBUTE_PRINTF_1;
2001-11-28 10:11:49 +08:00
static int
2005-03-27 13:28:42 +08:00
checkit (const char* format, ...)
2001-11-28 10:11:49 +08:00
{
int result;
va_list args;
va_start (args, format);
2001-11-28 10:11:49 +08:00
result = _doprnt (format, args, stdout);
va_end (args);
2001-11-28 10:11:49 +08:00
return result;
}
int
2005-03-27 13:28:42 +08:00
main (void)
2001-11-28 10:11:49 +08:00
{
RESULT(checkit ("<%d>\n", 0x12345678));
RESULT(printf ("<%d>\n", 0x12345678));
RESULT(checkit ("<%200d>\n", 5));
RESULT(printf ("<%200d>\n", 5));
RESULT(checkit ("<%.300d>\n", 6));
RESULT(printf ("<%.300d>\n", 6));
RESULT(checkit ("<%100.150d>\n", 7));
RESULT(printf ("<%100.150d>\n", 7));
RESULT(checkit ("<%s>\n",
"jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
777777777777777777333333333333366666666666622222222222777777777777733333"));
RESULT(printf ("<%s>\n",
"jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
777777777777777777333333333333366666666666622222222222777777777777733333"));
RESULT(checkit ("<%f><%0+#f>%s%d%s>\n",
1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
RESULT(printf ("<%f><%0+#f>%s%d%s>\n",
1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
RESULT(checkit ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
RESULT(printf ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
RESULT(checkit ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
RESULT(printf ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
75, 75, 75, 75, 75, 75, 75));
RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
75, 75, 75, 75, 75, 75, 75));
RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
75, 75, 75, 75, 75, 75, 75));
RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
75, 75, 75, 75, 75, 75, 75));
RESULT(checkit ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
RESULT(printf ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
#if defined(__GNUC__) || defined (HAVE_LONG_LONG)
RESULT(checkit ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
RESULT(printf ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
RESULT(checkit ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
RESULT(printf ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
#endif
#if defined(__GNUC__) || defined (HAVE_LONG_DOUBLE)
RESULT(checkit ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
1.23456, 1.234567890123456789L, 1.23456));
RESULT(printf ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
1.23456, 1.234567890123456789L, 1.23456));
#endif
return 0;
}
#endif /* TEST */