aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2008-12-10 23:01:14 +0000
committerDouglas Gregor <dgregor@apple.com>2008-12-10 23:01:14 +0000
commitcaaf29a08761b14fbe42a29080c22dd6961056d1 (patch)
tree459e8a607cb7e3c59241f527a28d52bc84db5007 /lib
parentbc76dd06eb881c70c9775b74bab8b88cd747f173 (diff)
Added a warning when referencing an if's condition variable in the
"else" clause, e.g., if (int X = foo()) { } else { if (X) { // warning: X is always zero in this context } } Fixes rdar://6425550 and lets me think about something other than DeclContext. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@60858 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Parse/ParseStmt.cpp3
-rw-r--r--lib/Sema/SemaExpr.cpp21
-rw-r--r--lib/Sema/SemaExprCXX.cpp4
3 files changed, 28 insertions, 0 deletions
diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp
index 8b5277bc36..cb35740465 100644
--- a/lib/Parse/ParseStmt.cpp
+++ b/lib/Parse/ParseStmt.cpp
@@ -509,8 +509,11 @@ Parser::StmtResult Parser::ParseIfStatement() {
ParseScope InnerScope(this, Scope::DeclScope,
C99orCXX && Tok.isNot(tok::l_brace));
+ bool WithinElse = CurScope->isWithinElse();
+ CurScope->setWithinElse(true);
ElseStmtLoc = Tok.getLocation();
ElseStmt = ParseStatement();
+ CurScope->setWithinElse(WithinElse);
// Pop the 'else' scope if needed.
InnerScope.Exit();
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 8cbbca3fbf..3870f0736e 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -483,6 +483,27 @@ Sema::ExprResult Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc,
// check if referencing an identifier with __attribute__((deprecated)).
if (VD->getAttr<DeprecatedAttr>())
Diag(Loc, diag::warn_deprecated) << VD->getDeclName();
+
+ if (VarDecl *Var = dyn_cast<VarDecl>(VD)) {
+ if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) {
+ Scope *CheckS = S;
+ while (CheckS) {
+ if (CheckS->isWithinElse() &&
+ CheckS->getControlParent()->isDeclScope(Var)) {
+ if (Var->getType()->isBooleanType())
+ Diag(Loc, diag::warn_value_always_false) << Var->getDeclName();
+ else
+ Diag(Loc, diag::warn_value_always_zero) << Var->getDeclName();
+ break;
+ }
+
+ // Move up one more control parent to check again.
+ CheckS = CheckS->getControlParent();
+ if (CheckS)
+ CheckS = CheckS->getParent();
+ }
+ }
+ }
// Only create DeclRefExpr's for valid Decl's.
if (VD->isInvalidDecl())
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index c4cab3316b..27b0ba34d1 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -648,6 +648,10 @@ Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
return true;
AddInitializerToDecl(Dcl, AssignExprVal);
+ // Mark this variable as one that is declared within a conditional.
+ if (VarDecl *VD = dyn_cast<VarDecl>((Decl *)Dcl))
+ VD->setDeclaredInCondition(true);
+
return new CXXConditionDeclExpr(StartLoc, EqualLoc,
cast<VarDecl>(static_cast<Decl *>(Dcl)));
}