[gdb/symtab] Prefer def over decl (inter-CU case, with context)
authorTom de Vries <tdevries@suse.de>
Thu, 23 Apr 2020 13:42:47 +0000 (15:42 +0200)
committerTom de Vries <tdevries@suse.de>
Thu, 23 Apr 2020 13:42:47 +0000 (15:42 +0200)
This is a follow-up patch on "[PATCH][gdb/symtab] Prefer def over decl
(inter-CU case)" (
https://sourceware.org/pipermail/gdb-patches/2020-April/167489.html ).

Consider the test-case from that patch.  It contains a decl and def of var a
in different CUs, and tests whether var a can be printed using the def, even
if the decl is found first.

However, the test-case does this in a contextless environment, so if we add to
the test-case like this to set the context to the CU containing main:
...
 gdb_test "p a" { = \{1, 2\}}
+
+if ![runto_main] then {
+    fail "can't run to main"
+    return 0
+}
+
+gdb_test "p a" { = \{1, 2\}}
...
then the second test fails, because the decl is found in the context.

Fix this by preferring defs over decls in lookup_global_symbol.

Build and reg-tested on x86_64-linux.

gdb/ChangeLog:

2020-04-23  Tom de Vries  <tdevries@suse.de>

* symtab.c (lookup_global_symbol): Prefer def over decl.

gdb/testsuite/ChangeLog:

2020-04-23  Tom de Vries  <tdevries@suse.de>

* gdb.base/decl-before-def.exp: Run to main and print a again.

gdb/ChangeLog
gdb/symtab.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/decl-before-def.exp

index f1ebf09ff468067947f29585198f85d6590bd32e..109456689aa22f259c571b37840df1293a4e7644 100644 (file)
@@ -1,3 +1,7 @@
+2020-04-23  Tom de Vries  <tdevries@suse.de>
+
+       * symtab.c (lookup_global_symbol): Prefer def over decl.
+
 2020-04-23  Tom de Vries  <tdevries@suse.de>
 
        PR symtab/25807
index 1eef97837e8b0f71a47e7d9c4a2dc9bcea0745ad..4e9f91056b8889383b38aca8807fccc4e6ed6706 100644 (file)
@@ -2718,17 +2718,23 @@ lookup_global_symbol (const char *name,
      global block first.  This yields "more expected" behavior, and is
      needed to support 'FILENAME'::VARIABLE lookups.  */
   const struct block *global_block = block_global_block (block);
+  symbol *sym = NULL;
   if (global_block != nullptr)
     {
-      symbol *sym = lookup_symbol_in_block (name,
-                                           symbol_name_match_type::FULL,
-                                           global_block, domain);
-      if (sym != nullptr)
+      sym = lookup_symbol_in_block (name,
+                                   symbol_name_match_type::FULL,
+                                   global_block, domain);
+      if (sym != NULL && best_symbol (sym, domain))
        return { sym, global_block };
     }
 
   struct objfile *objfile = lookup_objfile_from_block (block);
-  return lookup_global_or_static_symbol (name, GLOBAL_BLOCK, objfile, domain);
+  block_symbol bs
+    = lookup_global_or_static_symbol (name, GLOBAL_BLOCK, objfile, domain);
+  if (better_symbol (sym, bs.symbol, domain) == sym)
+    return { sym, global_block };
+  else
+    return bs;
 }
 
 bool
index e37aca2b5af2e92bf3a2293aa50b7e1aa0aecac5..10683db566d6eaf73b022955ff9bf6df04dc953d 100644 (file)
@@ -1,3 +1,7 @@
+2020-04-23  Tom de Vries  <tdevries@suse.de>
+
+       * gdb.base/decl-before-def.exp: Run to main and print a again.
+
 2020-04-23  Tom de Vries  <tdevries@suse.de>
 
        * gdb.base/decl-before-def-decl.c: New test.
index feb2084a82f293cd11301b8c483d174e226f9d81..0af3bdf3c75c93b69ea812d41178043141c857d3 100644 (file)
@@ -24,3 +24,10 @@ if {[prepare_for_testing "failed to prepare" $testfile $sources]} {
 gdb_test "maint expand-symtabs"
 
 gdb_test "p a" { = \{1, 2\}}
+
+if ![runto_main] then {
+    fail "can't run to main"
+    return 0
+}
+
+gdb_test "p a" { = \{1, 2\}}
This page took 0.040405 seconds and 4 git commands to generate.