public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-11100] libstdc++: Fix std::deque::operator[] Xmethod [PR112491]
@ 2023-11-15 11:42 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-11-15 11:42 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:7cfe05d1ee351c2c01f75368a295afb3b712c056

commit r11-11100-g7cfe05d1ee351c2c01f75368a295afb3b712c056
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Nov 14 15:08:13 2023 +0000

    libstdc++: Fix std::deque::operator[] Xmethod [PR112491]
    
    The Xmethod for std::deque::operator[] has the same bug that I recently
    fixed for the std::deque::size() Xmethod. The first node might have
    unused capacity at the start, which needs to be accounted for when
    indexing into the deque.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/112491
            * python/libstdcxx/v6/xmethods.py (DequeWorkerBase.index):
            Correctly handle unused capacity at the start of the first node.
            * testsuite/libstdc++-xmethods/deque.cc: Check index operator
            when elements have been removed from the front.
    
    (cherry picked from commit 452476db0c705caeac8712d560fc16ced0ca5226)

Diff:
---
 libstdc++-v3/python/libstdcxx/v6/xmethods.py       | 12 ++++++++----
 libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc |  6 +++++-
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/python/libstdcxx/v6/xmethods.py b/libstdc++-v3/python/libstdcxx/v6/xmethods.py
index 5223cce76ca..a50ebca2287 100644
--- a/libstdc++-v3/python/libstdcxx/v6/xmethods.py
+++ b/libstdc++-v3/python/libstdcxx/v6/xmethods.py
@@ -170,7 +170,7 @@ class DequeWorkerBase(gdb.xmethod.XMethodWorker):
     def size(self, obj):
         start = obj['_M_impl']['_M_start']
         finish = obj['_M_impl']['_M_finish']
-        if not start['_M_node']:
+        if start['_M_cur'] == finish['_M_cur']:
             return 0
         return (self._bufsize
                 * (finish['_M_node'] - start['_M_node'] - 1)
@@ -178,9 +178,13 @@ class DequeWorkerBase(gdb.xmethod.XMethodWorker):
                 + (start['_M_last'] - start['_M_cur']))
 
     def index(self, obj, idx):
-        first_node = obj['_M_impl']['_M_start']['_M_node']
-        index_node = first_node + int(idx) // self._bufsize
-        return index_node[0][idx % self._bufsize]
+        start = obj['_M_impl']['_M_start']
+        first_node_size = start['_M_last'] - start['_M_cur']
+        if idx < first_node_size:
+            return start['_M_cur'][idx]
+        idx = idx - first_node_size
+        index_node = start['_M_node'][1 + int(idx) // self._bufsize]
+        return index_node[idx % self._bufsize]
 
 class DequeEmptyWorker(DequeWorkerBase):
     def get_arg_types(self):
diff --git a/libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc b/libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc
index 1dd0bd8d875..f366d43e34f 100644
--- a/libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc
+++ b/libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc
@@ -69,20 +69,24 @@ main ()
 
   // PR libstdc++/112491
   std::deque<int> q5;
-  q5.push_front(0);
+  q5.push_front(5);
 // { dg-final { note-test q5.size() 1 } }
+// { dg-final { note-test q5\[0\] 5 } }
   std::deque<int> q6 = q1;
   q6.pop_front();
 // { dg-final { note-test {q6.size() == (q1_size-1)} true } }
+// { dg-final { note-test q6\[1\] 102 } }
   std::deque<int> q7 = q2;
   q7.pop_front();
   q7.pop_front();
 // { dg-final { note-test {q7.size() == (q2_size-2)} true } }
+// { dg-final { note-test q7\[1\] 203 } }
   std::deque<int> q8 = q3;
   q8.pop_front();
   q8.pop_front();
   q8.pop_front();
 // { dg-final { note-test {q8.size() == (q3_size-3)} true } }
+// { dg-final { note-test q8\[1\] 304 } }
   std::deque<int> q9 = q8;
   q9.clear();
 // { dg-final { note-test q9.size() 0 } }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-11-15 11:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-15 11:42 [gcc r11-11100] libstdc++: Fix std::deque::operator[] Xmethod [PR112491] Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).