[PATCH] D101741: [clangd] Improve resolution of static method calls in HeuristicResolver
Nathan Ridge via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun May 2 22:55:52 PDT 2021
nridge created this revision.
nridge added a reviewer: sammccall.
Herald added subscribers: usaxena95, kadircet, arphaman.
nridge requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D101741
Files:
clang-tools-extra/clangd/HeuristicResolver.cpp
clang-tools-extra/clangd/unittests/InlayHintTests.cpp
Index: clang-tools-extra/clangd/unittests/InlayHintTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -263,7 +263,6 @@
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.
@@ -272,7 +271,8 @@
};
)cpp",
ExpectedHint{"par1: ", "par1"},
- ExpectedHint{"par2: ", "par2"});
+ ExpectedHint{"par2: ", "par2"},
+ ExpectedHint{"par3: ", "par3"});
}
TEST(ParameterHints, VariadicFunction) {
Index: clang-tools-extra/clangd/HeuristicResolver.cpp
===================================================================
--- clang-tools-extra/clangd/HeuristicResolver.cpp
+++ clang-tools-extra/clangd/HeuristicResolver.cpp
@@ -16,6 +16,7 @@
// 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 @@
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 @@
BaseType = resolveExprToType(Base);
}
}
- return resolveDependentMember(BaseType, ME->getMember(), NonStaticFilter);
+ return resolveDependentMember(BaseType, ME->getMember(), NoFilter);
}
std::vector<const NamedDecl *> HeuristicResolver::resolveDeclRefExpr(
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101741.342315.patch
Type: text/x-patch
Size: 3054 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210503/f2fc0edc/attachment.bin>
More information about the cfe-commits
mailing list