[libclang] Indexing: only index implicit template instantiations via an opt-in indexing option.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150517 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/libclang/IndexBody.cpp b/tools/libclang/IndexBody.cpp
index c400491..684d5ce 100644
--- a/tools/libclang/IndexBody.cpp
+++ b/tools/libclang/IndexBody.cpp
@@ -97,7 +97,7 @@
   }
 
   bool VisitDeclStmt(DeclStmt *S) {
-    if (IndexCtx.indexFunctionLocalSymbols())
+    if (IndexCtx.shouldIndexFunctionLocalSymbols())
       IndexCtx.indexDeclGroupRef(S->getDeclGroup());
     return true;
   }
diff --git a/tools/libclang/IndexDecl.cpp b/tools/libclang/IndexDecl.cpp
index 55ae9e0..dcc9a1d 100644
--- a/tools/libclang/IndexDecl.cpp
+++ b/tools/libclang/IndexDecl.cpp
@@ -26,7 +26,7 @@
   void handleDeclarator(DeclaratorDecl *D, const NamedDecl *Parent = 0) {
     if (!Parent) Parent = D;
 
-    if (!IndexCtx.indexFunctionLocalSymbols()) {
+    if (!IndexCtx.shouldIndexFunctionLocalSymbols()) {
       IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent);
       IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
     } else {
@@ -245,9 +245,11 @@
 
   bool VisitClassTemplateSpecializationDecl(
                                            ClassTemplateSpecializationDecl *D) {
-    // FIXME: Notify subsequent callbacks that info comes from implicit
+    // FIXME: Notify subsequent callbacks if info comes from implicit
     // instantiation.
-    if (D->isThisDeclarationADefinition())
+    if (D->isThisDeclarationADefinition() &&
+        (IndexCtx.shouldIndexImplicitTemplateInsts() ||
+         !IndexCtx.isTemplateImplicitInstantiation(D)))
       IndexCtx.indexTagDecl(D);
     return true;
   }
diff --git a/tools/libclang/IndexTypeSourceInfo.cpp b/tools/libclang/IndexTypeSourceInfo.cpp
index 1e753781..34acffc 100644
--- a/tools/libclang/IndexTypeSourceInfo.cpp
+++ b/tools/libclang/IndexTypeSourceInfo.cpp
@@ -73,9 +73,15 @@
 
   bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
     if (const TemplateSpecializationType *T = TL.getTypePtr()) {
-      if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
-        IndexCtx.handleReference(RD, TL.getTemplateNameLoc(),
-                                 Parent, ParentDC);
+      if (IndexCtx.shouldIndexImplicitTemplateInsts()) {
+        if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
+          IndexCtx.handleReference(RD, TL.getTemplateNameLoc(),
+                                   Parent, ParentDC);
+      } else {
+        if (const TemplateDecl *D = T->getTemplateName().getAsTemplateDecl())
+          IndexCtx.handleReference(D, TL.getTemplateNameLoc(),
+                                   Parent, ParentDC);
+      }
     }
     return true;
   }
diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp
index 0d6da28..d1af388 100644
--- a/tools/libclang/Indexing.cpp
+++ b/tools/libclang/Indexing.cpp
@@ -137,11 +137,17 @@
   virtual void HandleInterestingDecl(DeclGroupRef D) {}
 
   virtual void HandleTagDeclDefinition(TagDecl *D) {
+    if (!IndexCtx.shouldIndexImplicitTemplateInsts())
+      return;
+
     if (IndexCtx.isTemplateImplicitInstantiation(D))
       IndexCtx.indexDecl(D);
   }
 
   virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) {
+    if (!IndexCtx.shouldIndexImplicitTemplateInsts())
+      return;
+
     IndexCtx.indexDecl(D);
   }
 };
@@ -194,7 +200,12 @@
     indexDiagnostics(CXTU, IndexCtx);
   }
 
-  virtual TranslationUnitKind getTranslationUnitKind() { return TU_Complete; }
+  virtual TranslationUnitKind getTranslationUnitKind() {
+    if (IndexCtx.shouldIndexImplicitTemplateInsts())
+      return TU_Complete;
+    else
+      return TU_Prefix;
+  }
   virtual bool hasCodeCompletionSupport() const { return false; }
 };
 
diff --git a/tools/libclang/IndexingContext.cpp b/tools/libclang/IndexingContext.cpp
index 0493f93..75184dd 100644
--- a/tools/libclang/IndexingContext.cpp
+++ b/tools/libclang/IndexingContext.cpp
@@ -35,7 +35,7 @@
                                 IdxCtx.getIndexLoc(Loc) };
     ProtInfos.push_back(ProtInfo);
 
