From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] c++: Fix up constexpr handling of char/signed char/short pre/post inc/decrement [PR105774]
Date: Mon, 24 Oct 2022 09:27:25 +0200 [thread overview]
Message-ID: <Y1Y+XfMW7lkamX2r@tucnak> (raw)
Hi!
signed char, char or short int pre/post inc/decrement are represented by
normal {PRE,POST}_{INC,DEC}REMENT_EXPRs in the FE and only gimplification
ensures that the {PLUS,MINUS}_EXPR is done in unsigned version of those
types:
case PREINCREMENT_EXPR:
case PREDECREMENT_EXPR:
case POSTINCREMENT_EXPR:
case POSTDECREMENT_EXPR:
{
tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
{
if (!TYPE_OVERFLOW_WRAPS (type))
type = unsigned_type_for (type);
return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
}
break;
}
This means during constant evaluation we need to do it similarly (either
using unsigned_type_for or using widening to integer_type_node).
The following patch does the latter.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2022-10-24 Jakub Jelinek <jakub@redhat.com>
PR c++/105774
* constexpr.cc (cxx_eval_increment_expression): For signed types
that promote to int, evaluate PLUS_EXPR or MINUS_EXPR in int type.
* g++.dg/cpp1y/constexpr-105774.C: New test.
--- gcc/cp/constexpr.cc.jj 2022-10-21 18:04:47.869797312 +0200
+++ gcc/cp/constexpr.cc 2022-10-23 18:43:27.003390282 +0200
@@ -6234,6 +6234,18 @@ cxx_eval_increment_expression (const con
offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
}
+ else if (c_promoting_integer_type_p (type)
+ && !TYPE_UNSIGNED (type)
+ && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
+ {
+ offset = fold_convert (integer_type_node, offset);
+ mod = fold_convert (integer_type_node, val);
+ tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
+ mod, offset);
+ mod = fold_convert (type, t);
+ if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
+ TREE_OVERFLOW (mod) = false;
+ }
else
mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
if (!ptr)
--- gcc/testsuite/g++.dg/cpp1y/constexpr-105774.C.jj 2022-10-23 18:44:15.587729613 +0200
+++ gcc/testsuite/g++.dg/cpp1y/constexpr-105774.C 2022-10-23 18:33:54.754170726 +0200
@@ -0,0 +1,15 @@
+// PR c++/105774
+// { dg-do compile { target c++14 } }
+
+constexpr signed char
+foo ()
+{
+#if __SCHAR_MAX__ < __INT_MAX__
+ signed char x = __SCHAR_MAX__;
+#else
+ signed char x = 0;
+#endif
+ return ++x;
+}
+
+constexpr auto a = foo ();
Jakub
next reply other threads:[~2022-10-24 7:27 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-24 7:27 Jakub Jelinek [this message]
2022-10-24 14:07 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y1Y+XfMW7lkamX2r@tucnak \
--to=jakub@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).