aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcc/c-family/ChangeLog6
-rw-r--r--gcc/c-family/c-common.c20
-rw-r--r--gcc/c-family/c-common.h3
-rw-r--r--gcc/c/ChangeLog5
-rw-r--r--gcc/c/c-typeck.c2
-rw-r--r--gcc/cp/ChangeLog5
-rw-r--r--gcc/cp/parser.c2
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/c-c++-common/Wlogical-not-parentheses-2.c20
9 files changed, 60 insertions, 7 deletions
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index fe98090..f8a7425 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,9 @@
+2016-08-25 Marek Polacek <polacek@redhat.com>
+ David Malcolm <dmalcolm@redhat.com>
+
+ * c-common.c (warn_logical_not_parentheses): Print fixit hints.
+ * c-common.h (warn_logical_not_parentheses): Update declaration.
+
2016-08-22 Marek Polacek <polacek@redhat.com>
PR c++/77321
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 3feb910..001070d 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -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,21 @@ 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, "(");
+ location_t finish = get_finish (lhs_loc);
+ location_t next_loc
+ = linemap_position_for_loc_and_offset (line_table, finish, 1);
+ richloc.add_fixit_insert (next_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.
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index bc22baa..42ce969 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -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);
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index dc863bb..774e580 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,5 +1,10 @@
2016-08-25 Marek Polacek <polacek@redhat.com>
+ * c-typeck.c (parser_build_binary_op): Pass LHS to
+ warn_logical_not_parentheses.
+
+2016-08-25 Marek Polacek <polacek@redhat.com>
+
PR c/77323
* c-decl.c (declspecs_add_type): Set typespec_word even when __intN
or _FloatN or _FloatNx is not supported.
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index bc8728a..2f8d611 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -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
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 705a034..4917719 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,8 @@
+2016-08-25 Marek Polacek <polacek@redhat.com>
+
+ * parser.c (cp_parser_binary_expression): Pass LHS to
+ warn_logical_not_parentheses.
+
2016-08-18 Marek Polacek <polacek@redhat.com>
PR c/7652
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 690e928..d54cf8a 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -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;
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index f4dddd8..caeaa7e 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,5 +1,9 @@
2016-08-25 Marek Polacek <polacek@redhat.com>
+ * c-c++-common/Wlogical-not-parentheses-2.c: New test.
+
+2016-08-25 Marek Polacek <polacek@redhat.com>
+
PR c/77323
* gcc.dg/pr77323.c: New test.
diff --git a/gcc/testsuite/c-c++-common/Wlogical-not-parentheses-2.c b/gcc/testsuite/c-c++-common/Wlogical-not-parentheses-2.c
new file mode 100644
index 0000000..ba8dce8
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wlogical-not-parentheses-2.c
@@ -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;
+}