static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
+static void var_decode_location (struct attribute *attr,
+ struct symbol *sym,
+ struct dwarf2_cu *cu);
+
static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
static const gdb_byte *read_attribute (const struct die_reader_specs *,
static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
-static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
+static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
static struct type *read_module_type (struct die_info *die,
struct dwarf2_cu *cu);
break;
case DW_TAG_imported_declaration:
cu->processing_has_namespace_info = true;
- if (read_namespace_alias (die, cu))
+ if (read_alias (die, cu))
break;
- /* The declaration is not a global namespace alias. */
+ /* The declaration is neither a global namespace nor a variable
+ alias. */
/* Fall through. */
case DW_TAG_imported_module:
cu->processing_has_namespace_info = true;
return retval;
}
-/* Inspect DIE in CU for a namespace alias. If one exists, record
- a new symbol for it.
+/* Inspect DIE in CU for a namespace alias or a variable with alias
+ attribute. If one exists, record a new symbol for it.
- Returns 1 if a namespace alias was recorded, 0 otherwise. */
+ Returns true if an alias was recorded, false otherwise. */
-static int
-read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
+static bool
+read_alias (struct die_info *die, struct dwarf2_cu *cu)
{
struct attribute *attr;
- /* If the die does not have a name, this is not a namespace
- alias. */
+ /* If the die does not have a name, this is neither a namespace
+ alias nor a variable alias. */
attr = dwarf2_attr (die, DW_AT_name, cu);
if (attr != NULL)
{
{
complaint (_("DIE at %s has too many recursively imported "
"declarations"), sect_offset_str (d->sect_off));
- return 0;
+ return false;
}
if (attr != NULL)
{
struct type *type;
- sect_offset sect_off = attr->get_ref_die_offset ();
-
- type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
- if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
+ if (d->tag == DW_TAG_variable)
{
- /* This declaration is a global namespace alias. Add
- a symbol for it whose type is the aliased namespace. */
- new_symbol (die, type, cu);
- return 1;
+ /* This declaration is a C/C++ global variable alias.
+ Add a symbol for it whose type is the same as the
+ aliased variable's. */
+ type = die_type (d, imported_cu);
+ struct symbol *sym = new_symbol (die, type, cu);
+ attr = dwarf2_attr (d, DW_AT_location, imported_cu);
+ sym->set_aclass_index (LOC_UNRESOLVED);
+ if (attr != nullptr)
+ var_decode_location (attr, sym, cu);
+ return true;
+ }
+ else
+ {
+ sect_offset sect_off = attr->get_ref_die_offset ();
+ type = get_die_type_at_offset (sect_off, cu->per_cu,
+ cu->per_objfile);
+ if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
+ {
+ /* This declaration is a global namespace alias. Add
+ a symbol for it whose type is the aliased
+ namespace. */
+ new_symbol (die, type, cu);
+ return true;
+ }
}
}
}
-
- return 0;
+ return false;
}
/* Return the using directives repository (global or local?) to use in the
# along with this program. If not, see <http://www.gnu.org/licenses/>.
standard_testfile symbol-alias.c symbol-alias2.c
+# Clang versions prior to v15 do not emit debug info for aliases.
+set old_clang [expr [test_compiler_info {clang-1[0-4]-*-*}] \
+ || [test_compiler_info {clang-[1-9]-*}]]
if { [prepare_for_testing "failed to prepare" ${testfile} [list $srcfile $srcfile2]] } {
return -1
gdb_test "p *${f}()" "= {field1 = 1, field2 = 2}"
}
-# Variables.
-foreach v {"g_var_s" "g_var_s_alias"} {
- gdb_test "p $v" "= {field1 = 1, field2 = 2}"
+# Extern global variable.
+gdb_test "p g_var_s" "= {field1 = 1, field2 = 2}"
+foreach v {"g_var_s_alias" "g_var_s_alias2"} {
+ gdb_test_multiple "p $v" "print alias variable $v" {
+ -re -wrap " = {field1 = 1, field2 = 2}" {
+ pass $gdb_test_name
+ }
+ -re -wrap "has unknown type; cast it to its declared type" {
+ if { $old_clang } {
+ xfail $gdb_test_name
+ } else {
+ fail $gdb_test_name
+ }
+ }
+ }
+}
+
+# Static global variable.
+gdb_test "p g_var" " = 1"
+gdb_test_multiple "p g_var_alias" "print alias of static variable" {
+ -re -wrap " = 1" {
+ pass $gdb_test_name
+ }
+ -re -wrap "has unknown type; cast it to its declared type" {
+ if { $old_clang } {
+ xfail $gdb_test_name
+ } else {
+ fail $gdb_test_name
+ }
+ }
+}
+
+# Alias of a variable that is declared later in the scope.
+gdb_test "p g_def_var" " = 2"
+gdb_test_multiple "p g_def_var_alias" "print g_def_var_alias" {
+ -re -wrap " = 2" {
+ pass $gdb_test_name
+ }
+ -re -wrap "has unknown type; cast it to its declared type" {
+ if { $old_clang } {
+ xfail $gdb_test_name
+ } else {
+ fail $gdb_test_name
+ }
+ }
+}
+gdb_test_multiple "p g_def_var_alias2" "print g_def_var_alias2" {
+ -re -wrap " = 2" {
+ pass $gdb_test_name
+ }
+ -re -wrap "has unknown type; cast it to its declared type" {
+ if { $old_clang } {
+ xfail $gdb_test_name
+ } else {
+ fail $gdb_test_name
+ }
+ }
}