[16/19] Convert typedef hash to new hash table

Message ID 20230407-t-robin-hood-hash-v1-16-900d93ef1510@tromey.com
State New
Headers
Series Add hash table to gdbsupport |

Commit Message

Tom Tromey April 7, 2023, 3:25 p.m. UTC
  This converts the typedef hash to use the new hash table.

This patch found a latent bug in the typedef code.  Previously, the
hash function looked at the type name, but the hash equality function
used types_equal -- but that strips typedefs, meaning that equality of
types did not imply equality of hashes.  This patch fixes the problem
and updates the relevant test.
---
 gdb/testsuite/gdb.cp/ptype-flags.exp | 25 +++++++---
 gdb/typeprint.c                      | 89 ++++--------------------------------
 gdb/typeprint.h                      | 29 ++++++++++--
 3 files changed, 55 insertions(+), 88 deletions(-)
  

Patch

diff --git a/gdb/testsuite/gdb.cp/ptype-flags.exp b/gdb/testsuite/gdb.cp/ptype-flags.exp
index 71018556840..7d9b2d54ac6 100644
--- a/gdb/testsuite/gdb.cp/ptype-flags.exp
+++ b/gdb/testsuite/gdb.cp/ptype-flags.exp
@@ -78,10 +78,18 @@  proc do_check_holder {name {flags ""} {show_typedefs 1} {show_methods 1}
 proc do_check_typedef_holder {name {flags ""} {show_typedefs 1} {show_methods 1}
 			      {raw 0}} {
 
-    set contents {
-	{ field public "double a;" }
-	{ field public "ns::scoped_double b;" }
-	{ field public "global_double c;" }
+    if {$raw} {
+	set contents {
+	    { field public "double a;" }
+	    { field public "ns::scoped_double b;" }
+	    { field public "global_double c;" }
+	}
+    } else {
+	set contents {
+	    { field public "class_double a;" }
+	    { field public "class_double b;" }
+	    { field public "class_double c;" }
+	}
     }
 
     if {$show_typedefs} {
@@ -89,8 +97,13 @@  proc do_check_typedef_holder {name {flags ""} {show_typedefs 1} {show_methods 1}
     }
 
     if {$show_methods} {
-	lappend contents { method private "double method1(ns::scoped_double);" }
-	lappend contents { method private "double method2(global_double);" }
+	if {$raw} {
+	    lappend contents { method private "double method1(ns::scoped_double);" }
+	    lappend contents { method private "double method2(global_double);" }
+	} else {
+	    lappend contents { method private "class_double method1(class_double);" }
+	    lappend contents { method private "class_double method2(class_double);" }
+	}
     }
 
     if {$raw} {
diff --git a/gdb/typeprint.c b/gdb/typeprint.c
index 06ecd2c4776..b4712460c19 100644
--- a/gdb/typeprint.c
+++ b/gdb/typeprint.c
@@ -195,27 +195,6 @@  print_offset_data::finish (struct type *type, int level,
 
 
 
-/* A hash function for a typedef_field.  */
-
-static hashval_t
-hash_typedef_field (const void *p)
-{
-  const struct decl_field *tf = (const struct decl_field *) p;
-
-  return htab_hash_string (TYPE_SAFE_NAME (tf->type));
-}
-
-/* An equality function for a typedef field.  */
-
-static int
-eq_typedef_field (const void *a, const void *b)
-{
-  const struct decl_field *tfa = (const struct decl_field *) a;
-  const struct decl_field *tfb = (const struct decl_field *) b;
-
-  return types_equal (tfa->type, tfb->type);
-}
-
 /* See typeprint.h.  */
 
 void
@@ -226,14 +205,7 @@  typedef_hash_table::recursively_update (struct type *t)
   for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i)
     {
       struct decl_field *tdef = &TYPE_TYPEDEF_FIELD (t, i);
-      void **slot;
-
-      slot = htab_find_slot (m_table.get (), tdef, INSERT);
-      /* Only add a given typedef name once.  Really this shouldn't
-	 happen; but it is safe enough to do the updates breadth-first
-	 and thus use the most specific typedef.  */
-      if (*slot == NULL)
-	*slot = tdef;
+      m_table.insert (tdef);
     }
 
   /* Recurse into superclasses.  */
@@ -251,7 +223,6 @@  typedef_hash_table::add_template_parameters (struct type *t)
   for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i)
     {
       struct decl_field *tf;
-      void **slot;
 
       /* We only want type-valued template parameters in the hash.  */
       if (TYPE_TEMPLATE_ARGUMENT (t, i)->aclass () != LOC_TYPEDEF)
@@ -261,45 +232,10 @@  typedef_hash_table::add_template_parameters (struct type *t)
       tf->name = TYPE_TEMPLATE_ARGUMENT (t, i)->linkage_name ();
       tf->type = TYPE_TEMPLATE_ARGUMENT (t, i)->type ();
 
-      slot = htab_find_slot (m_table.get (), tf, INSERT);
-      if (*slot == NULL)
-	*slot = tf;
+      m_table.insert (tf);
     }
 }
 
-/* See typeprint.h.  */
-
-typedef_hash_table::typedef_hash_table ()
-  : m_table (htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
-				NULL, xcalloc, xfree))
-{
-}
-
-/* Helper function for typedef_hash_table::copy.  */
-
-static int
-copy_typedef_hash_element (void **slot, void *nt)
-{
-  htab_t new_table = (htab_t) nt;
-  void **new_slot;
-
-  new_slot = htab_find_slot (new_table, *slot, INSERT);
-  if (*new_slot == NULL)
-    *new_slot = *slot;
-
-  return 1;
-}
-
-/* See typeprint.h.  */
-
-typedef_hash_table::typedef_hash_table (const typedef_hash_table &table)
-{
-  m_table.reset (htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
-				    NULL, xcalloc, xfree));
-  htab_traverse_noresize (table.m_table.get (), copy_typedef_hash_element,
-			  m_table.get ());
-}
-
 /* Look up the type T in the global typedef hash.  If it is found,
    return the typedef name.  If it is not found, apply the
    type-printers, if any, given by start_script_type_printers and return the
@@ -309,7 +245,6 @@  const char *
 typedef_hash_table::find_global_typedef (const struct type_print_options *flags,
 					 struct type *t)
 {
-  void **slot;
   struct decl_field tf, *new_tf;
 
   if (flags->global_typedefs == NULL)
@@ -318,12 +253,9 @@  typedef_hash_table::find_global_typedef (const struct type_print_options *flags,
   tf.name = NULL;
   tf.type = t;
 
-  slot = htab_find_slot (flags->global_typedefs->m_table.get (), &tf, INSERT);
-  if (*slot != NULL)
-    {
-      new_tf = (struct decl_field *) *slot;
-      return new_tf->name;
-    }
+  auto insert_pair = flags->global_typedefs->m_table.insert (&tf);
+  if (!insert_pair.second)
+    return (*insert_pair.first)->name;
 
   /* Put an entry into the hash table now, in case
      apply_ext_lang_type_printers recurses.  */
@@ -331,7 +263,7 @@  typedef_hash_table::find_global_typedef (const struct type_print_options *flags,
   new_tf->name = NULL;
   new_tf->type = t;
 
-  *slot = new_tf;
+  *insert_pair.first = new_tf;
 
   gdb::unique_xmalloc_ptr<char> applied
     = apply_ext_lang_type_printers (flags->global_printers, t);
@@ -351,15 +283,14 @@  typedef_hash_table::find_typedef (const struct type_print_options *flags,
 {
   if (flags->local_typedefs != NULL)
     {
-      struct decl_field tf, *found;
+      struct decl_field tf;
 
       tf.name = NULL;
       tf.type = t;
-      htab_t table = flags->local_typedefs->m_table.get ();
-      found = (struct decl_field *) htab_find (table, &tf);
+      auto iter = flags->local_typedefs->m_table.find (&tf);
 
-      if (found != NULL)
-	return found->name;
+      if (iter != flags->local_typedefs->m_table.end ())
+	return (*iter)->name;
     }
 
   return find_global_typedef (flags, t);
diff --git a/gdb/typeprint.h b/gdb/typeprint.h
index aa1a5872ca3..9a664c4b281 100644
--- a/gdb/typeprint.h
+++ b/gdb/typeprint.h
@@ -20,6 +20,7 @@ 
 #define TYPEPRINT_H
 
 #include "gdbsupport/gdb_obstack.h"
+#include "gdbsupport/hash-table.h"
 
 enum language;
 struct ui_file;
@@ -123,10 +124,12 @@  class typedef_hash_table
 public:
 
   /* Create a new typedef-lookup hash table.  */
-  typedef_hash_table ();
+  typedef_hash_table () = default;
 
   /* Copy a typedef hash.  */
-  typedef_hash_table (const typedef_hash_table &);
+  typedef_hash_table (const typedef_hash_table &other)
+    : m_table (other.m_table)
+  { }
 
   typedef_hash_table &operator= (const typedef_hash_table &) = delete;
 
@@ -150,8 +153,28 @@  class typedef_hash_table
 					  struct type *t);
 
 
+  /* Traits used for the hash table.  */
+  struct decl_field_traits
+  {
+    typedef decl_field *value_type;
+
+    static bool is_empty (const value_type &val)
+    { return val == nullptr; }
+
+    static bool equals (const value_type &lhs, const value_type &rhs)
+    { return types_equal (lhs->type, rhs->type); }
+
+    static size_t hash (const value_type &val)
+    {
+      /* The hash must agree with equals, and types_equal strips
+	 typedefs.  */
+      struct type *type = check_typedef (val->type);
+      return htab_hash_string (TYPE_SAFE_NAME (type));
+    }
+  };
+
   /* The actual hash table.  */
-  htab_up m_table;
+  gdb::traited_hash_table<decl_field_traits> m_table;
 
   /* Storage for typedef_field objects that must be synthesized.  */
   auto_obstack m_storage;