From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id V1RoEEa7PWkjvD4AWB0awg (envelope-from ) for ; Sat, 13 Dec 2025 14:15:18 -0500 Authentication-Results: simark.ca; dkim=fail reason="signature verification failed" (768-bit key; unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=OenuFqX8; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 2D2121E0B6; Sat, 13 Dec 2025 14:15:18 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: * X-Spam-Status: No, score=1.5 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_INVALID,DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=no autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 4F09F1E08D for ; Sat, 13 Dec 2025 14:15:17 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 666FD4BA2E23 for ; Sat, 13 Dec 2025 19:15:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 666FD4BA2E23 Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=OenuFqX8 Received: from omta40.uswest2.a.cloudfilter.net (omta40.uswest2.a.cloudfilter.net [35.89.44.39]) by sourceware.org (Postfix) with ESMTPS id EC3734BA2E04 for ; Sat, 13 Dec 2025 19:14:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EC3734BA2E04 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EC3734BA2E04 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=35.89.44.39 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1765653287; cv=none; b=m2kgB3bW98AkzUye/uVBlzem4eoT+fjwZgojfOLMSXAQcS9wvWbPEqoYbdZbUG7TG+I3mDdaTypi3z4otKsqDTnjWIyxTWhK45RQZKLnT4+s6ZY2C8JAe/FcDjwpx3ls3SzQcZRtBaAxdoWm49oaL+yfneHrcPTRAmEKT0/NaHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1765653287; c=relaxed/simple; bh=WFcRtvPject971X3zQ5vtlroUHLm0y9q7fjvGo7VTvo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Wd9rqtFflCb//pzc3uEdbynHzYcVYBiA9eucaAq0W3YuFpYEZQhPdhr4UdDaFU44xD38uksNYfUxBwykpmMgz0otFY+qXDualzb2CF7GwCJpmi95TPDs1c7dtR2R48Hb5+uhQkfy5Y1aTp0WT6H0FaRP/JguWEk6EL20PngbvnI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EC3734BA2E04 Received: from eig-obgw-6007b.ext.cloudfilter.net ([10.0.30.166]) by cmsmtp with ESMTPS id URSSveodBaPqLUV4nvaPze; Sat, 13 Dec 2025 19:14:45 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id UV4mvhNy3h8QWUV4nvZZPF; Sat, 13 Dec 2025 19:14:45 +0000 X-Authority-Analysis: v=2.4 cv=Mcdsu4/f c=1 sm=1 tr=0 ts=693dbb25 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=wP3pNCr1ah4A:10 a=ItBw4LHWJt0A:10 a=CCpqsmhAAAAA:8 a=zstS-IiYAAAA:8 a=zARbJcULIACosTIY-5EA:9 a=ul9cdbp4aOFLsgKbc677:22 a=4G6NA9xxw8l3yy4pmD5M:22 a=VS4QxaUSPT0UtbFnIvSC:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=rpd28ve+D4S5GQ0xRsVoy32Y+16wrXG7AeWcUOt42KI=; b=OenuFqX8x3TuT7ODtiqX9LBaRm w2qk83z2LyVguEnSNaPF1oAM9L1XCDJ58VSsNx4X9rhHi5O0tFj6ZSOV2nR5h2Tx4SaAVwvZW/HXH Hm4Xt+kNHgFhRqmHA5J3xigsS; Received: from 97-122-120-205.hlrn.qwest.net ([97.122.120.205]:48316 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vUV4l-00000000lUu-2M2T; Sat, 13 Dec 2025 12:14:43 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2] Remove best_symbol and better_symbol Date: Sat, 13 Dec 2025 12:14:28 -0700 Message-ID: <20251213191428.1928246-1-tom@tromey.com> X-Mailer: git-send-email 2.49.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.120.205 X-Source-L: No X-Exim-ID: 1vUV4l-00000000lUu-2M2T X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-120-205.hlrn.qwest.net (localhost.localdomain) [97.122.120.205]:48316 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfJFZ1LyNtbkcLYBe1WZywG/DY+y8jgOoybz4nOTxqUSfF7g5jVlwQDKoi6N/TahI6EQZDrgQ5tHaG1cLrgrZOHjwFLeC6xPD43NSD8DdFrQHlGsknav2 Wmjro8UYmxBMvlx9zTgVydCnbYHTF8Ha++s5kt+vILLed830PLO4CGsOG5LZuX7Mdh2HwppZFg3c3TMhYOhG1dt1hTXYap/deKM= X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org This patch removes best_symbol and better_symbol. Before some c-exp.y changes this year, best_symbol was the source of excess CU expansion, because searches that include SEARCH_VAR_DOMAIN will keep going until a symbol is found in that domain, or until all matches have been exhausted. So, for instance a "SEARCH_VFT" search for "int" will, most likely, expand all CUs -- even though the argument implies that a type would be suitable. Nowadays, best_symbol is more of an aesthetic offense and abstraction violation, encoding a workaround for a C++ expression lookup bug into a low-level lookup function. This patch changes the the C parser to simply do two lookups, prioritizing SEARCH_VAR_DOMAIN. This way, it gets the result it is presumably looking for, without causing excessive expansion (if that's currently possible) and without pushing its requirements into generic code. Note that the special case for "stub" symbols remains. This one is less problematic, at least for the time being, as such symbols aren't common. (Kevin does have a patch pending in this area but I don't believe this change should affect it, as the stub symbol case remains unchanged.) New in v2: addressed review comments and reworded the commit text. v1 is here: https://inbox.sourceware.org/gdb-patches/87345e71b1.fsf@tromey.com/T/#meb7622f6d4d5bb0c1cd6170d875b37a0ceb9a1ed Regression tested on x86-64 Fedora 40. --- gdb/block.c | 62 +++++++--------------------------------------------- gdb/block.h | 14 ++---------- gdb/c-exp.y | 8 ++++++- gdb/symtab.c | 14 ++++++++---- gdb/symtab.h | 7 ++++++ 5 files changed, 34 insertions(+), 71 deletions(-) diff --git a/gdb/block.c b/gdb/block.c index 3d2c51cc554..bcc35cc8b9e 100644 --- a/gdb/block.c +++ b/gdb/block.c @@ -578,46 +578,6 @@ block_iterator_next (struct block_iterator *iterator) return block_iter_match_step (iterator, 0); } -/* See block.h. */ - -bool -best_symbol (struct symbol *a, const domain_search_flags domain) -{ - if (a->loc_class () == LOC_UNRESOLVED) - return false; - - if ((domain & SEARCH_VAR_DOMAIN) != 0) - return a->domain () == VAR_DOMAIN; - - return a->matches (domain); -} - -/* See block.h. */ - -struct symbol * -better_symbol (struct symbol *a, struct symbol *b, - const domain_search_flags domain) -{ - if (a == NULL) - return b; - if (b == NULL) - return a; - - if (a->matches (domain) && !b->matches (domain)) - return a; - - if (b->matches (domain) && !a->matches (domain)) - return b; - - if (a->loc_class () != LOC_UNRESOLVED && b->loc_class () == LOC_UNRESOLVED) - return a; - - if (b->loc_class () != LOC_UNRESOLVED && a->loc_class () == LOC_UNRESOLVED) - return b; - - return a; -} - /* See block.h. Note that if NAME is the demangled form of a C++ symbol, we will fail @@ -677,6 +637,9 @@ best_symbol_tracker::search (compunit_symtab *symtab, { for (symbol *sym : block_iterator_range (block, &name)) { + if (!sym->matches (domain)) + continue; + /* With the fix for PR gcc/debug/91507, we get for: ... extern char *zzz[]; @@ -698,31 +661,22 @@ best_symbol_tracker::search (compunit_symtab *symtab, doesn't work either because the type of the decl does not specify a size. - To fix this, we prefer def over decl in best_symbol and - better_symbol. + To fix this, we prefer definitions over declarations. In absence of the gcc fix, both def and decl have type char *[], so the only option to make this work is improve the fallback to use the size of the minimal symbol. Filed as PR exp/24989. */ - if (best_symbol (sym, domain)) + if (sym->is_definition ()) { best_symtab = symtab; currently_best = { sym, block }; return true; } - /* This is a bit of a hack, but 'matches' might ignore - STRUCT vs VAR domain symbols. So if a matching symbol is found, - make sure there is no "better" matching symbol, i.e., one with - exactly the same domain. PR 16253. */ - if (sym->matches (domain)) + if (currently_best.symbol == nullptr) { - symbol *better = better_symbol (sym, currently_best.symbol, domain); - if (better != currently_best.symbol) - { - best_symtab = symtab; - currently_best = { better, block }; - } + best_symtab = symtab; + currently_best = { sym, block }; } } diff --git a/gdb/block.h b/gdb/block.h index 65e2e20877e..a9012c7207f 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -656,16 +656,6 @@ block_iterator_range (const block *block, return iterator_range (std::move (begin)); } -/* Return true if symbol A is the best match possible for DOMAIN. */ - -extern bool best_symbol (struct symbol *a, const domain_search_flags domain); - -/* Return symbol B if it is a better match than symbol A for DOMAIN. - Otherwise return A. */ - -extern struct symbol *better_symbol (struct symbol *a, struct symbol *b, - const domain_search_flags domain); - /* Search BLOCK for symbol NAME in DOMAIN. */ extern struct symbol *block_lookup_symbol (const struct block *block, @@ -673,8 +663,8 @@ extern struct symbol *block_lookup_symbol (const struct block *block, const domain_search_flags domain); /* When searching for a symbol, the "best" symbol is preferred over - one that is merely acceptable. See 'best_symbol'. This class - keeps track of this distinction while searching. */ + one that is merely acceptable. This class keeps track of this + distinction while searching. */ struct best_symbol_tracker { diff --git a/gdb/c-exp.y b/gdb/c-exp.y index d321e815ca4..bb0ec3cbe52 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -3133,8 +3133,14 @@ classify_name (struct parser_state *par_state, const struct block *block, std::string copy = copy_name (yylval.sval); - bsym = lookup_symbol (copy.c_str (), block, SEARCH_VFT, + /* Prefer variables over functions or types. This preserves some + historical parser behavior. */ + bsym = lookup_symbol (copy.c_str (), block, SEARCH_VAR_DOMAIN, &is_a_field_of_this); + if (bsym.symbol == nullptr) + bsym = lookup_symbol (copy.c_str (), block, + SEARCH_FUNCTION_DOMAIN | SEARCH_TYPE_DOMAIN, + &is_a_field_of_this); if (bsym.symbol && bsym.symbol->loc_class () == LOC_BLOCK) { diff --git a/gdb/symtab.c b/gdb/symtab.c index 5754d944b6c..47412516d11 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -2589,7 +2589,7 @@ lookup_global_symbol (const char *name, sym = lookup_symbol_in_block (name, symbol_name_match_type::FULL, global_block, domain); - if (sym != NULL && best_symbol (sym, domain)) + if (sym != nullptr && sym->is_definition ()) return { sym, global_block }; } @@ -2603,10 +2603,16 @@ lookup_global_symbol (const char *name, 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 + /* Prefer a definition over a declaration. (At this point, SYM is + either nullptr or a declaration.) */ + if (bs.symbol != nullptr && bs.symbol->is_definition ()) + return bs; + /* Prefer a declaration if that's all we have. */ + if (bs.symbol != nullptr && sym == nullptr) return bs; + /* Return either an empty result or the declaration we found in the + "local" global block. */ + return { sym, global_block }; } /* See symtab.h. */ diff --git a/gdb/symtab.h b/gdb/symtab.h index 9336f5d35a7..ab1a3d15950 100644 --- a/gdb/symtab.h +++ b/gdb/symtab.h @@ -1292,6 +1292,13 @@ struct symbol : public general_symbol_info, public allocate_on_obstack return this->impl ().loc_class; } + /* Return true if the symbol is not LOC_UNRESOLVED -- that is, if it + is a definition and not just a declaration. */ + bool is_definition () const + { + return loc_class () != LOC_UNRESOLVED; + } + /* Return true if this symbol's domain matches FLAGS. */ bool matches (domain_search_flags flags) const; -- 2.49.0