[v2,2/3] gdb/dwarf: move cooked_index_storage to cooked-index-storage.{h, c}

Message ID 20250306203105.259225-2-simon.marchi@polymtl.ca
State New
Headers
Series [v2,1/3] gdb/dwarf: move cutu_reader to read.h |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply

Commit Message

Simon Marchi March 6, 2025, 8:30 p.m. UTC
  From: Simon Marchi <simon.marchi@polymtl.ca>

cooked_index_storage is currently declared in `cooked-index.h` and
implemented in `read.c`.  Move all that to new
`cooked-index-storage.{h,c}` files.

Change-Id: I2a07eb446d8a07b15c5664dfe01e3a820cdd45be
---
 gdb/Makefile.in                   |   2 +
 gdb/dwarf2/cooked-index-storage.c |  76 ++++++++++++++++++++
 gdb/dwarf2/cooked-index-storage.h | 115 ++++++++++++++++++++++++++++++
 gdb/dwarf2/cooked-index.h         |  88 -----------------------
 gdb/dwarf2/read.c                 |  50 +------------
 5 files changed, 194 insertions(+), 137 deletions(-)
 create mode 100644 gdb/dwarf2/cooked-index-storage.c
 create mode 100644 gdb/dwarf2/cooked-index-storage.h
  

Comments

Tom Tromey March 7, 2025, 2:03 p.m. UTC | #1
>>>>> "Simon" == simon marchi <simon.marchi@polymtl.ca> writes:

Simon> From: Simon Marchi <simon.marchi@polymtl.ca>
Simon> cooked_index_storage is currently declared in `cooked-index.h` and
Simon> implemented in `read.c`.  Move all that to new
Simon> `cooked-index-storage.{h,c}` files.

Looks good, thanks.
Approved-By: Tom Tromey <tom@tromey.com>

Tom
  

Patch

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index a24c86fcff85..bf41f4142fbf 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1097,6 +1097,7 @@  COMMON_SFILES = \
 	dwarf2/attribute.c \
 	dwarf2/comp-unit-head.c \
 	dwarf2/cooked-index.c \
+	dwarf2/cooked-index-storage.c \
 	dwarf2/cu.c \
 	dwarf2/die.c \
 	dwarf2/dwz.c \
@@ -1354,6 +1355,7 @@  HFILES_NO_SRCDIR = \
 	dummy-frame.h \
 	dwarf2/aranges.h \
 	dwarf2/cooked-index.h \
+	dwarf2/cooked-index-storage.h \
 	dwarf2/cu.h \
 	dwarf2/frame-tailcall.h \
 	dwarf2/frame.h \
