]> granicus.if.org Git - clang/commitdiff
Emit a warning when the body of an if block is a NullStmt.
authorAnders Carlsson <andersca@mac.com>
Wed, 10 Oct 2007 20:50:11 +0000 (20:50 +0000)
committerAnders Carlsson <andersca@mac.com>
Wed, 10 Oct 2007 20:50:11 +0000 (20:50 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42840 91177308-0d34-0410-b5e6-96231b3b80d8

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 [new file with mode: 0644]

index b6d4c5580d46f88f597b6e8c2de9004db271ee4a..460b50ccbf349b04def77ec2864b0b8068d58f12 100644 (file)
@@ -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
index b5cf88f72c9ec1467023ec917411ee3b9a655754..c7f4908c0dd08acbc9c27dc74a05137725f0b6b4 100644 (file)
                08FB7793FE84155DC02AAC07 /* Project object */ = {
                        isa = PBXProject;
                        buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
+                       compatibilityVersion = "Xcode 2.4";
                        hasScannedForEncodings = 1;
                        mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
                        projectDirPath = "";
index 9238e23e637441914b8b1228553a364637e3661f..b71a6a655c1baad82793c772aaf63cf6c78933b5 100644 (file)
@@ -838,7 +838,9 @@ DIAG(err_typecheck_statement_requires_integer, ERROR,
     "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,
index 9d9a5265676df3e3db1552e2d1afc35661b3fcad..8d2e6e7a80c9ddf7e8947131cab444d7029cac1c 100644 (file)
@@ -8,11 +8,11 @@ int test() {
   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 *')}}
 }
 
index b51ab9a55cca8c2be5963545b4d264f1201fb183..92f7278033f52dc1fe511eb919b50cf58a07758d 100644 (file)
@@ -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 (file)
index 0000000..1dc9e44
--- /dev/null
@@ -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) {}
+}