This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

cpplib: have _cpp_lex_token return a pointer to const token


A step towards having tokens being handled through pointers rather
than copying of structs.  We still copy in two places: cpp_get_token,
and saving macro expansions.  It is easy to save macro expansions by
lexing them directly into permanent storage (patch soon).
cpp_get_token has to wait until we handle macro expansion on pointers
(tricky, but sadly only because of preprocessed output spacing
issues).

Bootstrapped x86 Linux no regressions.

Neil.

	* cpphash.h (_cpp_lex_token): Update prototype.
	* cpplex.c (_cpp_lex_token): New prototype.
	* cpplib.c (skip_rest_of_line, check_eol, _cpp_handle_directive,
	lex_macro_node, read_flag, do_pragma_poison): Update.
	* cppmacro.c (cpp_get_token, parse_params,
	lex_expansion_token): Update.

Index: cpphash.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpphash.h,v
retrieving revision 1.126
diff -u -p -r1.126 cpphash.h
--- cpphash.h	2001/09/15 10:18:02	1.126
+++ cpphash.h	2001/09/16 11:39:11
@@ -398,7 +398,7 @@ extern void _cpp_pop_file_buffer	PARAMS 
 extern int _cpp_parse_expr		PARAMS ((cpp_reader *));
 
 /* In cpplex.c */
-extern void _cpp_lex_token		PARAMS ((cpp_reader *, cpp_token *));
+extern const cpp_token *_cpp_lex_token	PARAMS ((cpp_reader *));
 extern int _cpp_equiv_tokens		PARAMS ((const cpp_token *,
 						 const cpp_token *));
 extern void _cpp_init_tokenrun		PARAMS ((tokenrun *, unsigned int));
Index: cpplex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplex.c,v
retrieving revision 1.162
diff -u -p -r1.162 cpplex.c
--- cpplex.c	2001/09/15 10:18:02	1.162
+++ cpplex.c	2001/09/16 11:39:24
@@ -933,10 +933,9 @@ next_tokenrun (run)
 }
 
 /* Lex a token into RESULT (external interface).  */
-void
-_cpp_lex_token (pfile, dest)
+const cpp_token *
+_cpp_lex_token (pfile)
      cpp_reader *pfile;
-     cpp_token *dest;
 {
   cpp_token *result;
 
@@ -979,7 +978,7 @@ _cpp_lex_token (pfile, dest)
 	break;
     }
 
-  *dest = *result;
+  return result;
 }
 
 /* Lex a token into RESULT.  When meeting a newline, returns CPP_EOF
Index: cpplib.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.c,v
retrieving revision 1.276
diff -u -p -r1.276 cpplib.c
--- cpplib.c	2001/09/16 11:23:55	1.276
+++ cpplib.c	2001/09/16 11:39:24
@@ -185,15 +185,14 @@ static void
 skip_rest_of_line (pfile)
      cpp_reader *pfile;
 {
-  cpp_token token;
-
   /* Discard all stacked contexts.  */
   while (pfile->context != &pfile->base_context)
     _cpp_pop_context (pfile);
 
   /* Sweep up all tokens remaining on the line.  */
-  while (! SEEN_EOL ())
-    _cpp_lex_token (pfile, &token);
+  if (! SEEN_EOL ())
+    while (_cpp_lex_token (pfile)->type != CPP_EOF)
+      ;
 }
 
 /* Ensure there are no stray tokens at the end of a directive.  */
@@ -201,15 +200,9 @@ static void
 check_eol (pfile)
      cpp_reader *pfile;
 {
-  if (! SEEN_EOL ())
-    {
-      cpp_token token;
-
-      _cpp_lex_token (pfile, &token);
-      if (token.type != CPP_EOF)
-	cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
-		     pfile->directive->name);
-    }
+  if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
+    cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
+		 pfile->directive->name);
 }
 
 /* Called when entering a directive, _Pragma or command-line directive.  */
@@ -307,20 +300,20 @@ _cpp_handle_directive (pfile, indented)
      int indented;
 {
   const directive *dir = 0;
-  cpp_token dname;
+  const cpp_token *dname;
   int skip = 1;
 
   start_directive (pfile);
-  _cpp_lex_token (pfile, &dname);
+  dname = _cpp_lex_token (pfile);
 
-  if (dname.type == CPP_NAME)
+  if (dname->type == CPP_NAME)
     {
-      if (dname.val.node->directive_index)
-	dir = &dtable[dname.val.node->directive_index - 1];
+      if (dname->val.node->directive_index)
+	dir = &dtable[dname->val.node->directive_index - 1];
     }
   /* We do not recognise the # followed by a number extension in
      assembler code.  */
-  else if (dname.type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
+  else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
     {
       dir = &dtable[T_LINE];
       pfile->state.line_extension = 1;
@@ -361,7 +354,7 @@ _cpp_handle_directive (pfile, indented)
 	    dir = 0;
 	}
     }
-  else if (dname.type == CPP_EOF)
+  else if (dname->type == CPP_EOF)
     ;	/* CPP_EOF is the "null directive".  */
   else
     {
@@ -373,7 +366,7 @@ _cpp_handle_directive (pfile, indented)
 	skip = 0;
       else if (!pfile->state.skipping)
 	cpp_error (pfile, "invalid preprocessing directive #%s",
-		   cpp_token_as_text (pfile, &dname));
+		   cpp_token_as_text (pfile, dname));
     }
 
   if (dir)
