From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway33.websitewelcome.com (gateway33.websitewelcome.com [192.185.146.210]) by sourceware.org (Postfix) with ESMTPS id 0C0333857C66 for ; Fri, 29 Jan 2021 03:20:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0C0333857C66 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=tom@tromey.com Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway33.websitewelcome.com (Postfix) with ESMTP id 94F6A1E69C for ; Thu, 28 Jan 2021 21:20:41 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 5KKvlrW9hDT645KKvllrpy; Thu, 28 Jan 2021 21:20:41 -0600 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date: References:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=mclFMsZ6TtvjD+ubV5avVopvgd0coBWECIzkTH8SBrY=; b=Wvd8VqGCgl9/hcXfvAjajdMAMx bSTv+iuG3C0m/rZ8HmIhppPm4qA1TGhLqB970a297/TD2LhJ4EpUJMgzIDD2fdwhYYwb5JTYqSYVn deNvjkc26MYaLcbT3xKCH0k7p; Received: from 97-122-91-54.hlrn.qwest.net ([97.122.91.54]:48588 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1l5KKv-001okn-8X; Thu, 28 Jan 2021 20:20:41 -0700 From: Tom Tromey To: Simon Marchi Cc: Tom Tromey , gdb-patches@sourceware.org Subject: Re: [PATCH] Extract symbol-writing function from parsers References: <20201213214406.15465-1-tom@tromey.com> X-Attribution: Tom Date: Thu, 28 Jan 2021 20:20:40 -0700 In-Reply-To: (Simon Marchi's message of "Mon, 14 Dec 2020 12:47:54 -0500") Message-ID: <87r1m45szb.fsf@tromey.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain 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.91.54 X-Source-L: No X-Exim-ID: 1l5KKv-001okn-8X X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-91-54.hlrn.qwest.net (localhost.localdomain) [97.122.91.54]:48588 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-1189.9 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_ABUSEAT, RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, TXREP, T_SPF_TEMPERROR 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: Fri, 29 Jan 2021 03:20:44 -0000 Here's an updated patch. Tom commit e3962882d01140283d0d241a0e0945154b9f595c Author: Tom Tromey Date: Thu Jan 28 20:19:27 2021 -0700 Extract symbol-writing function from parsers I noticed that several parsers shared the same code to write a symbol reference to an expression. This patch factors this code out into a new function. Regression tested on x86-64 Fedora 32. gdb/ChangeLog 2021-01-28 Tom Tromey * parser-defs.h (write_exp_symbol_reference): Declare. * parse.c (write_exp_symbol_reference): New function. * p-exp.y (variable): Use write_exp_symbol_reference. * m2-exp.y (variable): Use write_exp_symbol_reference. * f-exp.y (variable): Use write_exp_symbol_reference. * d-exp.y (PrimaryExpression): Use write_exp_symbol_reference. * c-exp.y (variable): Use write_exp_symbol_reference. diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 1de9d30f051..7d356a63adb 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,13 @@ +2021-01-28 Tom Tromey + + * parser-defs.h (write_exp_symbol_reference): Declare. + * parse.c (write_exp_symbol_reference): New function. + * p-exp.y (variable): Use write_exp_symbol_reference. + * m2-exp.y (variable): Use write_exp_symbol_reference. + * f-exp.y (variable): Use write_exp_symbol_reference. + * d-exp.y (PrimaryExpression): Use write_exp_symbol_reference. + * c-exp.y (variable): Use write_exp_symbol_reference. + 2021-01-28 Andrew Burgess * thread.c (thr_try_catch_cmd): Replace swith_to_thread with an diff --git a/gdb/c-exp.y b/gdb/c-exp.y index 71610e90968..13b06f39bbf 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -1104,30 +1104,12 @@ variable: qualified_name | COLONCOLON name_not_typename { std::string name = copy_name ($2.stoken); - struct symbol *sym; - struct bound_minimal_symbol msymbol; - - sym + struct block_symbol sym = lookup_symbol (name.c_str (), (const struct block *) NULL, - VAR_DOMAIN, NULL).symbol; - if (sym) - { - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - write_exp_elt_block (pstate, NULL); - write_exp_elt_sym (pstate, sym); - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - break; - } - - msymbol = lookup_bound_minimal_symbol (name.c_str ()); - if (msymbol.minsym != NULL) - write_exp_msymbol (pstate, msymbol); - else if (!have_full_symbols () && !have_partial_symbols ()) - error (_("No symbol table is loaded. Use the \"file\" command.")); - else - error (_("No symbol \"%s\" in current context."), - name.c_str ()); + VAR_DOMAIN, NULL); + write_exp_symbol_reference (pstate, name.c_str (), + sym); } ; diff --git a/gdb/d-exp.y b/gdb/d-exp.y index 35cd07413d5..c432f22bd9d 100644 --- a/gdb/d-exp.y +++ b/gdb/d-exp.y @@ -464,7 +464,6 @@ PrimaryExpression: been resolved, it's not likely to be found. */ if (type->code () == TYPE_CODE_MODULE) { - struct bound_minimal_symbol msymbol; struct block_symbol sym; const char *type_name = TYPE_SAFE_NAME (type); int type_name_len = strlen (type_name); @@ -477,35 +476,23 @@ PrimaryExpression: lookup_symbol (name.c_str (), (const struct block *) NULL, VAR_DOMAIN, NULL); - if (sym.symbol) - { - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - write_exp_elt_block (pstate, sym.block); - write_exp_elt_sym (pstate, sym.symbol); - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - break; - } - - msymbol = lookup_bound_minimal_symbol (name.c_str ()); - if (msymbol.minsym != NULL) - write_exp_msymbol (pstate, msymbol); - else if (!have_full_symbols () && !have_partial_symbols ()) - error (_("No symbol table is loaded. Use the \"file\" command.")); - else - error (_("No symbol \"%s\" in current context."), - name.c_str ()); + write_exp_symbol_reference (pstate, + name.c_str (), + sym); + } + else + { + /* Check if the qualified name resolves as a member + of an aggregate or an enum type. */ + if (!type_aggregate_p (type)) + error (_("`%s' is not defined as an aggregate type."), + TYPE_SAFE_NAME (type)); + + write_exp_elt_opcode (pstate, OP_SCOPE); + write_exp_elt_type (pstate, type); + write_exp_string (pstate, $3); + write_exp_elt_opcode (pstate, OP_SCOPE); } - - /* Check if the qualified name resolves as a member - of an aggregate or an enum type. */ - if (!type_aggregate_p (type)) - error (_("`%s' is not defined as an aggregate type."), - TYPE_SAFE_NAME (type)); - - write_exp_elt_opcode (pstate, OP_SCOPE); - write_exp_elt_type (pstate, type); - write_exp_string (pstate, $3); - write_exp_elt_opcode (pstate, OP_SCOPE); } | DOLLAR_VARIABLE { write_dollar_variable (pstate, $1); } diff --git a/gdb/f-exp.y b/gdb/f-exp.y index 3b0f23d5d73..92a70b4552d 100644 --- a/gdb/f-exp.y +++ b/gdb/f-exp.y @@ -540,32 +540,9 @@ exp : STRING_LITERAL variable: name_not_typename { struct block_symbol sym = $1.sym; - - if (sym.symbol) - { - if (symbol_read_needs_frame (sym.symbol)) - pstate->block_tracker->update (sym); - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - write_exp_elt_block (pstate, sym.block); - write_exp_elt_sym (pstate, sym.symbol); - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - break; - } - else - { - struct bound_minimal_symbol msymbol; - std::string arg = copy_name ($1.stoken); - - msymbol = - lookup_bound_minimal_symbol (arg.c_str ()); - if (msymbol.minsym != NULL) - write_exp_msymbol (pstate, msymbol); - else if (!have_full_symbols () && !have_partial_symbols ()) - error (_("No symbol table is loaded. Use the \"file\" command.")); - else - error (_("No symbol \"%s\" in current context."), - arg.c_str ()); - } + std::string name = copy_name ($1.stoken); + write_exp_symbol_reference (pstate, name.c_str (), + sym); } ; diff --git a/gdb/m2-exp.y b/gdb/m2-exp.y index 717c6e69e95..68bae48feb6 100644 --- a/gdb/m2-exp.y +++ b/gdb/m2-exp.y @@ -561,37 +561,15 @@ variable: NAME { struct block_symbol sym; struct field_of_this_result is_a_field_of_this; + std::string name = copy_name ($1); sym - = lookup_symbol (copy_name ($1).c_str (), + = lookup_symbol (name.c_str (), pstate->expression_context_block, VAR_DOMAIN, &is_a_field_of_this); - if (sym.symbol) - { - if (symbol_read_needs_frame (sym.symbol)) - pstate->block_tracker->update (sym); - - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - write_exp_elt_block (pstate, sym.block); - write_exp_elt_sym (pstate, sym.symbol); - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - } - else - { - struct bound_minimal_symbol msymbol; - std::string arg = copy_name ($1); - - msymbol = - lookup_bound_minimal_symbol (arg.c_str ()); - if (msymbol.minsym != NULL) - write_exp_msymbol (pstate, msymbol); - else if (!have_full_symbols () && !have_partial_symbols ()) - error (_("No symbol table is loaded. Use the \"symbol-file\" command.")); - else - error (_("No symbol \"%s\" in current context."), - arg.c_str ()); - } + write_exp_symbol_reference (pstate, name.c_str (), + sym); } ; diff --git a/gdb/p-exp.y b/gdb/p-exp.y index 5a43e890524..b025ac36070 100644 --- a/gdb/p-exp.y +++ b/gdb/p-exp.y @@ -691,33 +691,12 @@ variable: qualified_name | COLONCOLON name { std::string name = copy_name ($2); - struct symbol *sym; - struct bound_minimal_symbol msymbol; - - sym = - lookup_symbol (name.c_str (), - (const struct block *) NULL, - VAR_DOMAIN, NULL).symbol; - if (sym) - { - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - write_exp_elt_block (pstate, NULL); - write_exp_elt_sym (pstate, sym); - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - break; - } - msymbol - = lookup_bound_minimal_symbol (name.c_str ()); - if (msymbol.minsym != NULL) - write_exp_msymbol (pstate, msymbol); - else if (!have_full_symbols () - && !have_partial_symbols ()) - error (_("No symbol table is loaded. " - "Use the \"file\" command.")); - else - error (_("No symbol \"%s\" in current context."), - name.c_str ()); + struct block_symbol sym + = lookup_symbol (name.c_str (), nullptr, + VAR_DOMAIN, nullptr); + write_exp_symbol_reference (pstate, name.c_str (), + sym); } ; diff --git a/gdb/parse.c b/gdb/parse.c index 933960fa30c..08fde89d8f3 100644 --- a/gdb/parse.c +++ b/gdb/parse.c @@ -650,6 +650,32 @@ write_dollar_variable (struct parser_state *ps, struct stoken str) return; } +/* See parser-defs.h. */ + +void +write_exp_symbol_reference (struct parser_state *pstate, const char *name, + struct block_symbol sym) +{ + if (sym.symbol != nullptr) + { + if (symbol_read_needs_frame (sym.symbol)) + pstate->block_tracker->update (sym); + write_exp_elt_opcode (pstate, OP_VAR_VALUE); + write_exp_elt_block (pstate, NULL); + write_exp_elt_sym (pstate, sym.symbol); + write_exp_elt_opcode (pstate, OP_VAR_VALUE); + } + else + { + struct bound_minimal_symbol msymbol = lookup_bound_minimal_symbol (name); + if (msymbol.minsym != NULL) + write_exp_msymbol (pstate, msymbol); + else if (!have_full_symbols () && !have_partial_symbols ()) + error (_("No symbol table is loaded. Use the \"file\" command.")); + else + error (_("No symbol \"%s\" in current context."), name); + } +} const char * find_template_name_end (const char *p) diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h index 5ff5d522405..466230e4ad0 100644 --- a/gdb/parser-defs.h +++ b/gdb/parser-defs.h @@ -334,6 +334,16 @@ extern void write_exp_msymbol (struct expr_builder *, extern void write_dollar_variable (struct parser_state *, struct stoken str); +/* Write a reference to a symbol to the expression being built in PS. + NAME is the name of the symbol to write; SYM is the symbol. If SYM + is nullptr (meaning the 'symbol' member), a minimal symbol will be + searched for and used if available. Throws an exception if SYM is + nullptr and no minimal symbol can be found. */ + +extern void write_exp_symbol_reference (struct parser_state *ps, + const char *name, + struct block_symbol sym); + extern const char *find_template_name_end (const char *); extern std::string copy_name (struct stoken);