]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c++/54021 ([c++0x] __builtin_constant_p should be constexpr)
authorJason Merrill <jason@redhat.com>
Thu, 19 Jul 2012 20:02:08 +0000 (16:02 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Thu, 19 Jul 2012 20:02:08 +0000 (16:02 -0400)
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
gcc/cp/call.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/constexpr-builtin2.C [new file with mode: 0644]

index 9a3c65604ad2077c9e1e0f43adb4717f15f857d7..649b6561ca8d5ade316d2657d22830ae35e8c5b7 100644 (file)
@@ -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.
index 5b3245ffa4e41c709542cadb5cbd81b11645a851..cf50e88e132eb24c8a3908574d3322403cf06274 100644 (file)
@@ -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;
index e7aac5f5f08d0761ef104cb111a528301bedac70..c406d7ecaac6332b383406b8ac40f83d2a35cbef 100644 (file)
@@ -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 (file)
index 0000000..dde38f0
--- /dev/null
@@ -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);