kadircet created this revision.
kadircet added reviewers: ilya-biryukov, akyrtzi.
Herald added subscribers: cfe-commits, arphaman.
Herald added a project: clang.

Add indexing of UsingDecl itself.
Also enable generation of USRs for UsingDecls, using the qualified name of the
decl.


Repository:
  rC Clang

https://reviews.llvm.org/D58340

Files:
  lib/Index/IndexDecl.cpp
  lib/Index/USRGeneration.cpp
  test/Index/usrs.cpp
  unittests/Index/IndexTests.cpp


Index: unittests/Index/IndexTests.cpp
===================================================================
--- unittests/Index/IndexTests.cpp
+++ unittests/Index/IndexTests.cpp
@@ -134,6 +134,19 @@
   EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar"))));
 }
 
+TEST(IndexTest, UsingDecls) {
+  std::string Code = R"cpp(
+    void foo(int bar);
+    namespace std {
+      using ::foo;
+    }
+  )cpp";
+  auto Index = std::make_shared<Indexer>();
+  IndexingOptions Opts;
+  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+  EXPECT_THAT(Index->Symbols, Contains(QName("std::foo")));
+}
+
 } // namespace
 } // namespace index
 } // namespace clang
Index: test/Index/usrs.cpp
===================================================================
--- test/Index/usrs.cpp
+++ test/Index/usrs.cpp
@@ -158,7 +158,7 @@
 // CHECK: usrs.cpp c:@NA@foo_alias
 // CHECK-NOT: foo
 // CHECK: usrs.cpp c:@NA@foo_alias2
-// CHECK-NOT: ClsB
+// CHECK: usrs.cpp c:@ClsB Extent=[64:1 - 64:16]
 // CHECK: usrs.cpp c:@NA@foo_alias3
 // CHECK: usrs.cpp c:@aN Extent=[68:1 - 73:2]
 // CHECK: usrs.cpp c:usrs.cpp@aN@S@RDar9371763_Foo Extent=[69:1 - 72:2]
Index: lib/Index/USRGeneration.cpp
===================================================================
--- lib/Index/USRGeneration.cpp
+++ lib/Index/USRGeneration.cpp
@@ -111,7 +111,16 @@
   }
 
   void VisitUsingDecl(const UsingDecl *D) {
-    IgnoreResults = true;
+    VisitDeclContext(D->getDeclContext());
+    Out << "@";
+
+    if (EmitDeclName(D)) {
+      // The string can be empty if the declaration has no name; e.g., it is
+      // the ParmDecl with no name for declaration of a function pointer type,
+      // e.g.: void  (*f)(void *);
+      // In this case, don't generate a USR.
+      IgnoreResults = true;
+    }
   }
 
   bool ShouldGenerateLocation(const NamedDecl *D);
Index: lib/Index/IndexDecl.cpp
===================================================================
--- lib/Index/IndexDecl.cpp
+++ lib/Index/IndexDecl.cpp
@@ -585,6 +585,7 @@
 
     IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
                                          D->getLexicalDeclContext());
+    IndexCtx.handleDecl(D);
     for (const auto *I : D->shadows())
       IndexCtx.handleReference(I->getUnderlyingDecl(), D->getLocation(), 
Parent,
                                D->getLexicalDeclContext(), SymbolRoleSet());


Index: unittests/Index/IndexTests.cpp
===================================================================
--- unittests/Index/IndexTests.cpp
+++ unittests/Index/IndexTests.cpp
@@ -134,6 +134,19 @@
   EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar"))));
 }
 
+TEST(IndexTest, UsingDecls) {
+  std::string Code = R"cpp(
+    void foo(int bar);
+    namespace std {
+      using ::foo;
+    }
+  )cpp";
+  auto Index = std::make_shared<Indexer>();
+  IndexingOptions Opts;
+  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+  EXPECT_THAT(Index->Symbols, Contains(QName("std::foo")));
+}
+
 } // namespace
 } // namespace index
 } // namespace clang
Index: test/Index/usrs.cpp
===================================================================
--- test/Index/usrs.cpp
+++ test/Index/usrs.cpp
@@ -158,7 +158,7 @@
 // CHECK: usrs.cpp c:@NA@foo_alias
 // CHECK-NOT: foo
 // CHECK: usrs.cpp c:@NA@foo_alias2
-// CHECK-NOT: ClsB
+// CHECK: usrs.cpp c:@ClsB Extent=[64:1 - 64:16]
 // CHECK: usrs.cpp c:@NA@foo_alias3
 // CHECK: usrs.cpp c:@aN Extent=[68:1 - 73:2]
 // CHECK: usrs.cpp c:usrs.cpp@aN@S@RDar9371763_Foo Extent=[69:1 - 72:2]
Index: lib/Index/USRGeneration.cpp
===================================================================
--- lib/Index/USRGeneration.cpp
+++ lib/Index/USRGeneration.cpp
@@ -111,7 +111,16 @@
   }
 
   void VisitUsingDecl(const UsingDecl *D) {
-    IgnoreResults = true;
+    VisitDeclContext(D->getDeclContext());
+    Out << "@";
+
+    if (EmitDeclName(D)) {
+      // The string can be empty if the declaration has no name; e.g., it is
+      // the ParmDecl with no name for declaration of a function pointer type,
+      // e.g.: void  (*f)(void *);
+      // In this case, don't generate a USR.
+      IgnoreResults = true;
+    }
   }
 
   bool ShouldGenerateLocation(const NamedDecl *D);
Index: lib/Index/IndexDecl.cpp
===================================================================
--- lib/Index/IndexDecl.cpp
+++ lib/Index/IndexDecl.cpp
@@ -585,6 +585,7 @@
 
     IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent,
                                          D->getLexicalDeclContext());
+    IndexCtx.handleDecl(D);
     for (const auto *I : D->shadows())
       IndexCtx.handleReference(I->getUnderlyingDecl(), D->getLocation(), Parent,
                                D->getLexicalDeclContext(), SymbolRoleSet());
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to