c++: fix ICE with -Wduplicated-cond [PR107593]
authorMarek Polacek <polacek@redhat.com>
Thu, 26 Jan 2023 14:34:28 +0000 (09:34 -0500)
committerMarek Polacek <polacek@redhat.com>
Tue, 31 Jan 2023 16:35:45 +0000 (11:35 -0500)
Here we crash because a CAST_EXPR, representing T(), doesn't have
its operand, and operand_equal_p's STRIP_ANY_LOCATION_WRAPPER doesn't
expect that.  (o_e_p is called from warn_duplicated_cond_add_or_warn.)

In the past we've adjusted o_e_p to better cope with template codes,
but in this case I think we just want to avoid attempting to warn
about inst-dependent expressions; I don't think I've ever envisioned
-Wduplicated-cond to warn about them.  Also destroy the chain when
an inst-dependent expression is encountered to not warn in
Wduplicated-cond4.C.

The ICE started with r12-6022, two-stage name lookup for overloaded
operators, which gave dependent operators a TREE_TYPE (in particular,
DEPENDENT_OPERATOR_TYPE), so we no longer bail out here in o_e_p:

  /* Similar, if either does not have a type (like a template id),
     they aren't equal.  */
  if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
    return false;

PR c++/107593
PR c++/108597

gcc/c-family/ChangeLog:

* c-common.h (instantiation_dependent_expression_p): Declare.
* c-warn.cc (warn_duplicated_cond_add_or_warn): If the condition
is dependent, invalidate the chain.

gcc/c/ChangeLog:

* c-objc-common.cc (instantiation_dependent_expression_p): New.

gcc/cp/ChangeLog:

* cp-tree.h (instantiation_dependent_expression_p): Don't
declare here.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wduplicated-cond3.C: New test.
* g++.dg/warn/Wduplicated-cond4.C: New test.
* g++.dg/warn/Wduplicated-cond5.C: New test.

gcc/c-family/c-common.h
gcc/c-family/c-warn.cc
gcc/c/c-objc-common.cc
gcc/cp/cp-tree.h
gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wduplicated-cond4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wduplicated-cond5.C [new file with mode: 0644]

index bb6271d..2dadb33 100644 (file)
@@ -1101,6 +1101,7 @@ extern tree lookup_label (tree);
 extern tree lookup_name (tree);
 extern bool lvalue_p (const_tree);
 extern int maybe_adjust_arg_pos_for_attribute (const_tree);
+extern bool instantiation_dependent_expression_p (tree);
 
 extern bool vector_targets_convertible_p (const_tree t1, const_tree t2);
 extern bool vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note);
index 5ed7bca..29efce3 100644 (file)
@@ -2535,7 +2535,7 @@ warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
   if (*chain == NULL)
     return;
 
-  if (TREE_SIDE_EFFECTS (cond))
+  if (TREE_SIDE_EFFECTS (cond) || instantiation_dependent_expression_p (cond))
     {
       /* Uh-oh!  This condition has a side-effect, thus invalidates
         the whole chain.  */
index d4f0b26..0350733 100644 (file)
@@ -400,3 +400,11 @@ maybe_adjust_arg_pos_for_attribute (const_tree)
 {
   return 0;
 }
+
+/* In C, no expression is dependent.  */
+
+bool
+instantiation_dependent_expression_p (tree)
+{
+  return false;
+}
index 9f18872..31fd8af 100644 (file)
@@ -7428,7 +7428,6 @@ extern bool any_type_dependent_arguments_p      (const vec<tree, va_gc> *);
 extern bool any_type_dependent_elements_p       (const_tree);
 extern bool type_dependent_expression_p_push   (tree);
 extern bool value_dependent_expression_p       (tree);
-extern bool instantiation_dependent_expression_p (tree);
 extern bool instantiation_dependent_uneval_expression_p (tree);
 extern bool any_value_dependent_elements_p      (const_tree);
 extern bool dependent_omp_for_p                        (tree, tree, tree, tree);
diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C
new file mode 100644 (file)
index 0000000..3da054e
--- /dev/null
@@ -0,0 +1,38 @@
+// PR c++/107593
+// { dg-do compile }
+// { dg-options "-Wduplicated-cond" }
+
+template <typename T>
+void
+foo ()
+{
+  if (T() && T() && int())
+    ;
+  else if (T() && T() && int())
+    ;
+}
+
+template <typename T>
+void bar(T a)
+{
+  if (a)
+    ;
+  else if (a)
+    ;
+}
+
+template <typename>
+void baz(int a)
+{
+  if (a)
+    ;
+  else if (a) // { dg-warning "duplicated" }
+    ;
+}
+void
+f ()
+{
+  foo<int>();
+  bar(1);
+  baz<int>(1);
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond4.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond4.C
new file mode 100644 (file)
index 0000000..41bb9f0
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/107593
+// { dg-do compile }
+// { dg-options "-Wduplicated-cond" }
+
+int n;
+
+template<class T> bool g() { n = 42; return false; }
+
+template<class T>
+void f() {
+  if (n)
+    ;
+  else if (g<T>())
+    ;
+  else if (n)
+    ;
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond5.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond5.C
new file mode 100644 (file)
index 0000000..23a0bf2
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/108597
+// { dg-do compile }
+// { dg-options "-Wduplicated-cond" }
+
+template <typename T>
+struct MyStruct {
+
+    void check(int &x) {
+        if (&x == &_a) {
+        } else if (&x == &_b) {
+        }
+    }
+
+    int _a;
+    int _b;
+};