[pushed] gdb/python: Use copy-initialization more when possible

Message ID b9a03852-0263-3e65-7b52-66567480f807@redhat.com
State New
Headers show
Series
  • [pushed] gdb/python: Use copy-initialization more when possible
Related show

Commit Message

Pedro Alves Aug. 24, 2018, 10:12 p.m.
On 08/24/2018 09:06 PM, Simon Marchi wrote:
> On 2018-08-24 15:47, Pedro Alves wrote:

>> On 08/24/2018 08:44 PM, Pedro Alves wrote:

>>

>>>

>>> Yeah, I definitely prefer copy-init when possible.  In this

>>> case I'd prefer doing that as a separate patch though, because

>>> that's not something that the regression fix is adding, and

>>> also grepping around I see there's a lot of similar instances

>>> that can all get the same treatment.  I'll post a patch

>>> for that as follow up.

>> Here's what I mean.

> 

> Indeed, in the previous patch I just meant you could change the line you were touching (and indeed quoted the wrong hunk), but this more far-reaching cleanup is great too.  I wouldn't put this one in the 8.2 branch though, since it doesn't actually fix anything.


Agreed.  I've merged this to master now.

From d98fc15be2b8dd5699c1852db7d9d979231123dc Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>

Date: Fri, 24 Aug 2018 20:09:19 +0100
Subject: [PATCH] gdb/python: Use copy-initialization more when possible

gdb/ChangeLog:
2018-08-24  Pedro Alves  <palves@redhat.com>

	* python/py-bpevent.c (create_breakpoint_event_object): Use
	copy-initialization.
	* python/py-continueevent.c (emit_continue_event): Use
	copy-initialization.
	* python/py-exitedevent.c (create_exited_event_object): Return a
	gdbpy_ref<>.
	(emit_exited_event): Use copy-initialization.
	* python/py-inferior.c (python_new_inferior)
	(python_inferior_deleted, add_thread_object): Use
	copy-initialization.
	* python/py-infevents.c (create_inferior_call_event_object)
	(create_register_changed_event_object)
	(create_memory_changed_event_object): Return a gdbpy_ref<>.
	(emit_inferior_call_event, emit_memory_changed_event)
	(emit_register_changed_event): Use copy-initialization.
	* python/py-newobjfileevent.c (create_new_objfile_event_object):
	Return a gdbpy_ref<>.
	(emit_new_objfile_event): Use copy-initialization.
	(create_clear_objfiles_event_object): Return a gdbpy_ref<>.
	(emit_clear_objfiles_event): Use copy-initialization.
	* python/py-signalevent.c (create_signal_event_object): Use
	copy-initialization.
	* python/py-threadevent.c (create_thread_event_object): Use
	copy-initialization.
---
 gdb/ChangeLog                   | 27 +++++++++++++++++++++++++++
 gdb/python/py-bpevent.c         |  2 +-
 gdb/python/py-continueevent.c   |  2 +-
 gdb/python/py-exitedevent.c     |  8 ++++----
 gdb/python/py-inferior.c        |  8 ++++----
 gdb/python/py-infevents.c       | 22 +++++++++++-----------
 gdb/python/py-newobjfileevent.c | 24 ++++++++++--------------
 gdb/python/py-signalevent.c     |  5 ++---
 gdb/python/py-threadevent.c     |  2 +-
 9 files changed, 61 insertions(+), 39 deletions(-)

-- 
2.14.4

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index e1bdd392b1b..12dda6c541d 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,30 @@ 
+2018-08-24  Pedro Alves  <palves@redhat.com>
+
+	* python/py-bpevent.c (create_breakpoint_event_object): Use
+	copy-initialization.
+	* python/py-continueevent.c (emit_continue_event): Use
+	copy-initialization.
+	* python/py-exitedevent.c (create_exited_event_object): Return a
+	gdbpy_ref<>.
+	(emit_exited_event): Use copy-initialization.
+	* python/py-inferior.c (python_new_inferior)
+	(python_inferior_deleted, add_thread_object): Use
+	copy-initialization.
+	* python/py-infevents.c (create_inferior_call_event_object)
+	(create_register_changed_event_object)
+	(create_memory_changed_event_object): Return a gdbpy_ref<>.
+	(emit_inferior_call_event, emit_memory_changed_event)
+	(emit_register_changed_event): Use copy-initialization.
+	* python/py-newobjfileevent.c (create_new_objfile_event_object):
+	Return a gdbpy_ref<>.
+	(emit_new_objfile_event): Use copy-initialization.
+	(create_clear_objfiles_event_object): Return a gdbpy_ref<>.
+	(emit_clear_objfiles_event): Use copy-initialization.
+	* python/py-signalevent.c (create_signal_event_object): Use
+	copy-initialization.
+	* python/py-threadevent.c (create_thread_event_object): Use
+	copy-initialization.
+
 2018-08-24  Pedro Alves  <palves@redhat.com>
 	    Simon Marchi  <simon.marchi@ericsson.com>
 
