From 988c115ca0fee432e5d6823a44e3289c0b73ad15 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Wed, 8 Jun 2016 14:57:26 +0200 Subject: [PATCH] re PR c++/71448 (pointer relational comparison fails inside constant expression) PR c++/71448 * fold-const.c (fold_comparison): Handle CONSTANT_CLASS_P (base0) the same as DECL_P (base0) for indirect_base0. Use equality_code in one further place. * g++.dg/torture/pr71448.C: New test. Co-Authored-By: Richard Biener From-SVN: r237212 --- gcc/ChangeLog | 8 ++++++++ gcc/fold-const.c | 9 +++++---- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/torture/pr71448.C | 27 ++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/torture/pr71448.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 6767537e9c9..6ad6ddc5b90 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2016-06-08 Jakub Jelinek + Richard Biener + + PR c++/71448 + * fold-const.c (fold_comparison): Handle CONSTANT_CLASS_P (base0) + the same as DECL_P (base0) for indirect_base0. Use equality_code + in one further place. + 2016-06-08 Richard Sandiford * expmed.c (store_bit_field_1): Do not restrict a multiword op0 diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 0efa9d5b0b1..0b7ea82c034 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8527,9 +8527,9 @@ fold_comparison (location_t loc, enum tree_code code, tree type, if ((offset0 == offset1 || (offset0 && offset1 && operand_equal_p (offset0, offset1, 0))) - && (code == EQ_EXPR - || code == NE_EXPR - || (indirect_base0 && DECL_P (base0)) + && (equality_code + || (indirect_base0 + && (DECL_P (base0) || CONSTANT_CLASS_P (base0))) || POINTER_TYPE_OVERFLOW_UNDEFINED)) { @@ -8568,7 +8568,8 @@ fold_comparison (location_t loc, enum tree_code code, tree type, 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */ else if (bitpos0 == bitpos1 && (equality_code - || (indirect_base0 && DECL_P (base0)) + || (indirect_base0 + && (DECL_P (base0) || CONSTANT_CLASS_P (base0))) || POINTER_TYPE_OVERFLOW_UNDEFINED)) { /* By converting to signed sizetype we cover middle-end pointer diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b40b6c015f9..d8b668cfaab 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-06-08 Jakub Jelinek + + PR c++/71448 + * g++.dg/torture/pr71448.C: New test. + 2016-06-08 Bernd Schmidt PR debug/71432 diff --git a/gcc/testsuite/g++.dg/torture/pr71448.C b/gcc/testsuite/g++.dg/torture/pr71448.C new file mode 100644 index 00000000000..ca00ca83b36 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr71448.C @@ -0,0 +1,27 @@ +// PR c++/71448 +// { dg-do compile } +// { dg-additional-options "-std=c++11" } + +static constexpr const char foo[] = "foo"; +static constexpr const char *bar = "bar"; + +static_assert ((foo + 3 - foo) == 3, "check"); +static_assert (foo + 2 != foo, "check"); +static_assert (foo + 2 >= foo, "check"); +static_assert (3 + foo >= foo, "check"); +static_assert (foo <= foo + 2, "check"); +static_assert (foo <= 3 + foo, "check"); +static_assert (foo + 2 > foo, "check"); +static_assert (3 + foo > foo, "check"); +static_assert (foo < 2 + foo, "check"); +static_assert (foo < foo + 3, "check"); +static_assert ((bar + 3 - bar) == 3, "check"); +static_assert (bar + 2 != bar, "check"); +static_assert (2 + bar >= bar, "check"); +static_assert (bar + 3 >= bar, "check"); +static_assert (bar <= bar + 2, "check"); +static_assert (bar <= 3 + bar, "check"); +static_assert (bar + 2 > bar, "check"); +static_assert (3 + bar > bar, "check"); +static_assert (bar < 2 + bar, "check"); +static_assert (bar < bar + 3, "check"); -- 2.30.2