From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id C404E3857C4B for ; Thu, 13 Aug 2020 22:24:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C404E3857C4B Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 07DMOhwm023908 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 13 Aug 2020 18:24:48 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 07DMOhwm023908 Received: from [10.0.0.11] (173-246-6-90.qc.cable.ebox.net [173.246.6.90]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 9733A1E599; Thu, 13 Aug 2020 18:24:43 -0400 (EDT) Subject: [PATCH] gdb: add gdb_argv::as_array_view method To: Tom Tromey , Simon Marchi via Gdb-patches References: <20200812221245.69493-1-simon.marchi@polymtl.ca> <878seihc75.fsf@tromey.com> <122366de-1378-ca30-4e95-3bcd45120018@polymtl.ca> <874kp6h6d6.fsf@tromey.com> From: Simon Marchi Message-ID: Date: Thu, 13 Aug 2020 18:24:33 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: <874kp6h6d6.fsf@tromey.com> Content-Type: text/plain; charset=utf-8 Content-Language: fr Content-Transfer-Encoding: 7bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Thu, 13 Aug 2020 22:24:43 +0000 X-Spam-Status: No, score=-8.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-DCC: MGTINTERNET: antispam2020.polymtl.ca 1170; Body=1 Fuz1=1 Fuz2=1 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Aug 2020 22:24:53 -0000 On 2020-08-13 12:12 p.m., Tom Tromey wrote: >>>>>> "Simon" == Simon Marchi via Gdb-patches 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 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) : 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 (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 view = argv.as_array_view (); + + SELF_CHECK (view.data () == nullptr); + SELF_CHECK (view.size () == 0); + } + { + gdb_argv argv ("une bonne 50"); + + gdb::array_view 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 @@ -210,6 +211,13 @@ class gdb_argv return m_argv[arg]; } + /* Return the arguments array as an array view. */ + + gdb::array_view as_array_view () + { + return gdb::array_view (this->get (), this->count ()); + } + /* The iterator type. */ typedef char **iterator; -- 2.28.0