[cfe-commits] r43105 - in /cfe/trunk: AST/ExprCXX.cpp AST/Stmt.cpp AST/StmtIterator.cpp include/clang/AST/Decl.h include/clang/AST/Stmt.h include/clang/AST/StmtGraphTraits.h include/clang/AST/StmtIterator.h

Ted Kremenek kremenek at apple.com
Wed Oct 17 17:24:38 PDT 2007


Author: kremenek
Date: Wed Oct 17 19:24:38 2007
New Revision: 43105

URL: http://llvm.org/viewvc/llvm-project?rev=43105&view=rev
Log:
Implemented 90% functionality of new child_iterator for Stmt objects
that will (soon) allow iteration over the initializers in
declarations.  This new iterator mechanism is implemented by the
classes StmtIterator and ConstStmtIterator.

Patched a few files to use "operator++" instead of "operator+" on
child_iterators.

Friendship added in VarDecl to StmtIterator to allow returning a
reference to the initializer within the VarDecl.  We may not wish this
as a permanent solution.

Added:
    cfe/trunk/AST/StmtIterator.cpp
    cfe/trunk/include/clang/AST/StmtIterator.h
Modified:
    cfe/trunk/AST/ExprCXX.cpp
    cfe/trunk/AST/Stmt.cpp
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/Stmt.h
    cfe/trunk/include/clang/AST/StmtGraphTraits.h

Modified: cfe/trunk/AST/ExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/ExprCXX.cpp?rev=43105&r1=43104&r2=43105&view=diff

==============================================================================
--- cfe/trunk/AST/ExprCXX.cpp (original)
+++ cfe/trunk/AST/ExprCXX.cpp Wed Oct 17 19:24:38 2007
@@ -25,7 +25,7 @@
 }
 
 Stmt::child_iterator CXXCastExpr::child_end() {
-  return child_begin()+1;
+  return ++child_begin();
 }
 
 // CXXBoolLiteralExpr

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

==============================================================================
--- cfe/trunk/AST/Stmt.cpp (original)
+++ cfe/trunk/AST/Stmt.cpp Wed Oct 17 19:24:38 2007
@@ -168,7 +168,7 @@
   return reinterpret_cast<Stmt**>(&Target); 
 }
 
-Stmt::child_iterator IndirectGotoStmt::child_end() { return child_begin()+1; }
+Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); }
 
 // ContinueStmt
 Stmt::child_iterator ContinueStmt::child_begin() { return NULL; }

Added: cfe/trunk/AST/StmtIterator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtIterator.cpp?rev=43105&view=auto

==============================================================================
--- cfe/trunk/AST/StmtIterator.cpp (added)
+++ cfe/trunk/AST/StmtIterator.cpp Wed Oct 17 19:24:38 2007
@@ -0,0 +1,27 @@
+//===--- StmtIterator.cpp - Iterators for Statements ------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Ted Kremenek and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines internal methods for StmtIterator.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/StmtIterator.h"
+#include "clang/AST/Stmt.h"
+#include "clang/AST/Decl.h"
+
+using namespace clang;
+
+void StmtIterator::NextDecl() { assert(false); }
+void StmtIterator::PrevDecl() { assert(false); }
+
+Stmt*& StmtIterator::GetInitializer() const {
+  assert (D && isa<VarDecl>(D));
+  assert (cast<VarDecl>(D)->Init);
+  return reinterpret_cast<Stmt*&>(cast<VarDecl>(D)->Init);
+}

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

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Wed Oct 17 19:24:38 2007
@@ -268,7 +268,8 @@
     : ValueDecl(DK, L, Id, T, PrevDecl), Init(0) { SClass = SC; }
 private:
   StorageClass SClass;
-  Expr *Init;
+  Expr *Init;  
+  friend class StmtIterator;
 };
 
 /// BlockVarDecl - Represent a local variable declaration.

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

==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Wed Oct 17 19:24:38 2007
@@ -15,6 +15,7 @@
 #define LLVM_CLANG_AST_STMT_H
 
 #include "clang/Basic/SourceLocation.h"
+#include "clang/AST/StmtIterator.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/iterator"
 #include <iosfwd>
@@ -95,23 +96,24 @@
   /// Child Iterators: All subclasses must implement child_begin and child_end
   ///  to permit easy iteration over the substatements/subexpessions of an
   ///  AST node.  This permits easy iteration over all nodes in the AST.
-  typedef Stmt**                                               child_iterator;
-  typedef Stmt* const *                                  const_child_iterator;
+  typedef StmtIterator       child_iterator;
+  typedef ConstStmtIterator  const_child_iterator;
   
   typedef std::reverse_iterator<child_iterator>                
