diff options
author | Pedro Alves <palves@redhat.com> | 2019-06-05 09:17:16 +0100 |
---|---|---|
committer | Tom Tromey <tom@tromey.com> | 2019-10-01 15:12:38 -0600 |
commit | 2a3c1174c3c0db1140180fb3fc56ac324d1c0a7c (patch) | |
tree | 174cd0bf2a3a09ac1f910d32d8700e947399b955 /gdb/gdbsupport | |
parent | 0dfe5bfbb7e7a3e55c57d1b59c265dc1a3cd9fc7 (diff) | |
download | gdb-2a3c1174c3c0db1140180fb3fc56ac324d1c0a7c.zip gdb-2a3c1174c3c0db1140180fb3fc56ac324d1c0a7c.tar.gz gdb-2a3c1174c3c0db1140180fb3fc56ac324d1c0a7c.tar.bz2 |
Introduce gdb-specific %p format suffixes
This introduces a few gdb-specific %p format suffixes. This is useful
for emitting gdb-specific output in an ergonomic way. It also yields
code that is more i18n-friendly.
The comment before ui_out::message explains the details.
Note that the tests had to change a little. When using one of the gdb
printf functions with styling, there can be spurious style changes
emitted to the output. This did not seem worthwhile to fix, as the
low-level output functions are rather spaghetti-ish already, and I
didn't want to make them even worse.
This change also necessitated adding support for "*" as precision and
width in format_pieces. These are used in various spots in gdb, and
it seemed better to me to implement them than to remove the uses.
gdb/ChangeLog
2019-10-01 Pedro Alves <palves@redhat.com>
Tom Tromey <tom@tromey.com>
* unittests/format_pieces-selftests.c: Add gdb_format parameter.
(test_gdb_formats): New function.
(run_tests): Call it.
(test_format_specifier): Update.
* utils.h (fputs_filtered): Update comment.
(vfprintf_styled, vfprintf_styled_no_gdbfmt)
(fputs_styled_unfiltered): Declare.
* utils.c (fputs_styled_unfiltered): New function.
(vfprintf_maybe_filtered): Add gdbfmt parameter.
(vfprintf_filtered): Update.
(vfprintf_unfiltered, vprintf_filtered): Update.
(vfprintf_styled, vfprintf_styled_no_gdbfmt): New functions.
* ui-out.h (enum ui_out_flag) <unfiltered_output,
disallow_ui_out_field>: New constants.
(enum class field_kind): New.
(struct base_field_s, struct signed_field_s): New.
(signed_field): New function.
(struct string_field_s): New.
(string_field): New function.
(struct styled_string_s): New.
(styled_string): New function.
(class ui_out) <message>: Add comment.
<vmessage, call_do_message>: New methods.
<do_message>: Add style parameter.
* ui-out.c (ui_out::call_do_message, ui_out::vmessage): New
methods.
(ui_out::message): Rewrite.
* mi/mi-out.h (class mi_ui_out) <do_message>: Add style
parameter.
* mi/mi-out.c (mi_ui_out::do_message): Add style parameter.
* gdbsupport/format.h (class format_pieces) <format_pieces>: Add
gdb_extensions parameter.
(class format_piece): Add parameter to constructor.
(n_int_args): New field.
* gdbsupport/format.c (format_pieces::format_pieces): Add
gdb_extensions parameter. Handle '*'.
* cli-out.h (class cli_ui_out) <do_message>: Add style parameter.
* cli-out.c (cli_ui_out::do_message): Add style parameter. Call
vfprintf_styled_no_gdbfmt.
(cli_ui_out::do_field_string, cli_ui_out::do_spaces)
(cli_ui_out::do_text, cli_ui_out::field_separator): Allow
unfiltered output.
* ui-style.h (struct ui_file_style) <ptr>: New method.
gdb/testsuite/ChangeLog
2019-10-01 Tom Tromey <tom@tromey.com>
* gdb.base/style.exp: Update tests.
Diffstat (limited to 'gdb/gdbsupport')
-rw-r--r-- | gdb/gdbsupport/format.c | 166 | ||||
-rw-r--r-- | gdb/gdbsupport/format.h | 11 |
2 files changed, 111 insertions, 66 deletions
diff --git a/gdb/gdbsupport/format.c b/gdb/gdbsupport/format.c index a5a3670..1e80350 100644 --- a/gdb/gdbsupport/format.c +++ b/gdb/gdbsupport/format.c @@ -20,10 +20,10 @@ #include "common-defs.h" #include "format.h" -format_pieces::format_pieces (const char **arg) +format_pieces::format_pieces (const char **arg, bool gdb_extensions) { const char *s; - char *f, *string; + const char *string; const char *prev_start; const char *percent_loc; char *sub_start, *current_substring; @@ -31,70 +31,79 @@ format_pieces::format_pieces (const char **arg) s = *arg; - /* Parse the format-control string and copy it into the string STRING, - processing some kinds of escape sequence. */ + if (gdb_extensions) + { + string = *arg; + *arg += strlen (*arg); + } + else + { + /* 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); + char *f = (char *) alloca (strlen (s) + 1); + string = f; - while (*s != '"' && *s != '\0') - { - int c = *s++; - switch (c) + while ((gdb_extensions || *s != '"') && *s != '\0') { - case '\0': - continue; - - case '\\': - switch (c = *s++) + int c = *s++; + switch (c) { + case '\0': + continue; + case '\\': - *f++ = '\\'; - break; - case 'a': - *f++ = '\a'; - break; - case 'b': - *f++ = '\b'; - break; - case 'e': - *f++ = '\e'; - 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++ = '"'; + switch (c = *s++) + { + case '\\': + *f++ = '\\'; + break; + case 'a': + *f++ = '\a'; + break; + case 'b': + *f++ = '\b'; + break; + case 'e': + *f++ = '\e'; + 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: - /* ??? TODO: handle other escape sequences. */ - error (_("Unrecognized escape character \\%c in format string."), - c); + *f++ = c; } - break; - - default: - *f++ = c; } - } - /* Terminate our escape-processed copy. */ - *f++ = '\0'; + /* 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; + /* 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. */ @@ -105,7 +114,7 @@ format_pieces::format_pieces (const char **arg) argclass classifies the %-specs so we can give printf-type functions something of the right size. */ - f = string; + const char *f = string; prev_start = string; while (*f) if (*f++ == '%') @@ -115,6 +124,7 @@ format_pieces::format_pieces (const char **arg) 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; + int n_int_args = 0; /* Skip over "%%", it will become part of a literal piece. */ if (*f == '%') @@ -130,7 +140,7 @@ format_pieces::format_pieces (const char **arg) *current_substring++ = '\0'; if (*sub_start != '\0') - m_pieces.emplace_back (sub_start, literal_piece); + m_pieces.emplace_back (sub_start, literal_piece, 0); percent_loc = f - 1; @@ -155,16 +165,32 @@ format_pieces::format_pieces (const char **arg) } /* The next part of a format specifier is a width. */ - while (*f != '\0' && strchr ("0123456789", *f)) - f++; + if (gdb_extensions && *f == '*') + { + ++f; + ++n_int_args; + } + else + { + 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++; + if (gdb_extensions && *f == '*') + { + ++f; + ++n_int_args; + } + else + { + while (*f != '\0' && strchr ("0123456789", *f)) + f++; + } } /* The next part of a format specifier is a length modifier. */ @@ -252,6 +278,20 @@ format_pieces::format_pieces (const char **arg) bad = 1; if (seen_hash || seen_zero || seen_space || seen_plus) bad = 1; + + if (gdb_extensions) + { + switch (f[1]) + { + case 's': + case 'F': + case '[': + case ']': + f++; + break; + } + } + break; case 's': @@ -336,7 +376,7 @@ format_pieces::format_pieces (const char **arg) prev_start = f; - m_pieces.emplace_back (sub_start, this_argclass); + m_pieces.emplace_back (sub_start, this_argclass, n_int_args); } /* Record the remainder of the string. */ @@ -349,6 +389,6 @@ format_pieces::format_pieces (const char **arg) current_substring += f - prev_start; *current_substring++ = '\0'; - m_pieces.emplace_back (sub_start, literal_piece); + m_pieces.emplace_back (sub_start, literal_piece, 0); } } diff --git a/gdb/gdbsupport/format.h b/gdb/gdbsupport/format.h index 08ef66a..e2a47ba 100644 --- a/gdb/gdbsupport/format.h +++ b/gdb/gdbsupport/format.h @@ -50,9 +50,10 @@ enum argclass struct format_piece { - format_piece (const char *str, enum argclass argc) + format_piece (const char *str, enum argclass argc, int n) : string (str), - argclass (argc) + argclass (argc), + n_int_args (n) { } @@ -64,13 +65,17 @@ struct format_piece const char *string; enum argclass argclass; + /* Count the number of preceding 'int' arguments that must be passed + along. This is used for a width or precision of '*'. Note that + this feature is only available in "gdb_extensions" mode. */ + int n_int_args; }; class format_pieces { public: - format_pieces (const char **arg); + format_pieces (const char **arg, bool gdb_extensions = false); ~format_pieces () = default; DISABLE_COPY_AND_ASSIGN (format_pieces); |