diff --git a/gdb/python/py-bpevent.c b/gdb/python/py-bpevent.c
index 03863e4c9a2..f6f3c4c2378 100644
--- a/gdb/python/py-bpevent.c
+++ b/gdb/python/py-bpevent.c
@@ -28,7 +28,7 @@  gdbpy_ref<>
 create_breakpoint_event_object (PyObject *breakpoint_list, PyObject *first_bp)
 {
   gdbpy_ref<> breakpoint_event_obj
-    (create_stop_event_object (&breakpoint_event_object_type));
+    = create_stop_event_object (&breakpoint_event_object_type);
 
   if (breakpoint_event_obj == NULL)
     return NULL;
diff --git a/gdb/python/py-continueevent.c b/gdb/python/py-continueevent.c
index b047a83522b..759b4831366 100644
--- a/gdb/python/py-continueevent.c
+++ b/gdb/python/py-continueevent.c
@@ -50,7 +50,7 @@  emit_continue_event (ptid_t ptid)
   if (evregpy_no_listeners_p (gdb_py_events.cont))
     return 0;
 
-  gdbpy_ref<> event (create_continue_event_object (ptid));
+  gdbpy_ref<> event = create_continue_event_object (ptid);
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.cont);
   return -1;
diff --git a/gdb/python/py-exitedevent.c b/gdb/python/py-exitedevent.c
index 6a7c9f51fa1..22c095c7c79 100644
--- a/gdb/python/py-exitedevent.c
+++ b/gdb/python/py-exitedevent.c
@@ -20,10 +20,10 @@ 
 #include "defs.h"
 #include "py-event.h"
 
-static PyObject *
+static gdbpy_ref<>
 create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
 {
-  gdbpy_ref<> exited_event (create_event_object (&exited_event_object_type));
+  gdbpy_ref<> exited_event = create_event_object (&exited_event_object_type);
 
   if (exited_event == NULL)
     return NULL;
@@ -45,7 +45,7 @@  create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
 					     (PyObject *) inf_obj.get ()) < 0)
     return NULL;
 
-  return exited_event.release ();
+  return exited_event;
 }
 
 /* Callback that is used when an exit event occurs.  This function
@@ -57,7 +57,7 @@  emit_exited_event (const LONGEST *exit_code, struct inferior *inf)
   if (evregpy_no_listeners_p (gdb_py_events.exited))
     return 0;
 
-  gdbpy_ref<> event (create_exited_event_object (exit_code, inf));
+  gdbpy_ref<> event = create_exited_event_object (exit_code, inf);
 
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.exited);
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 9f09ae93756..1cf37296973 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -256,7 +256,7 @@  python_new_inferior (struct inferior *inf)
       return;
     }
 
-  gdbpy_ref<> event (create_event_object (&new_inferior_event_object_type));
+  gdbpy_ref<> event = create_event_object (&new_inferior_event_object_type);
   if (event == NULL
       || evpy_add_attribute (event.get (), "inferior",
 			     (PyObject *) inf_obj.get ()) < 0
@@ -284,7 +284,7 @@  python_inferior_deleted (struct inferior *inf)
       return;
     }
 
-  gdbpy_ref<> event (create_event_object (&inferior_deleted_event_object_type));
+  gdbpy_ref<> event = create_event_object (&inferior_deleted_event_object_type);
   if (event == NULL
       || evpy_add_attribute (event.get (), "inferior",
 			     (PyObject *) inf_obj.get ()) < 0
@@ -353,8 +353,8 @@  add_thread_object (struct thread_info *tp)
   if (evregpy_no_listeners_p (gdb_py_events.new_thread))
     return;
 
-  gdbpy_ref<> event (create_thread_event_object (&new_thread_event_object_type,
-						 (PyObject *) thread_obj));
+  gdbpy_ref<> event = create_thread_event_object (&new_thread_event_object_type,
+						  (PyObject *) thread_obj);
   if (event == NULL
       || evpy_emit_event (event.get (), gdb_py_events.new_thread) < 0)
     gdbpy_print_stack ();
diff --git a/gdb/python/py-infevents.c b/gdb/python/py-infevents.c
index 2bd0bc7f2c4..0df7d96df30 100644
--- a/gdb/python/py-infevents.c
+++ b/gdb/python/py-infevents.c
@@ -24,7 +24,7 @@ 
 /* Construct either a gdb.InferiorCallPreEvent or a
    gdb.InferiorCallPostEvent. */
 
