This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Add support for the sizeof function in Rust


`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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]