From patchwork Fri Jul 14 18:32:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 21621 Received: (qmail 88108 invoked by alias); 14 Jul 2017 18:54:15 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 88071 invoked by uid 89); 14 Jul 2017 18:54:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=OPERATION, TOP X-HELO: gproxy5.mail.unifiedlayer.com Received: from gproxy5-pub.mail.unifiedlayer.com (HELO gproxy5.mail.unifiedlayer.com) (67.222.38.55) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 14 Jul 2017 18:54:13 +0000 Received: from CMOut01 (unknown [10.0.90.82]) by gproxy5.mail.unifiedlayer.com (Postfix) with ESMTP id D5A27141ED3 for ; Fri, 14 Jul 2017 12:32:44 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by CMOut01 with id kiYh1v00c2f2jeq01iYkTr; Fri, 14 Jul 2017 12:32:44 -0600 X-Authority-Analysis: v=2.2 cv=FvB1xyjq c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=G3gG6ho9WtcA:10 a=zstS-IiYAAAA:8 a=20KFwNOVAAAA:8 a=7FJaAbBp04u-TmVc-mQA:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 174-29-39-24.hlrn.qwest.net ([174.29.39.24]:56560 helo=pokyo.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dW5OL-002iWS-Nb; Fri, 14 Jul 2017 12:32:41 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [FYI] Handle sizeof(type) in Rust Date: Fri, 14 Jul 2017 12:32:37 -0600 Message-Id: <20170714183237.14186-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1dW5OL-002iWS-Nb X-Source-Sender: 174-29-39-24.hlrn.qwest.net (pokyo.Home) [174.29.39.24]:56560 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== PR rust/21764 notes that "sizeof" does not work correctly for all types in Rust. The bug turns out to be an error in the conversion of the AST to gdb expressions. This patch fixes the bug and also avoids generating incorrect expressions in another case. Tested on the buildbot. I'm checking this in. 2017-07-14 Tom Tromey PR rust/21764: * rust-exp.y (convert_ast_to_expression): Add "want_type" parameter. : Split into separate case. : Handle want_type. Add error case. 2017-07-14 Tom Tromey PR rust/21764: * gdb.rust/simple.exp: Add tests. --- gdb/ChangeLog | 8 ++++++++ gdb/rust-exp.y | 27 ++++++++++++++++++++------- gdb/testsuite/ChangeLog | 5 +++++ gdb/testsuite/gdb.rust/simple.exp | 3 +++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 513cb6b..1da495f 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,13 @@ 2017-07-14 Tom Tromey + PR rust/21764: + * rust-exp.y (convert_ast_to_expression): Add "want_type" + parameter. + : Split into separate case. + : Handle want_type. Add error case. + +2017-07-14 Tom Tromey + PR rust/21763: * symtab.c (symbol_matches_domain): Add language_rust to special case. diff --git a/gdb/rust-exp.y b/gdb/rust-exp.y index 821abcd..3ac78a3 100644 --- a/gdb/rust-exp.y +++ b/gdb/rust-exp.y @@ -2100,7 +2100,8 @@ convert_name (struct parser_state *state, const struct rust_op *operation) static void convert_ast_to_expression (struct parser_state *state, const struct rust_op *operation, - const struct rust_op *top); + const struct rust_op *top, + bool want_type = false); /* A helper function that converts a vec of rust_ops to a gdb expression. */ @@ -2121,12 +2122,16 @@ convert_params_to_expression (struct parser_state *state, OPERATION is the operation to lower. TOP is a pointer to the top-most operation; it is used to handle the special case where the top-most expression is an identifier and can be optionally lowered - to OP_TYPE. */ + to OP_TYPE. WANT_TYPE is a flag indicating that, if the expression + is the name of a type, then emit an OP_TYPE for it (rather than + erroring). If WANT_TYPE is set, then the similar TOP handling is + not done. */ static void convert_ast_to_expression (struct parser_state *state, const struct rust_op *operation, - const struct rust_op *top) + const struct rust_op *top, + bool want_type) { switch (operation->opcode) { @@ -2166,12 +2171,16 @@ convert_ast_to_expression (struct parser_state *state, } break; + case UNOP_SIZEOF: + convert_ast_to_expression (state, operation->left.op, top, true); + write_exp_elt_opcode (state, UNOP_SIZEOF); + break; + case UNOP_PLUS: case UNOP_NEG: case UNOP_COMPLEMENT: case UNOP_IND: case UNOP_ADDR: - case UNOP_SIZEOF: convert_ast_to_expression (state, operation->left.op, top); write_exp_elt_opcode (state, operation->opcode); break; @@ -2337,7 +2346,8 @@ convert_ast_to_expression (struct parser_state *state, if (type == NULL) error (_("No symbol '%s' in current context"), varname); - if (TYPE_CODE (type) == TYPE_CODE_STRUCT + if (!want_type + && TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 0) { /* A unit-like struct. */ @@ -2346,13 +2356,16 @@ convert_ast_to_expression (struct parser_state *state, write_exp_elt_longcst (state, 0); write_exp_elt_opcode (state, OP_AGGREGATE); } - else if (operation == top) + else if (want_type || operation == top) { write_exp_elt_opcode (state, OP_TYPE); write_exp_elt_type (state, type); write_exp_elt_opcode (state, OP_TYPE); - break; } + else + error (_("Found type '%s', which can't be " + "evaluated in this context"), + varname); } } break; diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index b715bfd..3fac8da 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,5 +1,10 @@ 2017-07-14 Tom Tromey + PR rust/21764: + * gdb.rust/simple.exp: Add tests. + +2017-07-14 Tom Tromey + * gdb.rust/simple.exp: Add regression test for PR rust/21763. 2017-07-14 Pedro Alves diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp index db23162..83dab17 100644 --- a/gdb/testsuite/gdb.rust/simple.exp +++ b/gdb/testsuite/gdb.rust/simple.exp @@ -135,6 +135,9 @@ gdb_test "print simple::ByeBob\{field1: 0xff, field2:5\}" \ gdb_test "print simple::HiBob(0xff, 5)" \ "Type simple::HiBob is not a tuple struct" +gdb_test "print sizeof(simple::HiBob)" " = \[0-9\]+" +gdb_test "print simple::HiBob + 5" \ + "Found type 'simple::HiBob', which can't be evaluated in this context" gdb_test "print nosuchsymbol" \ "No symbol 'nosuchsymbol' in current context"