-static PyObject *
+static gdbpy_ref<>
 create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid,
 				   CORE_ADDR addr)
 {
@@ -56,17 +56,17 @@  create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid,
   if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
     return NULL;
 
-  return event.release ();
+  return event;
 }
 
 /* Construct a gdb.RegisterChangedEvent containing the affected
    register number. */
 
-static PyObject *
+static gdbpy_ref<>
 create_register_changed_event_object (struct frame_info *frame, 
 				      int regnum)
 {
-  gdbpy_ref<> event (create_event_object (&register_changed_event_object_type));
+  gdbpy_ref<> event = create_event_object (&register_changed_event_object_type);
   if (event == NULL)
     return NULL;
 
@@ -84,16 +84,16 @@  create_register_changed_event_object (struct frame_info *frame,
   if (evpy_add_attribute (event.get (), "regnum", regnum_obj.get ()) < 0)
     return NULL;
 
-  return event.release ();
+  return event;
 }
 
 /* Construct a gdb.MemoryChangedEvent describing the extent of the
    affected memory. */
 
-static PyObject *
+static gdbpy_ref<>
 create_memory_changed_event_object (CORE_ADDR addr, ssize_t len)
 {
-  gdbpy_ref<> event (create_event_object (&memory_changed_event_object_type));
+  gdbpy_ref<> event = create_event_object (&memory_changed_event_object_type);
 
   if (event == NULL)
     return NULL;
@@ -112,7 +112,7 @@  create_memory_changed_event_object (CORE_ADDR addr, ssize_t len)
   if (evpy_add_attribute (event.get (), "length", len_obj.get ()) < 0)
     return NULL;
 
-  return event.release ();
+  return event;
 }
 
 /* Callback function which notifies observers when an event occurs which
@@ -127,7 +127,7 @@  emit_inferior_call_event (inferior_call_kind flag, ptid_t thread,
   if (evregpy_no_listeners_p (gdb_py_events.inferior_call))
     return 0;
 
-  gdbpy_ref<> event (create_inferior_call_event_object (flag, thread, addr));
+  gdbpy_ref<> event = create_inferior_call_event_object (flag, thread, addr);
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.inferior_call);
   return -1;
@@ -142,7 +142,7 @@  emit_memory_changed_event (CORE_ADDR addr, ssize_t len)
   if (evregpy_no_listeners_p (gdb_py_events.memory_changed))
     return 0;
 
-  gdbpy_ref<> event (create_memory_changed_event_object (addr, len));
+  gdbpy_ref<> event = create_memory_changed_event_object (addr, len);
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.memory_changed);
   return -1;
@@ -157,7 +157,7 @@  emit_register_changed_event (struct frame_info* frame, int regnum)
   if (evregpy_no_listeners_p (gdb_py_events.register_changed))
     return 0;
 
-  gdbpy_ref<> event (create_register_changed_event_object (frame, regnum));
+  gdbpy_ref<> event = create_register_changed_event_object (frame, regnum);
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.register_changed);
   return -1;
diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c
index 86de9ac10ad..a9341a3be18 100644
--- a/gdb/python/py-newobjfileevent.c
+++ b/gdb/python/py-newobjfileevent.c
@@ -20,25 +20,23 @@ 
 #include "defs.h"
 #include "py-event.h"
 
-static PyObject *
+static gdbpy_ref<>
 create_new_objfile_event_object (struct objfile *objfile)
 {
-  PyObject *py_objfile;
-
   gdbpy_ref<> objfile_event
-    (create_event_object (&new_objfile_event_object_type));
+    = create_event_object (&new_objfile_event_object_type);
   if (objfile_event == NULL)
     return NULL;
 
   /* Note that objfile_to_objfile_object returns a borrowed reference,
      so we don't need a decref here.  */
-  py_objfile = objfile_to_objfile_object (objfile);
+  PyObject *py_objfile = objfile_to_objfile_object (objfile);
   if (!py_objfile || evpy_add_attribute (objfile_event.get (),
                                          "new_objfile",
                                          py_objfile) < 0)
     return NULL;
 
-  return objfile_event.release ();
+  return objfile_event;
 }
 
 /* Callback function which notifies observers when a new objfile event occurs.
@@ -51,7 +49,7 @@  emit_new_objfile_event (struct objfile *objfile)
   if (evregpy_no_listeners_p (gdb_py_events.new_objfile))
     return 0;
 
-  gdbpy_ref<> event (create_new_objfile_event_object (objfile));
+  gdbpy_ref<> event = create_new_objfile_event_object (objfile);
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.new_objfile);
   return -1;
@@ -60,25 +58,23 @@  emit_new_objfile_event (struct objfile *objfile)
 
 /* Subroutine of emit_clear_objfiles_event to simplify it.  */
 
