summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorReid Kleckner <rnk@google.com>2017-11-16 21:26:18 +0000
committerReid Kleckner <rnk@google.com>2017-11-16 21:26:18 +0000
commitadefb760a835a6bc0f6eef4d77d1f50b5082f147 (patch)
treee5ccfc57092d305d4d39dc934417dbdf1ea82cde
parent53418797fd7dc3286ca360d99d44a7baed0d6069 (diff)
downloadbcm5719-llvm-adefb760a835a6bc0f6eef4d77d1f50b5082f147.tar.gz
bcm5719-llvm-adefb760a835a6bc0f6eef4d77d1f50b5082f147.zip
Issue -Wempty-body warnings for else blocks
This looks like it was just an oversight. Fixes http://llvm.org/pr35319 llvm-svn: 318456
-rw-r--r--clang/include/clang/Basic/DiagnosticSemaKinds.td2
-rw-r--r--clang/lib/Sema/SemaStmt.cpp4
-rw-r--r--clang/test/SemaCXX/warn-empty-body.cpp20
3 files changed, 25 insertions, 1 deletions
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index c27b4951687..4542fc91940 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -8090,6 +8090,8 @@ def err_switch_incomplete_class_type : Error<
def warn_empty_if_body : Warning<
"if statement has empty body">, InGroup<EmptyBody>;
+def warn_empty_else_body : Warning<
+ "else clause has empty body">, InGroup<EmptyBody>;
def warn_empty_for_body : Warning<
"for loop has empty body">, InGroup<EmptyBody>;
def warn_empty_range_based_for_body : Warning<
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 3a3eb5e7b5e..07b70305c19 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -527,7 +527,9 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, bool IsConstexpr, Stmt *InitStmt,
CondExpr->getExprLoc()))
CommaVisitor(*this).Visit(CondExpr);
- if (!elseStmt)
+ if (elseStmt)
+ DiagnoseEmptyStmtBody(ElseLoc, elseStmt, diag::warn_empty_else_body);
+ else
DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), thenStmt,
diag::warn_empty_if_body);
diff --git a/clang/test/SemaCXX/warn-empty-body.cpp b/clang/test/SemaCXX/warn-empty-body.cpp
index a248c4251d5..bd6b47f053f 100644
--- a/clang/test/SemaCXX/warn-empty-body.cpp
+++ b/clang/test/SemaCXX/warn-empty-body.cpp
@@ -238,6 +238,26 @@ void test6(int x, int y) {
}
}
+void test_if_else(int x) {
+ if (x); // expected-warning{{if statement has empty body}} expected-note{{separate line}}
+
+ if (x)
+ ; // no-warning
+
+ if (x)
+ ; // no-warning
+ else
+ ; // no-warning
+
+ if (x)
+ ; // no-warning
+ else; // expected-warning{{else clause has empty body}} expected-note{{separate line}}
+
+ if (x)
+ ; // no-warning
+ else EMPTY(x); // no-warning
+}
+
void test_errors(int x) {
if (1)
aa; // expected-error{{use of undeclared identifier}}
OpenPOWER on IntegriCloud