public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Add add_internal_function overload
@ 2019-11-26 21:28 Tom Tromey
  0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2019-11-26 21:28 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1a6d41c6433a0980f302c480b1d1db71234b49e4

commit 1a6d41c6433a0980f302c480b1d1db71234b49e4
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Nov 15 16:49:17 2019 -0700

    Add add_internal_function overload
    
    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

Diff:
---
 gdb/ChangeLog            |  9 +++++++++
 gdb/python/py-function.c |  2 +-
 gdb/value.c              | 37 ++++++++++++++++++++++++++++---------
 gdb/value.h              | 19 ++++++++++++++++---
 4 files changed, 54 insertions(+), 13 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 821afd3..3d8d582 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,14 @@
 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".
 	(cmdpy_init): Set "doc_allocated".
 
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index 46a66cf..1c45887 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -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;
 }
diff --git a/gdb/value.c b/gdb/value.c
index 35a7a5c..8e22ac7 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -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
diff --git a/gdb/value.h b/gdb/value.h
index 2b5d784..fdef835 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -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,


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-11-26 21:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-26 21:28 [binutils-gdb] Add add_internal_function overload Tom Tromey

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).