fix for most of the preprocessor bugs that cropped up last week

Zack Weinberg zack@wolery.cumb.org
Tue Mar 28 13:44:00 GMT 2000


This fixes:

 - spurious errors for # in an object-like macro (cpp-hash1.c)
 - spurious errors for /**/ at the end of a #define with -traditional
   (cpp-hash2.c)
 - double error message for #include L"stdio.h" (cpp-wi1.c)
 - broken multiple include optimization (cpp-mi.c).

This does not fix:

 - incorrect parsing of (40) vs 40 in an #if line (cpp-ifparen.c).

The patch is rather long because I rearranged the hash calculation
again.  The hash functions don't have any side effects anymore.

If there were any preprocessor bugs reported last week that I have not
mentioned, please resend the reports.

zw

	* cppfiles.c (hash_IHASH): Just return i->hash.
	(cpp_included): Set dummy.hash using _cpp_calc_hash.  Use
	htab_find_with_hash.
	(cpp_read_file): Likewise.
	(find_include_file): Likewise.  Properly initialize
	ih->nshort.  Share ih->name and ih->nshort if possible.
	* cpphash.c (_cpp_calc_hash): New function.
	(hash_HASHNODE): Just return h->hash.
	(_cpp_lookup): Set dummy.hash using _cpp_calc_hash.  Use
	htab_find_with_hash.
	* cpphash.h: Prototype _cpp_calc_hash.
	* cppinit.c (initialize_builtins): Provide a valid hash
	to _cpp_make_hashnode, using _cpp_calc_hash.

	* cpphash.c (collect_expansion): # is not a special character
	in object-like macros.  In -traditional mode, /**/ is not
	token paste at the beginning or end of the line.
	* cpplib.c (do_include, do_import, do_include_next): If
	parse_include fails, return immediately.

===================================================================
Index: cppfiles.c
--- cppfiles.c	2000/03/15 22:03:37	1.49
+++ cppfiles.c	2000/03/28 21:35:13
@@ -66,18 +66,8 @@ static unsigned int
 hash_IHASH (x)
      const void *x;
 {
-  IHASH *i = (IHASH *)x;
-  unsigned int r = 0, len = 0;
-  const U_CHAR *s = i->nshort;
-
-  if (i->hash != (unsigned long)-1)
-    return i->hash;
-
-  do
-    len++, r = r * 67 + (*s++ - 113);
-  while (*s && *s != '.');
-  i->hash = r + len;
-  return r + len;
+  const IHASH *i = (const IHASH *)x;
+  return i->hash;
 }
 
 /* Compare an existing IHASH structure with a potential one.  */
@@ -158,8 +148,9 @@ cpp_included (pfile, fname)
 {
   IHASH dummy, *ptr;
   dummy.nshort = fname;
-  dummy.hash = -1;
-  ptr = htab_find (pfile->all_include_files, (const void *)&dummy);
+  dummy.hash = _cpp_calc_hash (fname, strlen (fname));
+  ptr = htab_find_with_hash (pfile->all_include_files,
+			     (const void *)&dummy, dummy.hash);
   return (ptr != NULL);
 }
 
