]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: add a constructor for symtab
authorAndrew Burgess <aburgess@redhat.com>
Fri, 31 Oct 2025 10:19:41 +0000 (10:19 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Sun, 2 Nov 2025 16:09:04 +0000 (16:09 +0000)
Convert symtab to use obstack_new, and have a real constructor.  The
filename, filename_for_id and m_compunit, members should really not
change once the symtab has been created, so make these members private
(m_compunit was already private) and set them just once from the
constructor.  The set_compunit function has been deleted, and new
getter functions for filename and filename_for_id have been added.

The language is also set at construction time, but can be updated
later, so set the language in the constructor, but retain
symtab::set_language for when the language needs to be updated.

Prior to this patch the symtab was allocated with OBSTACK_ZALLOC which
would zero out the symtab object.  With the call to objstack_new
fields in the symtab would no longer be initialised, so I've added
default member initialisation for everything not set in the
constructor.

The interesting changes are in symtab.h, and symfile.c.  Everything
else is just updating to handle symfile::filename and
symfile::filename_for_id becoming methods.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
15 files changed:
gdb/ada-lang.c
gdb/cli/cli-cmds.c
gdb/compile/compile-c-symbols.c
gdb/compile/compile-cplus-symbols.c
gdb/compile/compile-cplus-types.c
gdb/infrun.c
gdb/linespec.c
gdb/macroscope.c
gdb/python/py-breakpoint.c
gdb/skip.c
gdb/source.c
gdb/symfile-debug.c
gdb/symfile.c
gdb/symtab.c
gdb/symtab.h

index 0c6cdc653f504a6a277b257e921e74913602983e..7f15029579847936313285b7a84ca9f47ff3b076 100644 (file)
@@ -3519,8 +3519,8 @@ sort_choices (std::vector<struct block_symbol> &syms)
               if (!b.symbol->is_objfile_owned ())
                 return true;
 
-              const char *fna = a.symbol->symtab ()->filename;
-              const char *fnb = b.symbol->symtab ()->filename;
+              const char *fna = a.symbol->symtab ()->filename ();
+              const char *fnb = b.symbol->symtab ()->filename ();
 
               /* First sort by basename.  This is done because,
                  depending on how GNAT was invoked, different sources
@@ -11927,7 +11927,7 @@ is_known_support_routine (const frame_info_ptr &frame)
   for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
     {
       re_comp (known_runtime_file_name_patterns[i]);
-      if (re_exec (lbasename (sal.symtab->filename)))
+      if (re_exec (lbasename (sal.symtab->filename ())))
        return 1;
       if (sal.symtab->compunit ()->objfile () != NULL
          && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))
index 1374481cc9918a112ec3544307db4f7bca265bf9..6656f393580646226d9b5aa3f1ff4ed8f97b7f52 100644 (file)
@@ -2228,7 +2228,7 @@ cmp_symtabs (const symtab_and_line &sala, const symtab_and_line &salb)
        return r;
     }
 
-  r = filename_cmp (sala.symtab->filename, salb.symtab->filename);
+  r = filename_cmp (sala.symtab->filename (), salb.symtab->filename ());
   if (r)
     return r;
 
index 65789775fc4d988bf123e7ec5abc605fe737af13..a71f84be52cf4880fa6b193ce7cbd5ad1023790f 100644 (file)
@@ -56,7 +56,7 @@ convert_one_symbol (compile_c_instance *context,
                    int is_local)
 {
   gcc_type sym_type;
-  const char *filename = sym.symbol->symtab ()->filename;
+  const char *filename = sym.symbol->symtab ()->filename ();
   unsigned int line = sym.symbol->line ();
 
   context->error_symbol_once (sym.symbol);
index 96ebd84887acbff1a2bc561d96c3e8dda56ddd9f..5b43ba1cc51271e8cabe724d12cb08d471d5717d 100644 (file)
@@ -48,7 +48,7 @@ convert_one_symbol (compile_cplus_instance *instance,
 {
   /* Squash compiler warning.  */
   gcc_type sym_type = 0;
-  const char *filename = sym.symbol->symtab ()->filename;
+  const char *filename = sym.symbol->symtab ()->filename ();
   unsigned int line = sym.symbol->line ();
 
   instance->error_symbol_once (sym.symbol);
index cf70fe484d770d0e080cf8b95343bff9bc60a058..66e5ddd36fc458e169c3fdd46c6f5b98c2671e32 100644 (file)
@@ -624,7 +624,7 @@ compile_cplus_convert_struct_or_union_members
                       we can do but ignore this member.  */
                    continue;
                  }
