aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-common.c
diff options
context:
space:
mode:
authorDirk Mueller <dmueller@suse.de>2007-03-09 16:16:35 +0000
committerDirk Mueller <mueller@gcc.gnu.org>2007-03-09 16:16:35 +0000
commit63a0874077b3325c2b3cc47401d8bfe916d8f128 (patch)
tree0678efcbaa9704baaeb7ec0f167caf43462406c8 /gcc/c-common.c
parent0de2ae02735c71f5318af4486444d33c26fc211a (diff)
downloadgcc-63a0874077b3325c2b3cc47401d8bfe916d8f128.zip
gcc-63a0874077b3325c2b3cc47401d8bfe916d8f128.tar.gz
gcc-63a0874077b3325c2b3cc47401d8bfe916d8f128.tar.bz2
re PR c/17946 (wanted: warning for "a && MASK" when "a & MASK" was probably intended)
2007-03-09 Dirk Mueller <dmueller@suse.de> PR c++/17946 * doc/invoke.texi (-Wlogical-op): Document. * common.opt (-Wlogical-op): New. * c-common.h (warn_logical_operator): Declare. * c-common.c (warn_logical_operator): Define. * c-typeck.c (parser_build_binary_op): Call warn_logical_operator. * cp/call.c (build_new_op): Call warn_logical_operator. * testsuite/gcc.dg/Wlogical-op-1.c: New. * testsuite/g++.dg/warn/Wlogical-op-1.C: New. From-SVN: r122751
Diffstat (limited to 'gcc/c-common.c')
-rw-r--r--gcc/c-common.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/gcc/c-common.c b/gcc/c-common.c
index 0bcd462..6216d5e 100644
--- a/gcc/c-common.c
+++ b/gcc/c-common.c
@@ -978,6 +978,44 @@ overflow_warning (tree value)
}
}
+
+/* Warn about use of a logical || / && operator being used in a
+ context where it is likely that the bitwise equivalent was intended
+ by the programmer. CODE is the TREE_CODE of the operator, ARG1
+ and ARG2 the arguments. */
+
+void
+warn_logical_operator (enum tree_code code, tree arg1, tree
+ arg2)
+{
+ switch (code)
+ {
+ case TRUTH_ANDIF_EXPR:
+ case TRUTH_ORIF_EXPR:
+ case TRUTH_OR_EXPR:
+ case TRUTH_AND_EXPR:
+ if (!TREE_NO_WARNING (arg1)
+ && INTEGRAL_TYPE_P (TREE_TYPE (arg1))
+ && !CONSTANT_CLASS_P (arg1)
+ && TREE_CODE (arg2) == INTEGER_CST
+ && !integer_zerop (arg2)
+ && !integer_onep (arg2))
+ {
+ warning (OPT_Wlogical_op,
+ "logical %<%s%> with non-zero constant "
+ "will always evaluate as true",
+ ((code == TRUTH_ANDIF_EXPR)
+ || (code == TRUTH_AND_EXPR)) ? "&&" : "||");
+ TREE_NO_WARNING (arg1) = true;
+ }
+
+ break;
+ default:
+ break;
+ }
+}
+
+
/* Print a warning about casts that might indicate violation
of strict aliasing rules if -Wstrict-aliasing is used and
strict aliasing mode is in effect. OTYPE is the original