summaryrefslogtreecommitdiffstats
path: root/clang/lib/Sema/SemaExpr.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2010-07-24 01:10:11 +0000
committerChris Lattner <sabre@nondot.org>2010-07-24 01:10:11 +0000
commit938533db602b32ab435078e723b656ac6e779a1b (patch)
tree28fcb33de3164d91686eab700b56444ac612833f /clang/lib/Sema/SemaExpr.cpp
parent268ad093a7ae4633fec3b2525a1c7d66715e0553 (diff)
downloadbcm5719-llvm-938533db602b32ab435078e723b656ac6e779a1b.tar.gz
bcm5719-llvm-938533db602b32ab435078e723b656ac6e779a1b.zip
turn down the logical bitwise confusion warning to not warn
when the RHS of the ||/&& is ever 0 or 1. This handles a variety of creative idioms for "true" used in C programs and fixes many false positives at the expense of a few false negatives. This fixes rdar://8230351. llvm-svn: 109314
Diffstat (limited to 'clang/lib/Sema/SemaExpr.cpp')
-rw-r--r--clang/lib/Sema/SemaExpr.cpp25
1 files changed, 14 insertions, 11 deletions
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 8b2e0203b38..5281f3ff793 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -5819,18 +5819,21 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
// bitwise one. We do this when the LHS is a non-bool integer and the RHS
// is a constant.
if (lex->getType()->isIntegerType() && !lex->getType()->isBooleanType() &&
- rex->getType()->isIntegerType() && rex->isEvaluatable(Context) &&
- // Don't warn if the RHS is a (constant folded) boolean expression like
- // "sizeof(int) == 4".
- !rex->isKnownToHaveBooleanValue() &&
+ rex->getType()->isIntegerType() &&
// Don't warn in macros.
- !Loc.isMacroID())
- Diag(Loc, diag::warn_logical_instead_of_bitwise)
- << rex->getSourceRange()
- << (Opc == BinaryOperator::LAnd ? "&&" : "||")
- << (Opc == BinaryOperator::LAnd ? "&" : "|");
-
-
+ !Loc.isMacroID()) {
+ // If the RHS can be constant folded, and if it constant folds to something
+ // that isn't 0 or 1 (which indicate a potential logical operation that
+ // happened to fold to true/false) then warn.
+ Expr::EvalResult Result;
+ if (rex->Evaluate(Result, Context) && !Result.HasSideEffects &&
+ Result.Val.getInt() != 0 && Result.Val.getInt() != 1) {
+ Diag(Loc, diag::warn_logical_instead_of_bitwise)
+ << rex->getSourceRange()
+ << (Opc == BinaryOperator::LAnd ? "&&" : "||")
+ << (Opc == BinaryOperator::LAnd ? "&" : "|");
+ }
+ }
if (!Context.getLangOptions().CPlusPlus) {
UsualUnaryConversions(lex);
OpenPOWER on IntegriCloud