]> granicus.if.org Git - clang/commitdiff
[clang][Index] Fix usage of IndexImplicitInstantiation
authorKadir Cetinkaya <kadircet@google.com>
Mon, 18 Feb 2019 11:30:43 +0000 (11:30 +0000)
committerKadir Cetinkaya <kadircet@google.com>
Mon, 18 Feb 2019 11:30:43 +0000 (11:30 +0000)
Summary:
Indexing context was skipping explicit template instantiations as well.
This patch makes sure it only skips implicit ones.

Subscribers: arphaman, jdoerfert, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D58189

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@354262 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Index/IndexTypeSourceInfo.cpp
test/Index/Core/index-source.cpp
test/Index/index-refs.cpp
unittests/Index/IndexTests.cpp

index df80127fa5b687f535d9eaa5586570bd75b7d62a..e791a9d4a3c9f9be792e9d730db743e6f3093ffe 100644 (file)
@@ -129,10 +129,10 @@ public:
   template<typename TypeLocType>
   bool HandleTemplateSpecializationTypeLoc(TypeLocType TL) {
     if (const auto *T = TL.getTypePtr()) {
-      if (IndexCtx.shouldIndexImplicitInstantiation()) {
-        if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) {
-          IndexCtx.handleReference(RD, TL.getTemplateNameLoc(),
-                                   Parent, ParentDC, SymbolRoleSet(), Relations);
+      if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) {
+        if (!RD->isImplicit() || IndexCtx.shouldIndexImplicitInstantiation()) {
+          IndexCtx.handleReference(RD, TL.getTemplateNameLoc(), Parent,
+                                   ParentDC, SymbolRoleSet(), Relations);
           return true;
         }
       }
index 0bf663e30be35a37196313920a08cbf49e6de7bd..2058156ed7ab9e952fbb41baf88e4c36680b4140 100644 (file)
@@ -212,7 +212,7 @@ class PseudoOverridesInSpecializations<double, int>::InnerClass {
 };
 // CHECK: [[@LINE-2]]:54 | class(Gen)/C++ | InnerClass | c:@S@PseudoOverridesInSpecializations>#d#I@ST>1#T@InnerClass | <no-cgname> | Def,RelChild | rel: 1
 // CHECK-NEXT: RelChild
-// CHECK: [[@LINE-4]]:7 | class(Gen)/C++ | PseudoOverridesInSpecializations | c:@ST>2#T#T@PseudoOverridesInSpecializations | <no-cgname> | Ref,RelCont | rel: 1
+// CHECK: [[@LINE-4]]:7 | class(Gen,TS)/C++ | PseudoOverridesInSpecializations | c:@S@PseudoOverridesInSpecializations>#d#I | <no-cgname> | Ref,RelCont | rel: 1
 // CHECK-NEXT: RelCont
 
 template<typename S>
@@ -285,13 +285,13 @@ template<>
 class SpecializationDecl<int>;
 // CHECK: [[@LINE-1]]:7 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#I | <no-cgname> | Decl,RelSpecialization | rel: 1
 // CHECK-NEXT: RelSpecialization | SpecializationDecl | c:@ST>1#T@SpecializationDecl
-// CHECK: [[@LINE-3]]:7 | class(Gen)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | <no-cgname> | Ref | rel: 0
+// CHECK: [[@LINE-3]]:7 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#I | <no-cgname> | Ref | rel: 0
 
 template<>
 class SpecializationDecl<int> { };
 // CHECK: [[@LINE-1]]:7 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#I | <no-cgname> | Def,RelSpecialization | rel: 1
 // CHECK-NEXT: RelSpecialization | SpecializationDecl | c:@ST>1#T@SpecializationDecl
-// CHECK-NEXT: [[@LINE-3]]:7 | class(Gen)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | <no-cgname> | Ref | rel: 0
+// CHECK-NEXT: [[@LINE-3]]:7 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#I | <no-cgname> | Ref | rel: 0
 
 template<typename T>
 class PartialSpecilizationClass<Cls, T>;
@@ -306,7 +306,7 @@ class PartialSpecilizationClass<Cls, Cls> : Cls { };
 // CHECK-NEXT: RelSpecialization | PartialSpecilizationClass | c:@ST>2#T#T@PartialSpecilizationClass
 // CHECK-NEXT: [[@LINE-3]]:45 | class/C++ | Cls | c:@S@Cls | <no-cgname> | Ref,RelBase,RelCont | rel: 1
 // CHECK-NEXT: RelBase,RelCont | PartialSpecilizationClass | c:@S@PartialSpecilizationClass>#$@S@Cls#S0_
-// CHECK-NEXT: [[@LINE-5]]:7 | class(Gen)/C++ | PartialSpecilizationClass | c:@ST>2#T#T@PartialSpecilizationClass | <no-cgname> | Ref | rel: 0
+// CHECK-NEXT: [[@LINE-5]]:7 | class(Gen,TS)/C++ | PartialSpecilizationClass | c:@S@PartialSpecilizationClass>#$@S@Cls#S0_ | <no-cgname> | Ref | rel: 0
 // CHECK-NEXT: [[@LINE-6]]:33 | class/C++ | Cls | c:@S@Cls | <no-cgname> | Ref | rel: 0
 // CHECK-NEXT: [[@LINE-7]]:38 | class/C++ | Cls | c:@S@Cls | <no-cgname> | Ref | rel: 0
 
@@ -321,7 +321,7 @@ template<>
 void functionSp<SpecializationDecl<Cls>, Record::C>() {
 // CHECK: [[@LINE-1]]:6 | function(Gen,TS)/C++ | functionSp | c:@F@functionSp<#$@S@SpecializationDecl>#$@S@Cls#VI2># | __Z10functionSpI18SpecializationDeclI3ClsELi2EEvv | Def,RelSpecialization | rel: 1
 // CHECK:   RelSpecialization | functionSp | c:@FT@>2#T#NIfunctionSp#v#
-// CHECK: [[@LINE-3]]:17 | class(Gen)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | <no-cgname> | Ref,RelCont | rel: 1
+// CHECK: [[@LINE-3]]:17 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#$@S@Cls | <no-cgname> | Ref,RelCont | rel: 1
 // CHECK: [[@LINE-4]]:36 | class/C++ | Cls | c:@S@Cls | <no-cgname> | Ref,RelCont | rel: 1
 // CHECK: [[@LINE-5]]:50 | static-property/C++ | C | c:@S@Record@C | __ZN6Record1CE | Ref,RelCont | rel: 1
 // CHECK: [[@LINE-6]]:42 | struct/C++ | Record | c:@S@Record | <no-cgname> | Ref,RelCont | rel: 1
@@ -332,7 +332,7 @@ class ClassWithCorrectSpecialization { };
 
 template<>
 class ClassWithCorrectSpecialization<SpecializationDecl<Cls>, Record::C> { };
-// CHECK: [[@LINE-1]]:38 | class(Gen)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | <no-cgname> | Ref | rel: 0
+// CHECK: [[@LINE-1]]:38 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#$@S@Cls | <no-cgname> | Ref | rel: 0
 // CHECK: [[@LINE-2]]:57 | class/C++ | Cls | c:@S@Cls | <no-cgname> | Ref | rel: 0
 // CHECK: [[@LINE-3]]:71 | static-property/C++ | C | c:@S@Record@C | __ZN6Record1CE | Ref,Read | rel: 0
 // CHECK: [[@LINE-4]]:63 | struct/C++ | Record | c:@S@Record | <no-cgname> | Ref | rel: 0
@@ -494,7 +494,7 @@ void localStructuredBindingAndRef() {
 // CHECK: [[@LINE-1]]:69 | variable/C++ | structuredBinding2 | c:@N@cpp17structuredBinding@structuredBinding2 | <no-cgname> | Ref,Read,RelCont | rel: 1
 // CHECK-NEXT: RelCont | localStructuredBindingAndRef | c:@N@cpp17structuredBinding@F@localStructuredBindingAndRef#
 // CHECK-NOT: localBinding
-// LOCAL: [[@LINE-4]]:9 | variable(local)/C++ | localBinding1 | c:index-source.cpp@25382@N@cpp17structuredBinding@F@localStructuredBindingAndRef#@localBinding1
+// LOCAL: [[@LINE-4]]:9 | variable(local)/C++ | localBinding1 | c:index-source.cpp@25408@N@cpp17structuredBinding@F@localStructuredBindingAndRef#@localBinding1
 }
 
 }
@@ -505,7 +505,7 @@ struct Guided { T t; };
 // CHECK-NEXT: [[@LINE-2]]:19 | field/C++ | t | c:@ST>1#T@Guided@FI@t | <no-cgname> | Def,RelChild | rel: 1
 // CHECK-NEXT: RelChild | Guided | c:@ST>1#T@Guided
 Guided(double) -> Guided<float>;
-// CHECK: [[@LINE-1]]:19 | struct(Gen)/C++ | Guided | c:@ST>1#T@Guided | <no-cgname> | Ref | rel: 0
+// CHECK: [[@LINE-1]]:19 | struct(Gen,TS)/C++ | Guided | c:@S@Guided>#f | <no-cgname> | Ref | rel: 0
 // CHECK-NEXT: [[@LINE-2]]:1 | struct(Gen)/C++ | Guided | c:@ST>1#T@Guided | <no-cgname> | Ref | rel: 0
 auto guided = Guided{1.0};
 // CHECK: [[@LINE-1]]:6 | variable/C | guided | c:@guided | _guided | Def | rel: 0
index 760e4cfcf9782521164174112e09a77376fa390d..0e613e48522b125f7181aa31e63a0d527e8a84b4 100644 (file)
@@ -117,7 +117,7 @@ int ginitlist[] = {EnumVal};
 /* 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
+// CHECK-NEXT: [indexEntityReference]: kind: struct-template-partial-spec | name: TS | USR: c:@SP>1#T@TS>#t0.0#I | {{.*}} | loc: 55:3
 
 // CHECK:      [indexEntityReference]: kind: variable | name: array_size | {{.*}} | loc: 59:22
 // CHECK:      [indexEntityReference]: kind: variable | name: default_param | {{.*}} | loc: 62:19 | {{.*}} | role: ref read
index 2b574a53fc3235d22411298c49cdace497f36267..61c969f978c6be893945cfc480d6684a34e2d45f 100644 (file)
@@ -7,7 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/AST/ASTConsumer.h"
+#include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendAction.h"
 #include "clang/Index/IndexDataConsumer.h"
 
 namespace clang {
 namespace index {
+namespace {
+struct Position {
+  size_t Line = 0;
+  size_t Column = 0;
+
+  Position(size_t Line = 0, size_t Column = 0) : Line(Line), Column(Column) {}
+
+  static Position fromSourceLocation(SourceLocation Loc,
+                                     const SourceManager &SM) {
+    FileID FID;
+    unsigned Offset;
+    std::tie(FID, Offset) = SM.getDecomposedSpellingLoc(Loc);
+    Position P;
+    P.Line = SM.getLineNumber(FID, Offset);
+    P.Column = SM.getColumnNumber(FID, Offset);
+    return P;
+  }
+};
+
+bool operator==(const Position &LHS, const Position &RHS) {
+  return std::tie(LHS.Line, LHS.Column) == std::tie(RHS.Line, RHS.Column);
+}
+
+llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &Pos) {
+  return OS << Pos.Line << ':' << Pos.Column;
+}
 
 struct TestSymbol {
   std::string QName;
+  Position WrittenPos;
+  Position DeclPos;
   // FIXME: add more information.
 };
 
 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TestSymbol &S) {
-  return OS << S.QName;
+  return OS << S.QName << '[' << S.WrittenPos << ']' << '@' << S.DeclPos;
 }
 
-namespace {
 class Indexer : public IndexDataConsumer {
 public:
+  void initialize(ASTContext &Ctx) override {
+    AST = &Ctx;
+    IndexDataConsumer::initialize(Ctx);
+  }
+
   bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
-                           ArrayRef<SymbolRelation>, SourceLocation,
+                           ArrayRef<SymbolRelation>, SourceLocation Loc,
                            ASTNodeInfo) override {
     const auto *ND = llvm::dyn_cast<NamedDecl>(D);
     if (!ND)
       return true;
     TestSymbol S;
     S.QName = ND->getQualifiedNameAsString();
+    S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager());
+    S.DeclPos =
+        Position::fromSourceLocation(D->getLocation(), AST->getSourceManager());
     Symbols.push_back(std::move(S));
     return true;
   }
@@ -57,6 +95,7 @@ public:
   }
 
   std::vector<TestSymbol> Symbols;
+  const ASTContext *AST = nullptr;
 };
 
 class IndexAction : public ASTFrontendAction {
@@ -93,11 +132,14 @@ private:
   IndexingOptions Opts;
 };
 
+using testing::AllOf;
 using testing::Contains;
 using testing::Not;
 using testing::UnorderedElementsAre;
 
 MATCHER_P(QName, Name, "") { return arg.QName == Name; }
+MATCHER_P(WrittenAt, Pos, "") { return arg.WrittenPos == Pos; }
+MATCHER_P(DeclAt, Pos, "") { return arg.DeclPos == Pos; }
 
 TEST(IndexTest, Simple) {
   auto Index = std::make_shared<Indexer>();
@@ -134,6 +176,46 @@ TEST(IndexTest, IndexParametersInDecls) {
   EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar"))));
 }
 
+TEST(IndexTest, IndexExplicitTemplateInstantiation) {
+  std::string Code = R"cpp(
+    template <typename T>
+    struct Foo { void bar() {} };
+    template <>
+    struct Foo<int> { void bar() {} };
+    void foo() {
+      Foo<char> abc;
+      Foo<int> b;
+    }
+  )cpp";
+  auto Index = std::make_shared<Indexer>();
+  IndexingOptions Opts;
+  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+  EXPECT_THAT(Index->Symbols,
+              AllOf(Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)),
+                                   DeclAt(Position(5, 12)))),
+                    Contains(AllOf(QName("Foo"), WrittenAt(Position(7, 7)),
+                                   DeclAt(Position(3, 12))))));
+}
+
+TEST(IndexTest, IndexTemplateInstantiationPartial) {
+  std::string Code = R"cpp(
+    template <typename T1, typename T2>
+    struct Foo { void bar() {} };
+    template <typename T>
+    struct Foo<T, int> { void bar() {} };
+    void foo() {
+      Foo<char, char> abc;
+      Foo<int, int> b;
+    }
+  )cpp";
+  auto Index = std::make_shared<Indexer>();
+  IndexingOptions Opts;
+  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+  EXPECT_THAT(Index->Symbols,
+              Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)),
+                             DeclAt(Position(5, 12)))));
+}
+
 } // namespace
 } // namespace index
 } // namespace clang