diff mbox

Add std::unordered_* C++11 allocator support

Message ID 5192932E.70903@gmail.com
State New
Headers show

Commit Message

François Dumont May 14, 2013, 7:40 p.m. UTC
Indeed, here is the patch to fix it. Also fix rendering of std::tr1 
unordered containers.

2013-05-15  François Dumont <fdumont@gcc.gnu.org>

     * python/libstdcxx/v6/printers.py (Tr1HashtableIterator): Fix
     rendering of std::tr1 unordered containers iterator.
     (StdHashtableIterator): New, render std unordered containers iterator.
     * testsuite/libstdc++-prettyprinters/tr1.cc: New.


Tested under Linux x86_64 using gdb 7.6

Ok to commit ?

François

On 05/10/2013 06:43 PM, Paolo Carlini wrote:
> .. looks like the prettyprinting code needs again adjustments:
>
> FAIL: libstdc++-prettyprinters/cxx11.cc print uom
> FAIL: libstdc++-prettyprinters/cxx11.cc print uomm
> FAIL: libstdc++-prettyprinters/cxx11.cc print uos
> FAIL: libstdc++-prettyprinters/cxx11.cc print uoms
>
> Thanks,
> Paolo.
>

Comments

Jonathan Wakely May 14, 2013, 9:32 p.m. UTC | #1
On 14 May 2013 20:40, François Dumont wrote:
> Indeed, here is the patch to fix it. Also fix rendering of std::tr1
> unordered containers.

Excellent, thanks, this is OK to commit.
diff mbox

Patch

Index: python/libstdcxx/v6/printers.py
===================================================================
--- python/libstdcxx/v6/printers.py	(revision 198805)
+++ python/libstdcxx/v6/printers.py	(working copy)
@@ -621,8 +621,16 @@ 
 
 class Tr1HashtableIterator:
     def __init__ (self, hash):
-        self.node = hash['_M_bbegin']['_M_node']['_M_nxt']
-        self.node_type = find_type(hash.type, '__node_type').pointer()
+        self.buckets = hash['_M_buckets']
+        self.bucket = 0
+        self.bucket_count = hash['_M_bucket_count']
+        self.node_type = find_type(hash.type, '_Node').pointer()
+        self.node = 0
+        while self.bucket != self.bucket_count:
+            self.node = self.buckets[self.bucket]
+            if self.node:
+                break
+            self.bucket = self.bucket + 1        
 
     def __iter__ (self):
         return self
@@ -632,9 +640,33 @@ 
             raise StopIteration
         node = self.node.cast(self.node_type)
         result = node.dereference()['_M_v']
-        self.node = node.dereference()['_M_nxt']
+        self.node = node.dereference()['_M_next'];
+        if self.node == 0:
+            self.bucket = self.bucket + 1
+            while self.bucket != self.bucket_count:
+                self.node = self.buckets[self.bucket]
+                if self.node:
+                    break
+                self.bucket = self.bucket + 1
         return result
 
+class StdHashtableIterator:
+    def __init__(self, hash):
+        self.node = hash['_M_bbegin']['_M_node']['_M_nxt']
+        self.node_type = find_type(hash.type, '__node_type').pointer()
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        if self.node == 0:
+            raise StopIteration
+        elt = self.node.cast(self.node_type).dereference()
+        self.node = elt['_M_nxt']
+        valptr = elt['_M_storage'].address
+        valptr = valptr.cast(elt.type.template_argument(0).pointer())
+        return valptr.dereference()
+
 class Tr1UnorderedSetPrinter:
     "Print a tr1::unordered_set"
 
@@ -656,7 +688,9 @@ 
 
     def children (self):
         counter = itertools.imap (self.format_count, itertools.count())
