From 47c6006401cea02a55020b0df6e85e96fe5667b8 Mon Sep 17 00:00:00 2001
From: Erick Ochoa <eochoa@gcc.gnu.org>
Date: Wed, 8 Sep 2021 21:58:08 +0200
Subject: [PATCH 4/4] Runtime error when printing

---
 gcc/ipa-hash.c | 44 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 38 insertions(+), 6 deletions(-)

diff --git a/gcc/ipa-hash.c b/gcc/ipa-hash.c
index 9cee5ba5f14..2d7e2819f32 100644
--- a/gcc/ipa-hash.c
+++ b/gcc/ipa-hash.c
@@ -29,12 +29,12 @@
 class printable
 {
 public:
-  virtual char* str () = 0;
-  void print (void);
+  virtual char* str () const = 0;
+  void print (void) const;
 };
 
 void
-printable::print (void)
+printable::print (void) const
 {
   if (!dump_file) return;
   char* _str = str ();
@@ -56,11 +56,11 @@ public:
     : _symtab_node (s)
   { gcc_assert (s); }
 
-  virtual char* str (void)
+  virtual char* str (void) const
   {
     gcc_assert (_symtab_node);
     char *retval = NULL;
-    asprintf (&retval, "%sn", _symtab_node->name ());
+    asprintf (&retval, "%s", _symtab_node->name ());
     return retval;
   }
 };
@@ -83,7 +83,7 @@ public:
     return _impl.contains (k);
   }
 
-  void print (void)
+  void print (void) const
   {
     if (!dump_file) return;
     for (auto i = _impl.begin (), e = _impl.end (); i != e; ++i)
@@ -93,6 +93,34 @@ public:
   }
 };
 
+template <typename KeyId, typename Value, typename Traits>
+class my_map_t
+{
+private:
+  hash_map <KeyId, Value, Traits> _impl;
+public:
+  my_map_t () {}
+
+  void insert (const KeyId &k, const Value &v)
+  {
+    _impl.put (k, v);
+  }
+
+  Value *select (const KeyId &k)
+  {
+    return _impl.get (k);
+  }
+
+  void print (void)
+  {
+    if (!dump_file) return;
+    for (auto i = _impl.begin (), e = _impl.end (); i != e; ++i)
+    {
+      (*i).first.print ();
+    }
+  }
+};
+
 class symtab_wrapper_traits 
 {
 public:
@@ -138,18 +166,22 @@ public:
 };
 
 my_set_t <symtab_wrapper, false, symtab_wrapper_traits> my_set;
+my_map_t <symtab_wrapper, int, simple_hashmap_traits <symtab_wrapper_traits, int>> my_map;
 
 static void
 ipa_hash_generate_summary (void)
 {
   cgraph_node *cnode = NULL;
+  int i = 0;
   FOR_EACH_FUNCTION (cnode)
   {
     symtab_wrapper w (cnode);
     my_set.insert (w);
+    my_map.insert (w, i++);
   }
 
   my_set.print ();
+  my_map.print ();
 
   return;
 }
-- 
2.25.1

