diff mbox

[pph] Fix GC problems with symbol table (issue5532112)

Message ID 20120120225755.D7925AE1E8@tobiano.tor.corp.google.com
State New
Headers show

Commit Message

Diego Novillo Jan. 20, 2012, 10:57 p.m. UTC
This is a bit of a hack.  All the PPH data is short-lived, and explicitly
managed on the heap.  However, we store some GC'd objects in them and the
parser will sometimes run garbage collection.

This was causing problems when we tried to write out the symbol table.
Some trees in it had been GC'd out because the symbol table is not a
GC root (it's in the heap).

What we should really do is realize when these trees are discarded, so
they can be removed from the symbol table.  But, the simplest approach
here is to put them in a GC root vector until we are done with PPH
(thanks Tom for the suggestion).

	* config-lang.in (gtfiles): Remove pph.h and pph-core.c.
	Add pph-out.c
	* pph-out.c: Include gt-cp-pph-out.h.
	(pph_decls_in_symtab): Declare.
	(pph_writer_init): Initialize.
	(pph_add_decl_to_symtab): Add DECL to pph_decls_in_symtab.
	(pph_writer_finish): Set pph_decls_in_symtab and call
	ggc_collect.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/pph@183357 138bc75d-0d04-0410-961f-82ee72b054a4
---
 gcc/cp/ChangeLog.pph  |   11 +++++++++++
 gcc/cp/config-lang.in |    2 +-
 gcc/cp/pph-out.c      |   22 ++++++++++++++++++++++
 3 files changed, 34 insertions(+), 1 deletions(-)
diff mbox

Patch

diff --git a/gcc/cp/ChangeLog.pph b/gcc/cp/ChangeLog.pph
index 8f89273..8a2df86 100644
--- a/gcc/cp/ChangeLog.pph
+++ b/gcc/cp/ChangeLog.pph
@@ -1,3 +1,14 @@ 
+2012-01-20   Diego Novillo  <dnovillo@google.com>
+
+	* config-lang.in (gtfiles): Remove pph.h and pph-core.c.
+	Add pph-out.c
+	* pph-out.c: Include gt-cp-pph-out.h.
+	(pph_decls_in_symtab): Declare.
+	(pph_writer_init): Initialize.
+	(pph_add_decl_to_symtab): Add DECL to pph_decls_in_symtab.
+	(pph_writer_finish): Set pph_decls_in_symtab and call
+	ggc_collect.
+
 2012-01-20   Lawrence Crowl  <crowl@google.com>
 
 	* parser.c (cp_debug_parser_where): Protect against null pointers.
diff --git a/gcc/cp/config-lang.in b/gcc/cp/config-lang.in
index 2487260..ab8e686 100644
--- a/gcc/cp/config-lang.in
+++ b/gcc/cp/config-lang.in
@@ -30,4 +30,4 @@  compilers="cc1plus\$(exeext)"
 
 target_libs="target-libstdc++-v3"
 
-gtfiles="\$(srcdir)/cp/rtti.c \$(srcdir)/cp/mangle.c \$(srcdir)/cp/name-lookup.h \$(srcdir)/cp/name-lookup.c \$(srcdir)/cp/cp-tree.h \$(srcdir)/cp/decl.h \$(srcdir)/cp/call.c \$(srcdir)/cp/decl.c \$(srcdir)/cp/decl2.c \$(srcdir)/cp/pt.c \$(srcdir)/cp/repo.c \$(srcdir)/cp/semantics.c \$(srcdir)/cp/tree.c \$(srcdir)/cp/parser.h \$(srcdir)/cp/parser.c \$(srcdir)/cp/method.c \$(srcdir)/cp/typeck2.c \$(srcdir)/c-family/c-common.c \$(srcdir)/c-family/c-common.h \$(srcdir)/c-family/c-objc.h \$(srcdir)/c-family/c-lex.c \$(srcdir)/c-family/c-pragma.h \$(srcdir)/c-family/c-pragma.c \$(srcdir)/cp/class.c \$(srcdir)/cp/cp-objcp-common.c \$(srcdir)/cp/cp-lang.c \$(srcdir)/cp/pph.h \$(srcdir)/cp/pph-core.c \$(srcdir)/cp/except.c"
+gtfiles="\$(srcdir)/cp/rtti.c \$(srcdir)/cp/mangle.c \$(srcdir)/cp/name-lookup.h \$(srcdir)/cp/name-lookup.c \$(srcdir)/cp/cp-tree.h \$(srcdir)/cp/decl.h \$(srcdir)/cp/call.c \$(srcdir)/cp/decl.c \$(srcdir)/cp/decl2.c \$(srcdir)/cp/pt.c \$(srcdir)/cp/repo.c \$(srcdir)/cp/semantics.c \$(srcdir)/cp/tree.c \$(srcdir)/cp/parser.h \$(srcdir)/cp/parser.c \$(srcdir)/cp/method.c \$(srcdir)/cp/typeck2.c \$(srcdir)/c-family/c-common.c \$(srcdir)/c-family/c-common.h \$(srcdir)/c-family/c-objc.h \$(srcdir)/c-family/c-lex.c \$(srcdir)/c-family/c-pragma.h \$(srcdir)/c-family/c-pragma.c \$(srcdir)/cp/class.c \$(srcdir)/cp/cp-objcp-common.c \$(srcdir)/cp/cp-lang.c \$(srcdir)/cp/except.c \$(srcdir)/cp/pph-out.c"
diff --git a/gcc/cp/pph-out.c b/gcc/cp/pph-out.c
index e80ad7b..6e8eafd 100644
--- a/gcc/cp/pph-out.c
+++ b/gcc/cp/pph-out.c
@@ -52,6 +52,19 @@  static void pph_out_merge_key_tree (pph_stream *, tree);
 static pph_stream *pph_out_stream = NULL;
 
 
+/* List of all the symbols added to the PPH symbol table.
+
+   FIXME pph, the *only* purpose of this list is to act as a GC root
+   to avoid these DECLs from being garbage collected.
+
+   The parser will invoke garbage collection, which may clobber some
+   DECLs in the symbol table (because the symbol table is stored on
+   the heap).  Ideally, we would notice when DECLs go unused, but for
+   now this will just keep these DECLs around until we finalize PPH
+   processing.  */
+static GTY(()) VEC(tree,gc) *pph_decls_in_symtab;
+
+
 /* Initialize buffers and tables in STREAM for writing.  */
 
 void
@@ -81,6 +94,7 @@  pph_writer_init (void)
   pph_out_stream = pph_stream_open (pph_out_file, "wb");
   if (pph_out_stream == NULL)
     fatal_error ("Cannot open PPH file %s for writing: %m", pph_out_file);
+  pph_decls_in_symtab = NULL;
 }
 
 
@@ -2503,6 +2517,7 @@  pph_add_decl_to_symtab (tree decl, enum pph_symtab_action action,
   entry.top_level = top_level;
   entry.at_end = at_end;
   VEC_safe_push (pph_symtab_entry, heap, pph_out_stream->symtab.v, &entry);
+  VEC_safe_push (tree, gc, pph_decls_in_symtab, decl);
 }
 
 
@@ -2787,6 +2802,11 @@  pph_writer_finish (void)
 
   pph_stream_close (pph_out_stream);
   pph_out_stream = NULL;
+
+  /* Get rid of all the dead DECLs we may have had in the symbol
+     table.  */
+  pph_decls_in_symtab = NULL;
+  ggc_collect ();
 }
 
 
@@ -2814,3 +2834,5 @@  pph_disable_output (void)
   pph_stream_close_no_flush (pph_out_stream);
   pph_out_file = NULL;
 }
+
+#include "gt-cp-pph-out.h"