aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2015-03-20 21:53:40 +0000
committerMarek Polacek <mpolacek@gcc.gnu.org>2015-03-20 21:53:40 +0000
commitf594abf4b9ebf0976c6dbebc9abd51e0abd2da48 (patch)
tree9fd7386d7df1c1a211bdaee788626840958f801d
parent730c436a33f0978a689c866df072ea3540ed037d (diff)
downloadgcc-f594abf4b9ebf0976c6dbebc9abd51e0abd2da48.zip
gcc-f594abf4b9ebf0976c6dbebc9abd51e0abd2da48.tar.gz
gcc-f594abf4b9ebf0976c6dbebc9abd51e0abd2da48.tar.bz2
re PR c++/65398 ([C++11] GCC rejects constexpr variable definitions with valid initialization)
PR c++/65398 * constexpr.c (cxx_fold_indirect_ref): Transform *(&A[i] p+ j) into A[i + j]. * g++.dg/cpp0x/pr65398.C: New test. From-SVN: r221544
-rw-r--r--gcc/cp/ChangeLog6
-rw-r--r--gcc/cp/constexpr.c21
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/g++.dg/cpp0x/pr65398.C70
4 files changed, 102 insertions, 0 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index dadf673..9bc6337 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,5 +1,11 @@
2015-03-20 Marek Polacek <polacek@redhat.com>
+ PR c++/65398
+ * constexpr.c (cxx_fold_indirect_ref): Transform *(&A[i] p+ j) into
+ A[i + j].
+
+2015-03-20 Marek Polacek <polacek@redhat.com>
+
PR c++/65072
* typeck.c (lookup_anon_field): Make sure we're dealing with the main
variant.
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 1b5f50c..37b619d 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -2427,6 +2427,27 @@ cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
break;
}
}
+ /* *(&A[i] p+ j) => A[i + j] */
+ else if (TREE_CODE (op00) == ARRAY_REF
+ && TREE_CODE (TREE_OPERAND (op00, 1)) == INTEGER_CST
+ && TREE_CODE (op01) == INTEGER_CST)
+ {
+ tree t = fold_convert_loc (loc, ssizetype,
+ TREE_OPERAND (op00, 1));
+ tree nelts
+ = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00, 0)));
+ /* Don't fold an out-of-bound access. */
+ if (!tree_int_cst_le (t, nelts))
+ return NULL_TREE;
+ /* Make sure to treat the second operand of POINTER_PLUS_EXPR
+ as signed. */
+ op01 = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype,
+ cp_fold_convert (ssizetype, op01),
+ TYPE_SIZE_UNIT (type));
+ t = size_binop_loc (loc, PLUS_EXPR, op01, t);
+ return build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (op00, 0),
+ t, NULL_TREE, NULL_TREE);
+ }
}
}
/* *(foo *)fooarrptr => (*fooarrptr)[0] */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 8f6bde7..a62b54c 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2015-03-20 Marek Polacek <polacek@redhat.com>
+
+ PR c++/65398
+ * g++.dg/cpp0x/pr65398.C: New test.
+
2015-03-20 Jan Hubicka <hubicka@ucw.cz>
PR ipa/65475
diff --git a/gcc/testsuite/g++.dg/cpp0x/pr65398.C b/gcc/testsuite/g++.dg/cpp0x/pr65398.C
new file mode 100644
index 0000000..a4aeba5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/pr65398.C
@@ -0,0 +1,70 @@
+// PR c++/65398
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+constexpr char s[] = "abc";
+constexpr char c1 = *(&s[0] + 0);
+constexpr char c2 = *(&s[0] + 1);
+constexpr char c3 = *(&s[1] + 0);
+constexpr char c4 = *(&s[1] + 1);
+constexpr char c5 = *(&s[2] + 0);
+constexpr char c6 = *(&s[0] + 2);
+constexpr char c7 = *(&s[2] + 1);
+
+constexpr char d1 = *(&s[4] - 0); // { dg-error "array subscript out of bound" }
+constexpr char d2 = *(&s[4] - 1);
+constexpr char d3 = *(&s[4] - 2);
+constexpr char d4 = *(&s[4] - 3);
+constexpr char d5 = *(&s[4] - 4);
+constexpr char d6 = *(&s[4] - 5); // { dg-error "negative array subscript" }
+
+/* Don't accept invalid stuff. */
+constexpr char e1 = *(&s[5] - 1); // { dg-error "is not a constant expression" }
+constexpr char e2 = *(&s[5] - 2); // { dg-error "is not a constant expression" }
+constexpr char e3 = *(&s[5] - 3); // { dg-error "is not a constant expression" }
+
+SA (c1 == 'a');
+SA (c2 == 'b');
+SA (c3 == 'b');
+SA (c4 == 'c');
+SA (c5 == 'c');
+SA (c6 == 'c');
+SA (c7 == '\0');
+SA (d2 == '\0');
+SA (d3 == 'c');
+SA (d4 == 'b');
+SA (d5 == 'a');
+
+constexpr int l[] = { 'c', 'd', 'e', '\0' };
+constexpr int i1 = *(&l[0] + 0);
+constexpr int i2 = *(&l[0] + 1);
+constexpr int i3 = *(&l[1] + 0);
+constexpr int i4 = *(&l[1] + 1);
+constexpr int i5 = *(&l[2] + 0);
+constexpr int i6 = *(&l[0] + 2);
+constexpr int i7 = *(&l[2] + 1);
+
+constexpr char j1 = *(&l[4] - 0); // { dg-error "array subscript out of bound" }
+constexpr char j2 = *(&l[4] - 1);
+constexpr char j3 = *(&l[4] - 2);
+constexpr char j4 = *(&l[4] - 3);
+constexpr char j5 = *(&l[4] - 4);
+constexpr char j6 = *(&l[4] - 5); // { dg-error "negative array subscript" }
+
+/* Don't accept invalid stuff. */
+constexpr char k1 = *(&l[5] - 1); // { dg-error "is not a constant expression" }
+constexpr char k2 = *(&l[5] - 2); // { dg-error "is not a constant expression" }
+constexpr char k3 = *(&l[5] - 3); // { dg-error "is not a constant expression" }
+
+SA (i1 == 'c');
+SA (i2 == 'd');
+SA (i3 == 'd');
+SA (i4 == 'e');
+SA (i5 == 'e');
+SA (i6 == 'e');
+SA (i7 == '\0');
+SA (j2 == '\0');
+SA (j3 == 'e');
+SA (j4 == 'd');
+SA (j5 == 'c');