Revert r119838 "Don't warn for empty 'if' body if there is a macro that expands to nothing"
and use a better and more general approach, where NullStmt has a flag to indicate whether it was preceded by an empty macro.
Thanks to Abramo Bagnara for the hint!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@119887 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp
index 85e6407..acd77be 100644
--- a/lib/AST/Stmt.cpp
+++ b/lib/AST/Stmt.cpp
@@ -470,10 +470,8 @@
}
IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
- Stmt *then, SourceLocation EL, Stmt *elsev,
- bool macroExpandedInThenStmt)
- : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL),
- MacroExpandedInThenStmt(macroExpandedInThenStmt)
+ Stmt *then, SourceLocation EL, Stmt *elsev)
+ : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
{
setConditionVariable(C, var);
SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp
index 6d2c387..d3f3db3 100644
--- a/lib/Lex/PPMacroExpansion.cpp
+++ b/lib/Lex/PPMacroExpansion.cpp
@@ -176,7 +176,6 @@
/// expanded as a macro, handle it and return the next token as 'Identifier'.
bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
MacroInfo *MI) {
- MacroExpansionFlag = true;
if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
// If this is a macro expansion in the "#if !defined(x)" line for the file,
@@ -249,6 +248,7 @@
if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
}
+ Identifier.setFlag(Token::LeadingEmptyMacro);
++NumFastMacroExpanded;
return false;
diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp
index e3c1568..26d2279 100644
--- a/lib/Parse/ParseStmt.cpp
+++ b/lib/Parse/ParseStmt.cpp
@@ -147,8 +147,10 @@
case tok::l_brace: // C99 6.8.2: compound-statement
return ParseCompoundStatement(AttrList);
- case tok::semi: // C99 6.8.3p3: expression[opt] ';'
- return Actions.ActOnNullStmt(ConsumeToken());
+ case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
+ bool LeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
+ return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacro);
+ }
case tok::kw_if: // C99 6.8.4.1: if-statement
return ParseIfStatement(AttrList);
@@ -538,8 +540,7 @@
bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
Decl *&DeclResult,
SourceLocation Loc,
- bool ConvertToBoolean,
- bool *MacroExpandedAfterRParen) {
+ bool ConvertToBoolean) {
bool ParseError = false;
SourceLocation LParenLoc = ConsumeParen();
@@ -568,14 +569,7 @@
}
// Otherwise the condition is valid or the rparen is present.
-
- // Catch a macro expansion after ')'. This is used to know that there is a
- // macro for 'if' body and not warn for empty body if the macro is empty.
- PPMacroExpansionTrap MacroExpansionTrap(PP);
MatchRHSPunctuation(tok::r_paren, LParenLoc);
- if (MacroExpandedAfterRParen)
- *MacroExpandedAfterRParen = MacroExpansionTrap.hasMacroExpansionOccured();
-
return false;
}
@@ -618,9 +612,7 @@
// Parse the condition.
ExprResult CondExp;
Decl *CondVar = 0;
- bool MacroExpandedInThenStmt;
- if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true,
- &MacroExpandedInThenStmt))
+ if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
return StmtError();
FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
@@ -704,7 +696,7 @@
ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
- MacroExpandedInThenStmt, ElseLoc, ElseStmt.get());
+ ElseLoc, ElseStmt.get());
}
/// ParseSwitchStatement
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index c6194ed..68b7a8c 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -42,8 +42,8 @@
}
-StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
- return Owned(new (Context) NullStmt(SemiLoc));
+StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc, bool LeadingEmptyMacro) {
+ return Owned(new (Context) NullStmt(SemiLoc, LeadingEmptyMacro));
}
StmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
@@ -282,8 +282,8 @@
StmtResult
Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar,
- Stmt *thenStmt, bool MacroExpandedInThenStmt,
- SourceLocation ElseLoc, Stmt *elseStmt) {
+ Stmt *thenStmt, SourceLocation ElseLoc,
+ Stmt *elseStmt) {
ExprResult CondResult(CondVal.release());
VarDecl *ConditionVar = 0;
@@ -312,15 +312,14 @@
// if (condition)
// CALL(0);
//
- if (!MacroExpandedInThenStmt)
+ if (!stmt->hasLeadingEmptyMacro())
Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
}
DiagnoseUnusedExprResult(elseStmt);
return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr,
- thenStmt, ElseLoc, elseStmt,
- MacroExpandedInThenStmt));
+ thenStmt, ElseLoc, elseStmt));
}
/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h
index 3ae4e5c..807346c 100644
--- a/lib/Sema/TreeTransform.h
+++ b/lib/Sema/TreeTransform.h
@@ -772,11 +772,9 @@
/// By default, performs semantic analysis to build the new statement.
/// Subclasses may override this routine to provide different behavior.
StmtResult RebuildIfStmt(SourceLocation IfLoc, Sema::FullExprArg Cond,
- VarDecl *CondVar, Stmt *Then,
- bool MacroExpandedInThenStmt,
+ VarDecl *CondVar, Stmt *Then,
SourceLocation ElseLoc, Stmt *Else) {
- return getSema().ActOnIfStmt(IfLoc, Cond, CondVar, Then,
- MacroExpandedInThenStmt, ElseLoc, Else);
+ return getSema().ActOnIfStmt(IfLoc, Cond, CondVar, Then, ElseLoc, Else);
}
/// \brief Start building a new switch statement.
@@ -3694,7 +3692,7 @@
return SemaRef.Owned(S);
return getDerived().RebuildIfStmt(S->getIfLoc(), FullCond, ConditionVar,
- Then.get(), S->hasMacroExpandedInThenStmt(),
+ Then.get(),
S->getElseLoc(), Else.get());
}
diff --git a/lib/Serialization/ASTReaderStmt.cpp b/lib/Serialization/ASTReaderStmt.cpp
index a7b42cd..5aba1f8 100644
--- a/lib/Serialization/ASTReaderStmt.cpp
+++ b/lib/Serialization/ASTReaderStmt.cpp
@@ -202,6 +202,7 @@
void ASTStmtReader::VisitNullStmt(NullStmt *S) {
VisitStmt(S);
S->setSemiLoc(ReadSourceLocation(Record, Idx));
+ S->LeadingEmptyMacro = Record[Idx++];
}
void ASTStmtReader::VisitCompoundStmt(CompoundStmt *S) {
@@ -256,7 +257,6 @@
S->setElse(Reader.ReadSubStmt());
S->setIfLoc(ReadSourceLocation(Record, Idx));
S->setElseLoc(ReadSourceLocation(Record, Idx));
- S->MacroExpandedInThenStmt = Record[Idx++];
}
void ASTStmtReader::VisitSwitchStmt(SwitchStmt *S) {
diff --git a/lib/Serialization/ASTWriterStmt.cpp b/lib/Serialization/ASTWriterStmt.cpp
index a59b772..4d2fb8a 100644
--- a/lib/Serialization/ASTWriterStmt.cpp
+++ b/lib/Serialization/ASTWriterStmt.cpp
@@ -171,6 +171,7 @@
void ASTStmtWriter::VisitNullStmt(NullStmt *S) {
VisitStmt(S);
Writer.AddSourceLocation(S->getSemiLoc(), Record);
+ Record.push_back(S->LeadingEmptyMacro);
Code = serialization::STMT_NULL;
}
@@ -228,7 +229,6 @@
Writer.AddStmt(S->getElse());
Writer.AddSourceLocation(S->getIfLoc(), Record);
Writer.AddSourceLocation(S->getElseLoc(), Record);
- Record.push_back(S->MacroExpandedInThenStmt);
Code = serialization::STMT_IF;
}