From patchwork Sat Oct 12 17:34:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 98798 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 512F2385829B for ; Sat, 12 Oct 2024 17:35:07 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id D87F93858D20 for ; Sat, 12 Oct 2024 17:34:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D87F93858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D87F93858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728754486; cv=none; b=m+gQt1YoHQNXzqOhgmOhsL87v8W2am+E33d3A/QchW5MB27yea9+4KnhBB26gjRu+7YYHTLyQnkdj4ZoiBdV6CM0dbk0nqQXyXwU6acRM01IcrUhfq+E9bUcn7PXk+aKWbd2sDnSPx2tlRGRl+74rDphuBwq3aC6vzdpESoIzLs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728754486; c=relaxed/simple; bh=u/Qe5WdcYY0Ig8accHjVuzonTvRIuD/uEbIaQBkf7Uw=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-Id:MIME-Version; b=qxdFQ1I1FXVQsM51hOufnc2QfqNI3ylx/T8SM6v/qIkuOC4abEQz2rlj/VXmjldC6Kipfj+x19suRp3ZmkRNaOSwIhA+1wPyJx+LQR6RmoWroL25nEAHXNowYj2z6LJpKeVFZDlYWOkWYIVMCo1bMGUoVLExHWAwXRZyQzg/XDQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id B1D1321FC6 for ; Sat, 12 Oct 2024 17:34:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1728754483; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5GiU/LnMpWkI0/Tg+tUVJzYbhlb+MarJmESCuNUCLo8=; b=xucWpl28NSvN61Z0EIMKPzCOexu6vcqjdCt2UzfsEBP6g0ey3NOrQz8omdbvPuTPwUkqnG tWBp2/rlbrJmApo22VDnYQzMsTkFV67JLPui+u+0f5BS9uYS/1L5E0hPBM22hjp45f4Rba 2g34dBAepHzXynEEpQrFFy4lesQnKlM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1728754483; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5GiU/LnMpWkI0/Tg+tUVJzYbhlb+MarJmESCuNUCLo8=; b=IO5Jv9J0DNoJwH2RxfIG2i+xHDWPqxIic42F5ROxq3NaoepENL3I5hvoZyY9rwzA/3i8nv Zgr5UQO5W2hItjDw== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=xucWpl28; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=IO5Jv9J0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1728754483; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5GiU/LnMpWkI0/Tg+tUVJzYbhlb+MarJmESCuNUCLo8=; b=xucWpl28NSvN61Z0EIMKPzCOexu6vcqjdCt2UzfsEBP6g0ey3NOrQz8omdbvPuTPwUkqnG tWBp2/rlbrJmApo22VDnYQzMsTkFV67JLPui+u+0f5BS9uYS/1L5E0hPBM22hjp45f4Rba 2g34dBAepHzXynEEpQrFFy4lesQnKlM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1728754483; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5GiU/LnMpWkI0/Tg+tUVJzYbhlb+MarJmESCuNUCLo8=; b=IO5Jv9J0DNoJwH2RxfIG2i+xHDWPqxIic42F5ROxq3NaoepENL3I5hvoZyY9rwzA/3i8nv Zgr5UQO5W2hItjDw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 9836E136BA for ; Sat, 12 Oct 2024 17:34:43 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id yYyLIzOzCmeXOAAAD6G6ig (envelope-from ) for ; Sat, 12 Oct 2024 17:34:43 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH] [gdbsupport] Add gdb::array_view::{iterator,const_iterator} Date: Sat, 12 Oct 2024 19:34:49 +0200 Message-Id: <20241012173449.17092-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-Rspamd-Queue-Id: B1D1321FC6 X-Spam-Score: -3.01 X-Rspamd-Action: no action X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[99.99%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCVD_TLS_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:mid,suse.de:dkim]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; TO_DN_NONE(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+]; DKIM_TRACE(0.00)[suse.de:+] X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Level: X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org While trying to substitute some std::vector type A in the code with a gdb::array_view: ... - using A = std::vector + using A = gdb::array_view .... I ran into the problem that the code was using A::iterator while gdb_array_view doesn't define such a type. Fix this by: - adding types gdb::array_view::iterator and gdb::array_view::const_iterator, - using them in gdb::array_view::begin and gdb::array_view::end, as is usual, and - using them explicitly in a unit test. Tested on aarch64-linux. --- gdb/unittests/array-view-selftests.c | 36 ++++++++++++++++++++++++++++ gdbsupport/array-view.h | 10 ++++---- 2 files changed, 42 insertions(+), 4 deletions(-) base-commit: f552e90295583446b81a07dcdbad9cd85299ec93 diff --git a/gdb/unittests/array-view-selftests.c b/gdb/unittests/array-view-selftests.c index eb3ae304790..29a8fae3d31 100644 --- a/gdb/unittests/array-view-selftests.c +++ b/gdb/unittests/array-view-selftests.c @@ -364,6 +364,39 @@ check_range_for () SELF_CHECK (sum == 1 + 2 + 3 + 4); } +template +static void +check_iterator () +{ + T data[] = {1, 2, 3, 4}; + gdb::array_view view (data); + + typename std::decay::type sum = 0; + for (typename gdb::array_view::iterator it = view.begin (); + it != view.end (); it++) + { + *it *= 2; + sum += *it; + } + + SELF_CHECK (sum == 2 + 4 + 6 + 8); +} + +template +static void +check_const_iterator () +{ + T data[] = {1, 2, 3, 4}; + gdb::array_view view (data); + + typename std::decay::type sum = 0; + for (typename gdb::array_view::const_iterator it = view.begin (); + it != view.end (); it++) + sum += *it; + + SELF_CHECK (sum == 1 + 2 + 3 + 4); +} + /* Entry point. */ static void @@ -490,6 +523,9 @@ run_tests () check_range_for (); check_range_for (); + check_iterator (); + check_const_iterator (); + check_const_iterator (); /* Check that the right ctor overloads are taken when the element is a container. */ diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h index 93842a40ec1..769f0049636 100644 --- a/gdbsupport/array-view.h +++ b/gdbsupport/array-view.h @@ -88,6 +88,8 @@ class array_view using reference = T &; using const_reference = const T &; using size_type = size_t; + using const_iterator = const T *; + using iterator = T *; /* Default construction creates an empty view. */ constexpr array_view () noexcept @@ -157,11 +159,11 @@ class array_view constexpr T *data () noexcept { return m_array; } constexpr const T *data () const noexcept { return m_array; } - constexpr T *begin () noexcept { return m_array; } - constexpr const T *begin () const noexcept { return m_array; } + constexpr iterator begin () noexcept { return m_array; } + constexpr const_iterator begin () const noexcept { return m_array; } - constexpr T *end () noexcept { return m_array + m_size; } - constexpr const T *end () const noexcept { return m_array + m_size; } + constexpr iterator end () noexcept { return m_array + m_size; } + constexpr const_iterator end () const noexcept { return m_array + m_size; } constexpr reference operator[] (size_t index) noexcept {