/* Compare the hashes. */
if (h0 == h1
- && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC)
+ && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC
+ | OEP_ADDRESS_OF_SAME_FIELD)
/* Don't warn if any of the branches or their subexpressions comes
from a macro. */
&& !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r,
warn here, because the COND_EXPR will be turned into OP1. */
if (warn_duplicated_branches
&& TREE_CODE (ret) == COND_EXPR
- && (op1 == op2 || operand_equal_p (op1, op2, 0)))
+ && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
"this condition has identical branches");
warn here, because the COND_EXPR will be turned into ARG2. */
if (warn_duplicated_branches
&& (complain & tf_warning)
- && (arg2 == arg3 || operand_equal_p (arg2, arg3, 0)))
+ && (arg2 == arg3 || operand_equal_p (arg2, arg3,
+ OEP_ADDRESS_OF_SAME_FIELD)))
warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
"this condition has identical branches");
new_op2 = convert_to_void (op2, ICV_CAST, complain);
}
- expr = build3 (COND_EXPR, TREE_TYPE (new_op2),
- TREE_OPERAND (expr, 0), new_op1, new_op2);
+ expr = build3_loc (loc, COND_EXPR, TREE_TYPE (new_op2),
+ TREE_OPERAND (expr, 0), new_op1, new_op2);
break;
}
if (new_op1 != op1)
{
- tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
- TREE_OPERAND (expr, 0), new_op1);
+ tree t = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (new_op1),
+ TREE_OPERAND (expr, 0), new_op1);
expr = t;
}
flags &= ~OEP_ADDRESS_OF;
if (!OP_SAME (1))
{
- if (compare_address)
+ if (compare_address
+ && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
{
if (TREE_OPERAND (arg0, 2)
|| TREE_OPERAND (arg1, 2))
--- /dev/null
+// PR c++/99565
+// { dg-do compile }
+// { dg-options "-Wduplicated-branches" }
+
+struct A {
+ union { int a; int b; };
+ int& foo (bool x) { return x ? a : b; } // { dg-bogus "this condition has identical branches" }
+ void bar (bool x, int y) { if (x) a = y; else b = y; }
+};
--- /dev/null
+// PR c++/99565
+// { dg-do compile }
+// { dg-options "-Wduplicated-branches" }
+
+int a;
+
+void
+foo (bool x)
+{
+ x ? ++a : ++a; // { dg-warning "this condition has identical branches" }
+}
OEP_HASH_CHECK = 32,
/* Makes operand_equal_p handle more expressions: */
OEP_LEXICOGRAPHIC = 64,
- OEP_BITWISE = 128
+ OEP_BITWISE = 128,
+ /* For OEP_ADDRESS_OF of COMPONENT_REFs, only consider same fields as
+ equivalent rather than also different fields with the same offset. */
+ OEP_ADDRESS_OF_SAME_FIELD = 256
};
/* Enum and arrays used for tree allocation stats.