Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH v2 03/28] Emit some type declarations in .gdb_index
Date: Wed, 02 Apr 2025 17:45:02 -0600	[thread overview]
Message-ID: <20250402-search-in-psyms-v2-3-ea91704487cb@tromey.com> (raw)
In-Reply-To: <20250402-search-in-psyms-v2-0-ea91704487cb@tromey.com>

If you run struct-decl.exp with the .gdb_index board, you will see
that "the_type" is not emitted in the index.  This would cause a
failure in this series.  The fix is to ensure that certain necessary
type declarations are emitted.

However, a naive fix here will regress stub-array-size.exp, where a
type declaration and a type definition are both seen -- but the
declaration is seen first and causes a failure.  This is handled by
adding some code (including a mild hack) to filter out type
declarations when a corresponding type definition is seen.
---
 gdb/dwarf2/index-write.c | 79 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 74 insertions(+), 5 deletions(-)

diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index da1f6cd1e0869620a99959639718a554882bab2c..389fcaa727ce15706a735ce82c047374fbc57d2b 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -55,7 +55,7 @@
 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
   do { \
     gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
-		&& (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
+		&& (value) <= GDB_INDEX_SYMBOL_KIND_UNUSED5); \
     GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
   } while (0)
 
@@ -422,10 +422,53 @@ symtab_index_entry::minimize ()
   if (name == nullptr || cu_indices.empty ())
     return;
 
-  std::sort (cu_indices.begin (), cu_indices.end ());
+  /* We sort the indexes in a funny way: GDB_INDEX_SYMBOL_KIND_UNUSED5
+     is always sorted last; then otherwise we sort by numeric value.
+     This ensures that we prefer the definition when both a definition
+     and a declaration (stub type) are seen.  */
+  std::sort (cu_indices.begin (), cu_indices.end (),
+	     [] (offset_type vala, offset_type valb)
+	       {
+		 gdb_index_symbol_kind kinda
+		   = GDB_INDEX_SYMBOL_KIND_VALUE (vala);
+		 gdb_index_symbol_kind kindb
+		   = GDB_INDEX_SYMBOL_KIND_VALUE (valb);
+		 if (kinda == GDB_INDEX_SYMBOL_KIND_UNUSED5)
+		   {
+		     if (kindb == GDB_INDEX_SYMBOL_KIND_UNUSED5)
+		       return vala < valb;
+		     /* Declaration sorts last.  */
+		     return false;
+		   }
+		 else if (kindb == GDB_INDEX_SYMBOL_KIND_UNUSED5)
+		   {
+		     /* Declaration sorts last.  */
+		     return true;
+		   }
+		 return vala < valb;
+	       });
   auto from = std::unique (cu_indices.begin (), cu_indices.end ());
   cu_indices.erase (from, cu_indices.end ());
 
