public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Lancelot SIX <lsix@lancelotsix.com>
To: gdb-patches@sourceware.org
Cc: Lancelot SIX <lsix@lancelotsix.com>
Subject: [PATCH 2/2] Improve gdb::array_view ctor from contiguous containers
Date: Wed, 20 Oct 2021 21:05:25 +0100	[thread overview]
Message-ID: <20211020200525.122108-3-lsix@lancelotsix.com> (raw)
In-Reply-To: <20211020200525.122108-1-lsix@lancelotsix.com>

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
miss-used.

Lets consider the following code sample:

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

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

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

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

Then with the current version of the interface we can write something
like:

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

This compiles fine and produces the following output:

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

This is obviously wrong.  There is nowhere in memory a Parent-like
object for which the A member is 2.  This call to print_all<Parent>
should not compile at all (a call to print_all<Child> instead would
however be fine).

This comes down to the fact that the constructor used in this example
only requires that a Child* is convertible into a Parent*.  This
requirement is valid necessary but not sufficient.

The other constructors of a array_view<T> from data of type U have
stricter requirements on what U should be.  It should satisfy that a U*
can be converted to a T*, but they also require that (decayed) T and
(decayed) U are the same.

This patch proposes to change the constraints on the gdb::array_view
constructor that accepts a Container<U>.  It makes it so the
requirements on what U should be are now the same as for the other
constructors.

Applying this change required minimum adjustment in GDB codebase (two
'const' addition), which are also included in this patch.

Tested by rebuilding using GCC-10.3.0 on GNU/Linux.
---
 gdb/dwarf2/read.c       | 2 +-
 gdb/maint.c             | 2 +-
 gdbsupport/array-view.h | 7 ++++---
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index cbd9a3012eb..0b82f1bbad4 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -14934,7 +14934,7 @@ add_variant_property (struct field_info *fip, struct type *type,
     offset_map[fip->fields[i].offset] = i;
 
   struct objfile *objfile = cu->per_objfile->objfile;
-  gdb::array_view<variant_part> parts
+  gdb::array_view<const variant_part> parts
     = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
 			    fip->variant_parts);
 
diff --git a/gdb/maint.c b/gdb/maint.c
index 8aae53bdd65..a5e21f458ad 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -1128,7 +1128,7 @@ maintenance_selftest (const char *args, int from_tty)
 {
 #if GDB_SELF_TEST
   bool verbose = args != nullptr && check_for_argument (&args, "-verbose");
-  gdb_argv argv (args);
+  const gdb_argv argv (args);
   selftests::run_tests (argv.as_array_view (), verbose);
 #else
   printf_filtered (_("\
diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
index c41fd620f83..565ad988974 100644
--- a/gdbsupport/array-view.h
+++ b/gdbsupport/array-view.h
@@ -139,9 +139,10 @@ class array_view
   template<typename Container,
 	   typename = Requires<gdb::Not<IsDecayedT<Container>>>,
 	   typename
-	     = Requires<std::is_convertible
-			<decltype (std::declval<Container> ().data ()),
-			 T *>>,
+	     = Requires<DecayedConvertible
+			<typename std::remove_pointer
+			 <decltype (std::declval<Container> ().data ())
+			 >::type>>,
 	   typename
 	     = Requires<std::is_convertible
 			<decltype (std::declval<Container> ().size ()),
-- 
2.33.0


  parent reply	other threads:[~2021-10-20 20:05 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-20 20:05 [PATCH 0/2] Improve constraints on gdb::array_view::array_view Lancelot SIX
2021-10-20 20:05 ` [PATCH 1/2] Add a const version of gdb_argv:as_array_view Lancelot SIX
2021-10-30  2:03   ` Simon Marchi
2021-10-20 20:05 ` Lancelot SIX [this message]
2021-10-30  2:09   ` [PATCH 2/2] Improve gdb::array_view ctor from contiguous containers Simon Marchi
2021-11-02 14:10     ` Tom Tromey
2021-11-02 16:00   ` Pedro Alves
2021-11-02 22:51     ` Lancelot SIX
2021-11-03 22:20     ` [PATCH v2 " Lancelot SIX
2021-11-04 13:04       ` Pedro Alves
2021-11-08 23:02         ` Lancelot SIX
2021-11-09 17:52           ` [PATCH] gdb::array_view slicing/container selftest - test std::array too (Re: [PATCH v2 2/2] Improve gdb::array_view ctor from contiguous containers) Pedro Alves
2021-11-09 21:58             ` Lancelot SIX
2021-11-09 22:48               ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211020200525.122108-3-lsix@lancelotsix.com \
    --to=lsix@lancelotsix.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).