[binutils-gdb] gdbsupport: add front and back methods to gdb::span

Andrew Burgess aburgess@sourceware.org
Tue Dec 1 16:48:30 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ebc55d5cdf29e35502fcba9cda9afc574dac15cd

commit ebc55d5cdf29e35502fcba9cda9afc574dac15cd
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Fri Nov 13 19:39:51 2026 +0000

    gdbsupport: add front and back methods to gdb::span
    
    Add gdb::span::front and gdb::span::back to the pre-C++20 implementation
    of gdb::span.
    
    Another patch I'm working on would benefit from gdb::span::back.  I'm
    compiling with a compiler that uses C++20 by default, so I actually get
    std::span which has std::span::back.
    
    This commit adds the front and back helper functions to the pre-C++20
    implementation of gdb::span.  I'm adding both front and back even though
    I only need back simply for completeness.
    
    This commit does not add any real uses of these helper functions at this
    point, but I have added some unit tests and I forced my local build to
    compile the pre-C++20 implementation in order to test my new code.
    
    There should be no user visible changes with this commit.
    
    Approved-By: Tom Tromey <tom@tromey.com>

Diff:
---
 gdb/unittests/span-selftests.c | 24 ++++++++++++++++++++++++
 gdbsupport/span.h              | 16 ++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/gdb/unittests/span-selftests.c b/gdb/unittests/span-selftests.c
index c29b63a03c2..eaca7307de4 100644
--- a/gdb/unittests/span-selftests.c
+++ b/gdb/unittests/span-selftests.c
@@ -557,6 +557,30 @@ run_tests ()
       SELF_CHECK (slc[0] == data[2]);
     }
   }
+
+  /* Test front and back.  */
+  {
+    gdb_byte data[] = {0xaa, 0xbb, 0xcc};
+    gdb::span<gdb_byte> view = data;
+
+    SELF_CHECK (view.front () == 0xaa);
+    SELF_CHECK (view.back () == 0xcc);
+
+    SELF_CHECK (&view.front () == &data[0]);
+    SELF_CHECK (&view.back () == &data[2]);
+
+    view.front () = 0x11;
+    SELF_CHECK (data[0] == 0x11);
+
+    view.back () = 0x22;
+    SELF_CHECK (data[2] == 0x22);
+
+    gdb_byte data2[] = {0xaa};
+    gdb::span<gdb_byte> view2 = data2;
+
+    SELF_CHECK (view2.front () == view2.back ());
+    SELF_CHECK (&view2.front () == &view2.back ());
+  }
 }
 
 template <typename T>
diff --git a/gdbsupport/span.h b/gdbsupport/span.h
index 9ceb02effab..48c5ecb885f 100644
--- a/gdbsupport/span.h
+++ b/gdbsupport/span.h
@@ -186,6 +186,22 @@ public:
   constexpr size_type size () const noexcept { return m_size; }
   constexpr bool empty () const noexcept { return m_size == 0; }
 
+  constexpr reference back () const
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert (m_size > 0);
+#endif
+    return m_array[m_size - 1];
+  }
+
+  constexpr reference front () const
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert (m_size > 0);
+#endif
+    return m_array[0];
+  }
+
   /* Subspan of a span.  */
 
   /* Return a new span over SIZE elements starting at START.  */


More information about the Gdb-cvs mailing list