From 14f8b4ff660bcaa763974b8d0fae81857c594495 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Tue, 5 Aug 2008 20:46:55 +0000 Subject: [PATCH] Added decl_iterator to DeclStmt to provide an abstract interface to iterate over the ScopedDecls of a DeclStmt. Updated a few clients of DeclStmt::getNextDeclarator() to use decl_iterator instead. Will update other clients after additional testing. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54368 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Stmt.h | 17 ++++++++++++++++- lib/AST/Stmt.cpp | 5 +++++ lib/Analysis/CheckDeadStores.cpp | 5 +++-- lib/Analysis/LiveVariables.cpp | 5 +++-- lib/Analysis/UninitializedValues.cpp | 4 ++-- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 6db454479f..0461dab890 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -162,10 +162,25 @@ public: } static bool classof(const DeclStmt *) { return true; } - // Iterators + // Iterators over subexpressions. virtual child_iterator child_begin(); virtual child_iterator child_end(); + // Iterators over the decls. + class decl_iterator { + ScopedDecl* D; + public: + decl_iterator(ScopedDecl *d) : D(d) {} + bool operator==(const decl_iterator& I) const { return D == I.D; } + bool operator!=(const decl_iterator& I) const { return D != I.D; } + ScopedDecl* operator*() const { return D; } + decl_iterator& operator++(); + }; + + virtual decl_iterator decl_begin() { return TheDecl; } + virtual decl_iterator decl_end() { return 0; } + + // Serialization. virtual void EmitImpl(llvm::Serializer& S) const; static DeclStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); }; diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp index d3f3ff0e11..76538a8b18 100644 --- a/lib/AST/Stmt.cpp +++ b/lib/AST/Stmt.cpp @@ -193,6 +193,11 @@ ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc, Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); } Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); } +DeclStmt::decl_iterator& DeclStmt::decl_iterator::operator++() { + D = D->getNextDeclarator(); + return *this; +} + // NullStmt Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); } Stmt::child_iterator NullStmt::child_end() { return child_iterator(); } diff --git a/lib/Analysis/CheckDeadStores.cpp b/lib/Analysis/CheckDeadStores.cpp index f0d3dd1b62..38078e3823 100644 --- a/lib/Analysis/CheckDeadStores.cpp +++ b/lib/Analysis/CheckDeadStores.cpp @@ -161,9 +161,10 @@ public: else if (DeclStmt* DS = dyn_cast(S)) // Iterate through the decls. Warn if any initializers are complex // expressions that are not live (never used). - for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) { + for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); + DI != DE; ++DI) { - VarDecl* V = dyn_cast(SD); + VarDecl* V = dyn_cast(*DI); if (!V) continue; diff --git a/lib/Analysis/LiveVariables.cpp b/lib/Analysis/LiveVariables.cpp index 8f391dc561..f5a7fb40c7 100644 --- a/lib/Analysis/LiveVariables.cpp +++ b/lib/Analysis/LiveVariables.cpp @@ -229,8 +229,9 @@ void TransferFuncs::VisitAssign(BinaryOperator* B) { void TransferFuncs::VisitDeclStmt(DeclStmt* DS) { // Declarations effectively "kill" a variable since they cannot // possibly be live before they are declared. - for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator()) - if (VarDecl* VD = dyn_cast(D)) { + for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end(); + DI != DE; ++DI) + if (VarDecl* VD = dyn_cast(*DI)) { // Update liveness information. unsigned bit = AD.getIdx(VD); diff --git a/lib/Analysis/UninitializedValues.cpp b/lib/Analysis/UninitializedValues.cpp index 15c11ba6b0..83f1a7083e 100644 --- a/lib/Analysis/UninitializedValues.cpp +++ b/lib/Analysis/UninitializedValues.cpp @@ -131,8 +131,8 @@ bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { } bool TransferFuncs::VisitDeclStmt(DeclStmt* S) { - for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator()) { - VarDecl *VD = dyn_cast(D); + for (DeclStmt::decl_iterator I=S->decl_begin(), E=S->decl_end(); I!=E; ++I) { + VarDecl *VD = dyn_cast(*I); if (VD && VD->isBlockVarDecl()) { if (Stmt* I = VD->getInit()) V(VD,AD) = AD.FullUninitTaint ? V(cast(I),AD) : Initialized; -- 2.40.0