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. */
+/* Returns true if T corresponds to an assignment operator expression. */
+
static bool
-is_assignment_op_expr_p (tree call)
+is_assignment_op_expr_p (tree t)
{
- if (call == NULL_TREE)
+ if (t == NULL_TREE)
return false;
- call = extract_call_expr (call);
+ if (TREE_CODE (t) == MODIFY_EXPR
+ || (TREE_CODE (t) == MODOP_EXPR
+ && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR))
+ return true;
+
+ tree call = extract_call_expr (t);
if (call == NULL_TREE
|| call == error_mark_node
|| !CALL_EXPR_OPERATOR_SYNTAX (call))
&& DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR);
}
+/* Maybe warn about an unparenthesized 'a = b' (appearing in a
+ boolean context where 'a == b' might have been intended). */
+
+void
+maybe_warn_unparenthesized_assignment (tree t, tsubst_flags_t complain)
+{
+ if (REFERENCE_REF_P (t))
+ t = TREE_OPERAND (t, 0);
+
+ if ((complain & tf_warning)
+ && warn_parentheses
+ && is_assignment_op_expr_p (t)
+ /* A parenthesized expression would've had this warning
+ suppressed by finish_parenthesized_expr. */
+ && !warning_suppressed_p (t, OPT_Wparentheses))
+ {
+ warning_at (cp_expr_loc_or_input_loc (t), OPT_Wparentheses,
+ "suggest parentheses around assignment used as truth value");
+ suppress_warning (t, OPT_Wparentheses);
+ }
+}
+
/* 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. */
if (warn_sequence_point && !processing_template_decl)
verify_sequence_points (cond);
+ maybe_warn_unparenthesized_assignment (cond, tf_warning_or_error);
+
/* Do the conversion. */
cond = convert_from_reference (cond);
-
- tree inner = REFERENCE_REF_P (cond) ? TREE_OPERAND (cond, 0) : cond;
- if ((TREE_CODE (inner) == MODIFY_EXPR
- || (TREE_CODE (inner) == MODOP_EXPR
- && TREE_CODE (TREE_OPERAND (inner, 1)) == NOP_EXPR)
- || is_assignment_op_expr_p (inner))
- && warn_parentheses
- && !warning_suppressed_p (inner, OPT_Wparentheses)
- && warning_at (cp_expr_loc_or_input_loc (inner),
- OPT_Wparentheses, "suggest parentheses around "
- "assignment used as truth value"))
- suppress_warning (inner, OPT_Wparentheses);
-
return condition_conversion (cond);
}
{
if (EXPR_P (expr))
{
- /* This inhibits warnings in c_common_truthvalue_conversion. */
+ /* This inhibits warnings in maybe_warn_unparenthesized_assignment
+ and c_common_truthvalue_conversion. */
tree inner = REFERENCE_REF_P (expr) ? TREE_OPERAND (expr, 0) : *expr;
suppress_warning (inner, OPT_Wparentheses);
}
/* If -Wparentheses, warn about a = b = c when a has type bool and b
does not. */
- if (warn_parentheses
- && TREE_CODE (type) == BOOLEAN_TYPE
- && TREE_CODE (rhs) == MODIFY_EXPR
- && !warning_suppressed_p (rhs, OPT_Wparentheses)
- && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
- && (complain & tf_warning)
- && warning_at (rhs_loc, OPT_Wparentheses,
- "suggest parentheses around assignment used as "
- "truth value"))
- suppress_warning (rhs, OPT_Wparentheses);
+ if (TREE_CODE (type) == BOOLEAN_TYPE
+ && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE)
+ maybe_warn_unparenthesized_assignment (rhs, complain);
if (complain & tf_warning)
warn_for_address_or_pointer_of_packed_member (type, rhs);