-static PyObject *
+static gdbpy_ref<>
 create_clear_objfiles_event_object (void)
 {
-  PyObject *py_progspace;
-
   gdbpy_ref<> objfile_event
-    (create_event_object (&clear_objfiles_event_object_type));
+    = create_event_object (&clear_objfiles_event_object_type);
   if (objfile_event == NULL)
     return NULL;
 
   /* Note that pspace_to_pspace_object returns a borrowed reference,
      so we don't need a decref here.  */
-  py_progspace = pspace_to_pspace_object (current_program_space);
+  PyObject *py_progspace = pspace_to_pspace_object (current_program_space);
   if (!py_progspace || evpy_add_attribute (objfile_event.get (),
 					   "progspace",
 					   py_progspace) < 0)
     return NULL;
 
-  return objfile_event.release ();
+  return objfile_event;
 }
 
 /* Callback function which notifies observers when the "clear objfiles"
@@ -92,7 +88,7 @@  emit_clear_objfiles_event (void)
   if (evregpy_no_listeners_p (gdb_py_events.clear_objfiles))
     return 0;
 
-  gdbpy_ref<> event (create_clear_objfiles_event_object ());
+  gdbpy_ref<> event = create_clear_objfiles_event_object ();
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.clear_objfiles);
   return -1;
diff --git a/gdb/python/py-signalevent.c b/gdb/python/py-signalevent.c
index d88aaeaedea..a2e7519460c 100644
--- a/gdb/python/py-signalevent.c
+++ b/gdb/python/py-signalevent.c
@@ -23,14 +23,13 @@ 
 gdbpy_ref<>
 create_signal_event_object (enum gdb_signal stop_signal)
 {
-  const char *signal_name;
   gdbpy_ref<> signal_event_obj
-    (create_stop_event_object (&signal_event_object_type));
+    = create_stop_event_object (&signal_event_object_type);
 
   if (signal_event_obj == NULL)
     return NULL;
 
-  signal_name = gdb_signal_to_name (stop_signal);
+  const char *signal_name = gdb_signal_to_name (stop_signal);
 
   gdbpy_ref<> signal_name_obj (PyString_FromString (signal_name));
   if (signal_name_obj == NULL)
diff --git a/gdb/python/py-threadevent.c b/gdb/python/py-threadevent.c
index 1bc2fb06d54..a78f0a38310 100644
--- a/gdb/python/py-threadevent.c
+++ b/gdb/python/py-threadevent.c
@@ -50,7 +50,7 @@  create_thread_event_object (PyTypeObject *py_type, PyObject *thread)
 {
   gdb_assert (thread != NULL);
 
-  gdbpy_ref<> thread_event_obj (create_event_object (py_type));
+  gdbpy_ref<> thread_event_obj = create_event_object (py_type);
   if (thread_event_obj == NULL)
     return NULL;