https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96328

Mark Wielaard <mark at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED

--- Comment #2 from Mark Wielaard <mark at gcc dot gnu.org> ---
Only tested on the failing testcase, but this seems to fix it:

$ echo "friend" | /opt/local/install/gcc/bin/g++ -c -xc++ -
<stdin>:1:1: error: ‘friend’ used outside of class
<stdin>:2: error: expected unqualified-id at end of input

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 0c77c20da862..9c4f6a50523c 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -781,9 +781,13 @@ static cp_token *
 cp_lexer_safe_previous_token (cp_lexer *lexer)
 {
   if (lexer->buffer)
-    if (lexer->next_token != lexer->buffer->address ())
-      return cp_lexer_previous_token (lexer);
-
+    {
+      cp_token_position tp = cp_lexer_previous_token_position (lexer);
+      while (tp->purged_p && tp != lexer->buffer->address ())
+       tp--;
+      if (tp != lexer->buffer->address ())
+       return tp;
+    }
   return NULL;
 }

@@ -2932,15 +2936,15 @@ cp_parser_error_1 (cp_parser* parser, const char*
gmsgid,
   gcc_rich_location richloc (input_location);

   bool added_matching_location = false;
+  cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer);

-  if (missing_token_desc != RT_NONE)
+  if (prev_token && missing_token_desc != RT_NONE)
     {
       /* Potentially supply a fix-it hint, suggesting to add the
         missing token immediately after the *previous* token.
         This may move the primary location within richloc.  */
       enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
-      location_t prev_token_loc
-       = cp_lexer_previous_token (parser->lexer)->location;
+      location_t prev_token_loc = prev_token->location;
       maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);

       /* If matching_location != UNKNOWN_LOCATION, highlight it.
@@ -2957,7 +2961,6 @@ cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
      standard string literal constants defined in header files. If
      there is one, then add that as an hint to the error message. */
   name_hint h;
-  cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer);
   if (prev_token && cp_parser_is_string_literal (prev_token)
       && token->type == CPP_NAME)
     {

Reply via email to