[libclang] The annotation of tokens operation visits statement nodes code-recursively.
This can blow the stack with extremely deep hierarchies. Switch it to data-recursive.

This is implemented by introducing a post-children visitation callback that the
CursorVisitor is calling after child nodes of a cursor have been visited.
This is used by the annotate-tokens visitor to do extra work at that point.

rdar://11979525.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@163071 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/libclang/CursorVisitor.h b/tools/libclang/CursorVisitor.h
index 88b70a4..404af94 100644
--- a/tools/libclang/CursorVisitor.h
+++ b/tools/libclang/CursorVisitor.h
@@ -32,7 +32,7 @@
               NestedNameSpecifierLocVisitKind,
               DeclarationNameInfoVisitKind,
               MemberRefVisitKind, SizeOfPackExprPartsKind,
-              LambdaExprPartsKind };
+              LambdaExprPartsKind, PostChildrenVisitKind };
 protected:
   void *data[3];
   CXCursor parent;
@@ -55,6 +55,13 @@
 class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
                       public TypeLocVisitor<CursorVisitor, bool>
 {
+public:
+  /// \brief Callback called after child nodes of a cursor have been visited.
+  /// Return true to break visitation or false to continue.
+  typedef bool (*PostChildrenVisitorTy)(CXCursor cursor,
+                                        CXClientData client_data);
+
+private:
   /// \brief The translation unit we are traversing.
   CXTranslationUnit TU;
   ASTUnit *AU;
@@ -69,6 +76,8 @@
   /// \brief The visitor function.
   CXCursorVisitor Visitor;
 
+  PostChildrenVisitorTy PostChildrenVisitor;
+
   /// \brief The opaque client data, to be passed along to the visitor.
   CXClientData ClientData;
 
@@ -137,9 +146,11 @@
                 bool VisitPreprocessorLast,
                 bool VisitIncludedPreprocessingEntries = false,
                 SourceRange RegionOfInterest = SourceRange(),
-                bool VisitDeclsOnly = false)
+                bool VisitDeclsOnly = false,
+                PostChildrenVisitorTy PostChildrenVisitor = 0)
     : TU(TU), AU(static_cast<ASTUnit*>(TU->TUData)),
-      Visitor(Visitor), ClientData(ClientData),
+      Visitor(Visitor), PostChildrenVisitor(PostChildrenVisitor),
+      ClientData(ClientData),
       VisitPreprocessorLast(VisitPreprocessorLast),
       VisitIncludedEntities(VisitIncludedPreprocessingEntries),
       RegionOfInterest(RegionOfInterest),