[cfe-commits] r109314 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/CodeGenCXX/static-init-2.cpp test/Sema/exprs.c test/Sema/switch.c test/SemaCXX/switch.cpp
Chris Lattner
sabre at nondot.org
Fri Jul 23 18:10:11 PDT 2010
Author: lattner
Date: Fri Jul 23 20:10:11 2010
New Revision: 109314
URL: http://llvm.org/viewvc/llvm-project?rev=109314&view=rev
Log:
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.
Modified:
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/CodeGenCXX/static-init-2.cpp
cfe/trunk/test/Sema/exprs.c
cfe/trunk/test/Sema/switch.c
cfe/trunk/test/SemaCXX/switch.cpp
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=109314&r1=109313&r2=109314&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Jul 23 20:10:11 2010
@@ -5819,18 +5819,21 @@
// 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);
Modified: cfe/trunk/test/CodeGenCXX/static-init-2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/static-init-2.cpp?rev=109314&r1=109313&r2=109314&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/static-init-2.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/static-init-2.cpp Fri Jul 23 20:10:11 2010
@@ -3,4 +3,4 @@
// Make sure we don't crash generating y; its value is constant, but the
// initializer has side effects, so EmitConstantExpr should fail.
int x();
-int y = x() && 0; // expected-warning {{use of logical && with constant operand}}
+int y = x() && 0;
Modified: cfe/trunk/test/Sema/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/exprs.c?rev=109314&r1=109313&r2=109314&view=diff
==============================================================================
--- cfe/trunk/test/Sema/exprs.c (original)
+++ cfe/trunk/test/Sema/exprs.c Fri Jul 23 20:10:11 2010
@@ -145,5 +145,8 @@
int test20(int x) {
return x && 4; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
- return x && sizeof(int) == 4; // no warning.
+ return x && sizeof(int) == 4; // no warning, RHS is logical op.
+
+ // no warning, this is an idiom for "true" in old C style.
+ return x && (signed char)1;
}
Modified: cfe/trunk/test/Sema/switch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/switch.c?rev=109314&r1=109313&r2=109314&view=diff
==============================================================================
--- cfe/trunk/test/Sema/switch.c (original)
+++ cfe/trunk/test/Sema/switch.c Fri Jul 23 20:10:11 2010
@@ -50,14 +50,12 @@
}
switch (cond) {
- case g() && 0: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}} \
- expected-warning {{use of logical && with constant operand}}
+ case g() && 0: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
break;
}
switch (cond) {
- case 0 ... g() || 1: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}} \\
- expected-warning {{use of logical || with constant operand}}
+ case 0 ... g() || 1: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
break;
}
}
Modified: cfe/trunk/test/SemaCXX/switch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/switch.cpp?rev=109314&r1=109313&r2=109314&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/switch.cpp (original)
+++ cfe/trunk/test/SemaCXX/switch.cpp Fri Jul 23 20:10:11 2010
@@ -8,8 +8,7 @@
}
int n = 3;
- switch (n && 1) { // expected-warning {{bool}} \
- // expected-warning {{use of logical && with constant operand}}
+ switch (n && 1) { // expected-warning {{bool}}
case 1:
break;
}
More information about the cfe-commits
mailing list