diff options
author | Tom Tromey <tom@tromey.com> | 2017-11-22 20:17:28 -0700 |
---|---|---|
committer | Tom Tromey <tom@tromey.com> | 2017-12-08 10:23:43 -0700 |
commit | 8e481c3ba86e512b39b16b41de24e87a17f7d968 (patch) | |
tree | bfad22d6b383d85c5b82aa4356af50c81e7bd959 /gdb/common | |
parent | 10af2a65c8891435d0d63411a3e694cc74c9447c (diff) | |
download | gdb-8e481c3ba86e512b39b16b41de24e87a17f7d968.zip gdb-8e481c3ba86e512b39b16b41de24e87a17f7d968.tar.gz gdb-8e481c3ba86e512b39b16b41de24e87a17f7d968.tar.bz2 |
C++-ify parse_format_string
This replaces parse_format_string with a class, removing some
constructors along the way. While doing this, I found that one
argument to gen_printf is unused, so I removed it.
Also, I am not completely sure, but the use of `release' in
maint_agent_printf_command and parse_cmd_to_aexpr seems like it may
leak expressions.
Regression tested by the buildbot.
ChangeLog
2017-12-08 Tom Tromey <tom@tromey.com>
* printcmd.c (ui_printf): Update. Use std::vector.
* common/format.h (struct format_piece): Add constructor.
<string>: Now const.
(class format_pieces): New class.
(parse_format_string, free_format_pieces)
(free_format_pieces_cleanup): Remove.
* common/format.c (format_pieces::format_pieces): Rename from
parse_format_string. Update.
(free_format_pieces, free_format_pieces_cleanup): Remove.
* breakpoint.c (parse_cmd_to_aexpr): Update. Use std::vector.
* ax-gdb.h (gen_printf): Remove argument.
* ax-gdb.c (gen_printf): Remove "frags" argument.
(maint_agent_printf_command): Update. Use std::vector.
gdbserver/ChangeLog
2017-12-08 Tom Tromey <tom@tromey.com>
* ax.c (ax_printf): Update.
Diffstat (limited to 'gdb/common')
-rw-r--r-- | gdb/common/format.c | 62 | ||||
-rw-r--r-- | gdb/common/format.h | 42 |
2 files changed, 38 insertions, 66 deletions
diff --git a/gdb/common/format.c b/gdb/common/format.c index 8cb1551..95cb805 100644 --- a/gdb/common/format.c +++ b/gdb/common/format.c @@ -20,17 +20,13 @@ #include "common-defs.h" #include "format.h" -struct format_piece * -parse_format_string (const char **arg) +format_pieces::format_pieces (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; @@ -100,12 +96,7 @@ parse_format_string (const char **arg) /* 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; + m_storage.reset (current_substring); /* 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 @@ -135,9 +126,7 @@ parse_format_string (const char **arg) current_substring += f - 1 - prev_start; *current_substring++ = '\0'; - pieces[next_frag].string = sub_start; - pieces[next_frag].argclass = literal_piece; - next_frag++; + m_pieces.emplace_back (sub_start, literal_piece); percent_loc = f - 1; @@ -343,9 +332,7 @@ parse_format_string (const char **arg) prev_start = f; - pieces[next_frag].string = sub_start; - pieces[next_frag].argclass = this_argclass; - next_frag++; + m_pieces.emplace_back (sub_start, this_argclass); } /* Record the remainder of the string. */ @@ -356,44 +343,5 @@ parse_format_string (const char **arg) 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; + m_pieces.emplace_back (sub_start, literal_piece); } - -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; - } -} - diff --git a/gdb/common/format.h b/gdb/common/format.h index f3a94b8..dd083f9 100644 --- a/gdb/common/format.h +++ b/gdb/common/format.h @@ -48,22 +48,46 @@ enum argclass struct format_piece { - char *string; + format_piece (const char *str, enum argclass argc) + : string (str), + argclass (argc) + { + } + + const char *string; enum argclass argclass; }; -/* Return an array of printf fragments found at the given string, and - rewrite ARG with a pointer to the end of the format string. */ +class format_pieces +{ +public: + + format_pieces (const char **arg); + ~format_pieces () = default; + + DISABLE_COPY_AND_ASSIGN (format_pieces); -extern struct format_piece *parse_format_string (const char **arg); + format_piece &operator[] (size_t index) + { + return m_pieces[index]; + } -/* Given a pointer to an array of format pieces, free any memory that - would have been allocated by parse_format_string. */ + typedef std::vector<format_piece>::iterator iterator; -extern void free_format_pieces (struct format_piece *frags); + iterator begin () + { + return m_pieces.begin (); + } -/* Freeing, cast as a cleanup. */ + iterator end () + { + return m_pieces.end (); + } -extern void free_format_pieces_cleanup (void *); +private: + + std::vector<format_piece> m_pieces; + gdb::unique_xmalloc_ptr<char> m_storage; +}; #endif /* COMMON_FORMAT_H */ |