diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 6b5fd91b0b6..a0268fd5b5b 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,10 @@ +2011-03-15 Jason Merrill + + * c-common.c (max_constexpr_depth): New. + * c-common.h: Declare it. + * c-opts.c (c_common_handle_option): Set it. + * c.opt (fconstexpr-depth): New option. + 2011-03-11 Jason Merrill * c-common.c (attribute_takes_identifier_p): Add missing const. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 4da9a2da0ca..6674c58ffab 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -255,12 +255,15 @@ int flag_use_repository; enum cxx_dialect cxx_dialect = cxx98; /* Maximum template instantiation depth. This limit exists to limit the - time it takes to notice infinite template instantiations; the default - value of 1024 is likely to be in the next C++ standard. */ + time it takes to notice excessively recursive template instantiations; + the default value of 1024 is likely to be in the next C++ standard. */ int max_tinst_depth = 1024; +/* Likewise, for constexpr function call evaluations. N3225 specifies a + minimum of 512. */ +int max_constexpr_depth = 512; /* The elements of `ridpointers' are identifier nodes for the reserved type names and storage classes. It is indexed by a RID_... value. */ diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 406def96e16..b6fcee9e2d6 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -619,10 +619,14 @@ extern enum cxx_dialect cxx_dialect; /* Maximum template instantiation depth. This limit is rather arbitrary, but it exists to limit the time it takes to notice - infinite template instantiations. */ + excessively recursive template instantiations. */ extern int max_tinst_depth; +/* Likewise, for constexpr function call evaluations. */ + +extern int max_constexpr_depth; + /* Nonzero means that we should not issue warnings about problems that occur when the code is executed, because the code being processed is not expected to be executed. This is set during parsing. This diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c index 8d6e6e7e243..8d2a08bad9f 100644 --- a/gcc/c-family/c-opts.c +++ b/gcc/c-family/c-opts.c @@ -568,6 +568,10 @@ c_common_handle_option (size_t scode, const char *arg, int value, disable_builtin_function (arg); break; + case OPT_fconstexpr_depth_: + max_constexpr_depth = value; + break; + case OPT_fdirectives_only: cpp_opts->directives_only = value; break; diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index bb928fa9146..f791190f9e8 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -719,6 +719,10 @@ fconstant-string-class= ObjC ObjC++ Joined MissingArgError(no class name specified with %qs) -fconst-string-class= Use class for constant strings +fconstexpr-depth= +C++ ObjC++ Joined RejectNegative UInteger +-constexpr-depth= Specify maximum constexpr recursion depth + fdeduce-init-list C++ ObjC++ Var(flag_deduce_init_list) Init(1) -fno-deduce-init-list disable deduction of std::initializer_list for a template type parameter from a brace-enclosed initializer-list diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 910a7e2bd94..09ab014df09 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,8 @@ 2011-03-15 Jason Merrill + * semantics.c (push_cx_call_context): Return bool. + (cxx_eval_call_expression): Handle excess depth. + Core 1191 * method.c (synthesized_method_walk): Cleanups don't affect the triviality of a constructor, but do affect deletion and exception diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index a0f48c0ed45..53497f39876 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -5922,17 +5922,21 @@ cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t, /* Variables and functions to manage constexpr call expansion context. These do not need to be marked for PCH or GC. */ +/* FIXME remember and print actual constant arguments. */ static VEC(tree,heap) *call_stack = NULL; static int call_stack_tick; static int last_cx_error_tick; -static void +static bool push_cx_call_context (tree call) { ++call_stack_tick; if (!EXPR_HAS_LOCATION (call)) SET_EXPR_LOCATION (call, input_location); VEC_safe_push (tree, heap, call_stack, call); + if (VEC_length (tree, call_stack) > (unsigned) max_constexpr_depth) + return false; + return true; } static void @@ -5967,6 +5971,9 @@ cxx_eval_call_expression (const constexpr_call *old_call, tree t, tree result; constexpr_call new_call = { NULL, NULL, NULL, 0 }; constexpr_call **slot; + constexpr_call *entry; + bool depth_ok; + if (TREE_CODE (fun) != FUNCTION_DECL) { /* Might be a constexpr function pointer. */ @@ -6029,7 +6036,7 @@ cxx_eval_call_expression (const constexpr_call *old_call, tree t, if (*non_constant_p) return t; - push_cx_call_context (t); + depth_ok = push_cx_call_context (t); new_call.hash = iterative_hash_template_arg (new_call.bindings, @@ -6039,37 +6046,43 @@ cxx_eval_call_expression (const constexpr_call *old_call, tree t, maybe_initialize_constexpr_call_table (); slot = (constexpr_call **) htab_find_slot (constexpr_call_table, &new_call, INSERT); - if (*slot != NULL) - { - /* Calls which are in progress have their result set to NULL - so that we can detect circular dependencies. */ - if ((*slot)->result == NULL) - { - if (!allow_non_constant) - error ("call has circular dependency"); - (*slot)->result = result = error_mark_node; - } - else - { - result = (*slot)->result; - if (result == error_mark_node && !allow_non_constant) - /* Re-evaluate to get the error. */ - cxx_eval_constant_expression (&new_call, new_call.fundef->body, - allow_non_constant, addr, - non_constant_p); - } - } - else + entry = *slot; + if (entry == NULL) { /* We need to keep a pointer to the entry, not just the slot, as the slot can move in the call to cxx_eval_builtin_function_call. */ - constexpr_call *entry = ggc_alloc_constexpr_call (); + *slot = entry = ggc_alloc_constexpr_call (); *entry = new_call; - *slot = entry; - result - = cxx_eval_constant_expression (&new_call, new_call.fundef->body, - allow_non_constant, addr, - non_constant_p); + } + /* Calls which are in progress have their result set to NULL + so that we can detect circular dependencies. */ + else if (entry->result == NULL) + { + if (!allow_non_constant) + error ("call has circular dependency"); + *non_constant_p = true; + entry->result = result = error_mark_node; + } + + if (!depth_ok) + { + if (!allow_non_constant) + error ("constexpr evaluation depth exceeds maximum of %d (use " + "-fconstexpr-depth= to increase the maximum)", + max_constexpr_depth); + *non_constant_p = true; + entry->result = result = error_mark_node; + } + else + { + result = entry->result; + if (!result || (result == error_mark_node && !allow_non_constant)) + result = (cxx_eval_constant_expression + (&new_call, new_call.fundef->body, + allow_non_constant, addr, + non_constant_p)); + if (result == error_mark_node) + *non_constant_p = true; if (*non_constant_p) entry->result = result = error_mark_node; else diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index aff5a058956..b498eaccc60 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -181,7 +181,7 @@ in the following sections. @item C++ Language Options @xref{C++ Dialect Options,,Options Controlling C++ Dialect}. @gccoptlist{-fabi-version=@var{n} -fno-access-control -fcheck-new @gol --fconserve-space -ffriend-injection @gol +-fconserve-space -fconstexpr-depth=@var{n} -ffriend-injection @gol -fno-elide-constructors @gol -fno-enforce-eh-specs @gol -ffor-scope -fno-for-scope -fno-gnu-keywords @gol @@ -1881,6 +1881,13 @@ two definitions were merged. This option is no longer useful on most targets, now that support has been added for putting variables into BSS without making them common. +@item -fconstexpr-depth=@var{n} +@opindex fconstexpr-depth +Set the maximum nested evaluation depth for C++0x constexpr functions +to @var{n}. A limit is needed to detect endless recursion during +constant expression evaluation. The minimum specified by the standard +is 512; G++ defaults to 1024. + @item -fno-deduce-init-list @opindex fno-deduce-init-list Disable deduction of a template type parameter as diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 3d75d5ebb14..09bdda97c5c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,7 @@ 2011-03-15 Jason Merrill + * g++.dg/cpp0x/constexpr-recursion.C: New. + * g++.dg/cpp0x/implicit11.C: New. 2011-03-15 Rodrigo Rivas Costa diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion.C new file mode 100644 index 00000000000..2f9b4887df6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion.C @@ -0,0 +1,5 @@ +// Test that we catch excessive recursion. +// { dg-options "-std=c++0x -fconstexpr-depth=5" } +// { dg-prune-output "in constexpr expansion" } +constexpr int f (int i) { return f (i-1); } +constexpr int i = f(42); // { dg-error "constexpr evaluation depth" }