diff options
author | mpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-06-05 06:20:05 +0000 |
---|---|---|
committer | mpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-06-05 06:20:05 +0000 |
commit | 32dc1512122166b06d2f25370c4e52a146e71bf4 (patch) | |
tree | 10c8e02d49aad0155082bdf3d7725c4ee7041702 /gcc/c-family | |
parent | 64cf1262acdfe754cf395f1f4f6992376ee8de65 (diff) | |
download | gcc-32dc1512122166b06d2f25370c4e52a146e71bf4.tar.gz |
PR c/49706
* doc/invoke.texi: Document -Wlogical-not-parentheses.
c-family/
* c-common.c (warn_logical_not_parentheses): New function.
* c-common.h (warn_logical_not_parentheses): Declare.
* c.opt (Wlogical-not-parentheses): New option.
c/
* c-typeck.c (parser_build_binary_op): Warn when logical not is used
on the left hand side operand of a comparison.
cp/
* parser.c (cp_parser_binary_expression): Warn when logical not is
used on the left hand side operand of a comparison.
testsuite/
* c-c++-common/pr49706.c: New test.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@211255 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/c-family')
-rw-r--r-- | gcc/c-family/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/c-family/c-common.c | 23 | ||||
-rw-r--r-- | gcc/c-family/c-common.h | 2 | ||||
-rw-r--r-- | gcc/c-family/c.opt | 4 |
4 files changed, 36 insertions, 0 deletions
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index c6a041a037e..07bcdab2e4c 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,10 @@ +2014-06-05 Marek Polacek <polacek@redhat.com> + + PR c/49706 + * c-common.c (warn_logical_not_parentheses): New function. + * c-common.h (warn_logical_not_parentheses): Declare. + * c.opt (Wlogical-not-parentheses): New option. + 2014-06-04 Marek Polacek <polacek@redhat.com> PR c/30020 diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 07a17981642..fb6c61204c3 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -1722,6 +1722,29 @@ warn_logical_operator (location_t location, enum tree_code code, tree type, } } +/* Warn about logical not used on the left hand side operand of a comparison. + This function assumes that the LHS is inside of TRUTH_NOT_EXPR. + Do not warn if the LHS or RHS is of a boolean or a vector type. */ + +void +warn_logical_not_parentheses (location_t location, enum tree_code code, + tree lhs, tree rhs) +{ + if (TREE_CODE_CLASS (code) != tcc_comparison) + return; + if (TREE_TYPE (lhs) == NULL_TREE + || TREE_TYPE (rhs) == NULL_TREE) + ; + else if (TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE + || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE + || VECTOR_TYPE_P (TREE_TYPE (lhs)) + || VECTOR_TYPE_P (TREE_TYPE (rhs))) + return; + + warning_at (location, OPT_Wlogical_not_parentheses, + "logical not is only applied to the left hand side of " + "comparison"); +} /* Warn if EXP contains any computations whose results are not used. Return true if a warning is printed; false otherwise. LOCUS is the diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 0d34004e4ff..83d5dee30e0 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -772,6 +772,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, + tree); extern void check_main_parameter_types (tree decl); extern bool c_determine_visibility (tree); extern bool vector_types_compatible_elements_p (tree, tree); diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 5d36a80408d..76e67d78ac7 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -490,6 +490,10 @@ Wlogical-op C ObjC C++ ObjC++ Var(warn_logical_op) Init(0) Warning Warn when a logical operator is suspiciously always evaluating to true or false +Wlogical-not-parentheses +C ObjC C++ ObjC++ Var(warn_logical_not_paren) Warning +Warn when logical not is used on the left hand side operand of a comparison + Wlong-long C ObjC C++ ObjC++ Var(warn_long_long) Init(-1) Warning Do not warn about using \"long long\" when -pedantic |