[cfe-commits] r42840 - in /cfe/trunk: Sema/SemaStmt.cpp clang.xcodeproj/project.pbxproj include/clang/Basic/DiagnosticKinds.def test/Parser/pointer_promotion.c test/Sema/default.c test/Sema/if-empty-body.c

Anders Carlsson andersca at mac.com
Wed Oct 10 13:50:11 PDT 2007


Author: andersca
Date: Wed Oct 10 15:50:11 2007
New Revision: 42840

URL: http://llvm.org/viewvc/llvm-project?rev=42840&view=rev
Log:
Emit a warning when the body of an if block is a NullStmt.


Added:
    cfe/trunk/test/Sema/if-empty-body.c
Modified:
    cfe/trunk/Sema/SemaStmt.cpp
    cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/test/Parser/pointer_promotion.c
    cfe/trunk/test/Sema/default.c

Modified: cfe/trunk/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaStmt.cpp?rev=42840&r1=42839&r2=42840&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/Sema/SemaStmt.cpp Wed Oct 10 15:50:11 2007
@@ -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

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=42840&r1=42839&r2=42840&view=diff

==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Wed Oct 10 15:50:11 2007
@@ -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 = "";

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=42840&r1=42839&r2=42840&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Oct 10 15:50:11 2007
@@ -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,

Modified: cfe/trunk/test/Parser/pointer_promotion.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/pointer_promotion.c?rev=42840&r1=42839&r2=42840&view=diff

==============================================================================
--- cfe/trunk/test/Parser/pointer_promotion.c (original)
+++ cfe/trunk/test/Parser/pointer_promotion.c Wed Oct 10 15:50:11 2007
@@ -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 *')}}
 }
 

Modified: cfe/trunk/test/Sema/default.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/default.c?rev=42840&r1=42839&r2=42840&view=diff

==============================================================================
--- cfe/trunk/test/Sema/default.c (original)
+++ cfe/trunk/test/Sema/default.c Wed Oct 10 15:50:11 2007
@@ -3,6 +3,6 @@
 void f5 (int z) { 
   if (z) 
     default:  // expected-error {{not in switch statement}}
-      ; 
+      ; // expected-warning {{if statement has empty body}}
 } 
 

Added: cfe/trunk/test/Sema/if-empty-body.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/if-empty-body.c?rev=42840&view=auto

==============================================================================
--- cfe/trunk/test/Sema/if-empty-body.c (added)
+++ cfe/trunk/test/Sema/if-empty-body.c Wed Oct 10 15:50:11 2007
@@ -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) {}
+}





More information about the cfe-commits mailing list