diff --git a/gdb/dwarf2/cooked-index-storage.c b/gdb/dwarf2/cooked-index-storage.c
new file mode 100644
index 000000000000..820989e9aebe
--- /dev/null
+++ b/gdb/dwarf2/cooked-index-storage.c
@@ -0,0 +1,76 @@ 
+/* DWARF index storage
+
+   Copyright (C) 2022-2025 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program 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 of the License, or
+   (at your option) any later version.
+
+   This program 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 program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "dwarf2/cooked-index-storage.h"
+
+/* See cooked-index-storage.h.  */
+
+cooked_index_storage::cooked_index_storage ()
+  : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
+				      eq_cutu_reader,
+				      htab_delete_entry<cutu_reader>,
+				      xcalloc, xfree)),
+    m_shard (new cooked_index_shard)
+{
+}
+
+/* See cooked-index-storage.h.  */
+
+cutu_reader *
+cooked_index_storage::get_reader (dwarf2_per_cu *per_cu)
+{
+  int index = per_cu->index;
+  return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
+					      &index, index);
+}
+
+/* See cooked-index-storage.h.  */
+
+cutu_reader *
+cooked_index_storage::preserve (cutu_reader_up reader)
+{
+  m_abbrev_table_cache.add (reader->release_abbrev_table ());
+
+  int index = reader->cu ()->per_cu->index;
+  void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
+					  index, INSERT);
+  gdb_assert (*slot == nullptr);
+  cutu_reader *result = reader.get ();
+  *slot = reader.release ();
+  return result;
+}
+
+/* See cooked-index-storage.h.  */
+
+hashval_t
+cooked_index_storage::hash_cutu_reader (const void *a)
+{
+  const cutu_reader *reader = (const cutu_reader *) a;
+  return reader->cu ()->per_cu->index;
+}
+
+/* See cooked-index-storage.h.  */
+
+int
+cooked_index_storage::eq_cutu_reader (const void *a, const void *b)
+{
+  const cutu_reader *ra = (const cutu_reader *) a;
+  const int *rb = (const int *) b;
+  return ra->cu ()->per_cu->index == *rb;
+}
diff --git a/gdb/dwarf2/cooked-index-storage.h b/gdb/dwarf2/cooked-index-storage.h
new file mode 100644
index 000000000000..3d0b5b23f3a4
--- /dev/null
+++ b/gdb/dwarf2/cooked-index-storage.h
@@ -0,0 +1,115 @@ 
+/* DWARF index storage
+
+   Copyright (C) 2022-2025 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program 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 of the License, or
+   (at your option) any later version.
+
+   This program 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 program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_DWARF2_COOKED_INDEX_STORAGE_H
+#define GDB_DWARF2_COOKED_INDEX_STORAGE_H
+
+#include "dwarf2/abbrev-table-cache.h"
+#include "dwarf2/cooked-index.h"
+#include "dwarf2/types.h"
+
+struct cutu_reader;
+struct dwarf2_per_cu;
+
+using cutu_reader_up = std::unique_ptr<cutu_reader>;
+
+/* An instance of this is created when scanning DWARF to create a
+   cooked index.  */
+
+class cooked_index_storage
+{
+public:
+
+  cooked_index_storage ();
+  DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
+
+  /* Return the current abbrev table_cache.  */
+  const abbrev_table_cache &get_abbrev_table_cache () const
+  { return m_abbrev_table_cache; }
+
+  /* Return the DIE reader corresponding to PER_CU.  If no such reader
+     has been registered, return NULL.  */
+  cutu_reader *get_reader (dwarf2_per_cu *per_cu);
+
+  /* Preserve READER by storing it in the local hash table.  */
+  cutu_reader *preserve (cutu_reader_up reader);
+
+  /* Add an entry to the index.  The arguments describe the entry; see
+     cooked-index.h.  The new entry is returned.  */
+  cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
+			   cooked_index_flag flags,
+			   const char *name,
+			   cooked_index_entry_ref parent_entry,
+			   dwarf2_per_cu *per_cu)
+  {
+    return m_shard->add (die_offset, tag, flags, per_cu->lang (),
+			 name, parent_entry, per_cu);
+  }
+
+  /* Install the current addrmap into the shard being constructed,
+     then transfer ownership of the index to the caller.  */
+  cooked_index_shard_up release ()
+  {
+    m_shard->install_addrmap (&m_addrmap);
+    return std::move (m_shard);
+  }
+
+  /* Return the mutable addrmap that is currently being created.  */
+  addrmap_mutable *get_addrmap ()
+  {
+    return &m_addrmap;
+  }
+
+  /* Return the parent_map that is currently being created.  */
+  parent_map *get_parent_map ()
+  {
+    return &m_parent_map;
+  }
+
+  /* Return the parent_map that is currently being created.  Ownership
+     is passed to the caller.  */
+  parent_map release_parent_map ()
+  {
+    return std::move (m_parent_map);
+  }
+
+private:
+
+  /* Hash function for a cutu_reader.  */
+  static hashval_t hash_cutu_reader (const void *a);
+
+  /* Equality function for cutu_reader.  */
+  static int eq_cutu_reader (const void *a, const void *b);
+
+  /* The abbrev table cache used by this indexer.  */
+  abbrev_table_cache m_abbrev_table_cache;
+
+  /* A hash table of cutu_reader objects.  */
+  htab_up m_reader_hash;
+  /* The index shard that is being constructed.  */
+  cooked_index_shard_up m_shard;
+
+  /* Parent map for each CU that is read.  */
+  parent_map m_parent_map;
+
+  /* A writeable addrmap being constructed by this scanner.  */
+  addrmap_mutable m_addrmap;
+};
+
+#endif /* GDB_DWARF2_COOKED_INDEX_STORAGE_H */
diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h
index 94c13b65df52..f81c07840245 100644
--- a/gdb/dwarf2/cooked-index.h
+++ b/gdb/dwarf2/cooked-index.h
@@ -30,7 +30,6 @@ 
 #include "gdbsupport/iterator-range.h"
 #include "dwarf2/mapped-index.h"
 #include "dwarf2/read.h"
-#include "dwarf2/abbrev-table-cache.h"
 #include "dwarf2/parent-map.h"
 #include "gdbsupport/range-chain.h"
 #include "complaints.h"
@@ -362,93 +361,6 @@  class cooked_index_shard
 
 using cooked_index_shard_up = std::unique_ptr<cooked_index_shard>;
 
