aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2024-08-29 10:40:50 -0400
committerMarek Polacek <polacek@redhat.com>2024-09-17 16:59:09 -0400
commit7ca486889b1b1c7e7bcbbca3b6caa103294ec07d (patch)
tree04bd06fd4b9c5d2e48c18c82751caa0383dbac6c
parentdfe0d4389a3ce43179563a63046ad3e74d615a08 (diff)
downloadgcc-7ca486889b1b1c7e7bcbbca3b6caa103294ec07d.zip
gcc-7ca486889b1b1c7e7bcbbca3b6caa103294ec07d.tar.gz
gcc-7ca486889b1b1c7e7bcbbca3b6caa103294ec07d.tar.bz2
c++: ICE with -Wtautological-compare in template [PR116534]
Pre r14-4793, we'd call warn_tautological_cmp -> operand_equal_p with operands wrapped in NON_DEPENDENT_EXPR, which works, since o_e_p bails for codes it doesn't know. But now we pass operands not encapsulated in NON_DEPENDENT_EXPR, and crash, because the template tree for &a[x] has null DECL_FIELD_OFFSET. This patch extends r12-7797 to cover the case when DECL_FIELD_OFFSET is null. PR c++/116534 gcc/ChangeLog: * fold-const.cc (operand_compare::operand_equal_p): If either field's DECL_FIELD_OFFSET is null, compare the fields with ==. gcc/testsuite/ChangeLog: * g++.dg/warn/Wtautological-compare4.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
-rw-r--r--gcc/fold-const.cc12
-rw-r--r--gcc/testsuite/g++.dg/warn/Wtautological-compare4.C21
2 files changed, 30 insertions, 3 deletions
diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 70db167..0578f42 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -3601,9 +3601,15 @@ operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
/* Non-FIELD_DECL operands can appear in C++ templates. */
if (TREE_CODE (field0) != FIELD_DECL
- || TREE_CODE (field1) != FIELD_DECL
- || !operand_equal_p (DECL_FIELD_OFFSET (field0),
- DECL_FIELD_OFFSET (field1), flags)
+ || TREE_CODE (field1) != FIELD_DECL)
+ return false;
+
+ if (!DECL_FIELD_OFFSET (field0)
+ || !DECL_FIELD_OFFSET (field1))
+ return field0 == field1;
+
+ if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
+ DECL_FIELD_OFFSET (field1), flags)
|| !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
DECL_FIELD_BIT_OFFSET (field1),
flags))
diff --git a/gcc/testsuite/g++.dg/warn/Wtautological-compare4.C b/gcc/testsuite/g++.dg/warn/Wtautological-compare4.C
new file mode 100644
index 0000000..96308f4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wtautological-compare4.C
@@ -0,0 +1,21 @@
+// PR c++/116534
+// { dg-do compile }
+// { dg-options "-Wall" }
+
+template <class A>
+struct Test {
+ bool foo(unsigned x, unsigned y) {
+ bool test = &a[x] == &b[y];
+ return test;
+ }
+ unsigned *a;
+ unsigned *b;
+};
+
+void
+g ()
+{
+ Test<int> t;
+ t.foo (0u, 1u);
+ t.foo (0u, 0u);
+}