-        return itertools.izip (counter, Tr1HashtableIterator (self.hashtable()))
+        if self.typename.startswith('std::tr1'):
+            return itertools.izip (counter, Tr1HashtableIterator (self.hashtable()))
+        return itertools.izip (counter, StdHashtableIterator (self.hashtable()))
 
 class Tr1UnorderedMapPrinter:
     "Print a tr1::unordered_map"
@@ -690,9 +724,14 @@ 
     def children (self):
         counter = itertools.imap (self.format_count, itertools.count())
         # Map over the hash table and flatten the result.
-        data = self.flatten (itertools.imap (self.format_one, Tr1HashtableIterator (self.hashtable())))
+        if self.typename.startswith('std::tr1'):
+            data = self.flatten (itertools.imap (self.format_one, Tr1HashtableIterator (self.hashtable())))
+            # Zip the two iterators together.
+            return itertools.izip (counter, data)
+        data = self.flatten (itertools.imap (self.format_one, StdHashtableIterator (self.hashtable())))
         # Zip the two iterators together.
         return itertools.izip (counter, data)
+        
 
     def display_hint (self):
         return 'map'
Index: testsuite/libstdc++-prettyprinters/tr1.cc
===================================================================
--- testsuite/libstdc++-prettyprinters/tr1.cc	(revision 0)
+++ testsuite/libstdc++-prettyprinters/tr1.cc	(revision 0)
@@ -0,0 +1,90 @@ 
+// { dg-do run }
+// { dg-options "-g" }
+
+// Copyright (C) 2013 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library 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, or (at your option)
+// any later version.
+
+// This library 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 library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <tr1/unordered_map>
+#include <tr1/unordered_set>
+#include <string>
+#include <iostream>
+
+template<class T>
+void
+placeholder(const T &s)
+{
+  std::cout << s;
+}
+
+template<class T, class S>
+void
+placeholder(const std::pair<T,S> &s)
+{
+  std::cout << s.first;
+}
+
+template<class T>
+void
+use(const T &container)
+{
+  for (typename T::const_iterator i = container.begin();
+       i != container.end();
+       ++i)
+    placeholder(*i);
+}
+
+int
+main()
+{
+  std::tr1::unordered_map<int, std::string> eum;
+// { dg-final { note-test eum "std::tr1::unordered_map with 0 elements" } }
+  std::tr1::unordered_multimap<int, std::string> eumm;
+// { dg-final { note-test eumm "std::tr1::unordered_multimap with 0 elements" } }
+  std::tr1::unordered_set<int> eus;
+// { dg-final { note-test eus "std::tr1::unordered_set with 0 elements" } }
+  std::tr1::unordered_multiset<int> eums;
+// { dg-final { note-test eums "std::tr1::unordered_multiset with 0 elements" } }
+
+  std::tr1::unordered_map<int, std::string> uom;
+  uom[5] = "three";
+  uom[3] = "seven";
+// { dg-final { note-test uom {std::tr1::unordered_map with 2 elements = {[3] = "seven", [5] = "three"}} } }
+
+  std::tr1::unordered_multimap<int, std::string> uomm;
+  uomm.insert(std::pair<int, std::string> (5, "three"));
+  uomm.insert(std::pair<int, std::string> (5, "seven"));
+// { dg-final { note-test uomm {std::tr1::unordered_multimap with 2 elements = {[5] = "three", [5] = "seven"}} } }
+
+  std::tr1::unordered_set<int> uos;
+  uos.insert(5);
+// { dg-final { note-test uos {std::tr1::unordered_set with 1 elements = {[0] = 5}} } }
+
+  std::tr1::unordered_multiset<int> uoms;
+  uoms.insert(5);
+// { dg-final { note-test uoms {std::tr1::unordered_multiset with 1 elements = {[0] = 5}} } }
+
+  placeholder(""); // Mark SPOT
+  use(eum);
+  use(eumm);
+  use(eus);
+  use(eums);
+  use(uoms);
+
+  return 0;
+}
+
+// { dg-final { gdb-test SPOT } }