@@ -219,11 +210,12 @@ find_include_file (pfile, fname, search_
   int f;
   char *name;
 
-  dummy.hash = -1;
   dummy.nshort = fname;
+  dummy.hash = _cpp_calc_hash (fname, strlen (fname));
   path = (fname[0] == '/') ? ABSOLUTE_PATH : search_start;
-  slot = (IHASH **) htab_find_slot (pfile->all_include_files,
-				    (const void *)&dummy, 1);
+  slot = (IHASH **) htab_find_slot_with_hash (pfile->all_include_files,
+					      (const void *)&dummy,
+					      dummy.hash, 1);
 
   if (*slot && (ih = redundant_include_p (pfile, *slot, path)))
     {
@@ -280,10 +272,20 @@ find_include_file (pfile, fname, search_
     }
   else
     {
-      ih = (IHASH *) xmalloc (sizeof (IHASH) + strlen (name)
-			      + strlen (fname) + 1);
-      ih->nshort = ih->name + strlen (fname) + 1;
-      strcpy ((char *)ih->nshort, fname);
+      char *s;
+      
+      if ((s = strstr (name, fname)) != NULL)
+	{
+	  ih = (IHASH *) xmalloc (sizeof (IHASH) + strlen (name));
+	  ih->nshort = ih->name + (s - name);
+	}
+      else
+	{
+	  ih = (IHASH *) xmalloc (sizeof (IHASH) + strlen (name)
+				  + strlen (fname) + 1);
+	  ih->nshort = ih->name + strlen (name) + 1;
+	  strcpy ((char *)ih->nshort, fname);
+	}
     }
   strcpy ((char *)ih->name, name);
   ih->foundhere = path;
@@ -620,10 +622,11 @@ cpp_read_file (pfile, fname)
   if (fname == NULL)
     fname = "";
 
-  dummy.hash = -1;
   dummy.nshort = fname;
-  slot = (IHASH **) htab_find_slot (pfile->all_include_files,
-				    (const void *) &dummy, 1);
+  dummy.hash = _cpp_calc_hash (fname, strlen (fname));
+  slot = (IHASH **) htab_find_slot_with_hash (pfile->all_include_files,
+					      (const void *) &dummy,
+					      dummy.hash, 1);
   if (*slot && (ih = redundant_include_p (pfile, *slot, ABSOLUTE_PATH)))
     {
       if (ih == (IHASH *)-1)
===================================================================
Index: cpphash.c
--- cpphash.c	2000/03/15 21:54:47	1.58
+++ cpphash.c	2000/03/28 21:35:13
@@ -99,26 +99,30 @@ struct argdata
   int stringified_length;
 };
 
-/* Calculate hash of a HASHNODE structure.  */
-static unsigned int
-hash_HASHNODE (x)
-     const void *x;
+/* Calculate hash of a string of length LEN.  */
+unsigned int
+_cpp_calc_hash (str, len)
+     const U_CHAR *str;
+     size_t len;
 {
-  HASHNODE *h = (HASHNODE *)x;
-  const U_CHAR *s = h->name;
-  unsigned int len = h->length;
-  unsigned int n = len, r = 0;
+  size_t n = len;
+  unsigned int r = 0;
 
-  if (h->hash != (unsigned long)-1)
-    return h->hash;
-  
   do
-    r = r * 67 + (*s++ - 113);
+    r = r * 67 + (*str++ - 113);
   while (--n);
-  h->hash = r + len;
   return r + len;
 }
 
+/* Calculate hash of a HASHNODE structure.  */
+static unsigned int
+hash_HASHNODE (x)
+     const void *x;
+{
+  const HASHNODE *h = (const HASHNODE *)x;
+  return h->hash;
+}
+
 /* Compare two HASHNODE structures.  */
 static int
 eq_HASHNODE (x, y)
@@ -192,9 +196,10 @@ _cpp_lookup (pfile, name, len)
 
   dummy.name = name;
   dummy.length = len;
-  dummy.hash = -1;
+  dummy.hash = _cpp_calc_hash (name, len);
 
-  return (HASHNODE *) htab_find (pfile->hashtab, (void *)&dummy);
+  return (HASHNODE *) htab_find_with_hash (pfile->hashtab,
+					   (void *)&dummy, dummy.hash);
 }
 
 /* Find the hashtable slot for name "name".  Used to insert or delete.  */
@@ -218,9 +223,11 @@ _cpp_lookup_slot (pfile, name, len, inse
 
   dummy.name = name;
   dummy.length = len;
-  dummy.hash = -1;
+  dummy.hash = _cpp_calc_hash (name, len);
 
-  slot = (HASHNODE **) htab_find_slot (pfile->hashtab, (void *)&dummy, insert);
+  slot = (HASHNODE **) htab_find_slot_with_hash (pfile->hashtab,
+						 (void *)&dummy,
+						 dummy.hash, insert);
   if (insert)
     *hash = dummy.hash;
   return slot;
@@ -336,8 +343,13 @@ collect_expansion (pfile, arglist)
 	  break;
 
 	case CPP_STRINGIZE:
+	  /* # is not special in object-like macros.  It is special in
+	     function-like macros with no args.  (6.10.3.2 para 1.) */
+	  if (arglist == NULL)
+	    goto norm;
+	  /* # is not special immediately after PASTE.
+	     (Implied by 6.10.3.3 para 4.)  */
 	  if (last_token == PASTE)
-	    /* Not really a stringifier.  */
 	    goto norm;
 	  last_token = STRIZE;
 	  CPP_SET_WRITTEN (pfile, here);  /* delete from replacement text */
@@ -374,11 +386,15 @@ collect_expansion (pfile, arglist)
 	case CPP_COMMENT:
 	  /* We must be in -traditional mode.  Pretend this was a
 	     token paste, but only if there was no leading or
-	     trailing space.  */
+	     trailing space and it's in the middle of the line.  */
 	  CPP_SET_WRITTEN (pfile, here);
+	  if (last_token == START)
+	    break;
 	  if (is_hspace (pfile->token_buffer[here-1]))
 	    break;
 	  if (is_hspace (PEEKC ()))
+	    break;
+	  if (PEEKC () == '\n')
 	    break;
 	  if (last_token == ARG)
 	    endpat->raw_after = 1;
Index: cpphash.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpphash.h,v
retrieving revision 1.23
diff -u -p -r1.23 cpphash.h
--- cpphash.h	2000/03/14 06:34:11	1.23
+++ cpphash.h	2000/03/28 21:35:13
@@ -255,6 +255,7 @@ enum file_change_code {same_file, rename
 extern HASHNODE *_cpp_make_hashnode	PARAMS ((const U_CHAR *, size_t,
 						 enum node_type,
 						 unsigned long));
+extern unsigned int _cpp_calc_hash	PARAMS ((const U_CHAR *, size_t));
 extern HASHNODE *_cpp_lookup		PARAMS ((cpp_reader *,
 						 const U_CHAR *, int));
 extern HASHNODE **_cpp_lookup_slot	PARAMS ((cpp_reader *,
===================================================================
Index: cppinit.c
--- cppinit.c	2000/03/16 14:15:17	1.65
+++ cppinit.c	2000/03/28 21:35:14
@@ -660,7 +660,8 @@ initialize_builtins (pfile)
 	val = b->value;
 
       len = strlen (b->name);
-      hp = _cpp_make_hashnode (b->name, len, b->type, -1);
+      hp = _cpp_make_hashnode (b->name, len, b->type,
+			       _cpp_calc_hash (b->name, len));
       hp->value.cpval = val;
       *(htab_find_slot (pfile->hashtab, (void *)hp, 1)) = hp;
 
===================================================================
Index: cpplib.c
--- cpplib.c	2000/03/14 06:34:11	1.134
+++ cpplib.c	2000/03/28 21:35:14
@@ -508,6 +508,8 @@ do_include (pfile)
   char *token;
 
   len = parse_include (pfile, dtable[T_INCLUDE].name);
+  if (len == 0)
+    return 0;
   token = alloca (len + 1);
   strcpy (token, CPP_PWRITTEN (pfile));
   
@@ -537,6 +539,8 @@ do_import (pfile)
     }
 
   len = parse_include (pfile, dtable[T_IMPORT].name);
+  if (len == 0)
+    return 0;
   token = alloca (len + 1);
   strcpy (token, CPP_PWRITTEN (pfile));
   
@@ -559,7 +563,8 @@ do_include_next (pfile)
     cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
   
   len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
-
+  if (len == 0)
+    return 0;
   token = alloca (len + 1);
   strcpy (token, CPP_PWRITTEN (pfile));
   


More information about the Gcc-patches mailing list