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).


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92089 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ted Kremenek 2009-12-23 23:38:34 +00:00
Родитель 8a69366a1c
Коммит 35628d1f17
4 изменённых файлов: 38 добавлений и 17 удалений

Просмотреть файл

@ -624,9 +624,9 @@ class IfStmt : public Stmt {
SourceLocation ElseLoc; SourceLocation ElseLoc;
public: 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) 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[COND] = reinterpret_cast<Stmt*>(cond);
SubExprs[THEN] = then; SubExprs[THEN] = then;
SubExprs[ELSE] = elsev; SubExprs[ELSE] = elsev;
@ -674,9 +674,13 @@ public:
} }
static bool classof(const IfStmt *) { return true; } 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_begin();
virtual child_iterator child_end(); virtual child_iterator child_end();
protected:
virtual void DoDestroy(ASTContext &Ctx);
}; };
/// SwitchStmt - This represents a 'switch' stmt. /// SwitchStmt - This represents a 'switch' stmt.

Просмотреть файл

@ -66,7 +66,7 @@ protected:
Stmt*& GetDeclExpr() const; Stmt*& GetDeclExpr() const;
StmtIteratorBase(Stmt **s) : stmt(s), decl(0), RawVAPtr(0) {} StmtIteratorBase(Stmt **s) : stmt(s), decl(0), RawVAPtr(0) {}
StmtIteratorBase(Decl *d); StmtIteratorBase(Decl *d, Stmt **s);
StmtIteratorBase(VariableArrayType *t); StmtIteratorBase(VariableArrayType *t);
StmtIteratorBase(Decl **dgi, Decl **dge); StmtIteratorBase(Decl **dgi, Decl **dge);
StmtIteratorBase() : stmt(0), decl(0), RawVAPtr(0) {} StmtIteratorBase() : stmt(0), decl(0), RawVAPtr(0) {}
@ -82,9 +82,9 @@ protected:
StmtIteratorImpl(const StmtIteratorBase& RHS) : StmtIteratorBase(RHS) {} StmtIteratorImpl(const StmtIteratorBase& RHS) : StmtIteratorBase(RHS) {}
public: public:
StmtIteratorImpl() {} StmtIteratorImpl() {}
StmtIteratorImpl(Stmt** s) : StmtIteratorBase(s) {} StmtIteratorImpl(Stmt **s) : StmtIteratorBase(s) {}
StmtIteratorImpl(Decl** dgi, Decl** dge) : StmtIteratorBase(dgi, dge) {} StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {}
StmtIteratorImpl(Decl* d) : StmtIteratorBase(d) {} StmtIteratorImpl(Decl *d, Stmt **s) : StmtIteratorBase(d, s) {}
StmtIteratorImpl(VariableArrayType* t) : StmtIteratorBase(t) {} StmtIteratorImpl(VariableArrayType* t) : StmtIteratorBase(t) {}
DERIVED& operator++() { DERIVED& operator++() {
@ -125,11 +125,15 @@ struct StmtIterator : public StmtIteratorImpl<StmtIterator,Stmt*&> {
explicit StmtIterator() : StmtIteratorImpl<StmtIterator,Stmt*&>() {} explicit StmtIterator() : StmtIteratorImpl<StmtIterator,Stmt*&>() {}
StmtIterator(Stmt** S) : StmtIteratorImpl<StmtIterator,Stmt*&>(S) {} StmtIterator(Stmt** S) : StmtIteratorImpl<StmtIterator,Stmt*&>(S) {}
StmtIterator(Decl** dgi, Decl** dge) StmtIterator(Decl** dgi, Decl** dge)
: StmtIteratorImpl<StmtIterator,Stmt*&>(dgi, dge) {} : StmtIteratorImpl<StmtIterator,Stmt*&>(dgi, dge) {}
StmtIterator(VariableArrayType* t):StmtIteratorImpl<StmtIterator,Stmt*&>(t) {} StmtIterator(VariableArrayType* t)
StmtIterator(Decl* D) : StmtIteratorImpl<StmtIterator,Stmt*&>(D) {} : StmtIteratorImpl<StmtIterator,Stmt*&>(t) {}
StmtIterator(Decl* D, Stmt **s = 0)
: StmtIteratorImpl<StmtIterator,Stmt*&>(D, s) {}
}; };
struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator, struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator,

Просмотреть файл

@ -455,8 +455,21 @@ Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; } Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
// IfStmt // IfStmt
Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; } Stmt::child_iterator IfStmt::child_begin() {
Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; } 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 // SwitchStmt
Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; } Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }

Просмотреть файл

@ -65,7 +65,7 @@ void StmtIteratorBase::NextDecl(bool ImmediateAdvance) {
assert (getVAPtr() == NULL); assert (getVAPtr() == NULL);
if (inDecl()) { if (inDecl()) {
assert (decl); assert(decl);
// FIXME: SIMPLIFY AWAY. // FIXME: SIMPLIFY AWAY.
if (ImmediateAdvance) if (ImmediateAdvance)
@ -74,7 +74,7 @@ void StmtIteratorBase::NextDecl(bool ImmediateAdvance) {
return; return;
} }
else { else {
assert (inDeclGroup()); assert(inDeclGroup());
if (ImmediateAdvance) if (ImmediateAdvance)
++DGI; ++DGI;
@ -113,9 +113,9 @@ bool StmtIteratorBase::HandleDecl(Decl* D) {
return false; return false;
} }
StmtIteratorBase::StmtIteratorBase(Decl* d) StmtIteratorBase::StmtIteratorBase(Decl *d, Stmt **s)
: stmt(0), decl(d), RawVAPtr(DeclMode) { : stmt(s), decl(d), RawVAPtr(d ? DeclMode : 0) {
assert (decl); if (decl)
NextDecl(false); NextDecl(false);
} }