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]

cppfiles.c: Fix buglets; more cleanup


With this patch, cppfiles.c is starting to look reasonable (to
me at least) in that most paths through it are straight and
obvious.  They may well have been before, but not to me :-)

First, we had a bug where we would complain about there not
being an include path even when including an absolute filename.
Second, _cpp_compare_file_date () was doing some search logic
that was duplicated in _cpp_execute_include ().  Making this
common in find_include_file () also means that ccfd gets a
diagnostic about an empty include chain that it was missing
otherwise.  Third, we had "regressed" a little from cccp and
cpplib of 18 months ago in that a #include_next with no
subsequent remaining include chain didn't produce a diagnostic.

This fixes all these, mainly by clarifying the logic and sharing
code.  Bootstrapped x86 Linux; for mainline.

Things left to do in cppfiles.c: fix filename remapping, which has
a couple of bugs, fix a misleading diagnostic, and hopefully
get -include and -imacros using some kind of include chain
in addition to just the compiler's cwd.

Neil.

	* cppfiles.c (NO_INCLUDE_PATH): New macro.
	(find_include_file): Decide here which part of the include
	chain to start the search.  Complain about an empty include
	chain in all cases apart from an abolsute file name.
	(_cpp_execute_include): Don't choose the search chain here.
	Don't call handle_missing_include in the case of an empty
	include chain.
	(_cpp_compare_file_date): Don't choose the search chain here.
	* cpplib.c (do_include_next): Move the in-main-file diagnostic
	here from _cpp_execute_include.  Behave like #include if
	we're in the main file.
	
Index: gcc/cppfiles.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppfiles.c,v
retrieving revision 1.110
diff -u -p -r1.110 cppfiles.c
--- cppfiles.c	2001/03/12 19:33:08	1.110
+++ cppfiles.c	2001/03/13 19:29:02
@@ -82,6 +82,7 @@ struct include_file
 #define DO_NOT_REREAD(inc) \
 ((inc)->cmacro && ((inc)->cmacro == NEVER_REREAD \
 		   || ((inc)->cmacro->type == NT_MACRO) == (inc)->defined))
+#define NO_INCLUDE_PATH ((struct include_file *) -1)
 
 static struct file_name_map *read_name_map
 				PARAMS ((cpp_reader *, const char *));
@@ -90,9 +91,8 @@ static char *remap_filename 	PARAMS ((cp
 					 struct search_path *));
 static struct search_path *search_from PARAMS ((cpp_reader *,
 						struct include_file *));
-static struct include_file *find_include_file
-				PARAMS ((cpp_reader *, const char *,
-					 struct search_path *));
+static struct include_file *
+	find_include_file PARAMS ((cpp_reader *, const cpp_token *, int));
 static struct include_file *open_file PARAMS ((cpp_reader *, const char *));
 static void read_include_file	PARAMS ((cpp_reader *, struct include_file *));
 static void stack_include_file	PARAMS ((cpp_reader *, struct include_file *));
@@ -487,26 +487,46 @@ cpp_included (pfile, fname)
   return 0;
 }
 
-/* Search for include file FNAME in the include chain starting at
-   SEARCH_START.  Return 0 if there is no such file (or it's un-openable),
-   otherwise an include_file structure.  */
+/* Search for HEADER.  Return 0 if there is no such file (or it's
+   un-openable), in which case an error code will be in errno.  If
+   there is no include path to use it returns NO_INCLUDE_PATH,
+   otherwise an include_file structure.  If this request originates
+   from a #include_next directive, set INCLUDE_NEXT to true.  */
 
 static struct include_file *
-find_include_file (pfile, fname, search_start)
+find_include_file (pfile, header, include_next)
      cpp_reader *pfile;
