From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gproxy2-pub.mail.unifiedlayer.com (gproxy2-pub.mail.unifiedlayer.com [69.89.18.3]) by sourceware.org (Postfix) with ESMTPS id C5364385B83D for ; Thu, 17 Feb 2022 22:05:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C5364385B83D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw14.mail.unifiedlayer.com (unknown [10.0.90.129]) by progateway4.mail.pro1.eigbox.com (Postfix) with ESMTP id 22C801004751D for ; Thu, 17 Feb 2022 22:05:54 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id KouPnedTg2s5dKouPnLB3p; Thu, 17 Feb 2022 22:05:54 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=BOh2EHcG c=1 sm=1 tr=0 ts=620ec6c2 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10:nop_charset_1 a=oGFeUVbbRNcA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=CCpqsmhAAAAA:8 a=eaJ8nkpe0WTWIDOz_Y8A:9 a=QEXdDO2ut3YA:10:nop_charset_2 a=ul9cdbp4aOFLsgKbc677:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=CapPMmNf/5/mHeqRXAbCV6lcUkTq8NqTibhX6Vrr408=; b=WgRzrag1/yZmtex9fFCoxdWphN 8N4K9M4q/B3bf9wkRwzoyUWIzJTe7Cc7oy8edm45ZXiHZNvbaa8YjQyq2sKjJZUsce4nXtw9npinC yqs4I7JFtp0m73u82WDpLaxu4; Received: from 75-166-146-214.hlrn.qwest.net ([75.166.146.214]:41038 helo=prentzel.Home) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nKouP-002ABb-6P; Thu, 17 Feb 2022 15:05:53 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 14/18] Use generic_emit_char in Rust Date: Thu, 17 Feb 2022 15:05:42 -0700 Message-Id: <20220217220547.3874030-15-tom@tromey.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220217220547.3874030-1-tom@tromey.com> References: <20220217220547.3874030-1-tom@tromey.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.146.214 X-Source-L: No X-Exim-ID: 1nKouP-002ABb-6P X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-146-214.hlrn.qwest.net (prentzel.Home) [75.166.146.214]:41038 X-Source-Auth: tom+tromey.com X-Email-Count: 20 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3031.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Feb 2022 22:05:56 -0000 This changes the Rust code to use generic_emit_char, passing in a function to handle Rust escape sequences correctly. This is PR rust/20164. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20164 --- gdb/rust-lang.c | 80 ++++++++++++++++++------------ gdb/testsuite/gdb.rust/expr.exp | 6 +-- gdb/testsuite/gdb.rust/unicode.exp | 3 +- 3 files changed, 50 insertions(+), 39 deletions(-) diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index a6094cd3b25..0699e3a0468 100644 --- a/gdb/rust-lang.c +++ b/gdb/rust-lang.c @@ -218,16 +218,6 @@ rust_u8_type_p (struct type *type) && TYPE_LENGTH (type) == 1); } -/* Return true if TYPE is a Rust character type. */ - -static bool -rust_chartype_p (struct type *type) -{ - return (type->code () == TYPE_CODE_CHAR - && TYPE_LENGTH (type) == 4 - && type->is_unsigned ()); -} - /* If VALUE represents a trait object pointer, return the underlying pointer with the correct (i.e., runtime) type. Otherwise, return NULL. */ @@ -263,6 +253,51 @@ rust_get_trait_object_pointer (struct value *value) +/* A callback function for generic_emit_char and generic_printstr that + escapes characters Rust-style. */ +static void +rust_emit_char (obstack_wide_file *stream, + gdb_wint_t w, + gdb::array_view orig, + int width, + enum bfd_endian byte_order, + int quoter, + bool *need_escapep) +{ + if (gdb_iswprint (w) && !gdb_iswcntrl (w)) + default_emit_wchar (stream, w, orig, width, byte_order, quoter, + need_escapep); + else if (w == LCST ('\n')) + fputs_filtered ("\\n", stream); + else if (w == LCST ('\r')) + fputs_filtered ("\\r", stream); + else if (w == LCST ('\t')) + fputs_filtered ("\\t", stream); + else if (w == LCST ('\0')) + fputs_filtered ("\\0", stream); + else + { + int i; + + for (i = 0; i + width <= orig.size (); i += width) + { + ULONGEST value = extract_unsigned_integer (&orig[i], width, + byte_order); + if (value <= 255) + fprintf_filtered (stream, "\\x%02x", (int) value); + else + fprintf_filtered (stream, "\\u{%06lx}", (unsigned long) value); + } + + /* If we somehow have extra bytes, print them now. */ + while (i < orig.size ()) + { + fprintf_filtered (stream, "\\x%02x", orig[i] & 0xff); + ++i; + } + } +} + /* See language.h. */ void @@ -290,9 +325,8 @@ rust_language::printstr (struct ui_file *stream, struct type *type, } } - /* This is not ideal as it doesn't use our character printer. */ generic_printstr (stream, type, string, length, encoding, force_ellipses, - '"', 0, options); + '"', 0, options, rust_emit_char); } @@ -1595,27 +1629,7 @@ void rust_language::printchar (int ch, struct type *chtype, struct ui_file *stream) const { - fputs_filtered ("'", stream); - if (!rust_chartype_p (chtype)) - generic_emit_char (ch, chtype, stream, '\'', - target_charset (chtype->arch ())); - else if (ch == '\\') - fprintf_filtered (stream, "\\%c", ch); - else if (ch == '\n') - fputs_filtered ("\\n", stream); - else if (ch == '\r') - fputs_filtered ("\\r", stream); - else if (ch == '\t') - fputs_filtered ("\\t", stream); - else if (ch == '\0') - fputs_filtered ("\\0", stream); - else if (ch >= 32 && ch <= 127 && isprint (ch)) - fputc_filtered (ch, stream); - else if (ch <= 255) - fprintf_filtered (stream, "\\x%02x", ch); - else - fprintf_filtered (stream, "\\u{%06x}", ch); - fputs_filtered ("'", stream); + generic_emit_char (ch, chtype, stream, '\'', nullptr, rust_emit_char); } /* See language.h. */ diff --git a/gdb/testsuite/gdb.rust/expr.exp b/gdb/testsuite/gdb.rust/expr.exp index 0c445897338..34eba4bf997 100644 --- a/gdb/testsuite/gdb.rust/expr.exp +++ b/gdb/testsuite/gdb.rust/expr.exp @@ -115,10 +115,8 @@ gdb_test "print \[1,2 3" "',' or ']' expected" gdb_test "print \[1 2" "',', ';', or ']' expected" gdb_test "print b\"hi rust\"" " = b\"hi rust\"" -# This isn't rusty syntax yet, but that's another bug -- this is just -# testing that byte escapes work properly. -gdb_test "print b\"\\xddhi bob\"" " = b\"\\\\335hi bob\"" -gdb_test "print b\"has\\0nul\"" " = b\"has\\\\000nul\"" +gdb_test "print b\"\\xddhi bob\"" " = b\"\\\\xddhi bob\"" +gdb_test "print b\"has\\0nul\"" " = b\"has\\\\0nul\"" gdb_test "print br##\"hi\"##" " = b\"hi\"" gdb_test "print br##\"hi" "Unexpected EOF in string" diff --git a/gdb/testsuite/gdb.rust/unicode.exp b/gdb/testsuite/gdb.rust/unicode.exp index 9de0a0e724f..8378195bf5d 100644 --- a/gdb/testsuite/gdb.rust/unicode.exp +++ b/gdb/testsuite/gdb.rust/unicode.exp @@ -43,8 +43,7 @@ if {![runto ${srcfile}:$line]} { gdb_test "print ๐•ฏ" " = 98" "print D" gdb_test "print \"๐•ฏ\"" " = \"๐•ฏ\"" "print D in string" -# This output is maybe not ideal, but it also isn't incorrect. -gdb_test "print '๐•ฏ'" " = 120175 '\\\\u\\\{01d56f\\\}'" \ +gdb_test "print '๐•ฏ'" " = 120175 '๐•ฏ'" \ "print D as char" gdb_test "print cรง" " = 97" "print cc" -- 2.31.1