From patchwork Mon Oct 2 20:10:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 23286 Received: (qmail 12666 invoked by alias); 2 Oct 2017 20:11:01 -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 12534 invoked by uid 89); 2 Oct 2017 20:11:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 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=UD:rs, b9 X-HELO: gproxy9-pub.mail.unifiedlayer.com Received: from gproxy9-pub.mail.unifiedlayer.com (HELO gproxy9-pub.mail.unifiedlayer.com) (69.89.20.122) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 02 Oct 2017 20:10:58 +0000 Received: from cmgw2 (unknown [10.0.90.83]) by gproxy9.mail.unifiedlayer.com (Postfix) with ESMTP id 488611E0616 for ; Mon, 2 Oct 2017 14:10:57 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by cmgw2 with id GkAu1w0172f2jeq01kAxaQ; Mon, 02 Oct 2017 14:10:57 -0600 X-Authority-Analysis: v=2.2 cv=dZfw5Tfe c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=02M-m0pO-4AA:10 a=zstS-IiYAAAA:8 a=IZ34t0FNQk9mUhYiuLAA:9 a=YBjC5FxYqiQiUIeg:21 a=eUZc69oNDMHGPzgJ:21 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-0-208.hlrn.qwest.net ([75.166.0.208]:55566 helo=pokyo.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dz73G-003jcm-1a; Mon, 02 Oct 2017 14:10:54 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [FYI 3/3] Fix &str printing in Rust Date: Mon, 2 Oct 2017 14:10:41 -0600 Message-Id: <20171002201041.4815-4-tom@tromey.com> In-Reply-To: <20171002201041.4815-1-tom@tromey.com> References: <20171002201041.4815-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1dz73G-003jcm-1a X-Source-Sender: 75-166-0-208.hlrn.qwest.net (pokyo.Home) [75.166.0.208]:55566 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes Printing a string slice ("&str") in Rust would print until the terminating \0; but that is incorrect because a slice has a length. This fixes &str printing, and arranges to preserve the type name when slicing a slice, so that printing a slice of an "&str" works as well. This is PR rust/22236. 2017-10-02 Tom Tromey PR rust/22236: * rust-lang.c (rust_val_print_str): New function. (val_print_struct): Call it. (rust_subscript): Preserve name of slice type. 2017-10-02 Tom Tromey PR rust/22236: * gdb.rust/simple.rs (main): New variable "fslice". * gdb.rust/simple.exp: Add slice tests. Update string tests. --- gdb/ChangeLog | 7 +++++++ gdb/rust-lang.c | 29 +++++++++++++++++++++++++++-- gdb/testsuite/ChangeLog | 6 ++++++ gdb/testsuite/gdb.rust/simple.exp | 12 ++++++------ gdb/testsuite/gdb.rust/simple.rs | 2 ++ 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 11c1ca3..e0cf4c0 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,12 @@ 2017-10-02 Tom Tromey + PR rust/22236: + * rust-lang.c (rust_val_print_str): New function. + (val_print_struct): Call it. + (rust_subscript): Preserve name of slice type. + +2017-10-02 Tom Tromey + * rust-lang.c (rust_subscript): Handle slices in EVAL_AVOID_SIDE_EFFECTS case. diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index 9b64efa..261ddb1 100644 --- a/gdb/rust-lang.c +++ b/gdb/rust-lang.c @@ -467,6 +467,21 @@ rust_printstr (struct ui_file *stream, struct type *type, +/* Helper function to print a string slice. */ + +static void +rust_val_print_str (struct ui_file *stream, struct value *val, + const struct value_print_options *options) +{ + struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL, + "slice"); + struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice"); + + val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8", + value_as_address (base), value_as_long (len), stream, + options); +} + /* rust_print_type branch for structs and untagged unions. */ static void @@ -477,6 +492,13 @@ val_print_struct (struct type *type, int embedded_offset, { int i; int first_field; + + if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0) + { + rust_val_print_str (stream, val, options); + return; + } + bool is_tuple = rust_tuple_type_p (type); bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type); struct value_print_options opts; @@ -1562,8 +1584,11 @@ rust_subscript (struct expression *exp, int *pos, enum noside noside, usize = language_lookup_primitive_type (exp->language_defn, exp->gdbarch, "usize"); - slice = rust_slice_type ("&[*gdb*]", value_type (result), - usize); + const char *new_name = ((type != nullptr + && rust_slice_type_p (type)) + ? TYPE_NAME (type) : "&[*gdb*]"); + + slice = rust_slice_type (new_name, value_type (result), usize); addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice)); addr = value_as_long (addrval); diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 296878c..4de56f7 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,5 +1,11 @@ 2017-10-02 Tom Tromey + PR rust/22236: + * gdb.rust/simple.rs (main): New variable "fslice". + * gdb.rust/simple.exp: Add slice tests. Update string tests. + +2017-10-02 Tom Tromey + * gdb.rust/simple.exp: Test ptype of a slice. 2017-10-02 Tom Tromey diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp index b01841f..90516b9 100644 --- a/gdb/testsuite/gdb.rust/simple.exp +++ b/gdb/testsuite/gdb.rust/simple.exp @@ -64,6 +64,10 @@ gdb_test "ptype j2" " = struct simple::Unit" gdb_test "print simple::Unit" " = simple::Unit" gdb_test "print simple::Unit{}" " = simple::Unit" +gdb_test "print f" " = \"hi bob\"" +gdb_test "print fslice" " = \"bob\"" +gdb_test "print &f\[3..\]" " = \"bob\"" + gdb_test "print g" " = \\(u8 \\(\\*\\)\\\[6\\\]\\) $hex b\"hi bob\"" gdb_test "ptype g" " = u8 \\(\\*\\)\\\[6\\\]" @@ -200,13 +204,9 @@ gdb_test "ptype empty" "fn \\(\\)" gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21" -# We need the ".*" because currently we don't extract the length and -# use it to intelligently print the string data. -gdb_test "print \"hello rust\"" \ - " = &str \\{data_ptr: $hex \"hello rust.*\", length: 10\\}" +gdb_test "print \"hello rust\"" " = \"hello rust.*\"" gdb_test "print \"hello" "Unexpected EOF in string" -gdb_test "print r##\"hello \" rust\"##" \ - " = &str \\{data_ptr: $hex \"hello \\\\\" rust.*\", length: 12\\}" +gdb_test "print r##\"hello \" rust\"##" " = \"hello \\\\\" rust.*\"" gdb_test "print r\"hello" "Unexpected EOF in string" gdb_test "print r###\"###hello\"" "Unexpected EOF in string" gdb_test "print r###\"###hello\"##" "Unexpected EOF in string" diff --git a/gdb/testsuite/gdb.rust/simple.rs b/gdb/testsuite/gdb.rust/simple.rs index 9c154e7..d6d1755 100644 --- a/gdb/testsuite/gdb.rust/simple.rs +++ b/gdb/testsuite/gdb.rust/simple.rs @@ -95,6 +95,8 @@ fn main () { let g = b"hi bob"; let h = b'9'; + let fslice = &f[3..]; + let i = ["whatever"; 8]; let j = Unit;