[cfe-commits] r119887 - in /cfe/trunk: include/clang/AST/Stmt.h include/clang/Lex/Preprocessor.h include/clang/Lex/Token.h include/clang/Parse/Parser.h include/clang/Sema/Sema.h lib/AST/Stmt.cpp lib/Lex/PPMacroExpansion.cpp lib/Parse/ParseStmt.cpp lib/Sema/SemaStmt.cpp lib/Sema/TreeTransform.h lib/Serialization/ASTReaderStmt.cpp lib/Serialization/ASTWriterStmt.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Nov 19 18:04:01 PST 2010


Author: akirtzidis
Date: Fri Nov 19 20:04:01 2010
New Revision: 119887

URL: http://llvm.org/viewvc/llvm-project?rev=119887&view=rev
Log:
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!

Modified:
    cfe/trunk/include/clang/AST/Stmt.h
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/include/clang/Lex/Token.h
    cfe/trunk/include/clang/Parse/Parser.h
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/AST/Stmt.cpp
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/lib/Parse/ParseStmt.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/lib/Sema/TreeTransform.h
    cfe/trunk/lib/Serialization/ASTReaderStmt.cpp
    cfe/trunk/lib/Serialization/ASTWriterStmt.cpp

Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Fri Nov 19 20:04:01 2010
@@ -360,8 +360,16 @@
 ///
 class NullStmt : public Stmt {
   SourceLocation SemiLoc;
+
+  /// \brief Whether the null statement was preceded by an empty macro, e.g:
+  /// @code
+  ///   #define CALL(x)
+  ///   CALL(0);
+  /// @endcode
+  bool LeadingEmptyMacro;
 public:
-  NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {}
+  NullStmt(SourceLocation L, bool LeadingEmptyMacro = false)
+    : Stmt(NullStmtClass), SemiLoc(L), LeadingEmptyMacro(LeadingEmptyMacro) {}
 
   /// \brief Build an empty null statement.
   explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { }
@@ -369,6 +377,8 @@
   SourceLocation getSemiLoc() const { return SemiLoc; }
   void setSemiLoc(SourceLocation L) { SemiLoc = L; }
 
+  bool hasLeadingEmptyMacro() const { return LeadingEmptyMacro; }
+
   virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
 
   static bool classof(const Stmt *T) {
@@ -379,6 +389,9 @@
   // Iterators
   virtual child_iterator child_begin();
   virtual child_iterator child_end();
+
+  friend class ASTStmtReader;
+  friend class ASTStmtWriter;
 };
 
 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
@@ -651,19 +664,10 @@
 
   SourceLocation IfLoc;
   SourceLocation ElseLoc;
-
-  /// \brief True if we have code like:
-  /// @code
-  ///   #define CALL(x)
-  ///   if (condition)
-  ///     CALL(0);
-  /// @endcode
-  bool MacroExpandedInThenStmt;
-
+  
 public:
   IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond, 
-         Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0,
-         bool macroExpandedInThenStmt = false);
+         Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
   
   /// \brief Build an empty if/then/else statement
   explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
@@ -695,8 +699,6 @@
   SourceLocation getElseLoc() const { return ElseLoc; }
   void setElseLoc(SourceLocation L) { ElseLoc = L; }
 
-  bool hasMacroExpandedInThenStmt() const { return MacroExpandedInThenStmt; }
-
   virtual SourceRange getSourceRange() const {
     if (SubExprs[ELSE])
       return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
@@ -713,9 +715,6 @@
   // over the initialization expression referenced by the condition variable.
   virtual child_iterator child_begin();
   virtual child_iterator child_end();
-
-  friend class ASTStmtReader;
-  friend class ASTStmtWriter;
 };
 
 /// SwitchStmt - This represents a 'switch' stmt.

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Fri Nov 19 20:04:01 2010
@@ -47,7 +47,6 @@
 class CodeCompletionHandler;
 class DirectoryLookup;
 class PreprocessingRecord;
-class PPMacroExpansionTrap;
   
 /// Preprocessor - This object engages in a tight little dance with the lexer to
 /// efficiently preprocess tokens.  Lexers know only about tokens within a
@@ -111,11 +110,6 @@
   /// DisableMacroExpansion - True if macro expansion is disabled.
   bool DisableMacroExpansion : 1;
 
-  /// \brief This is set to true when a macro is expanded.
-  /// Used by PPMacroExpansionTrap.
-  bool MacroExpansionFlag : 1;
-  friend class PPMacroExpansionTrap;
-
   /// \brief Whether we have already loaded macros from the external source.
   mutable bool ReadMacrosFromExternalSource : 1;
 
