Add add_cmd function overloads
authorTom Tromey <tom@tromey.com>
Sun, 10 Sep 2017 02:51:33 +0000 (20:51 -0600)
committerTom Tromey <tom@tromey.com>
Wed, 27 Sep 2017 14:44:14 +0000 (08:44 -0600)
This adds two add_cmd overloads: one whose callback takes a const char *,
and one that doesn't accept a function at all.  The no-function overload
was introduced to avoid ambiguity when NULL was passed as the function.

Long term the goal is for all commands to take const arguments, and
for the non-const variants to be removed entirely.

gdb/ChangeLog
2017-09-27  Tom Tromey  <tom@tromey.com>

* cli/cli-decode.c (add_cmd, set_cmd_cfunc): New function
overloads.
(do_add_cmd): Rename from add_cmd.  Don't call set_cmd_cfunc.
(do_const_cfunc): New function.
(cmd_cfunc_eq): New overload.
(cli_user_command_p): Check do_const_cfunc.
* cli/cli-decode.h (struct cmd_list_element) <function>: New field
const_cfunc.
* command.h (add_cmd): Add const overload and no-function
overload.
(set_cmd_cfunc): Add const overload.
(cmd_const_cfunc_ftype): Declare.
(cmd_cfunc_eq): Add const overload.
* breakpoint.c, cli-cmds.c, cli-dump.c, guile/scm-cmd.c,
python/py-cmd.c, target.c, tracepoint.c: Use no-function add_cmd
overload.

gdb/ChangeLog
gdb/breakpoint.c
gdb/cli/cli-cmds.c
gdb/cli/cli-decode.c
gdb/cli/cli-decode.h
gdb/cli/cli-dump.c
gdb/command.h
gdb/guile/scm-cmd.c
gdb/python/py-cmd.c
gdb/target.c
gdb/tracepoint.c

index a12eebe891f4f6fce92ee6c9a2709294e31f823c..54bc5b0b295c4e7dec290c1f8cff548da784f677 100644 (file)
@@ -1,3 +1,22 @@
+2017-09-27  Tom Tromey  <tom@tromey.com>
+
+       * cli/cli-decode.c (add_cmd, set_cmd_cfunc): New function
+       overloads.
+       (do_add_cmd): Rename from add_cmd.  Don't call set_cmd_cfunc.
+       (do_const_cfunc): New function.
+       (cmd_cfunc_eq): New overload.
+       (cli_user_command_p): Check do_const_cfunc.
+       * cli/cli-decode.h (struct cmd_list_element) <function>: New field
+       const_cfunc.
+       * command.h (add_cmd): Add const overload and no-function
+       overload.
+       (set_cmd_cfunc): Add const overload.
+       (cmd_const_cfunc_ftype): Declare.
+       (cmd_cfunc_eq): Add const overload.
+       * breakpoint.c, cli-cmds.c, cli-dump.c, guile/scm-cmd.c,
+       python/py-cmd.c, target.c, tracepoint.c: Use no-function add_cmd
+       overload.
+
 2017-09-27  Tom Tromey  <tom@tromey.com>
 
        * macroexp.c (get_next_token_for_substitution): New function.
