default:;
}
- gcc_assert (TREE_CODE (call) == CALL_EXPR
- || TREE_CODE (call) == AGGR_INIT_EXPR
- || call == error_mark_node);
+ if (TREE_CODE (call) != CALL_EXPR
+ && TREE_CODE (call) != AGGR_INIT_EXPR
+ && call != error_mark_node)
+ return NULL_TREE;
return call;
}
*fn_p = fn;
/* Build the actual CALL_EXPR. */
call = build_over_call (cand, flags, complain);
+
+ /* Suppress warnings for if (my_struct.operator= (x)) where
+ my_struct is implicitly converted to bool. */
+ if (TREE_CODE (call) == MODIFY_EXPR)
+ suppress_warning (call, OPT_Wparentheses);
+
/* In an expression of the form `a->f()' where `f' turns
out to be a static member function, `a' is
none-the-less evaluated. */
return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
}
+/* Returns true if CALL is a (possibly wrapped) CALL_EXPR or AGGR_INIT_EXPR
+ to operator= () that is written as an operator expression. */
+static bool
+is_assignment_op_expr_p (tree call)
+{
+ if (call == NULL_TREE)
+ return false;
+
+ call = extract_call_expr (call);
+ if (call == NULL_TREE
+ || call == error_mark_node
+ || !CALL_EXPR_OPERATOR_SYNTAX (call))
+ return false;
+
+ tree fndecl = cp_get_callee_fndecl_nofold (call);
+ return fndecl != NULL_TREE
+ && DECL_ASSIGNMENT_OPERATOR_P (fndecl)
+ && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR);
+}
+
/* COND is the condition-expression for an if, while, etc.,
statement. Convert it to a boolean value, if appropriate.
In addition, verify sequence points if -Wsequence-point is enabled. */
/* Do the conversion. */
cond = convert_from_reference (cond);
- if (TREE_CODE (cond) == MODIFY_EXPR
+ if ((TREE_CODE (cond) == MODIFY_EXPR || is_assignment_op_expr_p (cond))
&& warn_parentheses
&& !warning_suppressed_p (cond, OPT_Wparentheses)
&& warning_at (cp_expr_loc_or_input_loc (cond),
--- /dev/null
+/* Test that -Wparentheses warns for struct/class assignments,
+ except for explicit calls to operator= (). */
+/* PR c++/25689 */
+/* { dg-options "-Wparentheses" } */
+
+struct A
+{
+ A& operator= (int);
+ A operator= (double);
+ operator bool ();
+};
+
+struct B
+{
+ bool x;
+ B& operator= (int);
+ B operator= (double);
+ operator bool ();
+};
+
+struct C
+{
+ C& operator= (int);
+ virtual C operator= (double);
+ operator bool ();
+};
+
+/* Test empty class */
+void f1 (A a1, A a2)
+{
+ if (a1 = 0); /* { dg-warning "suggest parentheses" } */
+ if (a1 = 0.); /* { dg-warning "suggest parentheses" } */
+ if (a1.operator= (0));
+ if (a1.operator= (a2));
+
+ /* Ideally, we'd warn for empty classes using trivial operator= (below),
+ but we don't do so yet as it is a non-trivial COMPOUND_EXPR. */
+ if (a1 = a2); /* { dg-warning "suggest parentheses" "" { xfail *-*-* } } */
+}
+
+/* Test non-empty class */
+void f2 (B b1, B b2)
+{
+ if (b1 = 0); /* { dg-warning "suggest parentheses" } */
+ if (b1 = 0.); /* { dg-warning "suggest parentheses" } */
+ if (b1 = b2); /* { dg-warning "suggest parentheses" } */
+ if (b1.operator= (0));
+ if (b1.operator= (b2));
+}
+
+/* Test class with vtable */
+void f3 (C c1, C c2)
+{
+ if (c1 = 0); /* { dg-warning "suggest parentheses" } */
+ if (c1 = 0.); /* { dg-warning "suggest parentheses" } */
+ if (c1 = c2); /* { dg-warning "suggest parentheses" } */
+ if (c1.operator= (0));
+ if (c1.operator= (c2));
+}