From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH] [gdb/symtab] Factor out new_symbol_file_line
Date: Tue, 21 Apr 2026 18:03:07 +0200 [thread overview]
Message-ID: <20260421160307.1655903-1-tdevries@suse.de> (raw)
I was looking at a patch [1] modifying the DW_AT_decl_file / DW_AT_call_file
handling of new_symbol, and noticed that:
- the code is fairly nested, and
- the patch adds another nesting layer.
Factor out function new_symbol_file_line out of new_symbol, handling both the
DW_AT_decl_file / DW_AT_call_file and DW_AT_call_line / DW_AT_decl_line
attributes.
Having factored out the code, simplify it using return, and modernize it using
bool and nullptr.
[1] https://sourceware.org/pipermail/gdb-patches/2026-March/226065.html
---
gdb/dwarf2/read.c | 87 +++++++++++++++++++++++++++--------------------
1 file changed, 51 insertions(+), 36 deletions(-)
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 375e8a75db4..d6695627bb2 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -15426,6 +15426,55 @@ is_ada_import_or_export (dwarf2_cu *cu, const char *name,
&& !streq (linkagename, "adainit"));
}
+/* Apply the DW_AT_{call,decl}_{file,line} attributes of DIE in CU to SYM. */
+
+static void
+new_symbol_file_line (struct die_info *die, struct dwarf2_cu *cu,
+ struct symbol *sym)
+{
+ struct attribute *attr = nullptr;
+
+ bool inlined_func = (die->tag == DW_TAG_inlined_subroutine);
+
+ /* Handle DW_AT_call_line / DW_AT_decl_line. */
+ attr = dwarf2_attr (die,
+ inlined_func ? DW_AT_call_line : DW_AT_decl_line,
+ cu);
+ if (attr != nullptr)
+ sym->set_line (attr->unsigned_constant ().value_or (0));
+
+ /* Handle DW_AT_call_file / DW_AT_decl_file. */
+ struct dwarf2_cu *file_cu = cu;
+ attr = dwarf2_attr (die,
+ inlined_func ? DW_AT_call_file : DW_AT_decl_file,
+ &file_cu);
+ if (attr == nullptr)
+ return;
+
+ std::optional<ULONGEST> index_cst = attr->unsigned_constant ();
+ if (!index_cst.has_value ())
+ return;
+
+ if (file_cu->line_header == nullptr)
+ {
+ file_and_directory fnd (nullptr, nullptr);
+ decode_line_header_for_cu (file_cu->dies, file_cu, fnd);
+ }
+
+ file_name_index file_index = (file_name_index) *index_cst;
+ struct file_entry *fe = nullptr;
+ if (file_cu->line_header != nullptr)
+ fe = file_cu->line_header->file_name_at (file_index);
+
+ if (fe == nullptr)
+ {
+ complaint (_("file index out of range"));
+ return;
+ }
+
+ sym->set_symtab (fe->symtab (*file_cu));
+}
+
/* Given a pointer to a DWARF information entry, figure out if we need
to make a symbol table entry for it, and if so, create a new entry
and return a pointer to it.
@@ -15446,8 +15495,6 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
struct attribute *attr2 = NULL;
std::vector<symbol *> *list_to_add = nullptr;
- int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
-
name = dwarf2_name (die, cu);
if (name == nullptr && (die->tag == DW_TAG_subprogram
|| die->tag == DW_TAG_inlined_subroutine
@@ -15500,41 +15547,9 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
sym->set_type (type);
else
sym->set_type (die_type (die, cu));
- attr = dwarf2_attr (die,
- inlined_func ? DW_AT_call_line : DW_AT_decl_line,
- cu);
- if (attr != nullptr)
- sym->set_line (attr->unsigned_constant ().value_or (0));
- struct dwarf2_cu *file_cu = cu;
- attr = dwarf2_attr (die,
- inlined_func ? DW_AT_call_file : DW_AT_decl_file,
- &file_cu);
- if (attr != nullptr)
- {
- std::optional<ULONGEST> index_cst = attr->unsigned_constant ();
- if (index_cst.has_value ())
- {
- file_name_index file_index = (file_name_index) *index_cst;
- struct file_entry *fe;
-
- if (file_cu->line_header == nullptr)
- {
- file_and_directory fnd (nullptr, nullptr);
- decode_line_header_for_cu (file_cu->dies, file_cu, fnd);
- }
-
- if (file_cu->line_header != nullptr)
- fe = file_cu->line_header->file_name_at (file_index);
- else
- fe = NULL;
-
- if (fe == NULL)
- complaint (_("file index out of range"));
- else
- sym->set_symtab (fe->symtab (*file_cu));
- }
- }
+ /* Handle DW_AT_{call,decl}_{file,line}. */
+ new_symbol_file_line (die, cu, sym);
switch (die->tag)
{
base-commit: c365a263f5dcf95982464cf6d53db00cc6f04c1c
--
2.51.0
next reply other threads:[~2026-04-21 16:05 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-21 16:03 Tom de Vries [this message]
2026-04-22 16:51 ` Tom Tromey
2026-04-23 13:01 ` Tom de Vries
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=20260421160307.1655903-1-tdevries@suse.de \
--to=tdevries@suse.de \
--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