From: Simon Marchi <simon.marchi@polymtl.ca>
To: Tom Tromey <tom@tromey.com>,
Simon Marchi via Gdb-patches <gdb-patches@sourceware.org>
Subject: [PATCH] gdb: add gdb_argv::as_array_view method
Date: Thu, 13 Aug 2020 18:24:33 -0400 [thread overview]
Message-ID: <f2c84670-0ab6-39de-00ce-0c714f7401aa@polymtl.ca> (raw)
In-Reply-To: <874kp6h6d6.fsf@tromey.com>
On 2020-08-13 12:12 p.m., Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
>
> Simon> Hmm do you think it will happen often enough that we pass the
> Simon> parsed arguments straight to a function taking an array view?
>
> I am not sure it will happen often.
>
> Simon> In any case, it's not difficult to do. Did you mean a simple
> Simon> method like the patch below, or a conversion operator that could
> Simon> be implemented like this, to be able to transparently pass a
> Simon> gdb_argv as an array_view?
>
> ...
> Simon> Personally I prefer things to be a bit more explicit, otherwise there's a bit too much hidden
> Simon> magic.
>
> Me too. I view implicit conversions as bad by default, needing special
> justification to be used.
All right, here's the same patch with a commit log and ChangeLog entry.
From fec96cb9a8a0b50d85697cb0f6783bc5319a167b Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@polymtl.ca>
Date: Thu, 13 Aug 2020 10:28:41 -0400
Subject: [PATCH] gdb: add gdb_argv::as_array_view method
Introduce the gdb_argv::as_array_view method, as a way to easily pass
the parsed arguments array to a function taking an array view. There is
currently one caller where we can use this (which prompted the
suggestion to implement this method).
Add some selftests for the new method, which at the same time test a
little bit gdb_argv. As far as I know, it's not tested currently.
gdb/ChangeLog:
* utils.h (class gdb_argv) <as_array_view>: New method.
* utils.c (gdb_argv_as_array_view_test): New.
(_initialize_utils): Register selftest.
* maint.c (maintenance_selftest): Use the new method.
Change-Id: I0645037613ed6549aabe60f14a36f3494513b177
---
gdb/maint.c | 2 +-
gdb/utils.c | 26 ++++++++++++++++++++++++++
gdb/utils.h | 8 ++++++++
3 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/gdb/maint.c b/gdb/maint.c
index fd37acce5226..3368769ad96f 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -1042,7 +1042,7 @@ maintenance_selftest (const char *args, int from_tty)
{
#if GDB_SELF_TEST
gdb_argv argv (args);
- selftests::run_tests (gdb::array_view<char *> (argv.get (), argv.count ()));
+ selftests::run_tests (argv.as_array_view ());
#else
printf_filtered (_("\
Selftests have been disabled for this build.\n"));
diff --git a/gdb/utils.c b/gdb/utils.c
index 102db28787fb..fb1308ac9ae3 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -2991,6 +2991,31 @@ gdb_realpath_tests ()
gdb_realpath_check_trailer ("", "");
}
+/* Test the gdb_argv::as_array_view method. */
+
+static void
+gdb_argv_as_array_view_test ()
+{
+ {
+ gdb_argv argv;
+
+ gdb::array_view<char *> view = argv.as_array_view ();
+
+ SELF_CHECK (view.data () == nullptr);
+ SELF_CHECK (view.size () == 0);
+ }
+ {
+ gdb_argv argv ("une bonne 50");
+
+ gdb::array_view<char *> view = argv.as_array_view ();
+
+ SELF_CHECK (view.size () == 3);
+ SELF_CHECK (strcmp (view[0], "une") == 0);
+ SELF_CHECK (strcmp (view[1], "bonne") == 0);
+ SELF_CHECK (strcmp (view[2], "50") == 0);
+ }
+}
+
#endif /* GDB_SELF_TEST */
/* Allocation function for the libiberty hash table which uses an
@@ -3489,5 +3514,6 @@ When set, debugging messages will be marked with seconds and microseconds."),
#if GDB_SELF_TEST
selftests::register_test ("gdb_realpath", gdb_realpath_tests);
+ selftests::register_test ("gdb_argv_array_view", gdb_argv_as_array_view_test);
#endif
}
diff --git a/gdb/utils.h b/gdb/utils.h
index 3434ff1caa25..9a235b963272 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -22,6 +22,7 @@
#define UTILS_H
#include "exceptions.h"
+#include "gdbsupport/array-view.h"
#include "gdbsupport/scoped_restore.h"
#include <chrono>
@@ -210,6 +211,13 @@ class gdb_argv
return m_argv[arg];
}
+ /* Return the arguments array as an array view. */
+
+ gdb::array_view<char *> as_array_view ()
+ {
+ return gdb::array_view<char *> (this->get (), this->count ());
+ }
+
/* The iterator type. */
typedef char **iterator;
--
2.28.0
next prev parent reply other threads:[~2020-08-13 22:24 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-12 22:12 [PATCH] gdb: allow specifying multiple filters when running selftests Simon Marchi
2020-08-13 1:46 ` Kevin Buettner
2020-08-13 12:06 ` Simon Marchi
2020-08-13 14:06 ` Tom Tromey
2020-08-13 15:01 ` Simon Marchi
2020-08-13 16:12 ` Tom Tromey
2020-08-13 22:24 ` Simon Marchi [this message]
2020-08-14 15:33 ` [PATCH] gdb: add gdb_argv::as_array_view method Tom Tromey
2020-08-14 16:33 ` Simon Marchi
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=f2c84670-0ab6-39de-00ce-0c714f7401aa@polymtl.ca \
--to=simon.marchi@polymtl.ca \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/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