-class cutu_reader;
-
-using cutu_reader_up = std::unique_ptr<cutu_reader>;
-
-/* An instance of this is created when scanning DWARF to create a
-   cooked index.  */
-
-class cooked_index_storage
-{
-public:
-
-  cooked_index_storage ();
-  DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
-
-  /* Return the current abbrev table_cache.  */
-  const abbrev_table_cache &get_abbrev_table_cache () const
-  { return m_abbrev_table_cache; }
-
-  /* Return the DIE reader corresponding to PER_CU.  If no such reader
-     has been registered, return NULL.  */
-  cutu_reader *get_reader (dwarf2_per_cu *per_cu);
-
-  /* Preserve READER by storing it in the local hash table.  */
-  cutu_reader *preserve (cutu_reader_up reader);
-
-  /* Add an entry to the index.  The arguments describe the entry; see
-     cooked-index.h.  The new entry is returned.  */
-  cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
-			   cooked_index_flag flags,
-			   const char *name,
-			   cooked_index_entry_ref parent_entry,
-			   dwarf2_per_cu *per_cu)
-  {
-    return m_shard->add (die_offset, tag, flags, per_cu->lang (),
-			 name, parent_entry, per_cu);
-  }
-
-  /* Install the current addrmap into the shard being constructed,
-     then transfer ownership of the index to the caller.  */
-  cooked_index_shard_up release ()
-  {
-    m_shard->install_addrmap (&m_addrmap);
-    return std::move (m_shard);
-  }
-
-  /* Return the mutable addrmap that is currently being created.  */
-  addrmap_mutable *get_addrmap ()
-  {
-    return &m_addrmap;
-  }
-
-  /* Return the parent_map that is currently being created.  */
-  parent_map *get_parent_map ()
-  {
-    return &m_parent_map;
-  }
-
-  /* Return the parent_map that is currently being created.  Ownership
-     is passed to the caller.  */
-  parent_map release_parent_map ()
-  {
-    return std::move (m_parent_map);
-  }
-
-private:
-
-  /* Hash function for a cutu_reader.  */
-  static hashval_t hash_cutu_reader (const void *a);
-
-  /* Equality function for cutu_reader.  */
-  static int eq_cutu_reader (const void *a, const void *b);
-
-  /* The abbrev table cache used by this indexer.  */
-  abbrev_table_cache m_abbrev_table_cache;
-
-  /* A hash table of cutu_reader objects.  */
-  htab_up m_reader_hash;
-  /* The index shard that is being constructed.  */
-  cooked_index_shard_up m_shard;
-
-  /* Parent map for each CU that is read.  */
-  parent_map m_parent_map;
-
-  /* A writeable addrmap being constructed by this scanner.  */
-  addrmap_mutable m_addrmap;
-};
-
 /* The possible states of the index.  See the explanatory comment
    before cooked_index for more details.  */
 enum class cooked_state
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index ccf532009686..765940d0b0c1 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -32,6 +32,7 @@ 
 #include "dwarf2/aranges.h"
 #include "dwarf2/attribute.h"
 #include "dwarf2/comp-unit-head.h"
+#include "dwarf2/cooked-index-storage.h"
 #include "dwarf2/cu.h"
 #include "dwarf2/index-cache.h"
 #include "dwarf2/leb.h"
@@ -3501,55 +3502,6 @@  get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
   gdb_assert (tu_group != nullptr);
   return tu_group;
 }
-
-
-cooked_index_storage::cooked_index_storage ()
-  : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
-				      eq_cutu_reader,
-				      htab_delete_entry<cutu_reader>,
-				      xcalloc, xfree)),
-    m_shard (new cooked_index_shard)
-{
-}
-
-cutu_reader *
-cooked_index_storage::get_reader (dwarf2_per_cu *per_cu)
-{
-  int index = per_cu->index;
-  return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
-					      &index, index);
-}
-
-cutu_reader *
-cooked_index_storage::preserve (cutu_reader_up reader)
-{
-  m_abbrev_table_cache.add (reader->release_abbrev_table ());
-
-  int index = reader->cu ()->per_cu->index;
-  void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
-					  index, INSERT);
-  gdb_assert (*slot == nullptr);
-  cutu_reader *result = reader.get ();
-  *slot = reader.release ();
-  return result;
-}
-
-/* Hash function for a cutu_reader.  */
-hashval_t
-cooked_index_storage::hash_cutu_reader (const void *a)
-{
-  const cutu_reader *reader = (const cutu_reader *) a;
-  return reader->cu ()->per_cu->index;
-}
-
-/* Equality function for cutu_reader.  */
-int
-cooked_index_storage::eq_cutu_reader (const void *a, const void *b)
-{
-  const cutu_reader *ra = (const cutu_reader *) a;
-  const int *rb = (const int *) b;
-  return ra->cu ()->per_cu->index == *rb;
-}
 
 /* An instance of this is created to index a CU.  */