Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Manish Goregaokar <manish@mozilla.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] Add support for the sizeof function in Rust
Date: Sat, 29 Oct 2016 13:10:00 -0000	[thread overview]
Message-ID: <CAFOnWkmRT7gsSphJt60fnOgNj2B8A=muQLg9aotgJf-Dkj+tWg@mail.gmail.com> (raw)

`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 <manish@mozilla.com>
Date: Sat, 29 Oct 2016 05:55:58 -0700
Subject: [PATCH] Add support for the sizeof function in Rust

2016-10-29  Manish Goregaokar  <manish@mozilla.com>

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 <voidval> KW_EXTERN
 %token <voidval> KW_CONST
 %token <voidval> KW_FN
+%token <voidval> KW_SIZEOF

 /* Operator tokens.  */
 %token <voidval> 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


             reply	other threads:[~2016-10-29 13:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-29 13:10 Manish Goregaokar [this message]
2016-10-30  3:18 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFOnWkmRT7gsSphJt60fnOgNj2B8A=muQLg9aotgJf-Dkj+tWg@mail.gmail.com' \
    --to=manish@mozilla.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox