]> granicus.if.org Git - clang/commitdiff
[libclang] Indexing: only index implicit template instantiations via an opt-in indexi...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 14 Feb 2012 22:23:11 +0000 (22:23 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 14 Feb 2012 22:23:11 +0000 (22:23 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150517 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang-c/Index.h
test/Index/index-refs.cpp
tools/libclang/IndexBody.cpp
tools/libclang/IndexDecl.cpp
tools/libclang/IndexTypeSourceInfo.cpp
tools/libclang/Indexing.cpp
tools/libclang/IndexingContext.cpp
tools/libclang/IndexingContext.h

index a5082d120a377d64f83c52a3204e5fbf55c15d52..0c6d6d1c762552b5758feeaf020e8fe5d2e4308b 100644 (file)
@@ -4461,7 +4461,13 @@ typedef enum {
    * \brief Function-local symbols should be indexed. If this is not set
    * function-local symbols will be ignored.
    */
-  CXIndexOpt_IndexFunctionLocalSymbols = 0x2
+  CXIndexOpt_IndexFunctionLocalSymbols = 0x2,
+
+  /**
+   * \brief Implicit function/class template instantiations should be indexed.
+   * If this is not set, implicit instantiations will be ignored.
+   */
+  CXIndexOpt_IndexImplicitTemplateInstantiations = 0x4
 } CXIndexOptFlags;
 
 /**
index e4c438896712f82b19b44b593997285e565268fc..35f4fe403c017263e2287a3d21cf353b6a449120 100644 (file)
@@ -89,7 +89,12 @@ void foo3() {
 // CHECK:      [indexDeclaration]: kind: c++-class-template | name: TS | {{.*}} | loc: 47:8
 // CHECK-NEXT: [indexDeclaration]: kind: struct-template-partial-spec | name: TS | USR: c:@SP>1#T@TS>#t0.0#I | {{.*}} | loc: 50:8
 // CHECK-NEXT: [indexDeclaration]: kind: typedef | name: MyInt | USR: c:index-refs.cpp@593@SP>1#T@TS>#t0.0#I@T@MyInt | {{.*}} | loc: 51:15 | semantic-container: [TS:50:8] | lexical-container: [TS:50:8]
-// CHECK-NEXT: [indexDeclaration]: kind: struct-template-spec | name: TS | USR: c:@S@TS>#I | {{.*}} | loc: 50:8
-// CHECK-NEXT: [indexDeclaration]: kind: typedef | name: MyInt | USR: c:index-refs.cpp@593@S@TS>#I@T@MyInt | {{.*}} | loc: 51:15 | semantic-container: [TS:50:8] | lexical-container: [TS:50:8]
+/* when indexing implicit instantiations
+  [indexDeclaration]: kind: struct-template-spec | name: TS | USR: c:@S@TS>#I | {{.*}} | loc: 50:8
+  [indexDeclaration]: kind: typedef | name: MyInt | USR: c:index-refs.cpp@593@S@TS>#I@T@MyInt | {{.*}} | loc: 51:15 | semantic-container: [TS:50:8] | lexical-container: [TS:50:8]
+ */
 // CHECK-NEXT: [indexDeclaration]: kind: function | name: foo3
-// CHECK-NEXT: [indexEntityReference]: kind: struct-template-spec | name: TS | USR: c:@S@TS>#I | {{.*}} | loc: 55:3
+/* when indexing implicit instantiations
+  [indexEntityReference]: kind: struct-template-spec | name: TS | USR: c:@S@TS>#I | {{.*}} | loc: 55:3
+*/
+// CHECK-NEXT: [indexEntityReference]: kind: c++-class-template | name: TS | USR: c:@ST>2#T#T@TS | {{.*}} | loc: 55:3
index c40049113287e8dc9633264a754dfe63d5f400d6..684d5ce3854ea60c39e7e0bd4396f2670fd49836 100644 (file)
@@ -97,7 +97,7 @@ public:
   }
 
   bool VisitDeclStmt(DeclStmt *S) {
-    if (IndexCtx.indexFunctionLocalSymbols())
+    if (IndexCtx.shouldIndexFunctionLocalSymbols())
       IndexCtx.indexDeclGroupRef(S->getDeclGroup());
     return true;
   }
index 55ae9e0afca9d6e6e019b01da43bd42c8a2e029c..dcc9a1dca3d87d2f0c77c75f516517a4435071bd 100644 (file)
@@ -26,7 +26,7 @@ public:
   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 @@ public:
 
   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;
   }
index 1e753781054e09f54ca201c706b39d35458e1fbd..34acffcf7adec78975604b316c6d6c9daf093dd5 100644 (file)
@@ -73,9 +73,15 @@ public:
 
   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;
   }
index 0d6da2824daf4de01800879ced5dd8c857c5909d..d1af38887b7b2994b7f6e31f82baa3bb2f845385 100644 (file)
@@ -137,11 +137,17 @@ public:
   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 @@ public:
     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; }
 };
 
index 0493f938aca3fa423d1aae03aced51d6e6f0e26e..75184dd3b0bfd73107453b49b1939cad40465e67 100644 (file)
@@ -35,7 +35,7 @@ IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo(
                                 IdxCtx.getIndexLoc(Loc) };
     ProtInfos.push_back(ProtInfo);
 
-    if (IdxCtx.suppressRefs())
+    if (IdxCtx.shouldSuppressRefs())
       IdxCtx.markEntityOccurrenceInFile(PD, Loc);
   }
 
@@ -265,11 +265,11 @@ bool IndexingContext::handleDecl(const NamedDecl *D,
 
   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::handleTypedefName(const TypedefNameDecl *D) {
 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 @@ bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) {
     BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
     BaseClass.loc = getIndexLoc(SuperLoc);
 
-    if (suppressRefs())
+    if (shouldSuppressRefs())
       markEntityOccurrenceInFile(SuperD, SuperLoc);
   }
   
@@ -411,7 +411,7 @@ bool IndexingContext::handleObjCImplementation(
 
 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 @@ bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) {
                                                      : D->getCategoryNameLoc();
   getEntityInfo(IFaceD, ClassEntity, SA);
 
-  if (suppressRefs())
+  if (shouldSuppressRefs())
     markEntityOccurrenceInFile(IFaceD, ClassLoc);
 
   ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
@@ -479,7 +479,7 @@ bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
   SourceLocation CategoryLoc = D->getCategoryNameLoc();
   getEntityInfo(IFaceD, ClassEntity, SA);
 
-  if (suppressRefs())
+  if (shouldSuppressRefs())
     markEntityOccurrenceInFile(IFaceD, ClassLoc);
 
   CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
@@ -573,14 +573,14 @@ bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
     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 @@ bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD,
     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];
index 38f7563b57cc5e83f4d3dce0b591a75931613281..66c635d6b9b13fed76f1c752f34d8a39ed42a52c 100644 (file)
@@ -322,14 +322,18 @@ public:
   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; }