aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2018-01-29 20:54:12 +0000
committerMarek Polacek <mpolacek@gcc.gnu.org>2018-01-29 20:54:12 +0000
commitb07c365100c92687361aa062ecf89c49650b8930 (patch)
treedf91c991284a3ede8cc05d8cee9c0ec35c95ac76
parent7d07a93a52153e7ccda52ce86847d2e365dc9f83 (diff)
downloadgcc-b07c365100c92687361aa062ecf89c49650b8930.zip
gcc-b07c365100c92687361aa062ecf89c49650b8930.tar.gz
gcc-b07c365100c92687361aa062ecf89c49650b8930.tar.bz2
re PR c++/83996 (ICE with zero-sized array)
PR c++/83996 * constexpr.c (cxx_fold_indirect_ref): Compute ((foo *)&fooarray)[1] => fooarray[1] in offset_int. * g++.dg/ext/pr83996.C: New test. From-SVN: r257160
-rw-r--r--gcc/cp/ChangeLog6
-rw-r--r--gcc/cp/constexpr.c16
-rw-r--r--gcc/testsuite/ChangeLog3
-rw-r--r--gcc/testsuite/g++.dg/ext/pr83996.C11
4 files changed, 31 insertions, 5 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 90e8ff1..e8b0e1e 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2018-01-29 Marek Polacek <polacek@redhat.com>
+
+ PR c++/83996
+ * constexpr.c (cxx_fold_indirect_ref): Compute ((foo *)&fooarray)[1]
+ => fooarray[1] in offset_int.
+
2018-01-29 Jason Merrill <jason@redhat.com>
PR c++/83942 - wrong unused warning with static_cast.
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 4d2ee4a..0a1944f 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -3143,11 +3143,17 @@ cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
tree min_val = size_zero_node;
if (type_domain && TYPE_MIN_VALUE (type_domain))
min_val = TYPE_MIN_VALUE (type_domain);
- op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
- TYPE_SIZE_UNIT (type));
- op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
- return build4_loc (loc, ARRAY_REF, type, op00, op01,
- NULL_TREE, NULL_TREE);
+ offset_int off = wi::to_offset (op01);
+ offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
+ offset_int remainder;
+ off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
+ if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST)
+ {
+ off = off + wi::to_offset (min_val);
+ op01 = wide_int_to_tree (sizetype, off);
+ return build4_loc (loc, ARRAY_REF, type, op00, op01,
+ NULL_TREE, NULL_TREE);
+ }
}
/* Also handle conversion to an empty base class, which
is represented with a NOP_EXPR. */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 3713a81..a74854b 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -3,6 +3,9 @@
PR c/83966
* gcc.dg/format/Wsuggest-attribute-1.c: New test.
+ PR c++/83996
+ * g++.dg/ext/pr83996.C: New test.
+
2018-01-29 Richard Biener <rguenther@suse.de>
PR tree-optimization/84057
diff --git a/gcc/testsuite/g++.dg/ext/pr83996.C b/gcc/testsuite/g++.dg/ext/pr83996.C
new file mode 100644
index 0000000..e663d72
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/pr83996.C
@@ -0,0 +1,11 @@
+// PR c++/83996
+// { dg-do compile }
+// { dg-options "" }
+
+int z[] = { };
+
+int
+main (void)
+{
+ __builtin_printf ("%d\n", *(z + 1));
+}