-  reverse_child_iterator;
+          reverse_child_iterator;
+  
   typedef std::reverse_iterator<const_child_iterator> 
-  const_reverse_child_iterator;
+          const_reverse_child_iterator;
   
   virtual child_iterator child_begin() = 0;
   virtual child_iterator child_end()   = 0;
   
   const_child_iterator child_begin() const {
-    return (child_iterator) const_cast<Stmt*>(this)->child_begin();
+    return const_child_iterator(const_cast<Stmt*>(this)->child_begin());
   }
   
   const_child_iterator child_end() const {
-    return (child_iterator) const_cast<Stmt*>(this)->child_end();  
+    return const_child_iterator(const_cast<Stmt*>(this)->child_end());
   }
   
   reverse_child_iterator child_rbegin() {

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

==============================================================================
--- cfe/trunk/include/clang/AST/StmtGraphTraits.h (original)
+++ cfe/trunk/include/clang/AST/StmtGraphTraits.h Wed Oct 17 19:24:38 2007
@@ -60,12 +60,12 @@
   
   static inline ChildIteratorType child_begin(NodeType* N) {
     if (N) return N->child_begin();
-    else return NULL;    
+    else return ChildIteratorType(NULL);    
   }
   
   static inline ChildIteratorType child_end(NodeType* N) {
     if (N) return N->child_end();
-    else return NULL;
+    else return ChildIteratorType(NULL);
   }
   
   static nodes_iterator nodes_begin(const clang::Stmt* S) {

Added: cfe/trunk/include/clang/AST/StmtIterator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtIterator.h?rev=43105&view=auto

==============================================================================
--- cfe/trunk/include/clang/AST/StmtIterator.h (added)
+++ cfe/trunk/include/clang/AST/StmtIterator.h Wed Oct 17 19:24:38 2007
@@ -0,0 +1,101 @@
+//===--- StmtIterator.h - Iterators for Statements ------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Ted Kremenek and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the StmtIterator and ConstStmtIterator classes.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_AST_STMT_ITR_H
+#define LLVM_CLANG_AST_STMT_ITR_H
+
+#include "llvm/ADT/iterator"
+
+namespace clang {
+
+class Stmt;
+class ScopedDecl;
+  
+class StmtIterator : public bidirectional_iterator<Stmt*, ptrdiff_t> {
+  Stmt** S;
+  ScopedDecl* D;
+  
+  void NextDecl();
+  void PrevDecl();
+  Stmt*& GetInitializer() const;
+public:  
+  StmtIterator(Stmt** s, ScopedDecl* d = NULL) : S(s), D(d) {}
+  
+  StmtIterator& operator++() { 
+    if (D) NextDecl();
+    else ++S;
+      
+    return *this;
+  }
+    
+  StmtIterator operator++(int) {
+    StmtIterator tmp = *this;
+    operator++();
+    return tmp;
+  }
+  
+  StmtIterator& operator--() {
+    if (D) PrevDecl();
+    else --S;
+    
+    return *this;
+  }
+  
+  StmtIterator operator--(int) {
+    StmtIterator tmp = *this;
+    operator--();
+    return tmp;
+  }
+  
+  reference operator*() const { return D ? GetInitializer() : *S; }
+  pointer operator->() const { return D ? &GetInitializer() : S; }
+
+  bool operator==(const StmtIterator& RHS) const {
+    return D == RHS.D && S == RHS.S;
+  }
+  
+  bool operator!=(const StmtIterator& RHS) const {
+    return D != RHS.D || S != RHS.S;
+  }
+};
+  
+class ConstStmtIterator: public bidirectional_iterator<const Stmt*, ptrdiff_t> {
+  StmtIterator I;
+public:
+  explicit ConstStmtIterator(const StmtIterator& i) : I(i) {}
+
+  ConstStmtIterator& operator++() { ++I; return *this; }
+  ConstStmtIterator& operator--() { --I; return *this; }
+
+  ConstStmtIterator operator++(int) {
+    ConstStmtIterator tmp = *this;
+    operator++();
+    return tmp;
+  }
+  
+  ConstStmtIterator operator--(int) {
+    ConstStmtIterator tmp = *this;
+    operator--();
+    return tmp;
+  }
+  
+  reference operator*() const { return const_cast<reference>(*I); }
+  pointer operator->() const { return const_cast<pointer>(I.operator->()); }
+  
+  bool operator==(const ConstStmtIterator& RHS) const { return I == RHS.I; }
+  bool operator!=(const ConstStmtIterator& RHS) const { return I != RHS.I; }
+};
+  
+} // end namespace clang
+
+#endif





More information about the cfe-commits mailing list