@@ -1035,17 +1029,6 @@
   virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
 };
 
-/// \brief RAII class that determines when any macro expansion has occurred
-/// between the time the instance was created and the time it was
-/// queried.
-class PPMacroExpansionTrap {
-  Preprocessor &PP;
-public:
-  PPMacroExpansionTrap(Preprocessor &PP) : PP(PP) { reset(); }
-  bool hasMacroExpansionOccured() const { return PP.MacroExpansionFlag; }
-  void reset() { PP.MacroExpansionFlag = false; }
-};
-
 }  // end namespace clang
 
 #endif

Modified: cfe/trunk/include/clang/Lex/Token.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Token.h?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Token.h (original)
+++ cfe/trunk/include/clang/Lex/Token.h Fri Nov 19 20:04:01 2010
@@ -76,7 +76,8 @@
     StartOfLine   = 0x01,  // At start of line or only after whitespace.
     LeadingSpace  = 0x02,  // Whitespace exists before this token.
     DisableExpand = 0x04,  // This identifier may never be macro expanded.
-    NeedsCleaning = 0x08   // Contained an escaped newline or trigraph.
+    NeedsCleaning = 0x08,   // Contained an escaped newline or trigraph.
+    LeadingEmptyMacro = 0x10 // Empty macro exists before this token.
   };
 
   tok::TokenKind getKind() const { return (tok::TokenKind)Kind; }
@@ -231,7 +232,13 @@
   /// newlines in it.
   ///
   bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; }
-    
+
+  /// \brief Return true if this token has an empty macro before it.
+  ///
+  bool hasLeadingEmptyMacro() const {
+    return (Flags & LeadingEmptyMacro) ? true : false;
+  }
+
 };
 
 /// PPConditionalInfo - Information about the conditional stack (#if directives)

Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Fri Nov 19 20:04:01 2010
@@ -1199,8 +1199,7 @@
   bool ParseParenExprOrCondition(ExprResult &ExprResult,
                                  Decl *&DeclResult,
                                  SourceLocation Loc,
-                                 bool ConvertToBoolean,
-                                 bool *MacroExpandedAfterRParen = 0);
+                                 bool ConvertToBoolean);
   StmtResult ParseIfStatement(AttributeList *Attr);
   StmtResult ParseSwitchStatement(AttributeList *Attr);
   StmtResult ParseWhileStatement(AttributeList *Attr);

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Fri Nov 19 20:04:01 2010
@@ -1566,7 +1566,8 @@
 
   StmtResult ActOnExprStmt(FullExprArg Expr);
 
-  StmtResult ActOnNullStmt(SourceLocation SemiLoc);
+  StmtResult ActOnNullStmt(SourceLocation SemiLoc,
+                           bool LeadingEmptyMacro = false);
   StmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R,
                                        MultiStmtArg Elts,
                                        bool isStmtExpr);
@@ -1590,7 +1591,7 @@
                             bool HasUnusedAttr);
   StmtResult ActOnIfStmt(SourceLocation IfLoc,
                                  FullExprArg CondVal, Decl *CondVar,
-                                 Stmt *ThenVal, bool MacroExpandedInThenStmt,
+                                 Stmt *ThenVal,
                                  SourceLocation ElseLoc, Stmt *ElseVal);
   StmtResult ActOnStartOfSwitchStmt(SourceLocation SwitchLoc,
                                             Expr *Cond,

Modified: cfe/trunk/lib/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Stmt.cpp?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Stmt.cpp (original)
+++ cfe/trunk/lib/AST/Stmt.cpp Fri Nov 19 20:04:01 2010
@@ -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);

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Fri Nov 19 20:04:01 2010
@@ -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;
 

Modified: cfe/trunk/lib/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseStmt.cpp?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Fri Nov 19 20:04:01 2010
@@ -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

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri Nov 19 20:04:01 2010
@@ -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

Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Fri Nov 19 20:04:01 2010
@@ -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());
 }
 

Modified: cfe/trunk/lib/Serialization/ASTReaderStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReaderStmt.cpp?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReaderStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReaderStmt.cpp Fri Nov 19 20:04:01 2010
@@ -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) {

Modified: cfe/trunk/lib/Serialization/ASTWriterStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriterStmt.cpp?rev=119887&r1=119886&r2=119887&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriterStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriterStmt.cpp Fri Nov 19 20:04:01 2010
@@ -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;
 }
 





More information about the cfe-commits mailing list