binutils-gdb/gdb/unittests
Lancelot SIX e92f2b5eef Improve gdb::array_view ctor from contiguous containers
While reading the interface of gdb::array_view, I realized that the
constructor that builds an array_view on top of a contiguous container
(such as std::vector, std::array or even gdb::array_view) can be
missused.

Lets consider the following code sample:

	struct Parent
	{
	  Parent (int a): a { a } {}
	  int a;
	};

	std::ostream &operator<< (std::ostream& os, const Parent & p)
	{ os << "Parent {a=" << p.a << "}"; return os; }

	struct Child : public Parent
	{
	  Child (int a, int b): Parent { a }, b { b } {}
	  int b;
	};

	std::ostream &operator<< (std::ostream& os, const Child & p)
	{ os << "Child {a=" << p.a << ", b=" << p.b << "}"; return os; }

	template <typename T>
	void print (const gdb::array_view<const T> &p)
	{
	  std::for_each (p.begin (), p.end (), [](const T &p) { std::cout << p << '\n'; });
	}

Then with the current interface nothinng prevents this usage of
array_view to be done:

	const std::array<Child, 3> elts = {
	  Child {1, 2},
	  Child {3, 4},
	  Child {5, 6}
	};
	print_all<Parent> (elts);

This compiles fine and produces the following output:

	Parent {a=1}
	Parent {a=2}
	Parent {a=3}

which is obviously wrong.  There is nowhere in memory a Parent-like
object for which the A member is 2 and this call to print_all<Parent>
shold not compile at all (calling print_all<Child> is however fine).

This comes down to the fact that a Child* is convertible into a Parent*,
and that an array view is constructed to a pointer to the first element
and a size.  The valid type pointed to that can be used with this
constructor are restricted using SFINAE, which requires that a
pointer to a member into the underlying container can be converted into a
pointer the array_view's data type.

This patch proposes to change the constraints on the gdb::array_view
ctor which accepts a container now requires that the (decayed) type of
the elements in the container match the (decayed) type of the array_view
being constructed.

Applying this change required minimum adjustment in GDB codebase, which
are also included in this patch.

Tested by rebuilding.
2021-11-08 21:55:36 +00:00
..
basic_string_view
optional
array-view-selftests.c Improve gdb::array_view ctor from contiguous containers 2021-11-08 21:55:36 +00:00
child-path-selftests.c
cli-utils-selftests.c
command-def-selftests.c gdb: add cmd_list_element::is_prefix 2021-05-17 14:01:26 -04:00
common-utils-selftests.c
copy_bitwise-selftests.c
enum-flags-selftests.c
environ-selftests.c
filtered_iterator-selftests.c
format_pieces-selftests.c Fix format_pieces selftest on Windows 2021-10-19 13:14:48 -06:00
function-view-selftests.c
gdb_tilde_expand-selftests.c
gmp-utils-selftests.c
intrusive_list-selftests.c [gdb] Change register_test to use std::function arg 2021-09-21 00:41:26 +02:00
lookup_name_info-selftests.c
main-thread-selftests.c
memory-map-selftests.c
memrange-selftests.c
mkdir-recursive-selftests.c
observable-selftests.c
offset-type-selftests.c
optional-selftests.c
parallel-for-selftests.c Fix unit test build on Windows 2021-09-08 09:17:39 -06:00
parse-connection-spec-selftests.c
ptid-selftests.c
rsp-low-selftests.c
scoped_fd-selftests.c gdbsupport: make gdb_mkostemp_cloexec return a scoped_fd 2021-09-30 15:21:48 -04:00
scoped_ignore_signal-selftests.c Add a unit test for scoped_ignore_sigpipe 2021-06-17 16:22:12 +01:00
scoped_mmap-selftests.c gdbsupport: make gdb_mkostemp_cloexec return a scoped_fd 2021-09-30 15:21:48 -04:00
scoped_restore-selftests.c
search-memory-selftests.c
string_view-selftests.c
style-selftests.c
tracepoint-selftests.c
tui-selftests.c
unpack-selftests.c
utils-selftests.c
vec-utils-selftests.c
xml-utils-selftests.c