]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Mark move constructors as "noexcept"
authorTom Tromey <tromey@adacore.com>
Mon, 20 Apr 2020 17:45:06 +0000 (11:45 -0600)
committerTom Tromey <tromey@adacore.com>
Mon, 20 Apr 2020 17:45:06 +0000 (11:45 -0600)
I recently learned that move constructors generally should be marked
"noexcept".  This ensures that standard containers will move objects
when possible, rather than copy them.

This patch fixes the cases I could find.  Note that implicitly-defined
or defaulted move constructors will automatically do what you'd
expect; that is, they are noexcept if all the members have noexcept
move constructors.

While doing this, I noticed a couple of odd cases where the move
constructor seemed to assume that the object being constructed could
have state requiring destruction.  I've fixed these as well.  See
completion_result and scoped_mmap.

gdb/ChangeLog
2020-04-20  Tom Tromey  <tromey@adacore.com>

* python/python.c (struct gdbpy_event): Mark move constructor as
noexcept.
* python/py-tui.c (class gdbpy_tui_window_maker): Mark move
constructor as noexcept.
* completer.h (struct completion_result): Mark move constructor as
noexcept.
* completer.c (completion_result::completion_result): Use
initialization style.  Don't call reset_match_list.

gdbsupport/ChangeLog
2020-04-20  Tom Tromey  <tromey@adacore.com>

* scoped_mmap.h (scoped_mmap): Mark move constructor as noexcept.
Use initialization style.  Don't call destroy.
* scoped_fd.h (class scoped_fd): Mark move constructor as
noexcept.
* gdb_ref_ptr.h (class ref_ptr): Mark move constructor as
noexcept.

gdb/ChangeLog
gdb/completer.c
gdb/completer.h
gdb/python/py-tui.c
gdb/python/python.c
gdbsupport/ChangeLog
gdbsupport/gdb_ref_ptr.h
gdbsupport/scoped_fd.h
gdbsupport/scoped_mmap.h

index b023bfe4b2a4af90b84ceb3bafb686c65ba973ff..aa99da0c4d93f65bda35073adfae2f23dd01227b 100644 (file)
@@ -1,3 +1,14 @@
+2020-04-20  Tom Tromey  <tromey@adacore.com>
+
+       * python/python.c (struct gdbpy_event): Mark move constructor as
+       noexcept.
+       * python/py-tui.c (class gdbpy_tui_window_maker): Mark move
+       constructor as noexcept.
+       * completer.h (struct completion_result): Mark move constructor as
+       noexcept.
+       * completer.c (completion_result::completion_result): Use
+       initialization style.  Don't call reset_match_list.
+
 2020-04-20  Mihails Strasuns  <mihails.strasuns@intel.com>
 
        * MAINTAINERS (Write After Approval): Add myself.
index 0dd91a7195f7e29d574f62f9bdb15b7faddde013..f9631f43cf49222d4841efa467582679dc69a8d3 100644 (file)
@@ -2327,15 +2327,11 @@ completion_result::~completion_result ()
 
 /* See completer.h  */
 
-completion_result::completion_result (completion_result &&rhs)
+completion_result::completion_result (completion_result &&rhs) noexcept
+  : match_list (rhs.match_list),
+    number_matches (rhs.number_matches)
 {
-  if (this == &rhs)
-    return;
-
-  reset_match_list ();
-  match_list = rhs.match_list;
   rhs.match_list = NULL;
-  number_matches = rhs.number_matches;
   rhs.number_matches = 0;
 }
 
index fd0d47b206bdb100a3e558ac78ec803d43721dc9..d3afa5fe3ec872f62d993a3936ea2a17e5f56912 100644 (file)
@@ -242,7 +242,7 @@ struct completion_result
   DISABLE_COPY_AND_ASSIGN (completion_result);
 
   /* Move a result.  */
-  completion_result (completion_result &&rhs);
+  completion_result (completion_result &&rhs) noexcept;
 
   /* Release ownership of the match list array.  */
   char **release_match_list ();
index de7c396be9f0bd58e0ef43665d286effd83223be..ca88f85eb9f9af4b38c2ed9276c9d7d9ab38dfc4 100644 (file)
@@ -233,7 +233,7 @@ public:
 
   ~gdbpy_tui_window_maker ();
 
-  gdbpy_tui_window_maker (gdbpy_tui_window_maker &&other)
+  gdbpy_tui_window_maker (gdbpy_tui_window_maker &&other) noexcept
     : m_constr (std::move (other.m_constr))
   {
   }
index d65cca403bec4755baf51507211a0331c19c8e7f..4875ffd29355be675ae9602b4241c0b9c896666b 100644 (file)
@@ -975,7 +975,7 @@ struct gdbpy_event
   {
   }
 
-  gdbpy_event (gdbpy_event &&other)
+  gdbpy_event (gdbpy_event &&other) noexcept
     : m_func (other.m_func)
   {
     other.m_func = nullptr;
index cd7033d58d6e31e4b41c02d3ccc6763387369b9c..78fbbe65c70e1d655ba4c29ce53cfcb52c93317c 100644 (file)
@@ -1,3 +1,12 @@
+2020-04-20  Tom Tromey  <tromey@adacore.com>
+
+       * scoped_mmap.h (scoped_mmap): Mark move constructor as noexcept.
+       Use initialization style.  Don't call destroy.
+       * scoped_fd.h (class scoped_fd): Mark move constructor as
+       noexcept.
+       * gdb_ref_ptr.h (class ref_ptr): Mark move constructor as
+       noexcept.
+
 2020-04-13  Tom Tromey  <tom@tromey.com>
 
        * event-loop.c: Move comment.  Remove obsolete  comment.
index c5ef13f3dfaf5d8f232018a2156cd23cb538adf2..de387f598dec7aee60f9966dccb44d7a3bd9226c 100644 (file)
@@ -78,7 +78,7 @@ class ref_ptr
   }
 
   /* Transfer ownership from OTHER.  */
-  ref_ptr (ref_ptr &&other)
+  ref_ptr (ref_ptr &&other) noexcept
     : m_obj (other.m_obj)
   {
     other.m_obj = NULL;
index f40ce8b0b50d227b054bdef1f87ad7401a593f62..ec654df5246b6e9ddfc4619c3cb940ab33696146 100644 (file)
@@ -30,7 +30,7 @@ class scoped_fd
 public:
   explicit scoped_fd (int fd = -1) noexcept : m_fd (fd) {}
 
-  scoped_fd (scoped_fd &&other)
+  scoped_fd (scoped_fd &&other) noexcept
     : m_fd (other.m_fd)
   {
     other.m_fd = -1;
index bab988f364cd020a9ebb72b10350d1efa665c8fe..9b74383fc5018d89337bb0982444d80660b1aa32 100644 (file)
@@ -42,13 +42,10 @@ public:
     destroy ();
   }
 
-  scoped_mmap (scoped_mmap &&rhs)
+  scoped_mmap (scoped_mmap &&rhs) noexcept
+    : m_mem (rhs.m_mem),
+      m_length (rhs.m_length)
   {
-    destroy ();
-
-    m_mem = rhs.m_mem;
-    m_length = rhs.m_length;
-
     rhs.m_mem = MAP_FAILED;
     rhs.m_length = 0;
   }