index 8585f5e8cb87834bef994bb23c4f74f52ae911db..5549fe735810b03f6b2221b17ad12bf4e7f54dc6 100644 (file)
@@ -15414,13 +15414,13 @@ add_catch_command (const char *name, const char *docstring,
 {
   struct cmd_list_element *command;
 
-  command = add_cmd (name, class_breakpoint, NULL, docstring,
+  command = add_cmd (name, class_breakpoint, docstring,
                     &catch_cmdlist);
   set_cmd_sfunc (command, sfunc);
   set_cmd_context (command, user_data_catch);
   set_cmd_completer (command, completer);
 
-  command = add_cmd (name, class_breakpoint, NULL, docstring,
+  command = add_cmd (name, class_breakpoint, docstring,
                     &tcatch_cmdlist);
   set_cmd_sfunc (command, sfunc);
   set_cmd_context (command, user_data_tcatch);
index cbafb13837f9b44413f28a2c3779c2272c5da28b..67910be2e51343c750aed23c954451c4981aecfb 100644 (file)
@@ -1693,28 +1693,28 @@ _initialize_cli_cmds (void)
   /* Define the classes of commands.
      They will appear in the help list in alphabetical order.  */
 
-  add_cmd ("internals", class_maintenance, NULL, _("\
+  add_cmd ("internals", class_maintenance, _("\
 Maintenance commands.\n\
 Some gdb commands are provided just for use by gdb maintainers.\n\
 These commands are subject to frequent change, and may not be as\n\
 well documented as user commands."),
           &cmdlist);
-  add_cmd ("obscure", class_obscure, NULL, _("Obscure features."), &cmdlist);
-  add_cmd ("aliases", class_alias, NULL,
+  add_cmd ("obscure", class_obscure, _("Obscure features."), &cmdlist);
+  add_cmd ("aliases", class_alias,
           _("Aliases of other commands."), &cmdlist);
-  add_cmd ("user-defined", class_user, NULL, _("\
+  add_cmd ("user-defined", class_user, _("\
 User-defined commands.\n\
 The commands in this class are those defined by the user.\n\
 Use the \"define\" command to define a command."), &cmdlist);
-  add_cmd ("support", class_support, NULL, _("Support facilities."), &cmdlist);
+  add_cmd ("support", class_support, _("Support facilities."), &cmdlist);
   if (!dbx_commands)
-    add_cmd ("status", class_info, NULL, _("Status inquiries."), &cmdlist);
-  add_cmd ("files", class_files, NULL, _("Specifying and examining files."),
+    add_cmd ("status", class_info, _("Status inquiries."), &cmdlist);
+  add_cmd ("files", class_files, _("Specifying and examining files."),
           &cmdlist);
-  add_cmd ("breakpoints", class_breakpoint, NULL,
+  add_cmd ("breakpoints", class_breakpoint,
           _("Making program stop at certain points."), &cmdlist);
-  add_cmd ("data", class_vars, NULL, _("Examining data."), &cmdlist);
-  add_cmd ("stack", class_stack, NULL, _("\
+  add_cmd ("data", class_vars, _("Examining data."), &cmdlist);
+  add_cmd ("stack", class_stack, _("\
 Examining the stack.\n\
 The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
 counting from zero for the innermost (currently executing) frame.\n\n\
@@ -1723,7 +1723,7 @@ Variable lookups are done with respect to the selected frame.\n\
 When the program being debugged stops, gdb selects the innermost frame.\n\
 The commands below can be used to select other frames by number or address."),
           &cmdlist);
-  add_cmd ("running", class_run, NULL, _("Running the program."), &cmdlist);
+  add_cmd ("running", class_run, _("Running the program."), &cmdlist);
 
   /* Define general commands.  */
 
index 1bbbe46c3c1a60b1bf3f1c1b83ecdf03e76e38a3..257a9c4a8086dec85c0fddd727225d65f6bfb69e 100644 (file)
@@ -103,7 +103,7 @@ print_help_for_command (struct cmd_list_element *c, const char *prefix,
 static void
 do_cfunc (struct cmd_list_element *c, char *args, int from_tty)
 {
-  c->function.cfunc (args, from_tty); /* Ok.  */
+  c->function.cfunc (args, from_tty);
 }
 
 void
@@ -113,13 +113,29 @@ set_cmd_cfunc (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
     cmd->func = NULL;
   else
     cmd->func = do_cfunc;
-  cmd->function.cfunc = cfunc; /* Ok.  */
+  cmd->function.cfunc = cfunc;
+}
+
+static void
+do_const_cfunc (struct cmd_list_element *c, char *args, int from_tty)
+{
+  c->function.const_cfunc (args, from_tty);
+}
+
+void
+set_cmd_cfunc (struct cmd_list_element *cmd, cmd_const_cfunc_ftype *cfunc)
+{
+  if (cfunc == NULL)
+    cmd->func = NULL;
+  else
+    cmd->func = do_const_cfunc;
+  cmd->function.const_cfunc = cfunc;
 }
 
 static void
 do_sfunc (struct cmd_list_element *c, char *args, int from_tty)
 {
-  c->function.sfunc (args, from_tty, c); /* Ok.  */
+  c->function.sfunc (args, from_tty, c);
 }
 
 void
@@ -129,7 +145,7 @@ set_cmd_sfunc (struct cmd_list_element *cmd, cmd_sfunc_ftype *sfunc)
     cmd->func = NULL;
   else
     cmd->func = do_sfunc;
-  cmd->function.sfunc = sfunc; /* Ok.  */
+  cmd->function.sfunc = sfunc;
 }
 
 int
@@ -138,6 +154,12 @@ cmd_cfunc_eq (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
   return cmd->func == do_cfunc && cmd->function.cfunc == cfunc;
 }
 
+int
+cmd_cfunc_eq (struct cmd_list_element *cmd, cmd_const_cfunc_ftype *cfunc)
+{
+  return cmd->func == do_const_cfunc && cmd->function.const_cfunc == cfunc;
+}
+
 void
 set_cmd_context (struct cmd_list_element *cmd, void *context)
 {
@@ -189,9 +211,9 @@ set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
    Returns a pointer to the added command (not necessarily the head 
    of *LIST).  */
 
-struct cmd_list_element *
-add_cmd (const char *name, enum command_class theclass, cmd_cfunc_ftype *fun,
-        const char *doc, struct cmd_list_element **list)
+static struct cmd_list_element *
+do_add_cmd (const char *name, enum command_class theclass,
+           const char *doc, struct cmd_list_element **list)
 {
   struct cmd_list_element *c = XNEW (struct cmd_list_element);
   struct cmd_list_element *p, *iter;
@@ -229,7 +251,6 @@ add_cmd (const char *name, enum command_class theclass, cmd_cfunc_ftype *fun,
 
   c->name = name;
   c->theclass = theclass;
-  set_cmd_cfunc (c, fun);
   set_cmd_context (c, NULL);
   c->doc = doc;
   c->cmd_deprecated = 0;
@@ -259,6 +280,35 @@ add_cmd (const char *name, enum command_class theclass, cmd_cfunc_ftype *fun,
   return c;
 }
 
+struct cmd_list_element *
+add_cmd (const char *name, enum command_class theclass, cmd_cfunc_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);
+  return result;
+}
+
+struct cmd_list_element *
+add_cmd (const char *name, enum command_class theclass,
+        const char *doc, struct cmd_list_element **list)
+{
+  cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
+  result->func = NULL;
+  result->function.cfunc = NULL; /* Ok.  */
+  return result;
+}
+
+struct cmd_list_element *
+add_cmd (const char *name, enum command_class theclass,
+        cmd_const_cfunc_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);
+  return result;
+}
+
 /* Deprecates a command CMD.
    REPLACEMENT is the name of the command which should be used in
    place of this command, or NULL if no such command exists.
@@ -301,7 +351,7 @@ add_alias_cmd (const char *name, cmd_list_element *old,
       return 0;
     }
 
-  struct cmd_list_element *c = add_cmd (name, theclass, NULL, old->doc, list);
+  struct cmd_list_element *c = add_cmd (name, theclass, old->doc, list);
 
   /* If OLD->DOC can be freed, we should make another copy.  */
   if (old->doc_allocated)
@@ -419,7 +469,7 @@ add_set_or_show_cmd (const char *name,
                     const char *doc,
                     struct cmd_list_element **list)
 {
-  struct cmd_list_element *c = add_cmd (name, theclass, NULL, doc, list);
+  struct cmd_list_element *c = add_cmd (name, theclass, doc, list);
 
   gdb_assert (type == set_cmd || type == show_cmd);
   c->type = type;
@@ -1909,5 +1959,6 @@ int
 cli_user_command_p (struct cmd_list_element *cmd)
 {
   return (cmd->theclass == class_user
-         && (cmd->func == do_cfunc || cmd->func == do_sfunc));
+         && (cmd->func == do_cfunc || cmd->func == do_sfunc
+             || cmd->func == do_const_cfunc));
 }
index 50b858c13fcfee7eb174b9397008cc52a05a4811..691bfe3f8fc88370de349d38cbfa449e2b053a62 100644 (file)
@@ -114,6 +114,8 @@ struct cmd_list_element
       {
        /* If type is not_set_cmd, call it like this: */
        cmd_cfunc_ftype *cfunc;
+       /* ... or like this.  */
+       cmd_const_cfunc_ftype *const_cfunc;
        /* If type is set_cmd or show_cmd, first set the variables,
           and then call this: */
        cmd_sfunc_ftype *sfunc;
index 8d57119751e84c3d187d2a81a9d58d1c64459194..550548a2c409f631e67f400f01c7de8a5ca63c36 100644 (file)
@@ -360,7 +360,7 @@ add_dump_command (const char *name,
   struct cmd_list_element *c;
   struct dump_context *d;
 
-  c = add_cmd (name, all_commands, NULL, descr, &dump_cmdlist);
+  c = add_cmd (name, all_commands, descr, &dump_cmdlist);
   c->completer =  filename_completer;
   d = XNEW (struct dump_context);
   d->func = func;
@@ -368,7 +368,7 @@ add_dump_command (const char *name,
   set_cmd_context (c, d);
   c->func = call_dump_func;
 
-  c = add_cmd (name, all_commands, NULL, descr, &append_cmdlist);
+  c = add_cmd (name, all_commands, descr, &append_cmdlist);
   c->completer =  filename_completer;
   d = XNEW (struct dump_context);
   d->func = func;
index 3a4a449eb01742635d4f58cc2e294de7034b1d46..3a6c58961ad34e59148201448fed9cf17880797c 100644 (file)
@@ -117,6 +117,7 @@ var_types;
 struct cmd_list_element;
 
 typedef void cmd_cfunc_ftype (char *args, int from_tty);
+typedef void cmd_const_cfunc_ftype (const char *args, int from_tty);
 
 /* This structure specifies notifications to be suppressed by a cli
    command interpreter.  */
@@ -140,6 +141,19 @@ extern struct cmd_list_element *add_cmd (const char *, enum command_class,
                                         const char *,
                                         struct cmd_list_element **);
 
+/* Const-correct variant of the above.  */
+
+extern struct cmd_list_element *add_cmd (const char *, enum command_class,
+                                        cmd_const_cfunc_ftype *fun,
+                                        const char *,
+                                        struct cmd_list_element **);
+
+/* Like add_cmd, but no command function is specified.  */
+
+extern struct cmd_list_element *add_cmd (const char *, enum command_class,
+                                        const char *,
+                                        struct cmd_list_element **);
+
 extern struct cmd_list_element *add_alias_cmd (const char *, const char *,
                                               enum command_class, int,
                                               struct cmd_list_element **);
@@ -171,6 +185,11 @@ extern struct cmd_list_element *add_abbrev_prefix_cmd (const char *,
 extern void set_cmd_cfunc (struct cmd_list_element *cmd,
                           cmd_cfunc_ftype *cfunc);
 
+/* Const-correct variant of the above.  */
+
+extern void set_cmd_cfunc (struct cmd_list_element *cmd,
+                          cmd_const_cfunc_ftype *cfunc);
+
 typedef void cmd_sfunc_ftype (char *args, int from_tty,
                              struct cmd_list_element *c);
 extern void set_cmd_sfunc (struct cmd_list_element *cmd,
@@ -205,6 +224,8 @@ extern void set_cmd_completer_handle_brkchars (struct cmd_list_element *,
    around in cmd objects to test the value of the commands sfunc().  */
 extern int cmd_cfunc_eq (struct cmd_list_element *cmd,
                         cmd_cfunc_ftype *cfun);
+extern int cmd_cfunc_eq (struct cmd_list_element *cmd,
+                        cmd_const_cfunc_ftype *cfun);
 
 /* Each command object has a local context attached to it.  */
 extern void set_cmd_context (struct cmd_list_element *cmd,
index 5501d31063447c68ccb7262d3e6e798eb0e0a692..0bd5105da5b60b5fc4cfc937940657bbb962a6c0 100644 (file)
@@ -774,7 +774,7 @@ gdbscm_register_command_x (SCM self)
       else
        {
          cmd = add_cmd (c_smob->cmd_name, c_smob->cmd_class,
-                        NULL, c_smob->doc, cmd_list);
+                        c_smob->doc, cmd_list);
        }
     }
   CATCH (except, RETURN_MASK_ALL)
index 2a7c613f18fc0d218f7bf4647b1912053107ff7c..d969b12157313d12658ad6ff82f38d7b286414e4 100644 (file)
@@ -612,7 +612,7 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
                                pfx_name, allow_unknown, cmd_list);
        }
       else
-       cmd = add_cmd (cmd_name, (enum command_class) cmdtype, NULL,
+       cmd = add_cmd (cmd_name, (enum command_class) cmdtype,
                       docstring, cmd_list);
 
       /* There appears to be no API to set this.  */
index 5a2c087483cc210b2fcd83f4f18bbadf7ebac3db..c26fba75b52dbb837f21b89b96a2f1e54acda3c1 100644 (file)
@@ -386,7 +386,7 @@ Remaining arguments are interpreted by the target protocol.  For more\n\
 information on the arguments for a particular protocol, type\n\
 `help target ' followed by the protocol name."),
                    &targetlist, "target ", 0, &cmdlist);
-  c = add_cmd (t->to_shortname, no_class, NULL, t->to_doc, &targetlist);
+  c = add_cmd (t->to_shortname, no_class, t->to_doc, &targetlist);
   set_cmd_sfunc (c, open_target);
   set_cmd_context (c, t);
   if (completer != NULL)
@@ -411,7 +411,7 @@ add_deprecated_target_alias (struct target_ops *t, const char *alias)
 
   /* If we use add_alias_cmd, here, we do not get the deprecated warning,
      see PR cli/15104.  */
-  c = add_cmd (alias, no_class, NULL, t->to_doc, &targetlist);
+  c = add_cmd (alias, no_class, t->to_doc, &targetlist);
   set_cmd_sfunc (c, open_target);
   set_cmd_context (c, t);
   alt = xstrprintf ("target %s", t->to_shortname);
index 437db59f442d2b64dfe3964f2f8f81bbffe47ff2..12cc2fb235e0fce83643de2e42ab794b40e4cf98 100644 (file)
@@ -4203,7 +4203,7 @@ _initialize_tracepoint (void)
   add_info ("scope", info_scope_command,
            _("List the variables local to a scope"));
 
-  add_cmd ("tracepoints", class_trace, NULL,
+  add_cmd ("tracepoints", class_trace,
           _("Tracing of program execution without stopping the program."),
           &cmdlist);
 
This page took 0.049903 seconds and 4 git commands to generate.