public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] Improve parsing of raw string literals
Date: Tue,  7 May 2024 16:29:17 +0000 (GMT)	[thread overview]
Message-ID: <20240507162917.5D67F3870846@sourceware.org> (raw)

https://gcc.gnu.org/g:e36c3ece68cecba3170ab2d35de6e0a0c8bac40e

commit e36c3ece68cecba3170ab2d35de6e0a0c8bac40e
Author: Owen Avery <powerboat9.gamer@gmail.com>
Date:   Tue Mar 26 21:10:05 2024 -0400

    Improve parsing of raw string literals
    
    gcc/rust/ChangeLog:
    
            * lex/rust-lex.cc
            (Lexer::parse_raw_string):
            Bring handling of edge cases to par with parse_raw_byte_string.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/raw-string-loc.rs: New test.
    
    Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>

Diff:
---
 gcc/rust/lex/rust-lex.cc                     | 21 ++++++++++++++++++---
 gcc/testsuite/rust/compile/raw-string-loc.rs |  6 ++++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc
index 7c37e83d6cb7..e5c9148976c1 100644
--- a/gcc/rust/lex/rust-lex.cc
+++ b/gcc/rust/lex/rust-lex.cc
@@ -2152,6 +2152,9 @@ Lexer::parse_raw_string (location_t loc, int initial_hash_count)
   str.reserve (16); // some sensible default
 
   int length = 1 + initial_hash_count;
+  current_column += length;
+
+  const location_t string_begin_locus = get_current_location ();
 
   if (initial_hash_count > 0)
     skip_input (initial_hash_count - 1);
@@ -2162,10 +2165,11 @@ Lexer::parse_raw_string (location_t loc, int initial_hash_count)
     rust_error_at (get_current_location (), "raw string has no opening %<\"%>");
 
   length++;
+  current_column++;
   skip_input ();
   current_char = peek_input ();
 
-  while (!current_char.is_eof ())
+  while (true)
     {
       if (current_char.value == '"')
 	{
@@ -2186,19 +2190,30 @@ Lexer::parse_raw_string (location_t loc, int initial_hash_count)
 	      skip_input (initial_hash_count);
 	      current_char = peek_input ();
 	      length += initial_hash_count + 1;
+	      current_column += initial_hash_count + 1;
 	      break;
 	    }
 	}
+      else if (current_char.is_eof ())
+	{
+	  rust_error_at (string_begin_locus, "unended raw string literal");
+	  return Token::make (END_OF_FILE, get_current_location ());
+	}
 
       length++;
+      current_column++;
+      if (current_char == '\n')
+	{
+	  current_line++;
+	  current_column = 1;
+	  start_line (current_line, max_column_hint);
+	}
 
       str += current_char.as_string ();
       skip_input ();
       current_char = peek_input ();
     }
 
-  current_column += length;
-
   loc += length - 1;
 
   str.shrink_to_fit ();
diff --git a/gcc/testsuite/rust/compile/raw-string-loc.rs b/gcc/testsuite/rust/compile/raw-string-loc.rs
new file mode 100644
index 000000000000..70977510ba38
--- /dev/null
+++ b/gcc/testsuite/rust/compile/raw-string-loc.rs
@@ -0,0 +1,6 @@
+const X: &'static str = r#"12
+12"#;
+
+BREAK
+// { dg-error "unrecognised token" "" { target *-*-* } .-1 }
+// { dg-excess-errors "error 'failed to parse item' does not have location" }

                 reply	other threads:[~2024-05-07 16:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240507162917.5D67F3870846@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).