[clang-tools-extra] 733777a - [clangd] Fix namespace aliases in findExplicitReferences

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 31 05:36:55 PDT 2019


Author: Ilya Biryukov
Date: 2019-10-31T13:35:25+01:00
New Revision: 733777a81662c40960e9298bb59da8c39a14f8d5

URL: https://github.com/llvm/llvm-project/commit/733777a81662c40960e9298bb59da8c39a14f8d5
DIFF: https://github.com/llvm/llvm-project/commit/733777a81662c40960e9298bb59da8c39a14f8d5.diff

LOG: [clangd] Fix namespace aliases in findExplicitReferences

Reviewers: kadircet

Reviewed By: kadircet

Subscribers: merge_guards_bot, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/FindTarget.cpp
    clang-tools-extra/clangd/unittests/FindTargetTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp
index 1ab80b72a90b..4e61d22dd7fb 100644
--- a/clang-tools-extra/clangd/FindTarget.cpp
+++ b/clang-tools-extra/clangd/FindTarget.cpp
@@ -674,10 +674,14 @@ class ExplicitReferenceColletor
       return refInDecl(D);
     if (auto *E = N.get<Expr>())
       return refInExpr(E);
-    if (auto *NNSL = N.get<NestedNameSpecifierLoc>())
-      return {ReferenceLoc{NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false,
-                           explicitReferenceTargets(DynTypedNode::create(
-                               *NNSL->getNestedNameSpecifier()))}};
+    if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
+      // (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases.
+      return {ReferenceLoc{
+          NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false,
+          explicitReferenceTargets(
+              DynTypedNode::create(*NNSL->getNestedNameSpecifier()),
+              DeclRelation::Alias)}};
+    }
     if (const TypeLoc *TL = N.get<TypeLoc>())
       return refInTypeLoc(*TL);
     if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {

diff  --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 6b0e51b228b8..8f7c7aaeaefe 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -882,6 +882,28 @@ TEST_F(FindExplicitReferencesTest, All) {
               "0: targets = {x}, decl\n"
               "1: targets = {fptr}, decl\n"
               "2: targets = {a}, decl\n"},
+          // Namespace aliases should be handled properly.
+          {
+              R"cpp(
+                namespace ns { struct Type {} }
+                namespace alias = ns;
+                namespace rec_alias = alias;
+
+                void foo() {
+                  $0^ns::$1^Type $2^a;
+                  $3^alias::$4^Type $5^b;
+                  $6^rec_alias::$7^Type $8^c;
+                }
+           )cpp",
+              "0: targets = {ns}\n"
+              "1: targets = {ns::Type}, qualifier = 'ns::'\n"
+              "2: targets = {a}, decl\n"
+              "3: targets = {alias}\n"
+              "4: targets = {ns::Type}, qualifier = 'alias::'\n"
+              "5: targets = {b}, decl\n"
+              "6: targets = {rec_alias}\n"
+              "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
+              "8: targets = {c}, decl\n"},
       };
 
   for (const auto &C : Cases) {


        


More information about the cfe-commits mailing list