[4/4] Remove operator== and operator!= from gdb::span

Message ID 20261103-span-part-1-v1-4-133180dd43a5@adacore.com
State New
Headers
Series Rename gdb::array_view to gdb::span |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed

Commit Message

Tom Tromey Nov. 3, 2026, 7:18 p.m. UTC
  gdb::span currently provides operator== and operator!=.  However,
these aren't part of the C++20 std::span.  There, instead, one must
use std::ranges::equal for deep equality.

This patch changes gdb::span to be similar, introducing
gdb::ranges::equal.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31422
---
 gdb/dwarf2/read-debug-names.c |  9 ++++++---
 gdb/value.c                   |  2 +-
 gdbsupport/span.h             | 14 +++++---------
 3 files changed, 12 insertions(+), 13 deletions(-)
  

Comments

Simon Marchi Nov. 10, 2026, 8:03 p.m. UTC | #1
On 11/3/26 2:18 PM, Tom Tromey wrote:
> diff --git a/gdbsupport/span.h b/gdbsupport/span.h
> index 01429bcc666..360ff766182 100644
> --- a/gdbsupport/span.h
> +++ b/gdbsupport/span.h
> @@ -229,9 +229,12 @@ void copy (gdb::span<U> src, gdb::span<T> dest)
>     RHS have the same sizes, and whether each pair of elements of LHS
>     and RHS at the same position compares equal.  */
>  
> +namespace ranges
> +{

I think the comment above should be inside the "namespace ranges", since
it applies to the function below.

Simon
  
Simon Marchi Nov. 10, 2026, 8:28 p.m. UTC | #2
On 11/10/26 3:03 PM, Simon Marchi wrote:
> On 11/3/26 2:18 PM, Tom Tromey wrote:
>> diff --git a/gdbsupport/span.h b/gdbsupport/span.h
>> index 01429bcc666..360ff766182 100644
>> --- a/gdbsupport/span.h
>> +++ b/gdbsupport/span.h
>> @@ -229,9 +229,12 @@ void copy (gdb::span<U> src, gdb::span<T> dest)
>>     RHS have the same sizes, and whether each pair of elements of LHS
>>     and RHS at the same position compares equal.  */
>>  
>> +namespace ranges
>> +{
> 
> I think the comment above should be inside the "namespace ranges", since
> it applies to the function below.

Also, maybe the function could be moved to gdbsupport/ranges.h, and
tested in ranges-selftest.c.

While at it (as another patch), we could move
gdb::ranges::views::enumerate to ranges.h as well.  Basically all the
functions that are a backport of <ranges>.

Simon
  
Tom Tromey Nov. 11, 2026, 3:29 p.m. UTC | #3
Simon> Also, maybe the function could be moved to gdbsupport/ranges.h, and
Simon> tested in ranges-selftest.c.

There aren't any existing tests for this.
I don't think I'll write them as part of this series.

Simon> While at it (as another patch), we could move
Simon> gdb::ranges::views::enumerate to ranges.h as well.  Basically all the
Simon> functions that are a backport of <ranges>.

Someone else will have to do this, v2 of this series is already long.

Tom
  
Simon Marchi Nov. 11, 2026, 3:46 p.m. UTC | #4
On 2026-11-11 10:29, Tom Tromey wrote:
> Simon> Also, maybe the function could be moved to gdbsupport/ranges.h, and
> Simon> tested in ranges-selftest.c.
> 
> There aren't any existing tests for this.
> I don't think I'll write them as part of this series.

Ah ok I thought it would be tested in array-view-selftests.c, no
worries.

> 
> Simon> While at it (as another patch), we could move
> Simon> gdb::ranges::views::enumerate to ranges.h as well.  Basically all the
> Simon> functions that are a backport of <ranges>.
> 
> Someone else will have to do this, v2 of this series is already long.

I don't mind doing it once the dust settles.

Simon
  
Tom Tromey Nov. 11, 2026, 4:24 p.m. UTC | #5
Simon> While at it (as another patch), we could move
Simon> gdb::ranges::views::enumerate to ranges.h as well.  Basically all the
Simon> functions that are a backport of <ranges>.

>> Someone else will have to do this, v2 of this series is already long.

Simon> I don't mind doing it once the dust settles.

The other thing I've been doing is changing these files to use the std::
versions when __cplusplus allows for it.  I think this should probably
be required for future shims.  Or at least future c++20 shims, I'm less
sure about whether we want to commit to c++23 stuff yet, I don't even
know if gdb can be built in that mode.

Tom
  
Simon Marchi Nov. 11, 2026, 4:30 p.m. UTC | #6
On 2026-11-11 11:24, Tom Tromey wrote:
> Simon> While at it (as another patch), we could move
> Simon> gdb::ranges::views::enumerate to ranges.h as well.  Basically all the
> Simon> functions that are a backport of <ranges>.
> 
>>> Someone else will have to do this, v2 of this series is already long.
> 
> Simon> I don't mind doing it once the dust settles.
> 
> The other thing I've been doing is changing these files to use the std::
> versions when __cplusplus allows for it.  I think this should probably
> be required for future shims.  Or at least future c++20 shims, I'm less
> sure about whether we want to commit to c++23 stuff yet, I don't even
> know if gdb can be built in that mode.

Note that we could also just use this:

https://github.com/nonstd-lite/span-lite/

It does the "use std::span if available" thing by default, although you
can change it via a macro.  It's one header file to drop in gdbsupport,
works well, I've used it (along with other "nonstd-lite" polyfills) in
other projects.  License is compatible.

Simon
  
Tom Tromey Nov. 11, 2026, 5:18 p.m. UTC | #7
Simon> Note that we could also just use this:

Simon> https://github.com/nonstd-lite/span-lite/

Simon> It does the "use std::span if available" thing by default, although you
Simon> can change it via a macro.  It's one header file to drop in gdbsupport,
Simon> works well, I've used it (along with other "nonstd-lite" polyfills) in
Simon> other projects.  License is compatible.

I generally don't like importing code and we already have an
implementation that has worked well for us for years.
Perhaps in a different scenario I would consider it more seriously.

Tom
  
Tom Tromey Nov. 11, 2026, 5:19 p.m. UTC | #8
Tom> I generally don't like importing code and we already have an
Tom> implementation that has worked well for us for years.

Worth noting here that maybe C++20 isn't crazily far off for gdb.

Tom
  

Patch

diff --git a/gdb/dwarf2/read-debug-names.c b/gdb/dwarf2/read-debug-names.c
index a1e9d8c12ee..ca3b46fc9b1 100644
--- a/gdb/dwarf2/read-debug-names.c
+++ b/gdb/dwarf2/read-debug-names.c
@@ -817,17 +817,20 @@  read_debug_names_from_section (dwarf2_per_objfile *per_objfile,
 			       aug_repr.c_str ());
     }
 
-  if (augmentation_string == gdb::make_span (dwarf5_augmentation_1))
+  if (gdb::ranges::equal (augmentation_string,
+			  gdb::make_span (dwarf5_augmentation_1)))
     {
       warning (_(".debug_names created by an old version of gdb; ignoring"));
       return false;
     }
-  else if (augmentation_string == gdb::make_span (dwarf5_augmentation_2))
+  else if (gdb::ranges::equal (augmentation_string,
+			       gdb::make_span (dwarf5_augmentation_2)))
     {
       map.augmentation_is_gdb = true;
       map.gdb_augmentation_version = 2;
     }
-  else if (augmentation_string == gdb::make_span (dwarf5_augmentation_3))
+  else if (gdb::ranges::equal (augmentation_string,
+			       gdb::make_span (dwarf5_augmentation_3)))
     {
       map.augmentation_is_gdb = true;
       map.gdb_augmentation_version = 3;
diff --git a/gdb/value.c b/gdb/value.c
index 36e81eb9139..f0e1905b9a7 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -4387,7 +4387,7 @@  static bool
 check_ranges_vector (gdb::span<const range> ranges,
 		     gdb::span<const range> expected)
 {
-  return ranges == expected;
+  return gdb::ranges::equal (ranges, expected);
 }
 
 /* Test the insert_into_bit_range_vector function.  */
diff --git a/gdbsupport/span.h b/gdbsupport/span.h
index 01429bcc666..360ff766182 100644
--- a/gdbsupport/span.h
+++ b/gdbsupport/span.h
@@ -229,9 +229,12 @@  void copy (gdb::span<U> src, gdb::span<T> dest)
    RHS have the same sizes, and whether each pair of elements of LHS
    and RHS at the same position compares equal.  */
 
+namespace ranges
+{
+
 template <typename T>
 bool
-operator== (const gdb::span<T> &lhs, const gdb::span<T> &rhs)
+equal (const gdb::span<T> &lhs, const gdb::span<T> &rhs)
 {
   if (lhs.size () != rhs.size ())
     return false;
@@ -243,14 +246,7 @@  operator== (const gdb::span<T> &lhs, const gdb::span<T> &rhs)
   return true;
 }
 
-/* Compare two spans for inequality.  */
-
-template <typename T>
-bool
-operator!= (const gdb::span<T> &lhs, const gdb::span<T> &rhs)
-{
-  return !(lhs == rhs);
-}
+} /* namespace ranges */
 
 /* Create a span from a pointer to an array and an element
    count.