Annotate tokens in a separate thread to avoid blowing out stack space.  While the CursorVisitor
is gradually becoming more data recursive, AnnotateTokensVisitor does its own recursive call
within the visitor that can still blow out the stack.  This can potentially be reworked to avoid this,
but for now just do token annotation on a separate thread.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@118783 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index de28875..f3aa99b 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -235,6 +235,8 @@
     StmtParent = 0;
   }
 
+  ASTUnit *getASTUnit() const { return TU; }
+
   bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
   
   std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
@@ -3998,6 +4000,9 @@
   void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
   enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
   void AnnotateTokens(CXCursor parent);
+  void AnnotateTokens() {
+    AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getASTUnit()));
+  }
 };
 }
 
@@ -4202,6 +4207,11 @@
   return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
 }
 
+// This gets run a separate thread to avoid stack blowout.
+static void runAnnotateTokensWorker(void *UserData) {
+  ((AnnotateTokensWorker*)UserData)->AnnotateTokens();
+}
+
 extern "C" {
 
 void clang_annotateTokens(CXTranslationUnit TU,
@@ -4298,7 +4308,12 @@
   // a specific cursor.
   AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens,
                          CXXUnit, RegionOfInterest);
-  W.AnnotateTokens(clang_getTranslationUnitCursor(CXXUnit));
+
+  // Run the worker within a CrashRecoveryContext.
+  llvm::CrashRecoveryContext CRC;
+  if (!RunSafely(CRC, runAnnotateTokensWorker, &W)) {
+    fprintf(stderr, "libclang: crash detected while annotating tokens\n");
+  }
 }
 } // end: extern "C"