From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 114190 invoked by alias); 29 Oct 2016 13:10:41 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 114164 invoked by uid 89); 29 Oct 2016 13:10:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=no version=3.3.2 spammy=bent, longest X-HELO: mail-wm0-f44.google.com Received: from mail-wm0-f44.google.com (HELO mail-wm0-f44.google.com) (74.125.82.44) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 29 Oct 2016 13:10:34 +0000 Received: by mail-wm0-f44.google.com with SMTP id e69so155225739wmg.0 for ; Sat, 29 Oct 2016 06:10:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=7VIzeH2bgF319IA8bz41DCSA8WywcKxy6tB1WypBC3w=; b=LaXuQ1Ft3a1MWBv2tq8DOAw7GtS+qIpZokN3bzwpSz9xMp4i0Qm1hQN8sruR3obnSn 5nYVL4ZKbhX4kwngn3h3dlBD0zJ+3x66A3rriRTugsKQIGYbHq2S6M+0g2CaTycu4c2c uTQTcvJ/oGFPnQ22TBWakqK7mxCu7eGZNHXIH6XL3QH9UVFfy6WrqCgga3PtowBPLUuV 1qvYbNJxPpd8owlRSx1z2h5Yz+8JIfniCTwNjUmdcKUo3a50sCkpOgKLg7Zm4Gg+lzBn o0+9dre1tgbhunKqtKvyAXmp11r0nbJui/fNB3N++aG8xZmKlQxOhpbTtj150n4Nm2zV ogJw== X-Gm-Message-State: ABUngvfgCdIEX2ITe7OI4NBAEM+DiwdlbE+E9JLKbdWwtPgthMMk7IvrcE/wP9v1hdarVEahewjaExATInXhj3Ae X-Received: by 10.194.157.169 with SMTP id wn9mr15127097wjb.195.1477746632102; Sat, 29 Oct 2016 06:10:32 -0700 (PDT) MIME-Version: 1.0 Received: by 10.28.131.21 with HTTP; Sat, 29 Oct 2016 06:10:11 -0700 (PDT) From: Manish Goregaokar Date: Sat, 29 Oct 2016 13:10:00 -0000 Message-ID: Subject: [PATCH] Add support for the sizeof function in Rust To: gdb-patches@sourceware.org Cc: Tom Tromey Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2016-10/txt/msg00833.txt.bz2 `sizeof` isn't currently a thing in Rust (there is `mem::size_of()`, but it's an intrinsic and can't be called via debugging even if it were used by the program). `sizeof` is a keyword so Rust functions by that name cannot exist. This parses `sizeof(thing)` as a `UNOP_SIZEOF`, which automatically is handled by the C expression handler. We already support indexing of pointers the C way to make it easier to debug vectors, even though Rust doesn't have an Index implementation on pointers. This is in the same spirit; sizeof is needed whilst debugging and we should avoid making the user `set language c` in this process. The expression could be `sizeof foo` instead of `sizeof(foo)`. I feel the latter is better because it's closer to how `mem::size_of` works. Not bent on keeping it that way, the former approach has the benefit of not introducing ambiguities in the syntax and not removing the ability to call non-rust functions named "sizeof". From: Manish Goregaokar Date: Sat, 29 Oct 2016 05:55:58 -0700 Subject: [PATCH] Add support for the sizeof function in Rust 2016-10-29 Manish Goregaokar gdb/ChangeLog: * rust-exp.y: Parse `sizeof(exp)` as `UNOP_SIZEOF` gdb/testsuite/ChangeLog: * gdb.rust/simple.exp: Add tests for `sizeof(expr)` --- gdb/rust-exp.y | 8 ++++++-- gdb/testsuite/gdb.rust/simple.exp | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/gdb/rust-exp.y b/gdb/rust-exp.y index 6dc4704..dffccd0 100644 --- a/gdb/rust-exp.y +++ b/gdb/rust-exp.y @@ -276,6 +276,7 @@ struct rust_op %token KW_EXTERN %token KW_CONST %token KW_FN +%token KW_SIZEOF /* Operator tokens. */ %token DOTDOT @@ -371,7 +372,7 @@ expr: | array_expr | idx_expr | range_expr -| unop_expr +| unop_expr /* Must precede call_expr because of ambiguity with sizeof. */ | binop_expr | paren_expr | call_expr @@ -577,7 +578,8 @@ unop_expr: | '&' KW_MUT expr %prec UNARY { $$ = ast_unary (UNOP_ADDR, $3); } - +| KW_SIZEOF '(' expr ')' %prec UNARY + { $$ = ast_unary (UNOP_SIZEOF, $3); } ; binop_expr: @@ -872,6 +874,7 @@ static const struct token_info identifier_tokens[] = { "true", KW_TRUE, OP_NULL }, { "extern", KW_EXTERN, OP_NULL }, { "fn", KW_FN, OP_NULL }, + { "sizeof", KW_SIZEOF, OP_NULL }, }; /* Operator tokens, sorted longest first. */ @@ -2194,6 +2197,7 @@ convert_ast_to_expression (struct parser_state *state, 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; diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp index 075bff7..9ab4b70 100644 --- a/gdb/testsuite/gdb.rust/simple.exp +++ b/gdb/testsuite/gdb.rust/simple.exp @@ -33,6 +33,7 @@ if {![runto ${srcfile}:$line]} { gdb_test "print a" " = \\(\\)" gdb_test "ptype a" " = \\(\\)" +gdb_test "print sizeof(a)" " = 0" gdb_test "print b" " = \\\[\\\]" gdb_test "ptype b" " = \\\[i32; 0\\\]" @@ -41,6 +42,7 @@ gdb_test "print *(&b as *const \[i32; 0_0\])" " = \\\[\\\]" gdb_test "print c" " = 99" gdb_test "ptype c" " = i32" +gdb_test "print sizeof(c)" " = 4" gdb_test "print c = 87" " = \\(\\)" gdb_test "print c" " = 87" @@ -124,7 +126,7 @@ gdb_test "print nosuchsymbol" \ gdb_test "print e" " = simple::MoreComplicated::Two\\(73\\)" gdb_test "print e2" \ " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}" - +gdb_test "print sizeof(e)" " = 24" gdb_test_sequence "ptype e" "" { " = enum simple::MoreComplicated \\{" " One," -- 2.10.1