summaryrefslogtreecommitdiffstats
path: root/clang/lib/Sema/SemaExpr.cpp
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
commit85970ca84cd6b3545b3b4a3ca5b6f7d87541ee38 (patch)
tree7e814814f679a0d4d23c66c028a0daa47e3b5804 /clang/lib/Sema/SemaExpr.cpp
parent0864a75ebf82c239ab62bdc91ee2fd3b7279245f (diff)
downloadbcm5719-llvm-85970ca84cd6b3545b3b4a3ca5b6f7d87541ee38.tar.gz
bcm5719-llvm-85970ca84cd6b3545b3b4a3ca5b6f7d87541ee38.zip
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. llvm-svn: 60858
Diffstat (limited to 'clang/lib/Sema/SemaExpr.cpp')
-rw-r--r--clang/lib/Sema/SemaExpr.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 8cbbca3fbf0..3870f0736ec 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/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())
OpenPOWER on IntegriCloud