From: Matthieu Longo <matthieu.longo@arm.com>
To: <gdb-patches@sourceware.org>
Cc: Andrew Burgess <aburgess@redhat.com>, Tom Tromey <tom@tromey.com>,
Matthieu Longo <matthieu.longo@arm.com>
Subject: [PATCH v3 1/8] gdb support: add gdb::ranges::replace algorithm
Date: Tue, 22 Sep 2026 14:50:43 +0100 [thread overview]
Message-ID: <20260922135050.236941-2-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260922135050.236941-1-matthieu.longo@arm.com>
Provide a C++17-compatible replacement for the C++20 std::ranges::replace
algorithm, allowing callers to use a consistent interface until GDB
transitions to C++20. The helper should be removed once the C++ standard
library implementation become available.
Also add unit tests for ranges::replace.
https://en.cppreference.com/cpp/algorithm/ranges/replace
---
gdb/Makefile.in | 1 +
gdb/unittests/ranges-selftests.c | 63 ++++++++++++++++++++++++++++++++
gdbsupport/ranges.h | 39 ++++++++++++++++++++
3 files changed, 103 insertions(+)
create mode 100644 gdb/unittests/ranges-selftests.c
create mode 100644 gdbsupport/ranges.h
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index d1574ec2d2c..9d34b6cd3e4 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -483,6 +483,7 @@ SELFTESTS_SRCS = \
unittests/ptid-selftests.c \
unittests/main-thread-selftests.c \
unittests/mkdir-recursive-selftests.c \
+ unittests/ranges-selftests.c \
unittests/remote-arg-selftests.c \
unittests/rsp-low-selftests.c \
unittests/scoped_fd-selftests.c \
diff --git a/gdb/unittests/ranges-selftests.c b/gdb/unittests/ranges-selftests.c
new file mode 100644
index 00000000000..6791502cc1f
--- /dev/null
+++ b/gdb/unittests/ranges-selftests.c
@@ -0,0 +1,63 @@
+/* Self tests for gdb::ranges algorithms for GDB, the GNU debugger.
+
+ Copyright (C) 2017-2026 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "gdbsupport/selftest.h"
+#include "gdbsupport/array-view.h"
+#include "gdbsupport/ranges.h"
+#include <array>
+#include <vector>
+
+namespace selftests {
+namespace ranges_replace_tests {
+
+/* Entry point. */
+
+static void
+run_tests ()
+{
+ /* With owning container, no copy and elements correctly replaced. */
+ {
+ std::vector<int> vec = {1, 2, 3, 2};
+ gdb::ranges::replace (vec, 2, 1);
+ SELF_CHECK (vec[0] == 1);
+ SELF_CHECK (vec[1] == 1);
+ SELF_CHECK (vec[2] == 3);
+ SELF_CHECK (vec[3] == 1);
+ }
+
+ /* With non-owning container. */
+ {
+ std::array<int, 4> array = {1, 2, 3, 2};
+ gdb::array_view<int> view = array;
+ gdb::ranges::replace (view, 2, 1);
+ SELF_CHECK (array[0] == 1);
+ SELF_CHECK (array[1] == 1);
+ SELF_CHECK (array[2] == 3);
+ SELF_CHECK (array[3] == 1);
+ }
+}
+
+} /* namespace ranges_replaceq_tests */
+} /* namespace selftests */
+
+INIT_GDB_FILE (ranges_selftests)
+{
+ selftests::register_test ("ranges_replace",
+ selftests::ranges_replace_tests::run_tests);
+}
diff --git a/gdbsupport/ranges.h b/gdbsupport/ranges.h
new file mode 100644
index 00000000000..96d9c0162bb
--- /dev/null
+++ b/gdbsupport/ranges.h
@@ -0,0 +1,39 @@
+/* Copyright (C) 2026 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef GDBSUPPORT_RANGES_H
+#define GDBSUPPORT_RANGES_H
+
+namespace gdb {
+namespace ranges {
+
+/* Replace all occurrences of a value in the provided range.
+
+ Note: this helper is a reimplementation of std::ranges::replace, only
+ available from C++20 onwards, and consequently, should be replaced by
+ std::ranges::replace once GDB switches to C++20. */
+
+template <class Range, typename T>
+void replace (Range &&r, const T &old_value, const T &new_value)
+{
+ std::replace (r.begin (), r.end (), old_value, new_value);
+}
+
+} /* namespace ranges */
+} /* namespace gdb */
+
+#endif /* GDBSUPPORT_RANGES_H */
--
2.55.0
next prev parent reply other threads:[~2026-09-22 13:52 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-22 13:50 [PATCH v3 0/8] gdb: introduce file_reader_t to read procfs files Matthieu Longo
2026-09-22 13:50 ` Matthieu Longo [this message]
2026-09-22 13:50 ` [PATCH v3 2/8] gdb/linux: remove redundant struct prefixes from smaps_data Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 3/8] gdb/linux: style filenames in /proc warning messages Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 4/8] gdbsupport: add extract_next_field helper Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 5/8] gdb/linux: refactor /proc mapping parsing to use array_view Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 6/8] gdb: introduce helper class target_file_reader Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 7/8] gdb/linux-tdep: use target_file_reader for procfs parsing Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 8/8] gdb/linux-tdep: remove legacy parse_smaps_data overload Matthieu Longo
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=20260922135050.236941-2-matthieu.longo@arm.com \
--to=matthieu.longo@arm.com \
--cc=aburgess@redhat.com \
--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