Emit a warning when the body of an if block is a NullStmt.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42840 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Sema/SemaStmt.cpp b/Sema/SemaStmt.cpp
index b6d4c55..460b50c 100644
--- a/Sema/SemaStmt.cpp
+++ b/Sema/SemaStmt.cpp
@@ -173,6 +173,8 @@
                   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 @@
     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
diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj
index b5cf88f..c7f4908 100644
--- a/clang.xcodeproj/project.pbxproj
+++ b/clang.xcodeproj/project.pbxproj
@@ -739,6 +739,7 @@
 		08FB7793FE84155DC02AAC07 /* Project object */ = {
 			isa = PBXProject;
 			buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
+			compatibilityVersion = "Xcode 2.4";
 			hasScannedForEncodings = 1;
 			mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
 			projectDirPath = "";
diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def
index 9238e23..b71a6a6 100644
--- a/include/clang/Basic/DiagnosticKinds.def
+++ b/include/clang/Basic/DiagnosticKinds.def
@@ -838,7 +838,9 @@
     "statement requires expression of integer type ('%0' invalid)")
 DIAG(err_multiple_default_labels_defined, ERROR,
     "multiple default labels in one switch")
-     
+DIAG(warn_empty_if_body, WARNING,
+    "if statement has empty body")
+
 DIAG(warn_return_missing_expr, WARNING,
      "non-void function '%0' should return a value")
 DIAG(ext_return_missing_expr, EXTENSION,
diff --git a/test/Parser/pointer_promotion.c b/test/Parser/pointer_promotion.c
index 9d9a526..8d2e6e7 100644
--- a/test/Parser/pointer_promotion.c
+++ b/test/Parser/pointer_promotion.c
@@ -8,11 +8,11 @@
   struct bar *bp;
   short sint = 7;
 
-  if (ip < cp) ; // expected-warning {{comparison of distinct pointer types ('int *' and 'char *')}}
-  if (cp < fp) ; // expected-warning {{comparison of distinct pointer types ('char *' and 'struct foo *')}}
-  if (fp < bp) ; // expected-warning {{comparison of distinct pointer types ('struct foo *' and 'struct bar *')}}
-  if (ip < 7) ; // expected-warning {{comparison between pointer and integer ('int *' and 'int')}}
-  if (sint < ip) ; // expected-warning {{comparison between pointer and integer ('int' and 'int *')}}
-  if (ip == cp) ; // expected-warning {{comparison of distinct pointer types ('int *' and 'char *')}}
+  if (ip < cp) {} // expected-warning {{comparison of distinct pointer types ('int *' and 'char *')}}
+  if (cp < fp) {} // expected-warning {{comparison of distinct pointer types ('char *' and 'struct foo *')}}
+  if (fp < bp) {} // expected-warning {{comparison of distinct pointer types ('struct foo *' and 'struct bar *')}}
+  if (ip < 7) {} // expected-warning {{comparison between pointer and integer ('int *' and 'int')}}
+  if (sint < ip) {} // expected-warning {{comparison between pointer and integer ('int' and 'int *')}}
+  if (ip == cp) {} // expected-warning {{comparison of distinct pointer types ('int *' and 'char *')}}
 }
 
diff --git a/test/Sema/default.c b/test/Sema/default.c
index b51ab9a..92f7278 100644
--- a/test/Sema/default.c
+++ b/test/Sema/default.c
@@ -3,6 +3,6 @@
 void f5 (int z) { 
   if (z) 
     default:  // expected-error {{not in switch statement}}
-      ; 
+      ; // expected-warning {{if statement has empty body}}
 } 
 
diff --git a/test/Sema/if-empty-body.c b/test/Sema/if-empty-body.c
new file mode 100644
index 0000000..1dc9e44
--- /dev/null
+++ b/test/Sema/if-empty-body.c
@@ -0,0 +1,9 @@
+// RUN: clang -parse-ast -verify %s
+
+void f1(int a) {
+    if (a); // expected-warning {{if statement has empty body}}
+}
+
+void f2(int a) {
+    if (a) {}
+}