Add add_internal_function overload
authorTom Tromey <tom@tromey.com>
Fri, 15 Nov 2019 23:49:17 +0000 (16:49 -0700)
committerTom Tromey <tom@tromey.com>
Tue, 26 Nov 2019 21:20:29 +0000 (14:20 -0700)
add_internal_function sets a command destroyer that frees the doc
string.  However, many callers do not pass in an allocated doc string.

This adds a new overload to clearly differentiate the two cases,
fixing the latent bug.

gdb/ChangeLog
2019-11-26  Tom Tromey  <tom@tromey.com>

* value.h (add_internal_function): Add new overload.  Move
documentation from value.h.
* value.c (do_add_internal_function): New function.
(add_internal_function): Use it.  Add new overload.
(function_destroyer): Don't free doc.
* python/py-function.c (fnpy_init): Update.

Change-Id: I3f6df925bc6b3e1bccbad9eeebc487b908bb5a2a

gdb/ChangeLog
gdb/python/py-function.c
gdb/value.c
gdb/value.h

index 821afd313d4c6bd6c731a1ab5b773c4e4f9c6f48..3d8d582d651fb5c99c878af8a1d9cb6dc9c0e0da 100644 (file)
@@ -1,3 +1,12 @@
+2019-11-26  Tom Tromey  <tom@tromey.com>
+
+       * value.h (add_internal_function): Add new overload.  Move
+       documentation from value.h.
+       * value.c (do_add_internal_function): New function.
+       (add_internal_function): Use it.  Add new overload.
+       (function_destroyer): Don't free doc.
+       * python/py-function.c (fnpy_init): Update.
+
 2019-11-26  Tom Tromey  <tom@tromey.com>
 
        * python/py-cmd.c (cmdpy_destroyer): Don't free "doc".
index 46a66cf3ecea46fc957a9747c27bb3cd0a8cf2f6..1c4588780e241ead98411c04435b7a48ada3cf78 100644 (file)
@@ -127,7 +127,7 @@ fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
   if (! docstring)
     docstring.reset (xstrdup (_("This function is not documented.")));
 
-  add_internal_function (name, docstring.release (), fnpy_call,
+  add_internal_function (name, std::move (docstring), fnpy_call,
                         self_ref.release ());
   return 0;
 }
index 35a7a5cdce3d894f0b2d55653bbfe111c993b46f..8e22ac7f8c1bce79605e7679ea2a4a3d5d214314 100644 (file)
@@ -2426,17 +2426,13 @@ static void
 function_destroyer (struct cmd_list_element *self, void *ignore)
 {
   xfree ((char *) self->name);
-  xfree ((char *) self->doc);
 }
 
-/* Add a new internal function.  NAME is the name of the function; DOC
-   is a documentation string describing the function.  HANDLER is
-   called when the function is invoked.  COOKIE is an arbitrary
-   pointer which is passed to HANDLER and is intended for "user
-   data".  */
-void
-add_internal_function (const char *name, const char *doc,
-                      internal_function_fn handler, void *cookie)
+/* Helper function that does the work for add_internal_function.  */
+
+static struct cmd_list_element *
+do_add_internal_function (const char *name, const char *doc,
+                         internal_function_fn handler, void *cookie)
 {
   struct cmd_list_element *cmd;
   struct internal_function *ifn;
@@ -2448,6 +2444,29 @@ add_internal_function (const char *name, const char *doc,
   cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
                 &functionlist);
   cmd->destroyer = function_destroyer;
+
+  return cmd;
+}
+
+/* See value.h.  */
+
+void
+add_internal_function (const char *name, const char *doc,
+                      internal_function_fn handler, void *cookie)
+{
+  do_add_internal_function (name, doc, handler, cookie);
+}
+
+/* See value.h.  */
+
+void
+add_internal_function (const char *name, gdb::unique_xmalloc_ptr<char> &&doc,
+                      internal_function_fn handler, void *cookie)
+{
+  struct cmd_list_element *cmd
+    = do_add_internal_function (name, doc.get (), handler, cookie);
+  doc.release ();
+  cmd->doc_allocated = 1;
 }
 
 /* Update VALUE before discarding OBJFILE.  COPIED_TYPES is used to
index 2b5d784a577a74c957ce8bba16b221f7c3337b66..fdef835f8d749644fc5aca1e85235bb12757edd7 100644 (file)
@@ -1165,9 +1165,22 @@ typedef struct value *(*internal_function_fn) (struct gdbarch *gdbarch,
                                               int argc,
                                               struct value **argv);
 
-void add_internal_function (const char *name, const char *doc,
-                           internal_function_fn handler,
-                           void *cookie);
+/* Add a new internal function.  NAME is the name of the function; DOC
+   is a documentation string describing the function.  HANDLER is
+   called when the function is invoked.  COOKIE is an arbitrary
+   pointer which is passed to HANDLER and is intended for "user
+   data".  */
+
+extern void add_internal_function (const char *name, const char *doc,
+                                  internal_function_fn handler,
+                                  void *cookie);
+
+/* This overload takes an allocated documentation string.  */
+
+extern void add_internal_function (const char *name,
+                                  gdb::unique_xmalloc_ptr<char> &&doc,
+                                  internal_function_fn handler,
+                                  void *cookie);
 
 struct value *call_internal_function (struct gdbarch *gdbarch,
                                      const struct language_defn *language,
This page took 0.03388 seconds and 4 git commands to generate.