Commit Graph

6 Commits

Author SHA1 Message Date
Pedro Alves 9bcb1f1630 Make inferior::detaching a bool, and introduce scoped_restore::release()
I left making inferior::detaching a bool to a separate patch, because
doing that makes a make_cleanup_restore_integer call in
infrun.c:prepare_for_detach no longer compile (passing a 'bool *' when
an 'int *' is expected).  Since we want to get rid of cleanups anyway,
I looked at converting that to a scoped_restore.  However,
prepare_for_detach wants to discard the cleanup on success, and
scoped_restore doesn't have an equivalent for that.  So I added one --
I called it "release()" because it seems like a natural fit in the way
standard components call similarly-spirited methods, and, it's also
what the proposal for a generic scope guard calls it too, AFAICS:

  http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189.pdf

I've added some scoped_guard unit tests, while at it.

gdb/ChangeLog:
2017-04-19  Pedro Alves  <palves@redhat.com>

	* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
	unittests/scoped_restore-selftests.c.
	(SUBDIR_UNITTESTS_OBS): Add scoped_restore-selftests.o.
	* common/scoped_restore.h (scoped_restore_base): Make "class".
	(scoped_restore_base::release): New public method.
	(scoped_restore_base::scoped_restore_base): New protected ctor.
	(scoped_restore_base::m_saved_var): New protected field.
	(scoped_restore_tmpl::scoped_restore_tmpl(T*)): Initialize the
	scoped_restore_base base class instead of m_saved_var directly.
	(scoped_restore_tmpl::scoped_restore_tmpl(T*, T2)): Likewise.
	(scoped_restore_tmpl::scoped_restore_tmpl(const
	scoped_restore_tmpl<T>&)): Likewise.
	(scoped_restore_tmpl::~scoped_restore_tmpl): Use the saved_var
	method.
	(scoped_restore_tmpl::saved_var): New method.
	(scoped_restore_tmpl::m_saved_var): Delete.
	* inferior.h (inferior::detaching): Now a bool.
	* infrun.c (prepare_for_detach): Use a scoped_restore instead of a
	cleanup.
	* unittests/scoped_restore-selftests.c: New file.
2017-04-19 13:12:23 +01:00
Pedro Alves d35d19584c gdb::optional unit tests
I thought I'd add some unit tests to make sure gdb::optional behaved
correctly, and started writing some, but then thought/realized that
libstdc++ already has extensive testing for C++17 std::optional, which
gdb::optional is a subset of, and thought why bother writing something
from scratch.  So I tried copying over a subset of libstdc++'s tests
(that ones that cover the subset supported by gdb::optional), and was
positively surprised that they mostly work OOTB.  This did help shake
out a few bugs from what I was implementing in the previous patch to
gdb::optional.  Still, it's a good chunk of code being copied over, so
if people dislike this copying/duplication, I can drop this patch.

gdb/ChangeLog:
2017-04-18  Pedro Alves  <palves@redhat.com>

	* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
	unittests/optional-selftests.c.
	(SUBDIR_UNITTESTS_OBS): Add optional-selftests.o.
	* unittests/optional-selftests.c: New file.
	* unittests/optional/assignment/1.cc: New file.
	* unittests/optional/assignment/2.cc: New file.
	* unittests/optional/assignment/3.cc: New file.
	* unittests/optional/assignment/4.cc: New file.
	* unittests/optional/assignment/5.cc: New file.
	* unittests/optional/assignment/6.cc: New file.
	* unittests/optional/assignment/7.cc: New file.
	* unittests/optional/cons/copy.cc: New file.
	* unittests/optional/cons/default.cc: New file.
	* unittests/optional/cons/move.cc: New file.
	* unittests/optional/cons/value.cc: New file.
	* unittests/optional/in_place.cc: New file.
	* unittests/optional/observers/1.cc: New file.
	* unittests/optional/observers/2.cc: New file.
2017-04-18 23:49:33 +01:00
Simon Marchi 6670ec1372 ptid-selftests: Fix erroneous assert messages
gdb/ChangeLog:

	* unittests/ptid-selftests.c: Fix erroneous assert messages.
2017-04-07 10:28:56 -04:00
Simon Marchi 436252de3e Class-ify ptid_t
I grew a bit tired of using ptid_get_{lwp,pid,tid} and friends, so I decided to
make it a bit easier to use by making it a proper class.  The fields are now
private, so it's not possible to change a ptid_t field by mistake.

The new methods of ptid_t map to existing functions/practice like this:

  ptid_t (pid, lwp, tid) -> ptid_build (pid, lwp, tid)
  ptid_t (pid) -> pid_to_ptid (pid)
  ptid.is_pid () -> ptid_is_pid (ptid)
  ptid == other -> ptid_equal (ptid, other)
  ptid != other -> !ptid_equal (ptid, other)
  ptid.pid () -> ptid_get_pid (ptid)
  ptid.lwp_p () -> ptid_lwp_p (ptid)
  ptid.lwp () -> ptid_get_lwp (ptid)
  ptid.tid_p () -> ptid_tid_p (ptid)
  ptid.tid () -> ptid_get_tid (ptid)
  ptid.matches (filter) -> ptid_match (ptid, filter)

I've replaced the implementation of the existing functions with calls to
the new methods.  People are encouraged to gradually switch to using the
ptid_t methods instead of the functions (or we can change them all in
one pass eventually).

Also, I'm not sure if it's worth it (because of ptid_t's relatively
small size), but I have made the functions and methods take ptid_t
arguments by const reference instead of by value.

