gdb: rename cfunc to simple_func

After browsing the CLI code for quite a while and trying really hard, I
reached the conclusion that I can't give a meaningful explanation of
what "sfunc" and "cfunc" functions are, in cmd_list_element.  I don't
see a logic at all.  That makes it very difficult to do any kind of
change.  Unless somebody can make sense out of all that, I'd like to try
to retro-fit some logic in the cmd_list_element callback function code
so that we can understand what is going on, do some cleanups and add new
features.

The first change is about "cfunc".  I can't figure out what the "c" in
cfunc means.  It's not const, because there's already "const" in
"cmd_const_cfunc_ftype", and the previous "cmd_cfunc_ftype" had nothing
const..  It's not "cmd" or "command", because there's already "cmd" in
"cmd_const_cfunc_ftype".

The "main" command callback, cmd_list_element::func, has three
parameters, whereas cfunc has two.  It is missing the cmd_list_element
parameter.  So the only reason I see for cfunc to exist is to be a shim
between the three and two parameter versions.  Most commands don't need
to receive the cmd_list_element object, so adding it everywhere would be
long and would just add more unnecessary boilerplate.  So since this is
the "simple" version of the callback, compared to the "full", I suggest
renaming cmd_const_cfunc_ftype into cmd_simple_func_ftype, as well as
everything (like the utility functions) that goes with it.

Change-Id: I4e46cacfd77a66bc1cbf683f6a362072504b7868
This commit is contained in:
Simon Marchi 2021-06-29 21:47:23 -04:00
parent 4e93ea6e67
commit 3a553c80da
5 changed files with 53 additions and 44 deletions

View file

@ -94,22 +94,23 @@ print_help_for_command (struct cmd_list_element *c,
/* Set the callback function for the specified command. For each both
the commands callback and func() are set. The latter set to a
bounce function (unless cfunc / sfunc is NULL that is). */
bounce function (unless simple_func / sfunc is NULL that is). */
static void
do_const_cfunc (struct cmd_list_element *c, const char *args, int from_tty)
do_simple_func (struct cmd_list_element *c, const char *args, int from_tty)
{
c->function.const_cfunc (args, from_tty);
c->function.simple_func (args, from_tty);
}
static void
set_cmd_cfunc (struct cmd_list_element *cmd, cmd_const_cfunc_ftype *cfunc)
set_cmd_simple_func (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
{
if (cfunc == NULL)
if (simple_func == NULL)
cmd->func = NULL;
else
cmd->func = do_const_cfunc;
cmd->function.const_cfunc = cfunc;
cmd->func = do_simple_func;
cmd->function.simple_func = simple_func;
}
static void
@ -129,9 +130,10 @@ set_cmd_sfunc (struct cmd_list_element *cmd, cmd_const_sfunc_ftype *sfunc)
}
int
cmd_cfunc_eq (struct cmd_list_element *cmd, cmd_const_cfunc_ftype *cfunc)
cmd_simple_func_eq (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
{
return cmd->func == do_const_cfunc && cmd->function.const_cfunc == cfunc;
return (cmd->func == do_simple_func
&& cmd->function.simple_func == simple_func);
}
void
@ -238,17 +240,17 @@ add_cmd (const char *name, enum command_class theclass,
{
cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
result->func = NULL;
result->function.const_cfunc = NULL;
result->function.simple_func = NULL;
return result;
}
struct cmd_list_element *
add_cmd (const char *name, enum command_class theclass,
cmd_const_cfunc_ftype *fun,
cmd_simple_func_ftype *fun,
const char *doc, struct cmd_list_element **list)
{
cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
set_cmd_cfunc (result, fun);
set_cmd_simple_func (result, fun);
return result;
}
@ -256,7 +258,7 @@ add_cmd (const char *name, enum command_class theclass,
struct cmd_list_element *
add_cmd_suppress_notification (const char *name, enum command_class theclass,
cmd_const_cfunc_ftype *fun, const char *doc,
cmd_simple_func_ftype *fun, const char *doc,
struct cmd_list_element **list,
int *suppress_notification)
{
@ -359,7 +361,7 @@ update_prefix_field_of_prefixed_commands (struct cmd_list_element *c)
struct cmd_list_element *
add_prefix_cmd (const char *name, enum command_class theclass,
cmd_const_cfunc_ftype *fun,
cmd_simple_func_ftype *fun,
const char *doc, struct cmd_list_element **subcommands,
int allow_unknown, struct cmd_list_element **list)
{
@ -432,7 +434,7 @@ add_show_prefix_cmd (const char *name, enum command_class theclass,
struct cmd_list_element *
add_prefix_cmd_suppress_notification
(const char *name, enum command_class theclass,
cmd_const_cfunc_ftype *fun,
cmd_simple_func_ftype *fun,
const char *doc, struct cmd_list_element **subcommands,
int allow_unknown, struct cmd_list_element **list,
int *suppress_notification)
@ -448,7 +450,7 @@ add_prefix_cmd_suppress_notification
struct cmd_list_element *
add_abbrev_prefix_cmd (const char *name, enum command_class theclass,
cmd_const_cfunc_ftype *fun, const char *doc,
cmd_simple_func_ftype *fun, const char *doc,
struct cmd_list_element **subcommands,
int allow_unknown, struct cmd_list_element **list)
{
@ -460,7 +462,7 @@ add_abbrev_prefix_cmd (const char *name, enum command_class theclass,
return c;
}
/* This is an empty "cfunc". */
/* This is an empty "simple func". */
void
not_just_help_class_command (const char *args, int from_tty)
{
@ -951,7 +953,7 @@ delete_cmd (const char *name, struct cmd_list_element **list,
/* Add an element to the list of info subcommands. */
struct cmd_list_element *
add_info (const char *name, cmd_const_cfunc_ftype *fun, const char *doc)
add_info (const char *name, cmd_simple_func_ftype *fun, const char *doc)
{
return add_cmd (name, class_info, fun, doc, &infolist);
}
@ -968,7 +970,7 @@ add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag)
struct cmd_list_element *
add_com (const char *name, enum command_class theclass,
cmd_const_cfunc_ftype *fun,
cmd_simple_func_ftype *fun,
const char *doc)
{
return add_cmd (name, theclass, fun, doc, &cmdlist);
@ -990,7 +992,7 @@ add_com_alias (const char *name, cmd_list_element *target,
struct cmd_list_element *
add_com_suppress_notification (const char *name, enum command_class theclass,
cmd_const_cfunc_ftype *fun, const char *doc,
cmd_simple_func_ftype *fun, const char *doc,
int *suppress_notification)
{
return add_cmd_suppress_notification (name, theclass, fun, doc,
@ -2167,5 +2169,5 @@ int
cli_user_command_p (struct cmd_list_element *cmd)
{
return (cmd->theclass == class_user
&& (cmd->func == do_const_cfunc || cmd->func == do_sfunc));
&& (cmd->func == do_simple_func || cmd->func == do_sfunc));
}