diff options
author | Jason Merrill <jason@redhat.com> | 2012-07-19 16:02:08 -0400 |
---|---|---|
committer | Jason Merrill <jason@gcc.gnu.org> | 2012-07-19 16:02:08 -0400 |
commit | a73b8b59fb0c47fdf8a31f812867ec56744ec6a4 (patch) | |
tree | 3930abc2d16c552b405d6e4d116fc01675144f2c | |
parent | e1310984b5ea59b3478b7a37d008d1f16c1effde (diff) | |
download | gcc-a73b8b59fb0c47fdf8a31f812867ec56744ec6a4.zip gcc-a73b8b59fb0c47fdf8a31f812867ec56744ec6a4.tar.gz gcc-a73b8b59fb0c47fdf8a31f812867ec56744ec6a4.tar.bz2 |
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
-rw-r--r-- | gcc/cp/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/cp/call.c | 10 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp0x/constexpr-builtin2.C | 16 |
4 files changed, 36 insertions, 1 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9a3c656..649b656 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2012-07-19 Jason Merrill <jason@redhat.com> + + PR c++/54021 + * call.c (build_cxx_call): Set optimize when folding + __builtin_constant_p in a constexpr function. + 2012-07-18 Jason Merrill <jason@redhat.com> * pt.c (instantiate_decl): Don't recheck substitutions. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 5b3245f..cf50e88 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 e7aac5f..c406d7e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2012-07-19 Jason Merrill <jason@redhat.com> + + PR c++/54021 + * g++.dg/cpp0x/constexpr-builtin2.C: New. + 2012-07-19 Eric Botcazou <ebotcazou@adacore.com> * 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 0000000..dde38f0 --- /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); |