From 35046a541ccc59e03c11de3fded8bc0ff7b7a396 Mon Sep 17 00:00:00 2001 From: Kriang Lerdsuwanakij Date: Mon, 16 Aug 2004 14:29:27 +0000 Subject: [PATCH] re PR c++/6749 (infinite loop with inheritance of abstract classes) PR c++/6749 * pt.c (instantiate_pending_templates): Add int parameter. Don't return anything. * cp-tree.h (instantiate_pending_templates): Adjust prototype. * decl2.c (finish_file): Adjust call to instantiate_pending_templates. * g++.dg/template/vtable2.C: New test. From-SVN: r86054 --- gcc/cp/ChangeLog | 9 +++++++ gcc/cp/cp-tree.h | 2 +- gcc/cp/decl2.c | 11 +++++--- gcc/cp/pt.c | 34 +++++++++++++++---------- gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/g++.dg/template/vtable2.C | 18 +++++++++++++ 6 files changed, 60 insertions(+), 19 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/vtable2.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 6e4ec464949..2946db75671 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,12 @@ +2004-08-16 Kriang Lerdsuwanakij + + PR c++/6749 + * pt.c (instantiate_pending_templates): Add int parameter. Don't + return anything. + * cp-tree.h (instantiate_pending_templates): Adjust prototype. + * decl2.c (finish_file): Adjust call to + instantiate_pending_templates. + 2004-08-15 Roger Sayle * call.c (build_vfield_ref, build_call, build_conditional_expr, diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index e2c94f25ed2..72011f7356f 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -3990,7 +3990,7 @@ extern void maybe_process_partial_specialization (tree); extern void maybe_check_template_type (tree); extern tree most_specialized_instantiation (tree); extern void print_candidates (tree); -extern int instantiate_pending_templates (void); +extern void instantiate_pending_templates (int); extern tree tsubst_default_argument (tree, tree, tree); extern tree tsubst_copy_and_build (tree, tree, tsubst_flags_t, tree, bool); extern tree most_general_template (tree); diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index ab83b982210..efb86b5534a 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -2721,6 +2721,7 @@ finish_file (void) size_t i; location_t locus; unsigned ssdf_count = 0; + int retries = 0; locus = input_location; at_eof = 1; @@ -2772,7 +2773,7 @@ finish_file (void) /* If there are templates that we've put off instantiating, do them now. */ - instantiate_pending_templates (); + instantiate_pending_templates (retries); ggc_collect (); /* Write out virtual tables as required. Note that writing out @@ -2780,7 +2781,7 @@ finish_file (void) instantiation of members of that class. If we write out vtables then we remove the class from our list so we don't have to look at it again. */ - + while (keyed_classes != NULL_TREE && maybe_emit_vtables (TREE_VALUE (keyed_classes))) { @@ -2806,14 +2807,14 @@ finish_file (void) next = TREE_CHAIN (t); } } - + /* Write out needed type info variables. We have to be careful looping through unemitted decls, because emit_tinfo_decl may cause other variables to be needed. We stick new elements (and old elements that we may need to reconsider) at the end of the array, then shift them back to the beginning once we're done. */ - + n_old = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls); for (i = 0; i < n_old; ++i) { @@ -2994,6 +2995,8 @@ finish_file (void) reconsider = true; if (cgraph_varpool_assemble_pending_decls ()) reconsider = true; + + retries++; } while (reconsider); diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 4590bd7dee2..d3d5267dcef 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -11272,17 +11272,30 @@ out: } /* Run through the list of templates that we wish we could - instantiate, and instantiate any we can. */ + instantiate, and instantiate any we can. RETRIES is the + number of times we retry pending template instantiation. */ -int -instantiate_pending_templates (void) +void +instantiate_pending_templates (int retries) { tree *t; tree last = NULL_TREE; - int instantiated_something = 0; int reconsider; location_t saved_loc = input_location; - + + /* Instantiating templates may trigger vtable generation. This in turn + may require further template instantiations. We place a limit here + to avoid infinite loop. */ + if (pending_templates && retries >= max_tinst_depth) + { + cp_error_at ("template instantiation depth exceeds maximum of %d" + " (use -ftemplate-depth-NN to increase the maximum)" + " instantiating `%+D', possibly from virtual table" + " generation", + max_tinst_depth, TREE_VALUE (pending_templates)); + return; + } + do { reconsider = 0; @@ -11309,10 +11322,7 @@ instantiate_pending_templates (void) instantiate_decl (fn, /*defer_ok=*/0, /*undefined_ok=*/0); if (COMPLETE_TYPE_P (instantiation)) - { - instantiated_something = 1; - reconsider = 1; - } + reconsider = 1; } if (COMPLETE_TYPE_P (instantiation)) @@ -11334,10 +11344,7 @@ instantiate_pending_templates (void) /*defer_ok=*/0, /*undefined_ok=*/0); if (DECL_TEMPLATE_INSTANTIATED (instantiation)) - { - instantiated_something = 1; - reconsider = 1; - } + reconsider = 1; } if (DECL_TEMPLATE_SPECIALIZATION (instantiation) @@ -11359,7 +11366,6 @@ instantiate_pending_templates (void) while (reconsider); input_location = saved_loc; - return instantiated_something; } /* Substitute ARGVEC into T, which is a list of initializers for diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 57780d9a457..6ef35f7b66e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2004-08-16 Kriang Lerdsuwanakij + + PR c++/6749 + * g++.dg/template/vtable2.C: New test. + 2004-08-14 Richard Henderson * gcc.dg/torture/builtin-attr-1.c: Fix scalbln prototype. diff --git a/gcc/testsuite/g++.dg/template/vtable2.C b/gcc/testsuite/g++.dg/template/vtable2.C new file mode 100644 index 00000000000..9f2bf0b2ff2 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/vtable2.C @@ -0,0 +1,18 @@ +// Use a small template instantiation depth to speed up testing +// { dg-options "-ftemplate-depth-5" } +// { dg-do compile } + +// Origin: rullo.pat@tiscalinet.it +// Nathanael Nerode +// Wolfgang Bangerth + +// PR c++/6749: Infinite loop generating vtable. + +template struct inner {}; + +template struct parent { + virtual void f() + { parent > p; }; // { dg-error "instantiation depth" } +}; + +template struct parent;