[cfe-commits] r119540 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/Sema/parentheses.c
Argyrios Kyrtzidis
akyrtzi at gmail.com
Wed Nov 17 11:18:19 PST 2010
Author: akirtzidis
Date: Wed Nov 17 13:18:19 2010
New Revision: 119540
URL: http://llvm.org/viewvc/llvm-project?rev=119540&view=rev
Log:
Don't emit warn_logical_and_in_logical_or for cases like "a && b || 0".
Modified:
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/Sema/parentheses.c
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=119540&r1=119539&r2=119540&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Nov 17 13:18:19 2010
@@ -7352,11 +7352,21 @@
return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res;
}
+/// \brief Returns true if the given expression can be evaluated as a constant
+/// 'false'.
+static bool EvaluatesAsFalse(Sema &S, Expr *E) {
+ bool Res;
+ return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res;
+}
+
/// \brief Look for '&&' in the left hand of a '||' expr.
static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc,
- Expr *E) {
- if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(E)) {
+ Expr *OrLHS, Expr *OrRHS) {
+ if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) {
if (Bop->getOpcode() == BO_LAnd) {
+ // If it's "a && b || 0" don't warn since the precedence doesn't matter.
+ if (EvaluatesAsFalse(S, OrRHS))
+ return;
// If it's "1 && a || b" don't warn since the precedence doesn't matter.
if (!EvaluatesAsTrue(S, Bop->getLHS()))
return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop);
@@ -7373,9 +7383,12 @@
/// \brief Look for '&&' in the right hand of a '||' expr.
static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc,
- Expr *E) {
- if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(E)) {
+ Expr *OrLHS, Expr *OrRHS) {
+ if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) {
if (Bop->getOpcode() == BO_LAnd) {
+ // If it's "0 || a && b" don't warn since the precedence doesn't matter.
+ if (EvaluatesAsFalse(S, OrLHS))
+ return;
// If it's "a || b && 1" don't warn since the precedence doesn't matter.
if (!EvaluatesAsTrue(S, Bop->getRHS()))
return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop);
@@ -7394,8 +7407,8 @@
// Warn about arg1 || arg2 && arg3, as GCC 4.3+ does.
// We don't warn for 'assert(a || b && "bad")' since this is safe.
if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) {
- DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs);
- DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, rhs);
+ DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs);
+ DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs);
}
}
Modified: cfe/trunk/test/Sema/parentheses.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/parentheses.c?rev=119540&r1=119539&r2=119540&view=diff
==============================================================================
--- cfe/trunk/test/Sema/parentheses.c (original)
+++ cfe/trunk/test/Sema/parentheses.c Wed Nov 17 13:18:19 2010
@@ -34,4 +34,6 @@
// expected-note {{place parentheses around the '&&' expression to silence this warning}}
(void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \
// expected-note {{place parentheses around the '&&' expression to silence this warning}}
+ (void)(i && i || 0); // no warning.
+ (void)(0 || i && i); // no warning.
}
More information about the cfe-commits
mailing list