summaryrefslogtreecommitdiffstats
path: root/clang/Sema/SemaStmt.cpp
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@mac.com>2007-10-10 20:50:11 +0000
committerAnders Carlsson <andersca@mac.com>2007-10-10 20:50:11 +0000
commitdb83d77c78772b0188495093186b88b4ad2ea857 (patch)
tree11dcc7eb63e157343e45ca1749aa4e7d4db94f30 /clang/Sema/SemaStmt.cpp
parent4479f86c0fa9853520f63d10d8e47c389dc32be9 (diff)
downloadbcm5719-llvm-db83d77c78772b0188495093186b88b4ad2ea857.tar.gz
bcm5719-llvm-db83d77c78772b0188495093186b88b4ad2ea857.zip
Emit a warning when the body of an if block is a NullStmt.
llvm-svn: 42840
Diffstat (limited to 'clang/Sema/SemaStmt.cpp')
-rw-r--r--clang/Sema/SemaStmt.cpp13
1 files changed, 12 insertions, 1 deletions
diff --git a/clang/Sema/SemaStmt.cpp b/clang/Sema/SemaStmt.cpp
index b6d4c5580d4..460b50ccbf3 100644
--- a/clang/Sema/SemaStmt.cpp
+++ b/clang/Sema/SemaStmt.cpp
@@ -173,6 +173,8 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
StmtTy *ThenVal, SourceLocation ElseLoc,
StmtTy *ElseVal) {
Expr *condExpr = (Expr *)CondVal;
+ Stmt *thenStmt = (Stmt *)ThenVal;
+
assert(condExpr && "ActOnIfStmt(): missing expression");
DefaultFunctionArrayConversion(condExpr);
@@ -182,7 +184,16 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
condType.getAsString(), condExpr->getSourceRange());
- return new IfStmt(IfLoc, condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
+ // Warn if the if block has a null body without an else value.
+ // this helps prevent bugs due to typos, such as
+ // if (condition);
+ // do_stuff();
+ if (!ElseVal) {
+ if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
+ Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
+ }
+
+ return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal);
}
Action::StmtResult
OpenPOWER on IntegriCloud