[binutils-gdb] Implement Rust raw identifiers

Tom Tromey tromey@sourceware.org
Fri Jun 11 14:24:57 GMT 2021


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=48ec4c05c68bfec4b79e95807bd82e0df4634a6c

commit 48ec4c05c68bfec4b79e95807bd82e0df4634a6c
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Jun 11 08:14:09 2021 -0600

    Implement Rust raw identifiers
    
    This patch implements Rust raw identifiers in the lexer in gdb.  There
    was an earlier patch to do this, but the contributor didn't reply to
    my email asking whether he had sorted out his copyright assignment.
    
    This is relatively straightforward, but a small test suite addition
    was needd to ensure that the new test is skipped on older versions of
    rustc -- ones that predate the introduction of raw identifiers.
    
    gdb/ChangeLog
    2021-06-11  Tom Tromey  <tom@tromey.com>
    
            PR rust/23427
            * rust-parse.c (rust_parser::lex_identifier): Handle raw
            identifiers.
            (rust_lex_tests): Add raw identifier tests.
    
    gdb/testsuite/ChangeLog
    2021-06-11  Tom Tromey  <tom@tromey.com>
    
            PR rust/23427
            * lib/rust-support.exp (rust_compiler_version): New caching proc.
            * gdb.rust/rawids.exp: New file.
            * gdb.rust/rawids.rs: New file.

Diff:
---
 gdb/ChangeLog                      |  7 +++++++
 gdb/rust-parse.c                   | 32 +++++++++++++++++++++++------
 gdb/testsuite/ChangeLog            |  7 +++++++
 gdb/testsuite/gdb.rust/rawids.exp  | 41 ++++++++++++++++++++++++++++++++++++++
 gdb/testsuite/gdb.rust/rawids.rs   | 26 ++++++++++++++++++++++++
 gdb/testsuite/lib/rust-support.exp | 17 ++++++++++++++++
 6 files changed, 124 insertions(+), 6 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 549ea69e7da..30c2aa73d87 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2021-06-11  Tom Tromey  <tom@tromey.com>
+
+	PR rust/23427
+	* rust-parse.c (rust_parser::lex_identifier): Handle raw
+	identifiers.
+	(rust_lex_tests): Add raw identifier tests.
+
 2021-06-10  Simon Marchi  <simon.marchi@polymtl.ca>
 
 	* lib/gdb.exp (default_gdb_exit): Unset gdb_tty_name.
diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index 2f2afcf7e9f..539e1c8256d 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -747,12 +747,21 @@ rust_identifier_start_p (char c)
 int
 rust_parser::lex_identifier ()
 {
-  const char *start = pstate->lexptr;
   unsigned int length;
   const struct token_info *token;
   int i;
   int is_gdb_var = pstate->lexptr[0] == '$';
 
+  bool is_raw = false;
+  if (pstate->lexptr[0] == 'r'
+      && pstate->lexptr[1] == '#'
+      && rust_identifier_start_p (pstate->lexptr[2]))
+    {
+      is_raw = true;
+      pstate->lexptr += 2;
+    }
+
+  const char *start = pstate->lexptr;
   gdb_assert (rust_identifier_start_p (pstate->lexptr[0]));
 
   ++pstate->lexptr;
@@ -769,13 +778,16 @@ rust_parser::lex_identifier ()
 
   length = pstate->lexptr - start;
   token = NULL;
-  for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i)
+  if (!is_raw)
     {
-      if (length == strlen (identifier_tokens[i].name)
-	  && strncmp (identifier_tokens[i].name, start, length) == 0)
+      for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i)
 	{
-	  token = &identifier_tokens[i];
-	  break;
+	  if (length == strlen (identifier_tokens[i].name)
+	      && strncmp (identifier_tokens[i].name, start, length) == 0)
+	    {
+	      token = &identifier_tokens[i];
+	      break;
+	    }
 	}
     }
 
@@ -789,6 +801,7 @@ rust_parser::lex_identifier ()
 	}
     }
   else if (token == NULL
+	   && !is_raw
 	   && (strncmp (start, "thread", length) == 0
 	       || strncmp (start, "task", length) == 0)
 	   && space_then_number (pstate->lexptr))
