This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C++ PATCH] Diagnose constexpr overflow (PR c++/70323)
- From: Jason Merrill <jason at redhat dot com>
- To: Jakub Jelinek <jakub at redhat dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Thu, 24 Mar 2016 13:59:30 -0400
- Subject: Re: [C++ PATCH] Diagnose constexpr overflow (PR c++/70323)
- Authentication-results: sourceware.org; auth=none
- References: <20160322213133 dot GP3017 at tucnak dot redhat dot com> <56F2E1B9 dot 6040107 at redhat dot com>
On 03/23/2016 02:34 PM, Jason Merrill wrote:
For GCC 7 we should do constexpr evaluation on the unfolded function,
but for GCC 6 this is OK.
And here's a fix for the -Wall case.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 75f153ad9c455c7f2340b6da6791e5a9a0787a8e
Author: Jason Merrill <jason@redhat.com>
Date: Thu Mar 24 12:53:32 2016 -0400
PR c++/70323
* constexpr.c (cxx_eval_call_expression): Don't cache result if
*overflow_p.
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 8427513..2d30a84 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -1448,7 +1448,7 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
if (result == error_mark_node)
*non_constant_p = true;
- if (*non_constant_p)
+ if (*non_constant_p || *overflow_p)
result = error_mark_node;
else if (!result)
result = void_node;
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-70323a.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-70323a.C
new file mode 100644
index 0000000..d166787
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-70323a.C
@@ -0,0 +1,11 @@
+// PR c++/70323
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wall" }
+
+constexpr int overflow_if_0 (int i) { return __INT_MAX__ + !i; }
+constexpr int overflow_if_1 (int i) { return __INT_MAX__ + i; }
+
+constexpr bool i0_0 = overflow_if_0 (0); // { dg-error "overflow in constant expression" }
+constexpr bool i0_1 = overflow_if_0 (1);
+constexpr bool i1_0 = overflow_if_1 (0);
+constexpr bool i1_1 = overflow_if_1 (1); // { dg-error "overflow in constant expression" }