+  /* Rewrite GDB_INDEX_SYMBOL_KIND_UNUSED5.  This ensures that a type
+     declaration will be deleted by the subsequent squashing step, if
+     warranted.  */
+  for (auto &val : cu_indices)
+    {
+      gdb_index_symbol_kind kind = GDB_INDEX_SYMBOL_KIND_VALUE (val);
+      if (kind != GDB_INDEX_SYMBOL_KIND_UNUSED5)
+	continue;
+
+      offset_type newval = 0;
+      DW2_GDB_INDEX_CU_SET_VALUE (newval, GDB_INDEX_CU_VALUE (val));
+      DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE
+	(newval, GDB_INDEX_SYMBOL_STATIC_VALUE (val));
+      DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (newval,
+					   GDB_INDEX_SYMBOL_KIND_TYPE);
+
+      val = newval;
+    }
+
   /* We don't want to enter a type more than once, so
      remove any such duplicates from the list as well.  When doing
      this, we want to keep the entry from the first CU -- but this is
@@ -1212,6 +1255,22 @@ write_cooked_index (cooked_index *table,
 		    const cu_index_map &cu_index_htab,
 		    struct mapped_symtab *symtab)
 {
+  /* A type declaration that is used as a (non-trivial) parent entry
+     must be written out.  */
+  gdb::unordered_set<const cooked_index_entry *> required_entries;
+  for (const cooked_index_entry *entry : table->all_entries ())
+    {
+      if ((entry->flags & IS_TYPE_DECLARATION) == 0
+	  && entry->get_parent () != nullptr)
+	{
+	  for (const cooked_index_entry *parent = entry->get_parent ();
+	       parent != nullptr;
+	       parent = parent->get_parent ())
+	    if ((parent->flags & IS_TYPE_DECLARATION) != 0)
+	      required_entries.insert (parent);
+	}
+    }
+
   for (const cooked_index_entry *entry : table->all_entries ())
     {
       const auto it = cu_index_htab.find (entry->per_cu);
@@ -1239,8 +1298,9 @@ write_cooked_index (cooked_index *table,
 	}
       else if ((entry->flags & IS_TYPE_DECLARATION) != 0)
 	{
-	  /* Don't add type declarations to the index.  */
-	  continue;
+	  /* Don't add most type declarations to the index.  */
+	  if (!required_entries.contains (entry))
+	    continue;
 	}
 
       gdb_index_symbol_kind kind;
@@ -1252,7 +1312,16 @@ write_cooked_index (cooked_index *table,
 	       || entry->tag == DW_TAG_enumerator)
 	kind = GDB_INDEX_SYMBOL_KIND_VARIABLE;
       else if (tag_is_type (entry->tag))
-	kind = GDB_INDEX_SYMBOL_KIND_TYPE;
+	{
+	  /* If we added a type declaration, we want to note this
+	     fact for later, because we don't want a type declaration
+	     to cause the real definition to be omitted from the
+	     index.  GDB_INDEX_SYMBOL_KIND_UNUSED5 is used here, but
+	     rewritten later before the index is written.  */
+	  kind = ((entry->flags & IS_TYPE_DECLARATION) == 0
+		  ? GDB_INDEX_SYMBOL_KIND_TYPE
+		  : GDB_INDEX_SYMBOL_KIND_UNUSED5);
+	}
       else
 	kind = GDB_INDEX_SYMBOL_KIND_OTHER;
 

-- 
2.46.1


  parent reply	other threads:[~2025-04-02 23:48 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-02 23:44 [PATCH v2 00/28] Search symbols via quick API Tom Tromey
2025-04-02 23:45 ` [PATCH v2 01/28] Add another minor hack to cooked_index_entry::full_name Tom Tromey
2025-04-02 23:45 ` [PATCH v2 02/28] Change ada_decode to preserve upper-case in some situations Tom Tromey
2025-04-02 23:45 ` Tom Tromey [this message]
2025-04-21  2:50   ` [PATCH v2 03/28] Emit some type declarations in .gdb_index Simon Marchi
2025-04-21 14:50     ` Tom Tromey
2025-04-23  4:11       ` Simon Marchi
2025-04-23 20:54         ` Tom Tromey
2025-04-02 23:45 ` [PATCH v2 04/28] Ada import functions not in index Tom Tromey
2025-04-02 23:45 ` [PATCH v2 05/28] Fix index's handling of DW_TAG_imported_declaration Tom Tromey
2025-04-02 23:45 ` [PATCH v2 06/28] Put all CTF symbols in global scope Tom Tromey
2025-04-02 23:45 ` [PATCH v2 07/28] Restore "ingestion" of .debug_str when writing .debug_names Tom Tromey
2025-04-02 23:45 ` [PATCH v2 08/28] Entries from anon-struct.exp not in cooked index Tom Tromey
2025-04-02 23:45 ` [PATCH v2 09/28] Remove dwarf2_per_cu_data::mark Tom Tromey
2025-04-21  3:09   ` Simon Marchi
2025-04-21 15:38     ` Tom Tromey
2025-04-23  4:12       ` Simon Marchi
2025-04-02 23:45 ` [PATCH v2 10/28] Have expand_symtabs_matching work for already-expanded CUs Tom Tromey
2025-04-23 15:53   ` Simon Marchi
2025-04-23 20:39     ` Tom Tromey
2025-04-23 20:57       ` Tom Tromey
2025-04-02 23:45 ` [PATCH v2 11/28] Rewrite the .gdb_index reader Tom Tromey
2025-04-23 17:22   ` Simon Marchi
2025-04-23 20:50     ` Tom Tromey
2025-04-24 14:37   ` Pedro Alves
2025-04-02 23:45 ` [PATCH v2 12/28] Convert default_collect_symbol_completion_matches_break_on Tom Tromey
2025-04-02 23:45 ` [PATCH v2 13/28] Convert gdbpy_lookup_static_symbols Tom Tromey
2025-04-02 23:45 ` [PATCH v2 14/28] Convert ada_add_global_exceptions Tom Tromey
2025-04-02 23:45 ` [PATCH v2 15/28] Convert ada_language_defn::collect_symbol_completion_matches Tom Tromey
2025-04-02 23:45 ` [PATCH v2 16/28] Convert ada-lang.c:map_matching_symbols Tom Tromey
2025-04-02 23:45 ` [PATCH v2 17/28] Remove expand_symtabs_matching Tom Tromey
2025-04-02 23:45 ` [PATCH v2 18/28] Simplify basic_lookup_transparent_type Tom Tromey
2025-04-02 23:45 ` [PATCH v2 19/28] Remove objfile::expand_symtabs_for_function Tom Tromey
2025-04-02 23:45 ` [PATCH v2 20/28] Convert linespec.c:iterate_over_all_matching_symtabs Tom Tromey
2025-04-02 23:45 ` [PATCH v2 21/28] Simplify block_lookup_symbol_primary Tom Tromey
2025-04-02 23:45 ` [PATCH v2 22/28] Pass lookup_name_info to block_lookup_symbol_primary Tom Tromey
2025-04-02 23:45 ` [PATCH v2 23/28] Simplify block_lookup_symbol Tom Tromey
2025-04-02 23:45 ` [PATCH v2 24/28] Add best_symbol_tracker Tom Tromey
2025-04-02 23:45 ` [PATCH v2 25/28] Convert lookup_symbol_via_quick_fns Tom Tromey
2025-04-02 23:45 ` [PATCH v2 26/28] Convert lookup_symbol_in_objfile Tom Tromey
2025-04-02 23:45 ` [PATCH v2 27/28] Make dw_expand_symtabs_matching_file_matcher static Tom Tromey
2025-04-23 20:00   ` Simon Marchi
2025-04-23 20:09     ` Tom Tromey
2025-04-23 20:44       ` Tom Tromey
2025-04-02 23:45 ` [PATCH v2 28/28] Remove enter_symbol_lookup Tom Tromey
2025-04-23 20:09 ` [PATCH v2 00/28] Search symbols via quick API Simon Marchi
2025-04-24 21:09   ` Tom Tromey
2025-04-28 14:07 ` Guinevere Larsen
2025-04-28 22:06   ` Tom Tromey
2025-04-29 19:31     ` Guinevere Larsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250402-search-in-psyms-v2-3-ea91704487cb@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox