From: Anders Carlsson Date: Wed, 10 Oct 2007 20:50:11 +0000 (+0000) Subject: Emit a warning when the body of an if block is a NullStmt. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2d85f8ba62fd6fdcf0ae303d77112b413d412cae;p=clang 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 b6d4c5580d..460b50ccbf 100644 --- a/Sema/SemaStmt.cpp +++ b/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(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 b5cf88f72c..c7f4908c0d 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 9238e23e63..b71a6a655c 100644 --- a/include/clang/Basic/DiagnosticKinds.def +++ b/include/clang/Basic/DiagnosticKinds.def @@ -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, diff --git a/test/Parser/pointer_promotion.c b/test/Parser/pointer_promotion.c index 9d9a526567..8d2e6e7a80 100644 --- a/test/Parser/pointer_promotion.c +++ b/test/Parser/pointer_promotion.c @@ -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 *')}} } diff --git a/test/Sema/default.c b/test/Sema/default.c index b51ab9a55c..92f7278033 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 0000000000..1dc9e44817 --- /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) {} +}