From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 1699B385B835 for ; Thu, 9 Apr 2020 21:08:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1699B385B835 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tdevries@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 70CCFAF3F; Thu, 9 Apr 2020 21:08:06 +0000 (UTC) Date: Thu, 9 Apr 2020 23:08:05 +0200 From: Tom de Vries To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH][gdb/symtab] Prefer def over decl (inter-CU case, with context) Message-ID: <20200409210804.GA6512@delia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-31.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Apr 2020 21:08:09 -0000 Hi, 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. OK for trunk? Thanks, - Tom [gdb/symtab] Prefer def over decl (inter-CU case, with context) gdb/ChangeLog: 2020-04-09 Tom de Vries * symtab.c (lookup_global_symbol): Prefer def over decl. gdb/testsuite/ChangeLog: 2020-04-09 Tom de Vries * gdb.base/decl-before-def.exp: Run to main and print a again. --- gdb/symtab.c | 16 +++++++++++----- gdb/testsuite/gdb.base/decl-before-def.exp | 7 +++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/gdb/symtab.c b/gdb/symtab.c index e063af80fe..6ae69f2e25 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -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 diff --git a/gdb/testsuite/gdb.base/decl-before-def.exp b/gdb/testsuite/gdb.base/decl-before-def.exp index feb2084a82..0af3bdf3c7 100644 --- a/gdb/testsuite/gdb.base/decl-before-def.exp +++ b/gdb/testsuite/gdb.base/decl-before-def.exp @@ -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\}}