[llvm-branch-commits] [cfe-branch] r119637 - /cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp

Daniel Dunbar daniel at zuster.org
Wed Nov 17 18:37:02 PST 2010


Author: ddunbar
Date: Wed Nov 17 20:37:02 2010
New Revision: 119637

URL: http://llvm.org/viewvc/llvm-project?rev=119637&view=rev
Log:
Merge r118957:
--
Author: Ted Kremenek <kremenek at apple.com>
Date:   Sat Nov 13 00:36:50 2010 +0000

    CursorVisitor: migrate DeclStmt over to data-recursive algorithm.

Modified:
    cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp

Modified: cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp?rev=119637&r1=119636&r2=119637&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp (original)
+++ cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp Wed Nov 17 20:37:02 2010
@@ -153,36 +153,6 @@
   
 typedef llvm::SmallVector<VisitorJob, 10> VisitorWorkList;
 
-#define DEF_JOB(NAME, DATA, KIND)\
-class NAME : public VisitorJob {\
-public:\
-  NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \
-  static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
-  DATA *get() const { return static_cast<DATA*>(dataA); }\
-};
-
-DEF_JOB(DeclVisit, Decl, DeclVisitKind)
-DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
-DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
-DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
-#undef DEF_JOB
-
-class TypeLocVisit : public VisitorJob {
-public:
-  TypeLocVisit(TypeLoc tl, CXCursor parent) :
-    VisitorJob(parent, VisitorJob::TypeLocVisitKind,
-               tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
-
-  static bool classof(const VisitorJob *VJ) {
-    return VJ->getKind() == TypeLocVisitKind;
-  }
-
-  TypeLoc get() { 
-    QualType T = QualType::getFromOpaquePtr(dataA);
-    return TypeLoc(T, dataB);
-  }
-};
-
 // Cursor visitor.
 class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
                       public TypeLocVisitor<CursorVisitor, bool>,
@@ -353,7 +323,6 @@
 
   // Statement visitors
   bool VisitStmt(Stmt *S);
-  bool VisitDeclStmt(DeclStmt *S);
   bool VisitGotoStmt(GotoStmt *S);
 
   // Expression visitors
@@ -384,6 +353,7 @@
   DATA_RECURSIVE_VISIT(CompoundLiteralExpr)
   DATA_RECURSIVE_VISIT(CXXMemberCallExpr)
   DATA_RECURSIVE_VISIT(CXXOperatorCallExpr)
+  DATA_RECURSIVE_VISIT(DeclStmt)
   DATA_RECURSIVE_VISIT(ExplicitCastExpr)
   DATA_RECURSIVE_VISIT(DoStmt)
   DATA_RECURSIVE_VISIT(IfStmt)
@@ -1483,18 +1453,6 @@
   return false;
 }
 
-bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
-  bool isFirst = true;
-  for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
-       D != DEnd; ++D) {
-    if (*D && Visit(MakeCXCursor(*D, TU, isFirst)))
-      return true;
-    isFirst = false;
-  }
-
-  return false;
-}
-
 bool CursorVisitor::VisitGotoStmt(GotoStmt *S) {
   return Visit(MakeCursorLabelRef(S->getLabel(), S->getLabelLoc(), TU));
 }
@@ -1799,6 +1757,47 @@
 //===----------------------------------------------------------------------===//
 
 namespace {
+#define DEF_JOB(NAME, DATA, KIND)\
+class NAME : public VisitorJob {\
+public:\
+  NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \
+  static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
+  DATA *get() const { return static_cast<DATA*>(dataA); }\
+};
+
+DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
+DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
+DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
+#undef DEF_JOB
+
+class DeclVisit : public VisitorJob {
+public:
+  DeclVisit(Decl *d, CXCursor parent, bool isFirst) :
+    VisitorJob(parent, VisitorJob::DeclVisitKind,
+               d, isFirst ? (void*) 1 : (void*) 0) {}
+  static bool classof(const VisitorJob *VJ) {
+    return VJ->getKind () == DeclVisitKind;
+  }
+  Decl *get() { return static_cast<Decl*>(dataA);}
+  bool isFirst() const { return dataB ? true : false; }
+};
+
+class TypeLocVisit : public VisitorJob {
+public:
+  TypeLocVisit(TypeLoc tl, CXCursor parent) :
+    VisitorJob(parent, VisitorJob::TypeLocVisitKind,
+               tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
+
+  static bool classof(const VisitorJob *VJ) {
+    return VJ->getKind() == TypeLocVisitKind;
+  }
+
+  TypeLoc get() { 
+    QualType T = QualType::getFromOpaquePtr(dataA);
+    return TypeLoc(T, dataB);
+  }
+};
+
 class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> {
   VisitorWorkList &WL;
   CXCursor Parent;
@@ -1808,6 +1807,7 @@
 
   void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
   void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
+  void VisitDeclStmt(DeclStmt *S);
   void VisitExplicitCastExpr(ExplicitCastExpr *E);
   void VisitForStmt(ForStmt *FS);
   void VisitIfStmt(IfStmt *If);
@@ -1822,7 +1822,7 @@
 
 private:
   void AddStmt(Stmt *S);
-  void AddDecl(Decl *D);
+  void AddDecl(Decl *D, bool isFirst = true);
   void AddTypeLoc(TypeSourceInfo *TI);
   void EnqueueChildren(Stmt *S);
 };
@@ -1832,9 +1832,9 @@
   if (S)
     WL.push_back(StmtVisit(S, Parent));
 }
-void EnqueueVisitor::AddDecl(Decl *D) {
+void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) {
   if (D)
-    WL.push_back(DeclVisit(D, Parent));
+    WL.push_back(DeclVisit(D, Parent, isFirst));
 }
 void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
   if (TI)
@@ -1865,6 +1865,21 @@
   AddStmt(CE->getCallee());
   AddStmt(CE->getArg(0));
 }
+void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) {
+  unsigned size = WL.size();
+  bool isFirst = true;
+  for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
+       D != DEnd; ++D) {
+    AddDecl(*D, isFirst);
+    isFirst = false;
+  }
+  if (size == WL.size())
+    return;
+  // Now reverse the entries we just added.  This will match the DFS
+  // ordering performed by the worklist.
+  VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
+  std::reverse(I, E);
+}
 void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
   EnqueueChildren(E);
   AddTypeLoc(E->getTypeInfoAsWritten());
@@ -1946,7 +1961,7 @@
           continue;
 
         // For now, perform default visitation for Decls.
-        if (Visit(MakeCXCursor(D, TU)))
+        if (Visit(MakeCXCursor(D, TU, cast<DeclVisit>(LI).isFirst())))
             return true;
 
         continue;





More information about the llvm-branch-commits mailing list