-               const char *filename = sym.symbol->symtab ()->filename;
+               const char *filename = sym.symbol->symtab ()->filename ();
                unsigned int line = sym.symbol->line ();
 
                physaddr = sym.symbol->value_address ();
@@ -763,7 +763,7 @@ compile_cplus_convert_struct_or_union_methods (compile_cplus_instance *instance,
              continue;
            }
 
-         const char *filename = sym.symbol->symtab ()->filename;
+         const char *filename = sym.symbol->symtab ()->filename ();
          unsigned int line = sym.symbol->line ();
          CORE_ADDR address = sym.symbol->value_block()->start ();
          const char *kind;
index 824479dd75a2c6ac54ac0b4569cbdb86f989a6be..81764e6b1a2a373291004c69ff7da665e4c3700b 100644 (file)
@@ -4842,7 +4842,7 @@ set_step_info (thread_info *tp, const frame_info_ptr &frame,
 
   infrun_debug_printf
     ("symtab = %s, line = %d, step_frame_id = %s, step_stack_frame_id = %s",
-     tp->current_symtab != nullptr ? tp->current_symtab->filename : "<null>",
+     tp->current_symtab != nullptr ? tp->current_symtab->filename () : "<null>",
      tp->current_line,
      tp->control.step_frame_id.to_string ().c_str (),
      tp->control.step_stack_frame_id.to_string ().c_str ());
index 08f7fdd0daa70073b13cfe8812491d577b5c6351..277b45b4b7fc238c14052b59e0e4add35d7e6e52 100644 (file)
@@ -2005,7 +2005,7 @@ create_sals_line_offset (struct linespec_state *self,
       set_default_source_symtab_and_line ();
       initialize_defaults (&self->default_symtab, &self->default_line);
       ls->file_symtabs
-       = collect_symtabs_from_filename (self->default_symtab->filename,
+       = collect_symtabs_from_filename (self->default_symtab->filename (),
                                         self->search_pspace);
       use_default = true;
     }
@@ -2291,7 +2291,7 @@ convert_linespec_to_sals (struct linespec_state *state, linespec *ls)
        /* Make sure we have a filename for canonicalization.  */
        if (ls->explicit_loc.source_filename == NULL)
          {
-           const char *filename = state->default_symtab->filename;
+           const char *filename = state->default_symtab->filename ();
 
            /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
               form so that displaying SOURCE_FILENAME can follow the current
index 563ef9931b390746d828748af740155b1c828354..32499fe5d21db08833c225f79f9a346c9bf2d5fd 100644 (file)
@@ -51,7 +51,8 @@ sal_macro_scope (struct symtab_and_line sal)
   macro_scope ms;
 
   main_file = macro_main (cust->macro_table ());
-  inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename_for_id);
+  inclusion = macro_lookup_inclusion (main_file,
+                                     sal.symtab->filename_for_id ());
 
   if (inclusion)
     {
index 9fd1fd013d25fd3c5c8908b55302f6eab561111d..7eb4e41a5079079e9957ca60212b76bd86c7e9df 100644 (file)
@@ -1644,7 +1644,7 @@ bploc_filepath (struct symtab *bploc_symtab)
     {
     }
 
-  return host_string_to_python_string (bploc_symtab->filename);
+  return host_string_to_python_string (bploc_symtab->filename ());
 }
 
 /* Python function to get the source file name path and line number
@@ -1763,7 +1763,7 @@ bplocpy_repr (PyObject *py_self)
                          paddress (self->bp_loc->owner->gdbarch,
                                    self->bp_loc->requested_address));
   if (self->bp_loc->symtab != nullptr)
-    str += string_printf (" source=%s:%d", self->bp_loc->symtab->filename,
+    str += string_printf (" source=%s:%d", self->bp_loc->symtab->filename (),
                          self->bp_loc->line_number);
 
   const auto fn_name = self->bp_loc->function_name.get ();
index a9ee223c27728819161afffb696553413208f869..a56b3555d1730c08679dfa6a6c2b955941a356f1 100644 (file)
@@ -488,20 +488,20 @@ skiplist_entry::do_skip_file_p (const symtab_and_line &function_sal) const
   if (debug_skip)
     gdb_printf (gdb_stdlog,
                "skip: checking if file %s matches non-glob %s...",
-               function_sal.symtab->filename, m_file.c_str ());
+               function_sal.symtab->filename (), m_file.c_str ());
 
   bool result;
 
   /* Check first sole SYMTAB->FILENAME.  It may not be a substring of
      symtab_to_fullname as it may contain "./" etc.  */
-  if (compare_filenames_for_search (function_sal.symtab->filename,
+  if (compare_filenames_for_search (function_sal.symtab->filename (),
                                    m_file.c_str ()))
     result = true;
 
   /* Before we invoke realpath, which can get expensive when many
      files are involved, do a quick comparison of the basenames.  */
   else if (!basenames_may_differ
-          && filename_cmp (lbasename (function_sal.symtab->filename),
+          && filename_cmp (lbasename (function_sal.symtab->filename ()),
                            lbasename (m_file.c_str ())) != 0)
     result = false;
   else
@@ -524,13 +524,13 @@ skiplist_entry::do_skip_gfile_p (const symtab_and_line &function_sal) const
   if (debug_skip)
     gdb_printf (gdb_stdlog,
                "skip: checking if file %s matches glob %s...",
-               function_sal.symtab->filename, m_file.c_str ());
+               function_sal.symtab->filename (), m_file.c_str ());
 
   bool result;
 
   /* Check first sole SYMTAB->FILENAME.  It may not be a substring of
      symtab_to_fullname as it may contain "./" etc.  */
-  if (gdb_filename_fnmatch (m_file.c_str (), function_sal.symtab->filename,
+  if (gdb_filename_fnmatch (m_file.c_str (), function_sal.symtab->filename (),
                            FNM_NOESCAPE) == 0)
     result = true;
 
@@ -541,7 +541,7 @@ skiplist_entry::do_skip_gfile_p (const symtab_and_line &function_sal) const
      isn't much of a win.  Oh well.  */
   else if (!basenames_may_differ
       && gdb_filename_fnmatch (lbasename (m_file.c_str ()),
-                              lbasename (function_sal.symtab->filename),
+                              lbasename (function_sal.symtab->filename ()),
                               FNM_NOESCAPE) != 0)
     result = false;
   else
index e56f2b5d73607d5f1338b828e5df4be54faa1024..a47176cd9a55c41fba23888594bd524335fb4137 100644 (file)
@@ -342,7 +342,7 @@ select_source_symtab ()
        {
          for (symtab *symtab : cu.filetabs ())
            {
-             const char *name = symtab->filename;
+             const char *name = symtab->filename ();
              int len = strlen (name);
 
              if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
@@ -683,7 +683,7 @@ info_source_command (const char *ignore, int from_tty)
     }
 
   cust = s->compunit ();
-  gdb_printf (_("Current source file is %s\n"), s->filename);
+  gdb_printf (_("Current source file is %s\n"), s->filename ());
   if (s->compunit ()->dirname () != NULL)
     gdb_printf (_("Compilation directory is %s\n"), s->compunit ()->dirname ());
   if (s->fullname () != nullptr)
@@ -1119,7 +1119,8 @@ open_source_file (struct symtab *s)
     return scoped_fd (-EINVAL);
 
   gdb::unique_xmalloc_ptr<char> fullname = s->release_fullname ();
-  scoped_fd fd = find_and_open_source (s->filename, s->compunit ()->dirname (),
+  scoped_fd fd = find_and_open_source (s->filename (),
+                                      s->compunit ()->dirname (),
                                       &fullname);
 
   if (fd.get () < 0)
@@ -1129,13 +1130,13 @@ open_source_file (struct symtab *s)
          const objfile *ofp = s->compunit ()->objfile ();
 
          std::string srcpath;
-         if (IS_ABSOLUTE_PATH (s->filename))
-           srcpath = s->filename;
+         if (IS_ABSOLUTE_PATH (s->filename ()))
+           srcpath = s->filename ();
          else if (s->compunit ()->dirname () != nullptr)
            {
              srcpath = s->compunit ()->dirname ();
              srcpath += SLASH_STRING;
-             srcpath += s->filename;
+             srcpath += s->filename ();
            }
 
          const struct bfd_build_id *build_id
@@ -1220,11 +1221,11 @@ symtab_to_fullname (struct symtab *s)
             should report the pathname where GDB tried to find the file.  */
 
          if (s->compunit ()->dirname () == nullptr
-             || IS_ABSOLUTE_PATH (s->filename))
-           fullname.reset (xstrdup (s->filename));
+             || IS_ABSOLUTE_PATH (s->filename ()))
+           fullname.reset (xstrdup (s->filename ()));
          else
            fullname.reset (concat (s->compunit ()->dirname (), SLASH_STRING,
-                                   s->filename, (char *) NULL));
+                                   s->filename (), (char *) NULL));
 
          s->set_fullname (rewrite_source_path (fullname.get ()));
          if (s->fullname () == nullptr)
@@ -1241,11 +1242,11 @@ const char *
 symtab_to_filename_for_display (struct symtab *symtab)
 {
   if (filename_display_string == filename_display_basename)
-    return lbasename (symtab->filename);
+    return lbasename (symtab->filename ());
   else if (filename_display_string == filename_display_absolute)
     return symtab_to_fullname (symtab);
   else if (filename_display_string == filename_display_relative)
-    return symtab->filename;
+    return symtab->filename ();
   else
     internal_error (_("invalid filename_display_string"));
 }
index b402a87b7a75165def4255477e1a81c55c30959d..e5ea72649976e2830a7dae4f56cc8c0ca58ee6ae 100644 (file)
@@ -184,7 +184,7 @@ iterate_over_one_compunit_symtab (const char *name,
 
   for (symtab *s : cust->filetabs ())
     {
-      if (compare_filenames_for_search (s->filename, name))
+      if (compare_filenames_for_search (s->filename (), name))
        {
          if (callback (s))
            return true;
@@ -194,7 +194,7 @@ iterate_over_one_compunit_symtab (const char *name,
       /* Before we invoke realpath, which can get expensive when many
         files are involved, do a quick comparison of the basenames.  */
       if (! basenames_may_differ
-         && FILENAME_CMP (base_name, lbasename (s->filename)) != 0)
+         && FILENAME_CMP (base_name, lbasename (s->filename ())) != 0)
        continue;
 
       if (compare_filenames_for_search (symtab_to_fullname (s), name))
index 806547d9d6fd978edc9818471d0308f619b9e9e4..85827cf3d49c5b41dc7dd9fdd9653a2460d21cbb 100644 (file)
@@ -2830,11 +2830,11 @@ allocate_symtab (struct compunit_symtab *cust, const char *filename,
 {
   struct objfile *objfile = cust->objfile ();
   struct symtab *symtab
-    = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symtab);
-
-  symtab->filename = objfile->intern (filename);
-  symtab->filename_for_id = objfile->intern (filename_for_id);
-  symtab->set_language (deduce_language_from_filename (filename));
+    = obstack_new<struct symtab> (&objfile->objfile_obstack,
+                                 cust,
+                                 objfile->intern (filename),
+                                 objfile->intern (filename_for_id),
+                                 deduce_language_from_filename (filename));
 
   /* This can be very verbose with lots of headers.
      Only print at higher debug levels.  */
@@ -2860,9 +2860,6 @@ allocate_symtab (struct compunit_symtab *cust, const char *filename,
   /* Add it to CUST's list of symtabs.  */
   cust->add_filetab (symtab);
 
-  /* Backlink to the containing compunit symtab.  */
-  symtab->set_compunit (cust);
-
   return symtab;
 }
 
index 3b0687c0750c9587dd4cccff0686f4069e0c448f..3195cd2e2ae9283b368e2b076a64a8b656b016a5 100644 (file)
@@ -3309,7 +3309,7 @@ find_line_symtab (symtab *sym_tab, int line, int *index)
                  const struct linetable *l;
                  int ind;
 
-                 if (FILENAME_CMP (sym_tab->filename, s->filename) != 0)
+                 if (FILENAME_CMP (sym_tab->filename (), s->filename ()) != 0)
                    continue;
                  if (FILENAME_CMP (symtab_to_fullname (sym_tab),
                                    symtab_to_fullname (s)) != 0)
@@ -4627,8 +4627,8 @@ symbol_search::compare_search_syms (const symbol_search &sym_a,
 {
   int c;
 
-  c = FILENAME_CMP (sym_a.symbol->symtab ()->filename,
-                   sym_b.symbol->symtab ()->filename);
+  c = FILENAME_CMP (sym_a.symbol->symtab ()->filename (),
+                   sym_b.symbol->symtab ()->filename ());
   if (c != 0)
     return c;
 
@@ -4802,9 +4802,9 @@ global_symbol_searcher::add_matching_symbols
              /* Check first sole REAL_SYMTAB->FILENAME.  It does
                 not need to be a substring of symtab_to_fullname as
                 it may contain "./" etc.  */
-             if (!(file_matches (real_symtab->filename, m_filenames, false)
+             if (!(file_matches (real_symtab->filename (), m_filenames, false)
                    || ((basenames_may_differ
-                        || file_matches (lbasename (real_symtab->filename),
+                        || file_matches (lbasename (real_symtab->filename ()),
                                          m_filenames, true))
                        && file_matches (symtab_to_fullname (real_symtab),
                                         m_filenames, false))))
@@ -6220,14 +6220,14 @@ make_source_files_completion_list (const char *text)
        {
          for (symtab *s : cu.filetabs ())
            {
-             if (not_interesting_fname (s->filename))
+             if (not_interesting_fname (s->filename ()))
                continue;
-             if (!filenames_seen.seen (s->filename)
-                 && filename_ncmp (s->filename, text, text_len) == 0)
+             if (!filenames_seen.seen (s->filename ())
+                 && filename_ncmp (s->filename (), text, text_len) == 0)
                {
                  /* This file matches for a completion; add it to the current
                     list of matches.  */
-                 add_filename_to_list (s->filename, text, text, &list);
+                 add_filename_to_list (s->filename (), text, text, &list);
                }
              else
                {
@@ -6235,8 +6235,8 @@ make_source_files_completion_list (const char *text)
                     debug info records leading directories, but not the other
                     way around.  This is what subroutines of breakpoint
                     command do when they parse file names.  */
-                 base_name = lbasename (s->filename);
-                 if (base_name != s->filename
+                 base_name = lbasename (s->filename ());
+                 if (base_name != s->filename ()
                      && !filenames_seen.seen (base_name)
                      && filename_ncmp (base_name, text, text_len) == 0)
                    add_filename_to_list (base_name, text, text, &list);
index e47033efd01d1b86d470f3b138829504dfcca327..486b99d123ae13af426d338b58ce7d6e509a2e81 100644 (file)
@@ -1698,14 +1698,21 @@ typedef std::vector<CORE_ADDR> section_offsets;
 
 struct symtab
 {
-  struct compunit_symtab *compunit () const
+  symtab (struct compunit_symtab *cust, const char *filename,
+         const char *filename_for_id, enum language language)
+    : m_filename (filename),
+      m_filename_for_id (filename_for_id),
+      m_compunit (cust),
+      m_language (language)
   {
-    return m_compunit;
+    gdb_assert (m_filename != nullptr);
+    gdb_assert (m_filename_for_id != nullptr);
+    gdb_assert (m_compunit != nullptr);
   }
 
-  void set_compunit (struct compunit_symtab *compunit)
+  struct compunit_symtab *compunit () const
   {
-    m_compunit = compunit;
+    return m_compunit;
   }
 
   const struct linetable *linetable () const
@@ -1749,16 +1756,24 @@ struct symtab
     m_fullname = name.release ();
   }
 
+  const char *filename () const
+  { return m_filename; }
+
+  const char *filename_for_id () const
+  { return m_filename_for_id; }
+
   /* Unordered chain of all filetabs in the compunit,  with the exception
      that the "main" source file is the first entry in the list.  */
 
-  struct symtab *next;
+  struct symtab *next = nullptr;
+
+private:
 
   /* Name of this source file, in a form appropriate to print to the user.
 
-     This pointer is never nullptr.  */
+     This pointer is never nullptr and is set from the constructor.  */
 
-  const char *filename;
+  const char *m_filename;
 
   /* Filename for this source file, used as an identifier to link with
      related objects such as associated macro_source_file objects.  It must
@@ -1767,28 +1782,29 @@ struct symtab
      follow that rule, or another form of the same file name, this is up to
      the specific debug info reader.
 
-     This pointer is never nullptr.*/
-  const char *filename_for_id;
+     This pointer is never nullptr, and is set from the constructor.  */
+  const char *m_filename_for_id;
 
-private:
+  /* Backlink to containing compunit symtab.
 
-  /* Backlink to containing compunit symtab.  */
+     This pointer is never nullptr, and is set from the constructor.  */
 
   struct compunit_symtab *m_compunit;
 
   /* Table mapping core addresses to line numbers for this file.
      Can be NULL if none.  Never shared between different symtabs.  */
 
-  const struct linetable *m_linetable;
+  const struct linetable *m_linetable = nullptr;
 
-  /* Language of this source file.  */
+  /* Language of this source file.  This is set in the object
+     constructor.  */
 
   enum language m_language;
 
   /* Full name of file as found by searching the source path.
      NULL if not yet known.  */
 
-  char *m_fullname;
+  char *m_fullname = nullptr;
 };
 
 /* A range adapter to allowing iterating over all the file tables in a list.  */