* [C++ PATCH] Diagnose constexpr overflow (PR c++/70323)
@ 2016-03-22 21:37 Jakub Jelinek
2016-03-23 18:57 ` Jason Merrill
0 siblings, 1 reply; 3+ messages in thread
From: Jakub Jelinek @ 2016-03-22 21:37 UTC (permalink / raw)
To: Jason Merrill; +Cc: gcc-patches
Hi!
On the following testcase, the first function is cp_folded into
return i == 0 ? 2147483648(OVF): 2147483647;
The problem is that we don't diagnose then the overflow at all.
We already have code that sets *overflow_p under right conditions,
just there wasn't any permerror call.
Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?
2016-03-22 Jakub Jelinek <jakub@redhat.com>
PR c++/70323
* constexpr.c (cxx_eval_constant_expression): Diagnose overflow
on TREE_OVERFLOW constants.
* g++.dg/cpp0x/constexpr-70323.C: New test.
--- gcc/cp/constexpr.c.jj 2016-03-22 09:05:32.000000000 +0100
+++ gcc/cp/constexpr.c 2016-03-22 10:38:58.598077573 +0100
@@ -3306,8 +3306,13 @@ cxx_eval_constant_expression (const cons
}
if (CONSTANT_CLASS_P (t))
{
- if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
- *overflow_p = true;
+ if (TREE_OVERFLOW (t))
+ {
+ if (!ctx->quiet)
+ permerror (input_location, "overflow in constant expression");
+ if (!flag_permissive || ctx->quiet)
+ *overflow_p = true;
+ }
return t;
}
--- gcc/testsuite/g++.dg/cpp0x/constexpr-70323.C.jj 2016-03-22 10:42:54.093884158 +0100
+++ gcc/testsuite/g++.dg/cpp0x/constexpr-70323.C 2016-03-22 10:42:29.000000000 +0100
@@ -0,0 +1,10 @@
+// PR c++/70323
+// { dg-do compile { target c++11 } }
+
+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" }
Jakub
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [C++ PATCH] Diagnose constexpr overflow (PR c++/70323)
2016-03-22 21:37 [C++ PATCH] Diagnose constexpr overflow (PR c++/70323) Jakub Jelinek
@ 2016-03-23 18:57 ` Jason Merrill
2016-03-24 18:25 ` Jason Merrill
0 siblings, 1 reply; 3+ messages in thread
From: Jason Merrill @ 2016-03-23 18:57 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
For GCC 7 we should do constexpr evaluation on the unfolded function,
but for GCC 6 this is OK.
Jason
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [C++ PATCH] Diagnose constexpr overflow (PR c++/70323)
2016-03-23 18:57 ` Jason Merrill
@ 2016-03-24 18:25 ` Jason Merrill
0 siblings, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2016-03-24 18:25 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
[-- Attachment #1: Type: text/plain, Size: 233 bytes --]
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.
[-- Attachment #2: 70323.patch --]
[-- Type: text/x-patch, Size: 1386 bytes --]
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" }
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-03-24 17:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-22 21:37 [C++ PATCH] Diagnose constexpr overflow (PR c++/70323) Jakub Jelinek
2016-03-23 18:57 ` Jason Merrill
2016-03-24 18:25 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).