mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2025-02-11 13:02:10 +08:00
This patch adds support for handling format strings to both floatformat_to_string and decimal_to_string, and then uses those routines to implement ui_printf formatted printing. There is already a subroutine printf_decfloat that ui_printf uses to handle decimal FP. This is renamed to printf_floating and updated to handle both binary and decimal FP. This includes the following set of changes: - printf_decfloat currently parses the format string again to determine the intended target format. This seems superfluous since the common parsing code in parse_format_string already did this, but then did not pass the result on to its users. Fixed by splitting the decfloat_arg argument class into three distinct classes, and passing them through. - Now we can rename printf_decfloat to printf_floating and also call it for the argument classes representing binary FP types. - The code will now use the argclass to detect the type the value should be printed at, and converts the input value to this type if necessary. To remain compatible with current behavior, for binary FP the code instead tries to re-interpret the input value as a FP type of the same size if that exists. (Maybe this behavior is more confusing than useful -- but this can be changed later if we want to ...) - Finally, we can use floatformat_to_string / decimal_to_string passing the format string to perform the formatted output using the desired target FP type. Note that we no longer generate different code depending on whether or not the host supports "long double" -- this check is obsolete anyway since C++11 mandates "long double", and in any case a %lg format string is intended to refer to the *target* long double type, not the host version. Note also that formatted printing of DFP numbers may not work correctly, since it attempts to use the host printf to do so (and makes unwarranted assumptions about the host ABI while doing so!). This is no change to the current behavior -- I simply moved the code from printf_decfloat to the decimal_to_string routine in dfp.c. If we want to fix it in the future, that is a more appropriate place anyway. ChangeLog: 2017-10-24 Ulrich Weigand <uweigand@de.ibm.com> * common/format.h (enum argclass): Replace decfloat_arg by dec32float_arg, dec64float_arg, and dec128float_arg. * common/format.c (parse_format_string): Update to return new decimal float argument classes. * printcmd.c (printf_decfloat): Rename to ... (printf_floating): ... this. Add argclass argument, and use it instead of parsing the format string again. Add support for binary floating-point values, using floatformat_to_string. Convert value to the target format if it doesn't already match. (ui_printf): Call printf_floating instead of printf_decfloat, also for double_arg / long_double_arg. Pass argclass. * dfp.c (decimal_to_string): Add format string argument. * dfp.h (decimal_to_string): Likewise. * doublest.c (floatformat_to_string): Add format string argument. * doublest.h (floatformat_to_string): Likewise.
400 lines
8.8 KiB
C
400 lines
8.8 KiB
C
/* Parse a printf-style format string.
|
|
|
|
Copyright (C) 1986-2017 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#include "common-defs.h"
|
|
#include "format.h"
|
|
|
|
struct format_piece *
|
|
parse_format_string (const char **arg)
|
|
{
|
|
const char *s;
|
|
char *f, *string;
|
|
const char *prev_start;
|
|
const char *percent_loc;
|
|
char *sub_start, *current_substring;
|
|
struct format_piece *pieces;
|
|
int next_frag;
|
|
int max_pieces;
|
|
enum argclass this_argclass;
|
|
|
|
s = *arg;
|
|
|
|
/* Parse the format-control string and copy it into the string STRING,
|
|
processing some kinds of escape sequence. */
|
|
|
|
f = string = (char *) alloca (strlen (s) + 1);
|
|
|
|
while (*s != '"' && *s != '\0')
|
|
{
|
|
int c = *s++;
|
|
switch (c)
|
|
{
|
|
case '\0':
|
|
continue;
|
|
|
|
case '\\':
|
|
switch (c = *s++)
|
|
{
|
|
case '\\':
|
|
*f++ = '\\';
|
|
break;
|
|
case 'a':
|
|
*f++ = '\a';
|
|
break;
|
|
case 'b':
|
|
*f++ = '\b';
|
|
break;
|
|
case 'f':
|
|
*f++ = '\f';
|
|
break;
|
|
case 'n':
|
|
*f++ = '\n';
|
|
break;
|
|
case 'r':
|
|
*f++ = '\r';
|
|
break;
|
|
case 't':
|
|
*f++ = '\t';
|
|
break;
|
|
case 'v':
|
|
*f++ = '\v';
|
|
break;
|
|
case '"':
|
|
*f++ = '"';
|
|
break;
|
|
default:
|
|
/* ??? TODO: handle other escape sequences. */
|
|
error (_("Unrecognized escape character \\%c in format string."),
|
|
c);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
*f++ = c;
|
|
}
|
|
}
|
|
|
|
/* Terminate our escape-processed copy. */
|
|
*f++ = '\0';
|
|
|
|
/* Whether the format string ended with double-quote or zero, we're
|
|
done with it; it's up to callers to complain about syntax. */
|
|
*arg = s;
|
|
|
|
/* Need extra space for the '\0's. Doubling the size is sufficient. */
|
|
|
|
current_substring = (char *) xmalloc (strlen (string) * 2 + 1000);
|
|
|
|
max_pieces = strlen (string) + 2;
|
|
|
|
pieces = XNEWVEC (struct format_piece, max_pieces);
|
|
|
|
next_frag = 0;
|
|
|
|
/* Now scan the string for %-specs and see what kinds of args they want.
|
|
argclass classifies the %-specs so we can give printf-type functions
|
|
something of the right size. */
|
|
|
|
f = string;
|
|
prev_start = string;
|
|
while (*f)
|
|
if (*f++ == '%')
|
|
{
|
|
int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
|
|
int seen_space = 0, seen_plus = 0;
|
|
int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
|
|
int seen_big_d = 0, seen_double_big_d = 0;
|
|
int bad = 0;
|
|
|
|
/* Skip over "%%", it will become part of a literal piece. */
|
|
if (*f == '%')
|
|
{
|
|
f++;
|
|
continue;
|
|
}
|
|
|
|
sub_start = current_substring;
|
|
|
|
strncpy (current_substring, prev_start, f - 1 - prev_start);
|
|
current_substring += f - 1 - prev_start;
|
|
*current_substring++ = '\0';
|
|
|
|
pieces[next_frag].string = sub_start;
|
|
pieces[next_frag].argclass = literal_piece;
|
|
next_frag++;
|
|
|
|
percent_loc = f - 1;
|
|
|
|
/* Check the validity of the format specifier, and work
|
|
out what argument it expects. We only accept C89
|
|
format strings, with the exception of long long (which
|
|
we autoconf for). */
|
|
|
|
/* The first part of a format specifier is a set of flag
|
|
characters. */
|
|
while (*f != '\0' && strchr ("0-+ #", *f))
|
|
{
|
|
if (*f == '#')
|
|
seen_hash = 1;
|
|
else if (*f == '0')
|
|
seen_zero = 1;
|
|
else if (*f == ' ')
|
|
seen_space = 1;
|
|
else if (*f == '+')
|
|
seen_plus = 1;
|
|
f++;
|
|
}
|
|
|
|
/* The next part of a format specifier is a width. */
|
|
while (*f != '\0' && strchr ("0123456789", *f))
|
|
f++;
|
|
|
|
/* The next part of a format specifier is a precision. */
|
|
if (*f == '.')
|
|
{
|
|
seen_prec = 1;
|
|
f++;
|
|
while (*f != '\0' && strchr ("0123456789", *f))
|
|
f++;
|
|
}
|
|
|
|
/* The next part of a format specifier is a length modifier. */
|
|
if (*f == 'h')
|
|
{
|
|
seen_h = 1;
|
|
f++;
|
|
}
|
|
else if (*f == 'l')
|
|
{
|
|
f++;
|
|
lcount++;
|
|
if (*f == 'l')
|
|
{
|
|
f++;
|
|
lcount++;
|
|
}
|
|
}
|
|
else if (*f == 'L')
|
|
{
|
|
seen_big_l = 1;
|
|
f++;
|
|
}
|
|
/* Decimal32 modifier. */
|
|
else if (*f == 'H')
|
|
{
|
|
seen_big_h = 1;
|
|
f++;
|
|
}
|
|
/* Decimal64 and Decimal128 modifiers. */
|
|
else if (*f == 'D')
|
|
{
|
|
f++;
|
|
|
|
/* Check for a Decimal128. */
|
|
if (*f == 'D')
|
|
{
|
|
f++;
|
|
seen_double_big_d = 1;
|
|
}
|
|
else
|
|
seen_big_d = 1;
|
|
}
|
|
|
|
switch (*f)
|
|
{
|
|
case 'u':
|
|
if (seen_hash)
|
|
bad = 1;
|
|
/* FALLTHROUGH */
|
|
|
|
case 'o':
|
|
case 'x':
|
|
case 'X':
|
|
if (seen_space || seen_plus)
|
|
bad = 1;
|
|
/* FALLTHROUGH */
|
|
|
|
case 'd':
|
|
case 'i':
|
|
if (lcount == 0)
|
|
this_argclass = int_arg;
|
|
else if (lcount == 1)
|
|
this_argclass = long_arg;
|
|
else
|
|
this_argclass = long_long_arg;
|
|
|
|
if (seen_big_l)
|
|
bad = 1;
|
|
break;
|
|
|
|
case 'c':
|
|
this_argclass = lcount == 0 ? int_arg : wide_char_arg;
|
|
if (lcount > 1 || seen_h || seen_big_l)
|
|
bad = 1;
|
|
if (seen_prec || seen_zero || seen_space || seen_plus)
|
|
bad = 1;
|
|
break;
|
|
|
|
case 'p':
|
|
this_argclass = ptr_arg;
|
|
if (lcount || seen_h || seen_big_l)
|
|
bad = 1;
|
|
if (seen_prec)
|
|
bad = 1;
|
|
if (seen_hash || seen_zero || seen_space || seen_plus)
|
|
bad = 1;
|
|
break;
|
|
|
|
case 's':
|
|
this_argclass = lcount == 0 ? string_arg : wide_string_arg;
|
|
if (lcount > 1 || seen_h || seen_big_l)
|
|
bad = 1;
|
|
if (seen_zero || seen_space || seen_plus)
|
|
bad = 1;
|
|
break;
|
|
|
|
case 'e':
|
|
case 'f':
|
|
case 'g':
|
|
case 'E':
|
|
case 'G':
|
|
if (seen_double_big_d)
|
|
this_argclass = dec128float_arg;
|
|
else if (seen_big_d)
|
|
this_argclass = dec64float_arg;
|
|
else if (seen_big_h)
|
|
this_argclass = dec32float_arg;
|
|
else if (seen_big_l)
|
|
this_argclass = long_double_arg;
|
|
else
|
|
this_argclass = double_arg;
|
|
|
|
if (lcount || seen_h)
|
|
bad = 1;
|
|
break;
|
|
|
|
case '*':
|
|
error (_("`*' not supported for precision or width in printf"));
|
|
|
|
case 'n':
|
|
error (_("Format specifier `n' not supported in printf"));
|
|
|
|
case '\0':
|
|
error (_("Incomplete format specifier at end of format string"));
|
|
|
|
default:
|
|
error (_("Unrecognized format specifier '%c' in printf"), *f);
|
|
}
|
|
|
|
if (bad)
|
|
error (_("Inappropriate modifiers to "
|
|
"format specifier '%c' in printf"),
|
|
*f);
|
|
|
|
f++;
|
|
|
|
sub_start = current_substring;
|
|
|
|
if (lcount > 1 && USE_PRINTF_I64)
|
|
{
|
|
/* Windows' printf does support long long, but not the usual way.
|
|
Convert %lld to %I64d. */
|
|
int length_before_ll = f - percent_loc - 1 - lcount;
|
|
|
|
strncpy (current_substring, percent_loc, length_before_ll);
|
|
strcpy (current_substring + length_before_ll, "I64");
|
|
current_substring[length_before_ll + 3] =
|
|
percent_loc[length_before_ll + lcount];
|
|
current_substring += length_before_ll + 4;
|
|
}
|
|
else if (this_argclass == wide_string_arg
|
|
|| this_argclass == wide_char_arg)
|
|
{
|
|
/* Convert %ls or %lc to %s. */
|
|
int length_before_ls = f - percent_loc - 2;
|
|
|
|
strncpy (current_substring, percent_loc, length_before_ls);
|
|
strcpy (current_substring + length_before_ls, "s");
|
|
current_substring += length_before_ls + 2;
|
|
}
|
|
else
|
|
{
|
|
strncpy (current_substring, percent_loc, f - percent_loc);
|
|
current_substring += f - percent_loc;
|
|
}
|
|
|
|
*current_substring++ = '\0';
|
|
|
|
prev_start = f;
|
|
|
|
pieces[next_frag].string = sub_start;
|
|
pieces[next_frag].argclass = this_argclass;
|
|
next_frag++;
|
|
}
|
|
|
|
/* Record the remainder of the string. */
|
|
|
|
sub_start = current_substring;
|
|
|
|
strncpy (current_substring, prev_start, f - prev_start);
|
|
current_substring += f - prev_start;
|
|
*current_substring++ = '\0';
|
|
|
|
pieces[next_frag].string = sub_start;
|
|
pieces[next_frag].argclass = literal_piece;
|
|
next_frag++;
|
|
|
|
/* Record an end-of-array marker. */
|
|
|
|
pieces[next_frag].string = NULL;
|
|
pieces[next_frag].argclass = literal_piece;
|
|
|
|
return pieces;
|
|
}
|
|
|
|
void
|
|
free_format_pieces (struct format_piece *pieces)
|
|
{
|
|
if (!pieces)
|
|
return;
|
|
|
|
/* We happen to know that all the string pieces are in the block
|
|
pointed to by the first string piece. */
|
|
if (pieces[0].string)
|
|
xfree (pieces[0].string);
|
|
|
|
xfree (pieces);
|
|
}
|
|
|
|
void
|
|
free_format_pieces_cleanup (void *ptr)
|
|
{
|
|
struct format_piece **location = (struct format_piece **) ptr;
|
|
|
|
if (location == NULL)
|
|
return;
|
|
|
|
if (*location != NULL)
|
|
{
|
|
free_format_pieces (*location);
|
|
*location = NULL;
|
|
}
|
|
}
|
|
|