gdb/ChangeLog:

	* common/ptid.h (struct ptid): Change to...
	(class ptid_t): ... this.
	<ptid_t>: New constructors.
	<pid, lwp_p, lwp, tid_p, tid, is_pid, operator==, operator!=,
	matches>: New methods.
	<make_null, make_minus_one>: New static methods.
	<pid>: Rename to...
	<m_pid>: ...this.
	<lwp>: Rename to...
	<m_lwp>: ...this.
	<tid>: Rename to...
	<m_tid>: ...this.
	(ptid_build, ptid_get_pid, ptid_get_lwp, ptid_get_tid, ptid_equal,
	ptid_is_pid, ptid_lwp_p, ptid_tid_p, ptid_match): Take ptid arguments
	as references, move comment to class ptid_t.
	* common/ptid.c (null_ptid, minus_one_ptid): Initialize with
	ptid_t static methods.
	(ptid_build, pid_to_ptid, ptid_get_pid, ptid_get_tid,
	ptid_equal, ptid_is_pid, ptid_lwp_p, ptid_tid_p, ptid_match):
	Take ptid arguments as references, implement using ptid_t methods.
	* unittests/ptid-selftests.c: New file.
	* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
	unittests/ptid-selftests.c.
	(SUBDIR_UNITTESTS_OBS): Add unittests/ptid-selftests.o.

gdb/gdbserver/ChangeLog:

	* server.c (handle_v_cont): Initialize thread_resume::thread
	with null_ptid.
2017-04-06 23:29:53 -04:00
Pedro Alves 9c54172556 Make sect_offset and cu_offset strong typedefs instead of structs
A while ago, back when GDB was a C program, the sect_offset and
cu_offset types were made structs in order to prevent incorrect mixing
of those offsets.  Now that we require C++11, we can make them
integers again, while keeping the safety, by exploiting "enum class".
We can add a bit more safety, even, by defining operators that the
types _should_ support, helping making the suspicious uses stand out
more.

Getting at the underlying type is done with the new to_underlying
function added by the previous patch, which also helps better spot
where do we need to step out of the safety net.  Mostly, that's around
parsing the DWARF, and when we print the offset for complaint/debug
purposes.  But there are other occasional uses.

Since we have to define the sect_offset/cu_offset types in a header
anyway, I went ahead and generalized/library-fied the idea of "offset"
types, making it trivial to add more such types if we find a use.  See
common/offset-type.h and the DEFINE_OFFSET_TYPE macro.

I needed a couple generaly-useful preprocessor bits (e.g., yet another
CONCAT implementation), so I started a new common/preprocessor.h file.

I included units tests covering the "offset" types API.  These are
mostly compile-time tests, using SFINAE to check that expressions that
shouldn't compile (e.g., comparing unrelated offset types) really are
invalid and would fail to compile.  This same idea appeared in my
pending enum-flags revamp from a few months ago (though this version
is a bit further modernized compared to what I had posted), and I plan
on reusing the "check valid expression" bits added here in that
series, so I went ahead and defined the CHECK_VALID_EXPR macro in its
own header -- common/valid-expr.h.  I think that's nicer regardless.

I was borderline between calling the new types "offset" types, or
"index" types, BTW.  I stuck with "offset" simply because that's what
we're already calling them, mostly.

gdb/ChangeLog:
2017-04-04  Pedro Alves  <palves@redhat.com>

	* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
	unittests/offset-type-selftests.c.
	(SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o.
	* common/offset-type.h: New file.
	* common/preprocessor.h: New file.
	* common/traits.h: New file.
	* common/valid-expr.h: New file.
	* dwarf2expr.c: Include "common/underlying.h".  Adjust to use
	sect_offset and cu_offset strong typedefs throughout.
	* dwarf2expr.h: Adjust to use sect_offset and cu_offset strong
	typedefs throughout.
	* dwarf2loc.c: Include "common/underlying.h".  Adjust to use
	sect_offset and cu_offset strong typedefs throughout.
	* dwarf2read.c: Adjust to use sect_offset and cu_offset strong
	typedefs throughout.
	* gdbtypes.h: Include "common/offset-type.h".
	(cu_offset): Now an offset type (strong typedef) instead of a
	struct.
	(sect_offset): Likewise.
	(union call_site_parameter_u): Rename "param_offset" field to
	"param_cu_off".
	* unittests/offset-type-selftests.c: New file.
2017-04-04 20:03:26 +01:00
Pedro Alves 07e253aa3b Introduce gdb::function_view
This commit adds a new function_view type.  This type holds a
non-owning reference to a callable.  It is meant to be used as
callback type of functions, instead of using the C-style pair of
function pointer and 'void *data' arguments.  function_view allows
passing references to stateful function objects / lambdas with
captures as callbacks efficiently, while function pointer + 'void *'
does not.

See the intro in the new function-view.h header for more.

Unit tests included, put into a new gdb/unittests/ subdir.

gdb/ChangeLog:
2017-02-23  Pedro Alves  <palves@redhat.com>

	* Makefile.in (SUBDIR_UNITTESTS_SRCS, SUBDIR_UNITTESTS_OBS): New.
	(%.o) <unittests/%.c>: New pattern.
	* configure.ac ($development): Add $(SUBDIR_UNITTESTS_OBS) to
	CONFIG_OBS, and $(SUBDIR_UNITTESTS_SRCS) to CONFIG_SRCS.
	* common/function-view.h: New file.
	* unittests/function-view-selftests.c: New file.
	* configure: Regenerate.
2017-02-23 16:14:08 +00:00