Patch: Fix PR c/29172

Tom Tromey tromey@redhat.com
Fri Nov 16 04:51:00 GMT 2007


This fixes PR c/29172.

The bug here is that --combine does not clear the cpp file cache
between compiles.  This means that #pragma once does the wrong thing
in the second compile.

The patch fixes this by adding a libccp API to clear the file cache.

Note that while writing this I found a few memory leaks in this part
of libcpp.  This patch fixes these as well.  libcpp uses a memory pool
for file hash entries.  I don't know if there is a particular reason
for this, but I kept it out of conservatism.  Now if we need multiple
pools they are linked together so they can actually be freed.

Bootstrapped and regtested on x86 FC-6.  Ok?

Tom

gcc/ChangeLog:
2007-11-15  Tom Tromey  <tromey@redhat.com>

	PR c/29172:
	* c-opts.c (c_common_parse_file): Call cpp_clear_file_cache.

libcpp/ChangeLog:
2007-11-15  Tom Tromey  <tromey@redhat.com>

	PR c/29172:
	* internal.h (struct cpp_reader) <file_hash_entries>: Changed
	type.
	<file_hash_entries_allocated, file_hash_entries_used>: Removed.
	* files.c (FILE_HASH_POOL_SIZE): New macro.
	(struct file_hash_entry_pool): New.
	(destroy_all_cpp_files): New function.
	(allocate_file_hash_entries): Allocate a file_hash_entry_pool.
	(new_file_hash_entry): Update.
	(free_file_hash_entries): New function.
	(_cpp_cleanup_files): Call free_file_hash_entries and
	destroy_all_cpp_files.
	(cpp_clear_file_cache): New function.
	* include/cpplib.h (cpp_clear_file_cache): Declare.

Index: gcc/c-opts.c
===================================================================
--- gcc/c-opts.c	(revision 130212)
+++ gcc/c-opts.c	(working copy)
@@ -1281,6 +1281,7 @@
       if (++i >= num_in_fnames)
 	break;
       cpp_undef_all (parse_in);
+      cpp_clear_file_cache (parse_in);
       this_input_filename
 	= cpp_read_main_file (parse_in, in_fnames[i]);
       /* If an input file is missing, abandon further compilation.
Index: libcpp/include/cpplib.h
===================================================================
--- libcpp/include/cpplib.h	(revision 130208)
+++ libcpp/include/cpplib.h	(working copy)
@@ -862,6 +862,7 @@
 extern cpp_buffer *cpp_get_buffer (cpp_reader *);
 extern struct _cpp_file *cpp_get_file (cpp_buffer *);
 extern cpp_buffer *cpp_get_prev (cpp_buffer *);
+extern void cpp_clear_file_cache (cpp_reader *);
 
 /* In cpppch.c */
 struct save_macro_data;
Index: libcpp/files.c
===================================================================
--- libcpp/files.c	(revision 130208)
+++ libcpp/files.c	(working copy)
@@ -150,6 +150,21 @@
   } u;
 };
 
+/* Number of entries to put in a file_hash_entry pool.  */
+#define FILE_HASH_POOL_SIZE 127
+
+/* A file hash entry pool.  We allocate file_hash_entry object from
+   one of these.  */
+struct file_hash_entry_pool
+{
+  /* Number of entries used from this pool.  */
+  unsigned int file_hash_entries_used;
+  /* Next pool in the chain; used when freeing.  */
+  struct file_hash_entry_pool *next;
+  /* The memory pool.  */
+  struct file_hash_entry pool[FILE_HASH_POOL_SIZE];
+};
+
 static bool open_file (_cpp_file *file);
 static bool pch_open_file (cpp_reader *pfile, _cpp_file *file,
 			   bool *invalid_pch);
@@ -963,6 +978,19 @@
   free (file);
 }
 
+/* Release all the files allocated by this reader.  */
+static void
+destroy_all_cpp_files (cpp_reader *pfile)
+{
+  _cpp_file *iter = pfile->all_files;
+  while (iter)
+    {
+      _cpp_file *next = iter->next_file;
+      destroy_cpp_file (iter);
+      iter = next;
+    }
+}
+
 /* A hash of directory names.  The directory names are the path names
    of files which contain a #include "", the included file name is
    appended to this directories.
@@ -1008,22 +1036,37 @@
 static void
 allocate_file_hash_entries (cpp_reader *pfile)
 {
-  pfile->file_hash_entries_used = 0;
-  pfile->file_hash_entries_allocated = 127;
-  pfile->file_hash_entries = XNEWVEC (struct file_hash_entry,
-                                      pfile->file_hash_entries_allocated);
+  struct file_hash_entry_pool *pool = XNEW (struct file_hash_entry_pool);
+  pool->file_hash_entries_used = 0;
+  pool->next = pfile->file_hash_entries;
+  pfile->file_hash_entries = pool;
 }
 
 /* Return a new file hash entry.  */
 static struct file_hash_entry *
 new_file_hash_entry (cpp_reader *pfile)
 {
-  if (pfile->file_hash_entries_used == pfile->file_hash_entries_allocated)
+  unsigned int idx;
+  if (pfile->file_hash_entries->file_hash_entries_used == FILE_HASH_POOL_SIZE)
     allocate_file_hash_entries (pfile);
 
-  return &pfile->file_hash_entries[pfile->file_hash_entries_used++];
+  idx = pfile->file_hash_entries->file_hash_entries_used++;
+  return &pfile->file_hash_entries->pool[idx];
 }
 
+/* Free the file hash entry pools.  */
+static void
+free_file_hash_entries (cpp_reader *pfile)
+{
+  struct file_hash_entry_pool *iter = pfile->file_hash_entries;
+  while (iter)
+    {
+      struct file_hash_entry_pool *next = iter->next;
+      free (iter);
+      iter = next;
+    }
+}
+
 /* Returns TRUE if a file FNAME has ever been successfully opened.
    This routine is not intended to correctly handle filenames aliased
    by links or redundant . or .. traversals etc.  */
@@ -1124,8 +1167,21 @@
   htab_delete (pfile->dir_hash);
   htab_delete (pfile->nonexistent_file_hash);
   obstack_free (&pfile->nonexistent_file_ob, 0);
+  free_file_hash_entries (pfile);
+  destroy_all_cpp_files (pfile);
 }
 
+/* Make the parser forget about files it has seen.  This can be useful
+   for resetting the parser to start another run.  */
+void
+cpp_clear_file_cache (cpp_reader *pfile)
+{
+  _cpp_cleanup_files (pfile);
+  pfile->file_hash_entries = NULL;
+  pfile->all_files = NULL;
+  _cpp_init_files (pfile);
+}
+
 /* Enter a file name in the hash for the sake of cpp_included.  */
 void
 _cpp_fake_include (cpp_reader *pfile, const char *fname)
Index: libcpp/internal.h
===================================================================
--- libcpp/internal.h	(revision 130208)
+++ libcpp/internal.h	(working copy)
@@ -360,8 +360,7 @@
   /* File and directory hash table.  */
   struct htab *file_hash;
   struct htab *dir_hash;
-  struct file_hash_entry *file_hash_entries;
-  unsigned int file_hash_entries_allocated, file_hash_entries_used;
+  struct file_hash_entry_pool *file_hash_entries;
 
   /* Negative path lookup hash table.  */
   struct htab *nonexistent_file_hash;



More information about the Gcc-patches mailing list