Skip to content

Commit

Permalink
Mark move constructors as "noexcept"
Browse files Browse the repository at this point in the history
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  <[email protected]>

	* 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  <[email protected]>

	* 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.
  • Loading branch information
tromey committed Apr 20, 2020
1 parent 9b2c992 commit 0fa7617
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 18 deletions.
11 changes: 11 additions & 0 deletions gdb/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
2020-04-20 Tom Tromey <[email protected]>

* 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 <[email protected]>

* MAINTAINERS (Write After Approval): Add myself.
Expand Down
10 changes: 3 additions & 7 deletions gdb/completer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion gdb/completer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
2 changes: 1 addition & 1 deletion gdb/python/py-tui.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class gdbpy_tui_window_maker

~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))
{
}
Expand Down
2 changes: 1 addition & 1 deletion gdb/python/python.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions gdbsupport/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2020-04-20 Tom Tromey <[email protected]>

* 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 <[email protected]>

* event-loop.c: Move comment. Remove obsolete comment.
Expand Down
2 changes: 1 addition & 1 deletion gdbsupport/gdb_ref_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion gdbsupport/scoped_fd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 3 additions & 6 deletions gdbsupport/scoped_mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ class scoped_mmap
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;
}
Expand Down

0 comments on commit 0fa7617

Please sign in to comment.