Skip to content

Commit 37b881a

Browse files
committed
clang: Tweak behaviour of warn_empty_while_body and warn_empty_if_body
Use the if/while statement right paren location instead of the end of the condition expression to determine if the semicolon is on its own line, for the purpose of not warning about code like this: while (foo()) ; Using the condition location meant that we would also not report a warning on code like this: while (MACRO(a, b)); body(); The right paren loc wasn't stored in the AST or passed into Sema::ActOnIfStmt when this logic was first written. Reviewed By: rnk, gribozavr2 Differential Revision: https://reviews.llvm.org/D128406
1 parent 8b987ca commit 37b881a

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16673,7 +16673,7 @@ void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
1667316673
Body = FS->getBody();
1667416674
DiagID = diag::warn_empty_for_body;
1667516675
} else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
16676-
StmtLoc = WS->getCond()->getSourceRange().getEnd();
16676+
StmtLoc = WS->getRParenLoc();
1667716677
Body = WS->getBody();
1667816678
DiagID = diag::warn_empty_while_body;
1667916679
} else

clang/lib/Sema/SemaStmt.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,8 +888,7 @@ StmtResult Sema::ActOnIfStmt(SourceLocation IfLoc,
888888
CommaVisitor(*this).Visit(CondExpr);
889889

890890
if (!ConstevalOrNegatedConsteval && !elseStmt)
891-
DiagnoseEmptyStmtBody(CondExpr->getEndLoc(), thenStmt,
892-
diag::warn_empty_if_body);
891+
DiagnoseEmptyStmtBody(RParenLoc, thenStmt, diag::warn_empty_if_body);
893892

894893
if (ConstevalOrNegatedConsteval ||
895894
StatementKind == IfStatementKind::Constexpr) {

clang/test/CXX/stmt.stmt/stmt.select/p3.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ void whileInitStatement() {
6363
// expected-note@-1 {{to match this '('}}
6464
// expected-error@-2 {{expected ';' after expression}}
6565
// expected-error@-3 {{expected expression}}
66-
// expected-warning@-4 {{while loop has empty body}}
67-
// expected-note@-5 {{put the semicolon on a separate line to silence this warning}}
6866
}
6967

7068
// TODO: This is needed because clang can't seem to diagnose invalid syntax after the

clang/test/SemaCXX/warn-empty-body.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ int c();
66

77
#define MACRO_A 0
88

9+
#define AND(x, y) ((x) && (y))
10+
911
void test1(int x, int y) {
1012
while(true) {
1113
if (x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
@@ -15,6 +17,15 @@ void test1(int x, int y) {
1517
if (x == MACRO_A); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
1618
if (MACRO_A == x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
1719

20+
// Check that we handle the case where the condition comes from a macro
21+
// expansion over multiple lines.
22+
if (AND(b(),
23+
c())); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
24+
25+
while (AND(b(),
26+
c())); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
27+
a(0);
28+
1829
int i;
1930
// PR11329
2031
for (i = 0; i < x; i++); { // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}

0 commit comments

Comments
 (0)