[PATCH v3 13/33] Add "fullname" handling to file_and_directory

Tom Tromey tom@tromey.com
Tue Dec 7 17:17:57 GMT 2021


>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> This changes the file_and_directory object to be able to compute and
Tom> cache the "fullname" in the same way that is done by other code, like
Tom> the psymtab reader.

This turned out to need another update, to
dwarf2_base_index_functions::map_symbol_filenames, to avoid some regressions.
I've appended the updated patch.

Tom

commit 562749e543b72c3999b8e759a5a320a0164c047d
Author: Tom Tromey <tom@tromey.com>
Date:   Mon Nov 29 17:56:56 2021 -0700

    Add "fullname" handling to file_and_directory
    
    This changes the file_and_directory object to be able to compute and
    cache the "fullname" in the same way that is done by other code, like
    the psymtab reader.

diff --git a/gdb/dwarf2/file-and-dir.h b/gdb/dwarf2/file-and-dir.h
index c56922ff90d..50a94a90525 100644
--- a/gdb/dwarf2/file-and-dir.h
+++ b/gdb/dwarf2/file-and-dir.h
@@ -28,6 +28,7 @@
 #define GDB_DWARF2_FILE_AND_DIR_H
 
 #include "objfiles.h"
+#include "source.h"
 #include <string>
 
 /* The return type of find_file_and_directory.  Note, the enclosed
@@ -90,6 +91,20 @@ struct file_and_directory
     m_name = m_name_storage.get ();
   }
 
+  /* Return the full name, computing it if necessary.  */
+  const char *get_fullname ()
+  {
+    if (m_fullname == nullptr)
+      m_fullname = find_source_or_rewrite (get_name (), get_comp_dir ());
+    return m_fullname.get ();
+  }
+
+  /* Forget the full name.  */
+  void forget_fullname ()
+  {
+    m_fullname.reset ();
+  }
+
 private:
 
   /* The filename.  */
@@ -106,6 +121,9 @@ struct file_and_directory
 
   /* The compilation directory, if it needed to be allocated.  */
   std::string m_comp_dir_storage;
+
+  /* The full name.  */
+  gdb::unique_xmalloc_ptr<char> m_fullname;
 };
 
 #endif /* GDB_DWARF2_FILE_AND_DIR_H */
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 1ae90088d54..a14c5616e65 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -3139,6 +3139,9 @@ dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
 void
 dwarf2_per_cu_data::free_cached_file_names ()
 {
+  if (fnd != nullptr)
+    fnd->forget_fullname ();
+
   if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
     return;
 
@@ -4362,6 +4365,27 @@ dw_expand_symtabs_matching_file_matcher
       if (per_objfile->symtab_set_p (per_cu.get ()))
 	continue;
 
+      if (per_cu->fnd != nullptr)
+	{
+	  file_and_directory *fnd = per_cu->fnd.get ();
+
+	  if (file_matcher (fnd->get_name (), false))
+	    {
+	      per_cu->v.quick->mark = 1;
+	      continue;
+	    }
+
+	  /* Before we invoke realpath, which can get expensive when many
+	     files are involved, do a quick comparison of the basenames.  */
+	  if ((basenames_may_differ
+	       || file_matcher (lbasename (fnd->get_name ()), true))
+	      && file_matcher (fnd->get_fullname (), false))
+	    {
+	      per_cu->v.quick->mark = 1;
+	      continue;
+	    }
+	}
+
       quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
 							per_objfile);
       if (file_data == NULL)
@@ -4556,6 +4580,24 @@ dwarf2_base_index_functions::map_symbol_filenames
       if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
 	continue;
 
+      if (per_cu->fnd != nullptr)
+	{
+	  file_and_directory *fnd = per_cu->fnd.get ();
+
+	  const char *filename = fnd->get_name ();
+	  const char *key = filename;
+	  const char *fullname = nullptr;
+
+	  if (need_fullname)
+	    {
+	      fullname = fnd->get_fullname ();
+	      key = fullname;
+	    }
+
+	  if (!filenames_cache.seen (key))
+	    fun (filename, fullname);
+	}
+
       quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
       if (file_data == nullptr
 	  || qfn_cache.find (file_data) != qfn_cache.end ())


More information about the Gdb-patches mailing list