From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Simplify macro_define_command
Date: Mon, 22 Apr 2024 09:49:23 -0600 [thread overview]
Message-ID: <20240422154923.1601231-1-tromey@adacore.com> (raw)
This simplifies macro_define_command, using std::vector to remove some
manual memory management. temporary_macro_definition is also
simplified -- much of the code there was simply unnecessary.
Regression tested on x86-64 Fedora 38.
---
gdb/macrocmd.c | 55 +++++++++++++++-----------------------------------
1 file changed, 16 insertions(+), 39 deletions(-)
diff --git a/gdb/macrocmd.c b/gdb/macrocmd.c
index ddabfede0f5..0d0db5d1b90 100644
--- a/gdb/macrocmd.c
+++ b/gdb/macrocmd.c
@@ -308,33 +308,24 @@ extract_identifier (const char **expp, int is_parameter)
return gdb::unique_xmalloc_ptr<char> (result);
}
-struct temporary_macro_definition : public macro_definition
+struct temporary_macro_definition
{
- temporary_macro_definition ()
+ ~temporary_macro_definition ()
{
- table = nullptr;
- kind = macro_object_like;
- argc = 0;
- argv = nullptr;
- replacement = nullptr;
+ free_vector_argv (argv);
}
- ~temporary_macro_definition ()
+ void add_argument (gdb::unique_xmalloc_ptr<char> &&value)
{
- int i;
-
- for (i = 0; i < argc; ++i)
- xfree ((char *) argv[i]);
- xfree ((char *) argv);
- /* Note that the 'replacement' field is not allocated. */
+ argv.push_back (value.release ());
}
+
+ std::vector<char *> argv;
};
static void
macro_define_command (const char *exp, int from_tty)
{
- temporary_macro_definition new_macro;
-
if (!exp)
error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
@@ -344,13 +335,7 @@ macro_define_command (const char *exp, int from_tty)
error (_("Invalid macro name."));
if (*exp == '(')
{
- /* Function-like macro. */
- int alloced = 5;
- char **argv = XNEWVEC (char *, alloced);
-
- new_macro.kind = macro_function_like;
- new_macro.argc = 0;
- new_macro.argv = (const char * const *) argv;
+ temporary_macro_definition new_macro;
/* Skip the '(' and whitespace. */
++exp;
@@ -358,26 +343,18 @@ macro_define_command (const char *exp, int from_tty)
while (*exp != ')')
{
- int i;
-
- if (new_macro.argc == alloced)
- {
- alloced *= 2;
- argv = (char **) xrealloc (argv, alloced * sizeof (char *));
- /* Must update new_macro as well... */
- new_macro.argv = (const char * const *) argv;
- }
- argv[new_macro.argc] = extract_identifier (&exp, 1).release ();
- if (! argv[new_macro.argc])
+ auto arg = extract_identifier (&exp, 1);
+ if (! arg)
error (_("Macro is missing an argument."));
- ++new_macro.argc;
- for (i = new_macro.argc - 2; i >= 0; --i)
+ for (const char *existing : new_macro.argv)
{
- if (! strcmp (argv[i], argv[new_macro.argc - 1]))
+ if (! strcmp (existing, arg.get ()))
error (_("Two macro arguments with identical names."));
}
+ new_macro.add_argument (std::move (arg));
+
skip_ws (&exp);
if (*exp == ',')
{
@@ -392,8 +369,8 @@ macro_define_command (const char *exp, int from_tty)
skip_ws (&exp);
macro_define_function (macro_main (macro_user_macros), -1, name.get (),
- new_macro.argc, (const char **) new_macro.argv,
- exp);
+ new_macro.argv.size (),
+ (const char **) new_macro.argv.data (), exp);
}
else
{
--
2.43.0
next reply other threads:[~2024-04-22 15:49 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-22 15:49 Tom Tromey [this message]
2024-04-22 16:46 ` Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240422154923.1601231-1-tromey@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).