-    if (IdxCtx.suppressRefs())
+    if (IdxCtx.shouldSuppressRefs())
       IdxCtx.markEntityOccurrenceInFile(PD, Loc);
   }
 
@@ -265,11 +265,11 @@
 
   ScratchAlloc SA(*this);
   getEntityInfo(D, DInfo.EntInfo, SA);
-  if ((!indexFunctionLocalSymbols() && !DInfo.EntInfo.USR)
+  if ((!shouldIndexFunctionLocalSymbols() && !DInfo.EntInfo.USR)
       || Loc.isInvalid())
     return false;
 
-  if (suppressRefs())
+  if (shouldSuppressRefs())
     markEntityOccurrenceInFile(D, Loc);
   
   DInfo.entityInfo = &DInfo.EntInfo;
@@ -357,7 +357,7 @@
 bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) {
   // For @class forward declarations, suppress them the same way as references.
   if (!D->isThisDeclarationADefinition()) {
-    if (suppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
+    if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
       return false; // already occurred.
 
     // FIXME: This seems like the wrong definition for redeclaration.
@@ -382,7 +382,7 @@
     BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
     BaseClass.loc = getIndexLoc(SuperLoc);
 
-    if (suppressRefs())
+    if (shouldSuppressRefs())
       markEntityOccurrenceInFile(SuperD, SuperLoc);
   }
   
@@ -411,7 +411,7 @@
 
 bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) {
   if (!D->isThisDeclarationADefinition()) {
-    if (suppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
+    if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
       return false; // already occurred.
     
     // FIXME: This seems like the wrong definition for redeclaration.
@@ -448,7 +448,7 @@
                                                      : D->getCategoryNameLoc();
   getEntityInfo(IFaceD, ClassEntity, SA);
 
-  if (suppressRefs())
+  if (shouldSuppressRefs())
     markEntityOccurrenceInFile(IFaceD, ClassLoc);
 
   ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
@@ -479,7 +479,7 @@
   SourceLocation CategoryLoc = D->getCategoryNameLoc();
   getEntityInfo(IFaceD, ClassEntity, SA);
 
-  if (suppressRefs())
+  if (shouldSuppressRefs())
     markEntityOccurrenceInFile(IFaceD, ClassLoc);
 
   CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
@@ -573,14 +573,14 @@
     return false;
   if (Loc.isInvalid())
     return false;
-  if (!indexFunctionLocalSymbols() && D->getParentFunctionOrMethod())
+  if (!shouldIndexFunctionLocalSymbols() && D->getParentFunctionOrMethod())
     return false;
   if (isNotFromSourceFile(D->getLocation()))
     return false;
   if (D->isImplicit() && shouldIgnoreIfImplicit(D))
     return false;
 
-  if (suppressRefs()) {
+  if (shouldSuppressRefs()) {
     if (markEntityOccurrenceInFile(D, Loc))
       return false; // already occurred.
   }
@@ -660,7 +660,7 @@
     CXXDInfo.CXXClassInfo.bases = BaseList.getBases();
     CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases();
 
-    if (suppressRefs()) {
+    if (shouldSuppressRefs()) {
       // Go through bases and mark them as referenced.
       for (unsigned i = 0, e = BaseList.getNumBases(); i != e; ++i) {
         const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i];
diff --git a/tools/libclang/IndexingContext.h b/tools/libclang/IndexingContext.h
index 38f7563..66c635d 100644
--- a/tools/libclang/IndexingContext.h
+++ b/tools/libclang/IndexingContext.h
@@ -322,14 +322,18 @@
   void setASTContext(ASTContext &ctx);
   void setPreprocessor(Preprocessor &PP);
 
-  bool suppressRefs() const {
+  bool shouldSuppressRefs() const {
     return IndexOptions & CXIndexOpt_SuppressRedundantRefs;
   }
 
-  bool indexFunctionLocalSymbols() const {
+  bool shouldIndexFunctionLocalSymbols() const {
     return IndexOptions & CXIndexOpt_IndexFunctionLocalSymbols;
   }
 
+  bool shouldIndexImplicitTemplateInsts() const {
+    return IndexOptions & CXIndexOpt_IndexImplicitTemplateInstantiations;
+  }
+
   bool shouldAbort();
 
   bool hasDiagnosticCallback() const { return CB.diagnostic; }