@@ -1485,7 +1485,7 @@ warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
void
warn_logical_not_parentheses (location_t location, enum tree_code code,
- tree rhs)
+ tree lhs, tree rhs)
{
if (TREE_CODE_CLASS (code) != tcc_comparison
|| TREE_TYPE (rhs) == NULL_TREE
@@ -1498,9 +1498,18 @@ warn_logical_not_parentheses (location_t location, enum tree_code code,
&& integer_zerop (rhs))
return;
- warning_at (location, OPT_Wlogical_not_parentheses,
- "logical not is only applied to the left hand side of "
- "comparison");
+ if (warning_at (location, OPT_Wlogical_not_parentheses,
+ "logical not is only applied to the left hand side of "
+ "comparison")
+ && EXPR_HAS_LOCATION (lhs))
+ {
+ location_t lhs_loc = EXPR_LOCATION (lhs);
+ rich_location richloc (line_table, lhs_loc);
+ richloc.add_fixit_insert (lhs_loc, "(");
+ richloc.add_fixit_insert (get_finish (lhs_loc), ")");
+ inform_at_rich_loc (&richloc, "add parentheses around left hand side "
+ "expression to silence this warning");
+ }
}
/* Warn if EXP contains any computations whose results are not used.
@@ -847,7 +847,8 @@ extern void overflow_warning (location_t, tree);
extern bool warn_if_unused_value (const_tree, location_t);
extern void warn_logical_operator (location_t, enum tree_code, tree,
enum tree_code, tree, enum tree_code, tree);
-extern void warn_logical_not_parentheses (location_t, enum tree_code, tree);
+extern void warn_logical_not_parentheses (location_t, enum tree_code, tree,
+ tree);
extern void warn_tautological_cmp (location_t, enum tree_code, tree, tree);
extern void check_main_parameter_types (tree decl);
extern bool c_determine_visibility (tree);
@@ -3696,7 +3696,7 @@ parser_build_binary_op (location_t location, enum tree_code code,
while (1);
}
if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
- warn_logical_not_parentheses (location, code, arg2.value);
+ warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
}
/* Warn about comparisons against string literals, with the exception
@@ -8922,7 +8922,7 @@ cp_parser_binary_expression (cp_parser* parser, bool cast_p,
|| TREE_TYPE (current.lhs) == NULL_TREE
|| TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
warn_logical_not_parentheses (current.loc, current.tree_type,
- maybe_constant_value (rhs));
+ current.lhs, maybe_constant_value (rhs));
overload = NULL;
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-Wlogical-not-parentheses -fdiagnostics-show-caret" } */
+
+ /* Test fixit hints. */
+
+int
+foo (int aaa, int bbb)
+{
+ int r = 0;
+ r += (!aaa) == bbb;
+ r += !aaa == bbb; /* { dg-warning "logical not is only applied" } */
+/* { dg-begin-multiline-output "" }
+ r += !aaa == bbb;
+ ^~
+ r += !aaa == bbb;
+ ^~~~
+ ( )
+ { dg-end-multiline-output "" } */
+ return r;
+}