[clang-tools-extra] f976b99 - [clangd] Improve resolution of static method calls in HeuristicResolver

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 2 17:30:27 PDT 2021


Author: Nathan Ridge
Date: 2021-06-02T20:30:19-04:00
New Revision: f976b9997ee55a130b139efe7b6e6f3b0384016b

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

LOG: [clangd] Improve resolution of static method calls in HeuristicResolver

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/HeuristicResolver.cpp
    clang-tools-extra/clangd/unittests/InlayHintTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/HeuristicResolver.cpp b/clang-tools-extra/clangd/HeuristicResolver.cpp
index 316b7d11224b..b0a7448a04ef 100644
--- a/clang-tools-extra/clangd/HeuristicResolver.cpp
+++ b/clang-tools-extra/clangd/HeuristicResolver.cpp
@@ -16,6 +16,7 @@ namespace clangd {
 
 // Convenience lambdas for use as the 'Filter' parameter of
 // HeuristicResolver::resolveDependentMember().
+const auto NoFilter = [](const NamedDecl *D) { return true; };
 const auto NonStaticFilter = [](const NamedDecl *D) {
   return D->isCXXInstanceMember();
 };
@@ -90,6 +91,28 @@ const Type *HeuristicResolver::getPointeeType(const Type *T) const {
 
 std::vector<const NamedDecl *> HeuristicResolver::resolveMemberExpr(
     const CXXDependentScopeMemberExpr *ME) const {
+  // If the expression has a qualifier, first try resolving the member
+  // inside the qualifier's type.
+  // Note that we cannot use a NonStaticFilter in either case, for a couple
+  // of reasons:
+  //   1. It's valid to access a static member using instance member syntax,
+  //      e.g. `instance.static_member`.
+  //   2. We can sometimes get a CXXDependentScopeMemberExpr for static
+  //      member syntax too, e.g. if `X::static_member` occurs inside
+  //      an instance method, it's represented as a CXXDependentScopeMemberExpr
+  //      with `this` as the base expression as `X` as the qualifier
+  //      (which could be valid if `X` names a base class after instantiation).
+  if (NestedNameSpecifier *NNS = ME->getQualifier()) {
+    if (const Type *QualifierType = resolveNestedNameSpecifierToType(NNS)) {
+      auto Decls =
+          resolveDependentMember(QualifierType, ME->getMember(), NoFilter);
+      if (!Decls.empty())
+        return Decls;
+    }
+  }
+
+  // If that didn't yield any results, try resolving the member inside
+  // the expression's base type.
   const Type *BaseType = ME->getBaseType().getTypePtrOrNull();
   if (ME->isArrow()) {
     BaseType = getPointeeType(BaseType);
@@ -105,7 +128,7 @@ std::vector<const NamedDecl *> HeuristicResolver::resolveMemberExpr(
       BaseType = resolveExprToType(Base);
     }
   }
-  return resolveDependentMember(BaseType, ME->getMember(), NonStaticFilter);
+  return resolveDependentMember(BaseType, ME->getMember(), NoFilter);
 }
 
 std::vector<const NamedDecl *> HeuristicResolver::resolveDeclRefExpr(

diff  --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index 026479275a47..3c5503448ab8 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -285,7 +285,6 @@ TEST(ParameterHints, DependentCalls) {
       void bar(A<T> a, T t) {
         nonmember($par1[[t]]);
         a.member($par2[[t]]);
-        // FIXME: This one does not work yet.
         A<T>::static_member($par3[[t]]);
         // We don't want to arbitrarily pick between
         // "anInt" or "aDouble", so just show no hint.
@@ -294,7 +293,8 @@ TEST(ParameterHints, DependentCalls) {
     };
   )cpp",
                        ExpectedHint{"par1: ", "par1"},
-                       ExpectedHint{"par2: ", "par2"});
+                       ExpectedHint{"par2: ", "par2"},
+                       ExpectedHint{"par3: ", "par3"});
 }
 
 TEST(ParameterHints, VariadicFunction) {


        


More information about the cfe-commits mailing list