@@ -414,11 +407,8 @@ static cpp_hashnode *
 lex_macro_node (pfile)
      cpp_reader *pfile;
 {
-  cpp_token token;
   cpp_hashnode *node;
-
-  /* Lex the macro name directly.  */
-  _cpp_lex_token (pfile, &token);
+  const cpp_token *token = _cpp_lex_token (pfile);
 
   /* The token immediately after #define must be an identifier.  That
      identifier may not be "defined", per C99 6.10.8p4.
@@ -427,22 +417,22 @@ lex_macro_node (pfile)
      Finally, the identifier may not have been poisoned.  (In that case
      the lexer has issued the error message for us.)  */
 
-  if (token.type != CPP_NAME)
+  if (token->type != CPP_NAME)
     {
-      if (token.type == CPP_EOF)
+      if (token->type == CPP_EOF)
 	cpp_error (pfile, "no macro name given in #%s directive",
 		   pfile->directive->name);
-      else if (token.flags & NAMED_OP)
+      else if (token->flags & NAMED_OP)
 	cpp_error (pfile,
 	   "\"%s\" cannot be used as a macro name as it is an operator in C++",
-		   NODE_NAME (token.val.node));
+		   NODE_NAME (token->val.node));
       else
 	cpp_error (pfile, "macro names must be identifiers");
 
       return 0;
     }
 
-  node = token.val.node;
+  node = token->val.node;
   if (node->flags & NODE_POISONED)
     return 0;
 
@@ -654,12 +644,11 @@ read_flag (pfile, last)
      cpp_reader *pfile;
      unsigned int last;
 {
-  cpp_token token;
+  const cpp_token *token = _cpp_lex_token (pfile);
 
-  _cpp_lex_token (pfile, &token);
-  if (token.type == CPP_NUMBER && token.val.str.len == 1)
+  if (token->type == CPP_NUMBER && token->val.str.len == 1)
     {
-      unsigned int flag = token.val.str.text[0] - '0';
+      unsigned int flag = token->val.str.text[0] - '0';
 
       if (flag > last && flag <= 4
 	  && (flag != 4 || last == 3)
@@ -667,9 +656,9 @@ read_flag (pfile, last)
 	return flag;
     }
 
-  if (token.type != CPP_EOF)
+  if (token->type != CPP_EOF)
     cpp_error (pfile, "invalid flag \"%s\" in line directive",
-	       cpp_token_as_text (pfile, &token));
+	       cpp_token_as_text (pfile, token));
   return 0;
 }
 
@@ -1033,22 +1022,22 @@ do_pragma_poison (pfile)
 {
   /* Poison these symbols so that all subsequent usage produces an
      error message.  */
-  cpp_token tok;
+  const cpp_token *tok;
   cpp_hashnode *hp;
 
   pfile->state.poisoned_ok = 1;
   for (;;)
     {
-      _cpp_lex_token (pfile, &tok);
-      if (tok.type == CPP_EOF)
+      tok = _cpp_lex_token (pfile);
+      if (tok->type == CPP_EOF)
 	break;
-      if (tok.type != CPP_NAME)
+      if (tok->type != CPP_NAME)
 	{
 	  cpp_error (pfile, "invalid #pragma GCC poison directive");
 	  break;
 	}
 
-      hp = tok.val.node;
+      hp = tok->val.node;
       if (hp->flags & NODE_POISONED)
 	continue;
 
Index: cppmacro.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppmacro.c,v
retrieving revision 1.69
diff -u -p -r1.69 cppmacro.c
--- cppmacro.c	2001/09/16 11:23:56	1.69
+++ cppmacro.c	2001/09/16 11:39:27
@@ -912,7 +912,7 @@ cpp_get_token (pfile, token)
 
       /* Context->prev == 0 <=> base context.  */
       if (!context->prev)
-	_cpp_lex_token (pfile, token);
+	*token = *_cpp_lex_token (pfile);
       else if (context->list.first != context->list.limit)
 	{
 	  *token = *context->list.first++;
@@ -1124,19 +1124,18 @@ parse_params (pfile, macro)
      cpp_reader *pfile;
      cpp_macro *macro;
 {
-  cpp_token token;
   unsigned int prev_ident = 0;
 
   macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool);
   for (;;)
     {
-      _cpp_lex_token (pfile, &token);
+      const cpp_token *token = _cpp_lex_token (pfile);
 
-      switch (token.type)
+      switch (token->type)
 	{
 	default:
 	  cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
-		     cpp_token_as_text (pfile, &token));
+		     cpp_token_as_text (pfile, token));
 	  return 0;
 
 	case CPP_NAME:
@@ -1147,7 +1146,7 @@ parse_params (pfile, macro)
 	    }
 	  prev_ident = 1;
 
-	  if (save_parameter (pfile, macro, token.val.node))
+	  if (save_parameter (pfile, macro, token->val.node))
 	    return 0;
 	  continue;
 
@@ -1179,8 +1178,8 @@ parse_params (pfile, macro)
 	    cpp_pedwarn (pfile, "ISO C does not permit named variadic macros");
 
 	  /* We're at the end, and just expect a closing parenthesis.  */
-	  _cpp_lex_token (pfile, &token);
-	  if (token.type == CPP_CLOSE_PAREN)
+	  token = _cpp_lex_token (pfile);
+	  if (token->type == CPP_CLOSE_PAREN)
 	    break;
 	  /* Fall through.  */
 
@@ -1214,7 +1213,7 @@ lex_expansion_token (pfile, macro)
     }
 
   macro->count++;
-  _cpp_lex_token (pfile, token);
+  *token = *_cpp_lex_token (pfile);
 
   /* Is this an argument?  */
   if (token->type == CPP_NAME && token->val.node->arg_index)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]