@@ -2300,6 +2313,13 @@ rust_lex_tests (void)
   rust_lex_stringish_test (&parser, "hibob", "hibob", IDENT);
   rust_lex_stringish_test (&parser, "hibob__93", "hibob__93", IDENT);
   rust_lex_stringish_test (&parser, "thread", "thread", IDENT);
+  rust_lex_stringish_test (&parser, "r#true", "true", IDENT);
+
+  const int expected1[] = { IDENT, DECIMAL_INTEGER, 0 };
+  rust_lex_test_sequence (&parser, "r#thread 23", ARRAY_SIZE (expected1),
+			  expected1);
+  const int expected2[] = { IDENT, '#', 0 };
+  rust_lex_test_sequence (&parser, "r#", ARRAY_SIZE (expected2), expected2);
 
   rust_lex_stringish_test (&parser, "\"string\"", "string", STRING);
   rust_lex_stringish_test (&parser, "\"str\\ting\"", "str\ting", STRING);
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 37376643d32..9168a6a4b70 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,10 @@
+2021-06-11  Tom Tromey  <tom@tromey.com>
+
+	PR rust/23427
+	* lib/rust-support.exp (rust_compiler_version): New caching proc.
+	* gdb.rust/rawids.exp: New file.
+	* gdb.rust/rawids.rs: New file.
+
 2021-06-10  Tom de Vries  <tdevries@suse.de>
 
 	* gdb.mi/user-selected-context-sync.c (child_sub_function, main):
diff --git a/gdb/testsuite/gdb.rust/rawids.exp b/gdb/testsuite/gdb.rust/rawids.exp
new file mode 100644
index 00000000000..31a56d3bae6
--- /dev/null
+++ b/gdb/testsuite/gdb.rust/rawids.exp
@@ -0,0 +1,41 @@
+# Copyright (C) 2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test raw identifiers.
+
+load_lib rust-support.exp
+if {[skip_rust_tests]} {
+    continue
+}
+
+set v [split [rust_compiler_version] .]
+if {[lindex $v 0] == 1 && [lindex $v 1] < 30} {
+    untested "raw identifiers require rust 1.30 or greater"
+    return -1
+}
+
+standard_testfile .rs
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug rust}]} {
+    return -1
+}
+
+set line [gdb_get_line_number "set breakpoint here"]
+if {![runto ${srcfile}:$line]} {
+    untested "could not run to breakpoint"
+    return -1
+}
+
+gdb_test "print r#if" " = 23"
+gdb_test "print r#thread" " = 27"
diff --git a/gdb/testsuite/gdb.rust/rawids.rs b/gdb/testsuite/gdb.rust/rawids.rs
new file mode 100644
index 00000000000..f37e862d5da
--- /dev/null
+++ b/gdb/testsuite/gdb.rust/rawids.rs
@@ -0,0 +1,26 @@
+// Copyright (C) 2021 Free Software Foundation, Inc.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#![allow(dead_code)]
+#![allow(unused_variables)]
+#![allow(unused_assignments)]
+
+
+fn main () {
+    let r#if = 23;
+    let thread = 27;
+
+    println!("{}, {}", r#if, r#thread);        // set breakpoint here
+}
diff --git a/gdb/testsuite/lib/rust-support.exp b/gdb/testsuite/lib/rust-support.exp
index d2ffac94be3..2896ac82453 100644
--- a/gdb/testsuite/lib/rust-support.exp
+++ b/gdb/testsuite/lib/rust-support.exp
@@ -54,3 +54,20 @@ gdb_caching_proc rust_llvm_version {
     }
     return 0.0
 }
+
+# Return the version of the Rust compiler.
+gdb_caching_proc rust_compiler_version {
+    set rustc [find_rustc]
+    if {$rustc == ""} {
+	verbose "could not find rustc"
+    } else {
+	set output [lindex [remote_exec host "$rustc --version --verbose"] 1]
+	foreach line [split $output \n] {
+	    if {[regexp "rustc (\[0-9.\]+) .*\$" $output ignore version]} {
+		return $version
+	    }
+	}
+	verbose "could not match rustc version output: $output"
+    }
+    return 0.0
+}


More information about the Gdb-cvs mailing list