[v2,2/6] gdb support: add gdb::ranges::replace algorithm

Message ID 20260825100912.514232-3-matthieu.longo@arm.com
State New
Headers
Series gdb: introduce file_reader_t to read procfs files |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply

Commit Message

Matthieu Longo Aug. 25, 2026, 10:09 a.m. UTC
  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.

https://en.cppreference.com/cpp/algorithm/ranges/replace
---
 gdbsupport/array-view.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
  

Comments

Andrew Burgess Sept. 10, 2026, 11:07 a.m. UTC | #1
Matthieu Longo <matthieu.longo@arm.com> writes:

> 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.
>
> https://en.cppreference.com/cpp/algorithm/ranges/replace
> ---
>  gdbsupport/array-view.h | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>
> diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
> index 8431d7f5add..f9842ecff30 100644
> --- a/gdbsupport/array-view.h
> +++ b/gdbsupport/array-view.h
> @@ -225,6 +225,22 @@ void copy (gdb::array_view<U> src, gdb::array_view<T> dest)
>      std::copy_backward (src.begin (), src.end (), dest.end ());
>  }
>  
> +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)

Looking at the linked cppreference page, the C++20 functions take R as
'Range &&r'.  Doesn't your versions create a copy of the range?  This
will work fine for non-owning ranges, like gdb::array_view, but will
mean replace operates on a copy of the range for something like
std::vector.  Even if what you have above is intentional, I think this
difference should be highlighted and explained.

Also, as this isn't specifically tied to gdb::array_view, I wonder if
this would be better put into a new file gdbsupport/ranges.h ? I don't
think anything much is needed to create the new file other than just
adding the file, so that should be pretty easy to do.

Thanks,
Andrew
  
Andrew Burgess Sept. 10, 2026, 11:08 a.m. UTC | #2
Andrew Burgess <aburgess@redhat.com> writes:

> Matthieu Longo <matthieu.longo@arm.com> writes:
>
>> 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.
>>
>> https://en.cppreference.com/cpp/algorithm/ranges/replace
>> ---
>>  gdbsupport/array-view.h | 16 ++++++++++++++++
>>  1 file changed, 16 insertions(+)
>>
>> diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
>> index 8431d7f5add..f9842ecff30 100644
>> --- a/gdbsupport/array-view.h
>> +++ b/gdbsupport/array-view.h
>> @@ -225,6 +225,22 @@ void copy (gdb::array_view<U> src, gdb::array_view<T> dest)
>>      std::copy_backward (src.begin (), src.end (), dest.end ());
>>  }
>>  
>> +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)
>
> Looking at the linked cppreference page, the C++20 functions take R as
> 'Range &&r'.  Doesn't your versions create a copy of the range?  This
> will work fine for non-owning ranges, like gdb::array_view, but will
> mean replace operates on a copy of the range for something like
> std::vector.  Even if what you have above is intentional, I think this
> difference should be highlighted and explained.
>
> Also, as this isn't specifically tied to gdb::array_view, I wonder if
> this would be better put into a new file gdbsupport/ranges.h ? I don't
> think anything much is needed to create the new file other than just
> adding the file, so that should be pretty easy to do.

Just as I hit send I realised that I should also say this would benefit
from some unittests, see gdb/unittests/ for examples.

Thanks,
Andrew
  

Patch

diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
index 8431d7f5add..f9842ecff30 100644
--- a/gdbsupport/array-view.h
+++ b/gdbsupport/array-view.h
@@ -225,6 +225,22 @@  void copy (gdb::array_view<U> src, gdb::array_view<T> dest)
     std::copy_backward (src.begin (), src.end (), dest.end ());
 }
 
+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 */
+
 /* Compare LHS and RHS for (deep) equality.  That is, whether LHS and
    RHS have the same sizes, and whether each pair of elements of LHS
    and RHS at the same position compares equal.  */