-     const char *fname;
-     struct search_path *search_start;
+     const cpp_token *header;
+     int include_next;
 {
+  const char *fname = (const char *) header->val.str.text;
   struct search_path *path;
-  char *name;
   struct include_file *file;
+  char *name;
 
   if (IS_ABSOLUTE_PATHNAME (fname))
     return open_file (pfile, fname);
-      
+
+  /* For #include_next, skip in the search path past the dir in which
+     the current file was found.  If this is the last directory in the
+     search path, don't include anything.  If the current file was
+     specified with an absolute path, use the normal search logic.  */
+  if (include_next && pfile->buffer->inc->foundhere)
+    path = pfile->buffer->inc->foundhere->next;
+  else if (header->type == CPP_HEADER_NAME)
+    path = CPP_OPTION (pfile, bracket_include);
+  else
+    path = pfile->buffer->search_from;
+
+  if (path == NULL)
+    {
+      cpp_error (pfile, "No include path in which to find %s", fname);
+      return NO_INCLUDE_PATH;
+    }
+
   /* Search directory path for the file.  */
   name = (char *) alloca (strlen (fname) + pfile->max_include_len + 2);
-  for (path = search_start; path; path = path->next)
+  for (; path; path = path->next)
     {
       memcpy (name, path->name, path->len);
       name[path->len] = '/';
@@ -631,9 +651,6 @@ _cpp_execute_include (pfile, header, no_
      int no_reinclude;
      int include_next;
 {
-  struct search_path *search_start = 0;
-  unsigned int angle_brackets = header->type == CPP_HEADER_NAME;
-  const char *fname = (const char *) header->val.str.text;
   struct include_file *inc;
 
   /* Help protect #include or similar from recursion.  */
@@ -650,45 +667,14 @@ _cpp_execute_include (pfile, header, no_
       return;
     }
 
-  /* For #include_next, skip in the search path past the dir in which
-     the current file was found.  If this is the last directory in the
-     search path, don't include anything.  If the current file was
-     specified with an absolute path, use the normal search logic.  If
-     this is the primary source file, use the normal search logic and
-     generate a warning.  */
-  if (include_next)
-    {
-      if (! pfile->buffer->prev)
-	cpp_warning (pfile, "#include_next in primary source file");
-      else
-	{
-	  if (pfile->buffer->inc->foundhere)
-	    {
-	      search_start = pfile->buffer->inc->foundhere->next;
-	      if (! search_start)
-		return;
-	    }
-	}
-    }
+  inc = find_include_file (pfile, header, include_next);
 
-  if (!search_start)
+  if (inc == 0)
+    handle_missing_header (pfile, (const char *) header->val.str.text,
+			   header->type == CPP_HEADER_NAME);
+  else if (inc != NO_INCLUDE_PATH)
     {
-      if (angle_brackets)
-	search_start = CPP_OPTION (pfile, bracket_include);
-      else
-	search_start = pfile->buffer->search_from;
-
-      if (!search_start)
-	{
-	  cpp_error (pfile, "No include path in which to find %s", fname);
-	  return;
-	}
-    }
-
-  inc = find_include_file (pfile, fname, search_start);
-  if (inc)
-    {
-      if (angle_brackets)
+      if (header->type == CPP_HEADER_NAME)
 	pfile->system_include_depth++;
 
       stack_include_file (pfile, inc);
@@ -708,37 +694,28 @@ _cpp_execute_include (pfile, header, no_
 	    }
 	}
     }
-  else
-    handle_missing_header (pfile, fname, angle_brackets);
 }
 
-/* Locate file F, and determine whether it is newer than PFILE. Return -1,
-   if F cannot be located or dated, 1, if it is newer and 0 if older.  */
+/* Locate HEADER, and determine whether it is newer than the current
+   file.  If it cannot be located or dated, return -1, if it is newer
+   newer, return 1, otherwise 0.  */
 int
-_cpp_compare_file_date (pfile, f)
+_cpp_compare_file_date (pfile, header)
      cpp_reader *pfile;
-     const cpp_token *f;
+     const cpp_token *header;
 {
-  const char *fname = (const char *) f->val.str.text;
-  struct search_path *search_start;
-  struct include_file *inc;
-
-  if (f->type == CPP_HEADER_NAME)
-    search_start = CPP_OPTION (pfile, bracket_include);
-  else
-    search_start = pfile->buffer->search_from;
-
-  inc = find_include_file (pfile, fname, search_start);
+  struct include_file *inc = find_include_file (pfile, header, 0);
   
-  if (!inc)
+  if (inc == NULL || inc == NO_INCLUDE_PATH)
     return -1;
+
   if (inc->fd > 0)
     {
       close (inc->fd);
       inc->fd = -1;
     }
     
-  return inc->st.st_mtime > CPP_BUFFER (pfile)->inc->st.st_mtime;
+  return inc->st.st_mtime > pfile->buffer->inc->st.st_mtime;
 }
Index: gcc/cpplib.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.c,v
retrieving revision 1.242
diff -u -p -r1.242 cpplib.c
--- cpplib.c	2001/03/04 12:02:02	1.242
+++ cpplib.c	2001/03/13 19:29:09
@@ -619,7 +619,14 @@ do_include_next (pfile)
   cpp_token header;
 
   if (!parse_include (pfile, &header))
-    _cpp_execute_include (pfile, &header, 0, 1);
+    {
+      /* If this is the primary source file, warn and use the normal
+	 search logic.  */
+      if (! pfile->buffer->prev)
+	cpp_warning (pfile, "#include_next in primary source file");
+
+      _cpp_execute_include (pfile, &header, 0, pfile->buffer->prev != 0);
+    }
 }
 
 /* Subroutine of do_line.  Read possible flags after file name.  LAST


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