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: new lexer: fix-header adjustments (2/3)


Notice how the code gets simpler.  The remaining uglinesses
(token->val.name.text, yuck) will hopefully go away when we switch to
storing hashtable pointers for identifier tokens.

zw

	* fix-header.c (struct partial_proto): Remove unnecessary fields.
	(recognized_extern, recognized_function, read_scan_file):
	Update for new scheme.
	(check_protection): It's still a multiple include guard even
	if it doesn't always trigger.
	* scan-decls.c (skip_to_closing_brace, scan_decls): Update for
	new scheme.
	* scan.h: Declare struct cpp_token.  Update prototypes.

===================================================================
Index: fix-header.c
--- fix-header.c	2000/06/21 23:08:17	1.44
+++ fix-header.c	2000/07/04 01:00:56
@@ -432,8 +432,6 @@ write_lbrac ()
 struct partial_proto
 {
   struct partial_proto *next;
-  char *fname;	/* name of function */
-  char *rtype;	/* return type */
   struct fn_decl *fn;
   int line_seen;
 };
@@ -497,15 +495,13 @@ recognized_macro (fname)
 }
 
 void
-recognized_extern (name, name_length, type, type_length)
-     const char *name;
-     const char *type ATTRIBUTE_UNUSED;
-     int name_length, type_length ATTRIBUTE_UNUSED;
+recognized_extern (name)
+     const cpp_token *name;
 {
   switch (special_file_handling)
     {
     case errno_h:
-      if (name_length == 5 && strncmp (name, "errno", 5) == 0 && !seen_errno)
+      if (!cpp_idcmp (name->val.name.text, name->val.name.len, "errno"))
 	seen_errno = 1, required_other--;
       break;
 
@@ -515,25 +511,17 @@ recognized_extern (name, name_length, ty
 }
 
 /* Called by scan_decls if it saw a function definition for a function
-   named FNAME, with return type RTYPE, and argument list ARGS,
-   in source file FILE_SEEN on line LINE_SEEN.
-   KIND is 'I' for an inline function;
-   'F' if a normal function declaration preceded by 'extern "C"'
-   (or nested inside 'extern "C"' braces); or
+   named FNAME, in source file FILE_SEEN on line LINE_SEEN.  KIND is
+   'I' for an inline function; 'F' if a normal function declaration
+   preceded by 'extern "C"' (or nested inside 'extern "C"' braces); or
    'f' for other function declarations.  */
 
 void
-recognized_function (fname, fname_length,
-		     kind, rtype, rtype_length,
-		     have_arg_list, file_seen, line_seen)
-     const char *fname;
-     int fname_length;
+recognized_function (fname, kind, have_arg_list, file_seen)
+     const cpp_token *fname;
      int kind; /* One of 'f' 'F' or 'I' */
-     const char *rtype;
-     int rtype_length;
      int have_arg_list;
      const char *file_seen;
-     int line_seen;
 {
   struct partial_proto *partial;
   int i;
@@ -543,7 +531,8 @@ recognized_function (fname, fname_length
     missing_extern_C_count++;
 #endif
 
-  fn = lookup_std_proto (fname, fname_length);
+  fn = lookup_std_proto ((const char *)fname->val.name.text,
+			 fname->val.name.len);
 
   /* Remove the function from the list of required function.  */
   if (fn)
@@ -577,12 +566,7 @@ recognized_function (fname, fname_length
   partial_count++;
   partial = (struct partial_proto *)
     obstack_alloc (&scan_file_obstack, sizeof (struct partial_proto));
-  partial->fname = obstack_alloc (&scan_file_obstack, fname_length + 1);
-  bcopy (fname, partial->fname, fname_length);
-  partial->fname[fname_length] = 0;
-  partial->rtype = obstack_alloc (&scan_file_obstack, rtype_length + 1);
-  sprintf (partial->rtype, "%.*s", rtype_length, rtype);
-  partial->line_seen = line_seen;
+  partial->line_seen = fname->line;
   partial->fn = fn;
   fn->partial = partial;
   partial->next = partial_proto_list;
@@ -590,7 +574,7 @@ recognized_function (fname, fname_length
   if (verbose)
     {
       fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
-	       inc_filename, partial->fname);
+	       inc_filename, fn->fname);
     }
 }
 
@@ -646,19 +630,12 @@ read_scan_file (in_fname, argc, argv)
   for (cur_symbols = &symbol_table[0]; cur_symbols->names; cur_symbols++)
     check_macro_names (&scan_in, cur_symbols->names);
 
-  if (verbose && (scan_in.errors + warnings) > 0)
-    fprintf (stderr, "(%s: %d errors and %d warnings from cpp)\n",
-	     inc_filename, scan_in.errors, warnings);
-  if (scan_in.errors)
-    exit (SUCCESS_EXIT_CODE);
-
   /* Traditionally, getc and putc are defined in terms of _filbuf and _flsbuf.
      If so, those functions are also required.  */
   if (special_file_handling == stdio_h
       && (fn = lookup_std_proto ("_filbuf", 7)) != NULL)
     {
       static const unsigned char getchar_call[] = "getchar();";
-      int old_written = CPP_WRITTEN (&scan_in);
       int seen_filbuf = 0;
       cpp_buffer *buf = CPP_BUFFER (&scan_in);
       if (cpp_push_buffer (&scan_in, getchar_call,
@@ -668,14 +645,17 @@ read_scan_file (in_fname, argc, argv)
       /* Scan the macro expansion of "getchar();".  */
       for (;;)
 	{
-	  enum cpp_ttype token = cpp_get_token (&scan_in);
-	  int length = CPP_WRITTEN (&scan_in) - old_written;
-	  unsigned char *id = scan_in.token_buffer + old_written;
-	  
-	  CPP_SET_WRITTEN (&scan_in, old_written);
-	  if (token == CPP_EOF && CPP_BUFFER (&scan_in) == buf)
-	    break;
-	  if (token == CPP_NAME && cpp_idcmp (id, length, "_filbuf") == 0)
+	  const cpp_token *t = cpp_get_token (&scan_in);
+
+	  if (t->type == CPP_EOF)
+	    {
+	      cpp_pop_buffer (&scan_in);
+	      if (CPP_BUFFER (&scan_in) == buf)
+		break;
+	    }
+	  else if (t->type == CPP_NAME && cpp_idcmp (t->val.name.text,
+						     t->val.name.len,
+						     "_filbuf") == 0)
 	    seen_filbuf++;
 	}
       if (seen_filbuf)
@@ -1030,8 +1010,6 @@ check_protection (ifndef_line, endif_lin
 	}
       else if (!strcmp (buf.base, "define"))
 	{
-	  if (if_nesting != 1)
-	    goto skip_to_eol;
 	  c = inf_skip_spaces (c);
 	  c = inf_scan_ident (&buf, c);
 	  if (buf.base[0] > 0 && strcmp (buf.base, protect_name) == 0)
===================================================================
Index: scan-decls.c
--- scan-decls.c	2000/05/02 16:09:12	1.18
+++ scan-decls.c	2000/07/04 01:00:56
@@ -45,7 +45,7 @@ skip_to_closing_brace (pfile)
   int nesting = 1;
   for (;;)
     {
-      enum cpp_ttype token = cpp_get_token (pfile);
+      enum cpp_ttype token = cpp_get_token (pfile)->type;
       if (token == CPP_EOF)
 	break;
       if (token == CPP_OPEN_BRACE)
@@ -84,24 +84,17 @@ scan_decls (pfile, argc, argv)
      char **argv ATTRIBUTE_UNUSED;
 {
   int saw_extern, saw_inline;
-  int start_written;
-  /* If declarator_start is non-zero, it marks the start of the current
-     declarator.  If it is zero, we are either still parsing the
-     decl-specs, or prev_id_start marks the start of the declarator.  */
-  int declarator_start;
-  int prev_id_start, prev_id_end = 0;
-  enum cpp_ttype token;
+  const cpp_token *prev_id;
+  const cpp_token *token;
 
  new_statement:
-  CPP_SET_WRITTEN (pfile, 0);
-  start_written = 0;
   token = cpp_get_token (pfile);
 
  handle_statement:
   current_extern_C = 0;
   saw_extern = 0;
   saw_inline = 0;
-  if (token == CPP_OPEN_BRACE)
+  if (token->type == CPP_OPEN_BRACE)
     {
       /* Pop an 'extern "C"' nesting level, if appropriate.  */
       if (extern_C_braces_length
@@ -110,120 +103,112 @@ scan_decls (pfile, argc, argv)
       brace_nesting--;
       goto new_statement;
     }
-  if (token == CPP_OPEN_BRACE)
+  if (token->type == CPP_OPEN_BRACE)
     {
       brace_nesting++;
       goto new_statement;
     }
-  if (token == CPP_EOF)
+  if (token->type == CPP_EOF)
     {
+      cpp_pop_buffer (pfile);
       if (CPP_BUFFER (pfile) == NULL)
 	return 0;
-      else
-	goto new_statement;
+
+      goto new_statement;
     }
-  if (token == CPP_SEMICOLON)
+  if (token->type == CPP_SEMICOLON)
     goto new_statement;
-  if (token != CPP_NAME)
+  if (token->type != CPP_NAME)
     goto new_statement;
 
-  prev_id_start = 0;
-  declarator_start = 0;
+  prev_id = 0;
   for (;;)
     {
-      switch (token)
+      switch (token->type)
 	{
+	default:
+	  goto handle_statement;
+	case CPP_MULT:
+	case CPP_AND:
+	case CPP_PLACEMARKER:
+	  /* skip */
+	  break;
+
+	case CPP_COMMA:
+	case CPP_SEMICOLON:
+	  if (prev_id && saw_extern)
+	    {
+	      recognized_extern (prev_id);
+	    }
+	  if (token->type == CPP_COMMA)
+	    break;
+	  /* ... fall through ...  */
+	case CPP_OPEN_BRACE:  case CPP_CLOSE_BRACE:
+	  goto new_statement;
+	  
+	case CPP_EOF:
+	  cpp_pop_buffer (pfile);
+	  if (CPP_BUFFER (pfile) == NULL)
+	    return 0;
+	  break;
+
 	case CPP_OPEN_PAREN:
 	  /* Looks like this is the start of a formal parameter list.  */
-	  if (prev_id_start)
+	  if (prev_id)
 	    {
 	      int nesting = 1;
 	      int have_arg_list = 0;
-	      cpp_buffer *fbuf = cpp_file_buffer (pfile);
-	      unsigned int func_lineno = CPP_BUF_LINE (fbuf);
 	      for (;;)
 		{
 		  token = cpp_get_token (pfile);
-		  if (token == CPP_OPEN_PAREN)
+		  if (token->type == CPP_OPEN_PAREN)
 		    nesting++;
-		  else if (token == CPP_CLOSE_PAREN)
+		  else if (token->type == CPP_CLOSE_PAREN)
 		    {
 		      nesting--;
 		      if (nesting == 0)
 			break;
 		    }
-		  else if (token == CPP_EOF)
+		  else if (token->type == CPP_EOF)
 		    break;
-		  else if (token == CPP_NAME || token == CPP_ELLIPSIS)
+		  else if (token->type == CPP_NAME
+			   || token->type == CPP_ELLIPSIS)
 		    have_arg_list = 1;
 		}
-	      recognized_function (pfile->token_buffer + prev_id_start,
-				   prev_id_end - prev_id_start,
+	      recognized_function (prev_id, 
 				   (saw_inline ? 'I'
 				    : in_extern_C_brace || current_extern_C
-				    ? 'F' : 'f'),
-				   pfile->token_buffer, prev_id_start,
-				   have_arg_list,
-				   fbuf->nominal_fname, func_lineno);
-	      token = cpp_get_non_space_token (pfile);
-	      if (token == CPP_OPEN_BRACE)
+				    ? 'F' : 'f'), have_arg_list,
+				   CPP_BUFFER (pfile)->nominal_fname);
+	      token = cpp_get_token (pfile);
+	      if (token->type == CPP_OPEN_BRACE)
 		{
 		  /* skip body of (normally) inline function */
 		  skip_to_closing_brace (pfile);
 		  goto new_statement;
 		}
-	      goto maybe_handle_comma;
-	    }
-	  break;
-	case CPP_OTHER:
-	  if (CPP_WRITTEN (pfile) == (size_t) start_written + 1
-	      && (CPP_PWRITTEN (pfile)[-1] == '*'
-		  || CPP_PWRITTEN (pfile)[-1] == '&'))
-	    declarator_start = start_written;
-	  else
-	    goto handle_statement;
-	  break;
-	case CPP_COMMA:
-	case CPP_SEMICOLON:
-	  if (prev_id_start && saw_extern)
-	    {
-	      recognized_extern (pfile->token_buffer + prev_id_start,
-				 prev_id_end - prev_id_start,
-				 pfile->token_buffer,
-				 prev_id_start);
+	      if (token->type == CPP_SEMICOLON)
+		goto new_statement;
 	    }
-	  /* ... fall through ...  */
-	maybe_handle_comma:
-	  if (token != CPP_COMMA)
-	    goto new_statement;
-
-	  /* Handle multiple declarators in a single declaration,
-	     as in:  extern char *strcpy (), *strcat (), ... ; */
-	  if (declarator_start == 0)
-	    declarator_start = prev_id_start;
-	  CPP_SET_WRITTEN (pfile, declarator_start);
 	  break;
 	case CPP_NAME:
 	  /* "inline" and "extern" are recognized but skipped */
-	  if (!cpp_idcmp (pfile->token_buffer,
-			  CPP_WRITTEN (pfile), "inline"))
+	  if (!cpp_idcmp (token->val.name.text, token->val.name.len, "inline"))
 	    {
 	      saw_inline = 1;
-	      CPP_SET_WRITTEN (pfile, start_written);
 	    }
-	  else if (!cpp_idcmp (pfile->token_buffer,
-			       CPP_WRITTEN (pfile), "extern"))
+	  else if (!cpp_idcmp (token->val.name.text,
+			       token->val.name.len, "extern"))
 	    {
 	      saw_extern = 1;
-	      CPP_SET_WRITTEN (pfile, start_written);
-	      token = cpp_get_non_space_token (pfile);
-	      if (token == CPP_STRING
-		  && strcmp (pfile->token_buffer, "\"C\"") == 0)
+	      token = cpp_get_token (pfile);
+	      if (token->type == CPP_STRING
+		  && !cpp_idcmp (token->val.name.text,
+				 token->val.name.len, "C"))
 		{
-		  CPP_SET_WRITTEN (pfile, start_written);
 		  current_extern_C = 1;
-		  token = cpp_get_non_space_token (pfile);
-		  if (token == CPP_OPEN_BRACE)
+		  token = cpp_get_token (pfile);
+		  if (token->type == CPP_OPEN_BRACE)
 		    {
 		      brace_nesting++;
 		      extern_C_braces[extern_C_braces_length++]
@@ -236,29 +221,9 @@ scan_decls (pfile, argc, argv)
 	      break;
 	    }
 	  /* This may be the name of a variable or function.  */
-	  prev_id_start = start_written;
-	  prev_id_end = CPP_WRITTEN (pfile);
-	  break;
-
-	case CPP_OPEN_BRACE:  case CPP_CLOSE_BRACE:  case CPP_DIRECTIVE:
-	  goto new_statement;  /* handle_statement? */
-	  
-	case CPP_EOF:
-	  if (CPP_BUFFER (pfile) == NULL)
-	    return 0;
-	  /* else fall through */
-
-	case CPP_HSPACE:  case CPP_VSPACE:  case CPP_COMMENT:
-	  /* Skip initial white space.  */
-	  if (start_written == 0)
-	    CPP_SET_WRITTEN (pfile, 0);
+	  prev_id = token;
 	  break;
-
-	 default:
-	  prev_id_start = 0;
 	}
-
-      start_written = CPP_WRITTEN (pfile);
       token = cpp_get_token (pfile);
     }
 }
===================================================================
Index: scan.h
--- scan.h	2000/02/10 02:23:08	1.12
+++ scan.h	2000/07/04 01:00:56
@@ -50,6 +50,8 @@ struct fn_decl
   struct partial_proto *partial;
 };
 
+struct cpp_token;
+
 extern int lineno;
 extern void sstring_append _PARAMS((sstring *, sstring *));
 extern void make_sstring_space _PARAMS((sstring *, int));
@@ -58,8 +60,9 @@ extern int scan_ident _PARAMS((FILE *, s
 extern int scan_string _PARAMS((FILE *, sstring *, int));
 extern int read_upto _PARAMS((FILE *, sstring *, int));
 extern unsigned long hash _PARAMS((const char *));
-extern void recognized_function _PARAMS((const char *, int, int, const char *, int, int, const char *, int));
-extern void recognized_extern _PARAMS((const char *, int, const char *, int));
+extern void recognized_function _PARAMS((const struct cpp_token *, int, int,
+					 const char *));
+extern void recognized_extern _PARAMS((const struct cpp_token *));
 extern unsigned int hashstr _PARAMS((const char *, unsigned int));
 
 struct cpp_reader;

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