[cfe-commits] r92089 - in /cfe/trunk: include/clang/AST/Stmt.h include/clang/AST/StmtIterator.h lib/AST/Stmt.cpp lib/AST/StmtIterator.cpp

Ted Kremenek kremenek at apple.com
Wed Dec 23 15:38:34 PST 2009


Author: kremenek
Date: Wed Dec 23 17:38:34 2009
New Revision: 92089

URL: http://llvm.org/viewvc/llvm-project?rev=92089&view=rev
Log:
Add StmtIterator support for iterating over both the condition
variable initializer and the other expressions in an IfStmt.

This change required adding a 'DoDestroy()' method for IfStmt that did
not include destroying the initializer (since that is owned by the
VarDecl).

Modified:
    cfe/trunk/include/clang/AST/Stmt.h
    cfe/trunk/include/clang/AST/StmtIterator.h
    cfe/trunk/lib/AST/Stmt.cpp
    cfe/trunk/lib/AST/StmtIterator.cpp

Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=92089&r1=92088&r2=92089&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Wed Dec 23 17:38:34 2009
@@ -624,9 +624,9 @@
   SourceLocation ElseLoc;
   
 public:
-  IfStmt(SourceLocation IL, VarDecl *Var, Expr *cond, Stmt *then,
+  IfStmt(SourceLocation IL, VarDecl *var, Expr *cond, Stmt *then,
          SourceLocation EL = SourceLocation(), Stmt *elsev = 0)
-    : Stmt(IfStmtClass), Var(Var), IfLoc(IL), ElseLoc(EL)  {
+    : Stmt(IfStmtClass), Var(var), IfLoc(IL), ElseLoc(EL)  {
     SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
     SubExprs[THEN] = then;
     SubExprs[ELSE] = elsev;
@@ -674,9 +674,13 @@
   }
   static bool classof(const IfStmt *) { return true; }
 
-  // Iterators
+  // Iterators over subexpressions.  The iterators will include iterating
+  // over the initialization expression referenced by the condition variable.
   virtual child_iterator child_begin();
   virtual child_iterator child_end();
+
+protected:
+  virtual void DoDestroy(ASTContext &Ctx);
 };
 
 /// SwitchStmt - This represents a 'switch' stmt.

Modified: cfe/trunk/include/clang/AST/StmtIterator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtIterator.h?rev=92089&r1=92088&r2=92089&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/StmtIterator.h (original)
+++ cfe/trunk/include/clang/AST/StmtIterator.h Wed Dec 23 17:38:34 2009
@@ -66,7 +66,7 @@
   Stmt*& GetDeclExpr() const;
 
   StmtIteratorBase(Stmt **s) : stmt(s), decl(0), RawVAPtr(0) {}
-  StmtIteratorBase(Decl *d);
+  StmtIteratorBase(Decl *d, Stmt **s);
   StmtIteratorBase(VariableArrayType *t);
   StmtIteratorBase(Decl **dgi, Decl **dge);
   StmtIteratorBase() : stmt(0), decl(0), RawVAPtr(0) {}
@@ -82,9 +82,9 @@
   StmtIteratorImpl(const StmtIteratorBase& RHS) : StmtIteratorBase(RHS) {}
 public:
   StmtIteratorImpl() {}
-  StmtIteratorImpl(Stmt** s) : StmtIteratorBase(s) {}
-  StmtIteratorImpl(Decl** dgi, Decl** dge) : StmtIteratorBase(dgi, dge) {}
-  StmtIteratorImpl(Decl* d) : StmtIteratorBase(d) {}
+  StmtIteratorImpl(Stmt **s) : StmtIteratorBase(s) {}
+  StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {}
+  StmtIteratorImpl(Decl *d, Stmt **s) : StmtIteratorBase(d, s) {}
   StmtIteratorImpl(VariableArrayType* t) : StmtIteratorBase(t) {}
 
   DERIVED& operator++() {
@@ -125,11 +125,15 @@
   explicit StmtIterator() : StmtIteratorImpl<StmtIterator,Stmt*&>() {}
 
   StmtIterator(Stmt** S) : StmtIteratorImpl<StmtIterator,Stmt*&>(S) {}
+
   StmtIterator(Decl** dgi, Decl** dge)
    : StmtIteratorImpl<StmtIterator,Stmt*&>(dgi, dge) {}
 
-  StmtIterator(VariableArrayType* t):StmtIteratorImpl<StmtIterator,Stmt*&>(t) {}
-  StmtIterator(Decl* D) : StmtIteratorImpl<StmtIterator,Stmt*&>(D) {}
+  StmtIterator(VariableArrayType* t)
+    : StmtIteratorImpl<StmtIterator,Stmt*&>(t) {}
+
+  StmtIterator(Decl* D, Stmt **s = 0)
+    : StmtIteratorImpl<StmtIterator,Stmt*&>(D, s) {}
 };
 
 struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator,

Modified: cfe/trunk/lib/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Stmt.cpp?rev=92089&r1=92088&r2=92089&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Stmt.cpp (original)
+++ cfe/trunk/lib/AST/Stmt.cpp Wed Dec 23 17:38:34 2009
@@ -455,8 +455,21 @@
 Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
 
 // IfStmt
-Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
-Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
+Stmt::child_iterator IfStmt::child_begin() {
+  return child_iterator(Var, &SubExprs[0]);
+}
+Stmt::child_iterator IfStmt::child_end() {
+  return child_iterator(0, &SubExprs[0]+END_EXPR);
+}
+void IfStmt::DoDestroy(ASTContext &C) {
+  // We do not use child_iterator here because that will include
+  // the expressions referenced by the condition variable.
+  for (Stmt **I = &SubExprs[0], **E = &SubExprs[END_EXPR]; I != E; ++I)
+    if (Stmt *Child = *I) Child->Destroy(C);
+
+  this->~Stmt();
+  C.Deallocate((void *)this);
+}
 
 // SwitchStmt
 Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }

Modified: cfe/trunk/lib/AST/StmtIterator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtIterator.cpp?rev=92089&r1=92088&r2=92089&view=diff

==============================================================================
--- cfe/trunk/lib/AST/StmtIterator.cpp (original)
+++ cfe/trunk/lib/AST/StmtIterator.cpp Wed Dec 23 17:38:34 2009
@@ -65,7 +65,7 @@
   assert (getVAPtr() == NULL);
 
   if (inDecl()) {
-    assert (decl);
+    assert(decl);
 
     // FIXME: SIMPLIFY AWAY.
     if (ImmediateAdvance)
@@ -74,7 +74,7 @@
       return;
   }
   else {
-    assert (inDeclGroup());
+    assert(inDeclGroup());
 
     if (ImmediateAdvance)
       ++DGI;
@@ -113,10 +113,10 @@
   return false;
 }
 
-StmtIteratorBase::StmtIteratorBase(Decl* d)
-  : stmt(0), decl(d), RawVAPtr(DeclMode) {
-  assert (decl);
-  NextDecl(false);
+StmtIteratorBase::StmtIteratorBase(Decl *d, Stmt **s)
+  : stmt(s), decl(d), RawVAPtr(d ? DeclMode : 0) {
+  if (decl)
+    NextDecl(false);
 }
 
 StmtIteratorBase::StmtIteratorBase(Decl** dgi, Decl** dge)





More information about the cfe-commits mailing list