Implement Rust raw identifiers

Message ID 20210602175808.2012175-1-tom@tromey.com
State New
Headers show
Series
  • Implement Rust raw identifiers
Related show

Commit Message

Tom Tromey June 2, 2021, 5:58 p.m.
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-02  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-02  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.
---
 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(-)
 create mode 100644 gdb/testsuite/gdb.rust/rawids.exp
 create mode 100644 gdb/testsuite/gdb.rust/rawids.rs

-- 
2.26.3

Comments

Tom Tromey June 11, 2021, 2:13 p.m. | #1
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:


Tom> This patch implements Rust raw identifiers in the lexer in gdb.  There
Tom> was an earlier patch to do this, but the contributor didn't reply to
Tom> my email asking whether he had sorted out his copyright assignment.

Tom> This is relatively straightforward, but a small test suite addition
Tom> was needd to ensure that the new test is skipped on older versions of
Tom> rustc -- ones that predate the introduction of raw identifiers.

I'm checking this in now.

Tom

Patch

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/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
+}