From a73b8b59fb0c47fdf8a31f812867ec56744ec6a4 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Thu, 19 Jul 2012 16:02:08 -0400 Subject: [PATCH] re PR c++/54021 ([c++0x] __builtin_constant_p should be constexpr) PR c++/54021 * call.c (build_cxx_call): Set optimize when folding __builtin_constant_p in a constexpr function. From-SVN: r189677 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/call.c | 10 +++++++++- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/cpp0x/constexpr-builtin2.C | 16 ++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-builtin2.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9a3c65604ad2..649b6561ca8d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2012-07-19 Jason Merrill + + PR c++/54021 + * call.c (build_cxx_call): Set optimize when folding + __builtin_constant_p in a constexpr function. + 2012-07-18 Jason Merrill * pt.c (instantiate_decl): Don't recheck substitutions. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 5b3245ffa4e4..cf50e88e132e 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -6900,6 +6900,7 @@ tree build_cxx_call (tree fn, int nargs, tree *argarray) { tree fndecl; + int optimize_sav; /* Remember roughly where this call is. */ location_t loc = EXPR_LOC_OR_HERE (fn); @@ -6916,8 +6917,15 @@ build_cxx_call (tree fn, int nargs, tree *argarray) return error_mark_node; /* Some built-in function calls will be evaluated at compile-time in - fold (). */ + fold (). Set optimize to 1 when folding __builtin_constant_p inside + a constexpr function so that fold_builtin_1 doesn't fold it to 0. */ + optimize_sav = optimize; + if (!optimize && fndecl && DECL_IS_BUILTIN_CONSTANT_P (fndecl) + && current_function_decl + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) + optimize = 1; fn = fold_if_not_in_template (fn); + optimize = optimize_sav; if (VOID_TYPE_P (TREE_TYPE (fn))) return fn; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e7aac5f5f08d..c406d7ecaac6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2012-07-19 Jason Merrill + + PR c++/54021 + * g++.dg/cpp0x/constexpr-builtin2.C: New. + 2012-07-19 Eric Botcazou * gnat.dg/discr38.adb: New test. diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-builtin2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-builtin2.C new file mode 100644 index 000000000000..dde38f05acde --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-builtin2.C @@ -0,0 +1,16 @@ +// PR c++/54021 +// { dg-do compile { target c++11 } } + +extern int nonconst_func(int); +constexpr int identity(int x) { return x; } +constexpr int zero() { return identity(0); } +constexpr int one() { return identity(1); } + +// These are the same. Only the latter is accepted, though. +constexpr int rejected_const_4(int x) +{ return __builtin_constant_p(x) ? 4 : nonconst_func(x); } +constexpr int accepted_const_4(int x) +{ return identity(__builtin_constant_p(x)) ? 4 : nonconst_func(x); } + +// This is rejected. I would like it to work. +constexpr int four = accepted_const_4(1); -- 2.39.5