]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit
gdb/dwarf: Use the function scope for DW_TAG_imported_declaration
authorAbdul Basit Ijaz <abdul.b.ijaz@intel.com>
Wed, 8 Apr 2026 08:03:57 +0000 (10:03 +0200)
committerAbdul Basit Ijaz <abdul.b.ijaz@intel.com>
Tue, 5 May 2026 15:02:25 +0000 (17:02 +0200)
commit90fe72f797828eabae2a876f7b4356ca7845123c
treee618c6940a77c72313ccb62ec87d3e84c52dc26b
parent297fe552edd546e4fe9f47c7a0765a55ce084f0c
gdb/dwarf: Use the function scope for DW_TAG_imported_declaration

All Fortran imported variable aliases (`use module, alias => var`)
were being added to "global scope", regardless of whether they appeared
in:
- Program/module scope (should be global)
- Function scope (should be local)

This caused conflicts when different functions had the same alias name
pointing to different variables.

DW_TAG_imported_declaration and DW_TAG_namespace cases are now handled
separately.  This patch modifies the case for DW_TAG_imported_
declaration in the function new_symbol () to use cu->list_in_scope
instead of global symbols for all languages.  This ensures that
function-scoped aliases use the current scope rather than being forced
into the global scope.

Bug Scenario:

  subroutine sub1
    use mod1, var_i_alias=>var_i  ! alias points to mod1::var_i
    var_i_alias = 3
    var_i = 4
  end subroutine

  subroutine sub2
    use mod2, var_i_alias=>var_i  ! alias points to mod2::var_i
    var_i_alias = 23
    var_i = 25
  end subroutine

Before: var_i_alias in sub2 incorrectly resolved to mod1::var_i
(value 25)
After: Each function's alias correctly resolves to its own imported
variable (value 23)

New test files verify the fix and include regression tests for global
program-scope imports:
  - gdb/testsuite/gdb.fortran/module_declarations.exp
  - gdb/testsuite/gdb.fortran/module_declarations.f90

Before the change:
(gdb) print var_i_alias
$4 = 25
FAIL: gdb.fortran/module_declarations.exp: sub2_test: print var_i_alias

After the change:
(gdb) print var_i_alias
$4 = 23
PASS: gdb.fortran/module_declarations.exp: sub2_test: print var_i_alias

Approved-By: Tom Tromey <tom@tromey.com>
gdb/dwarf2/read.c
gdb/testsuite/gdb.fortran/module_declarations.exp [new file with mode: 0644]
gdb/testsuite/gdb.fortran/module_declarations.f90 [new file with mode: 0644]