Skip to content

Commit cd4e8d7

Browse files
committed
[clangd] Fix an assertion failure in TargetFinder's heuristic resolution of dependent type.
The assertion is not true anymore after D82739, this patch just removes it, and rename related functions. And also fixes a missing cases. Differential Revision: https://reviews.llvm.org/D84837
1 parent 07bb824 commit cd4e8d7

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

clang-tools-extra/clangd/FindTarget.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,12 @@ const Type *getPointeeType(const Type *T) {
163163
}
164164

165165
// Forward declaration, needed as this function is mutually recursive
166-
// with resolveDependentExprToDecls.
167-
const Type *resolveDependentExprToType(const Expr *E);
166+
// with resolveExprToDecls.
167+
const Type *resolveExprToType(const Expr *E);
168168

169-
// Try to heuristically resolve a dependent expression `E` to one
169+
// Try to heuristically resolve a possibly-dependent expression `E` to one
170170
// or more declarations that it likely references.
171-
std::vector<const NamedDecl *> resolveDependentExprToDecls(const Expr *E) {
172-
assert(E->isTypeDependent());
171+
std::vector<const NamedDecl *> resolveExprToDecls(const Expr *E) {
173172
if (const auto *ME = dyn_cast<CXXDependentScopeMemberExpr>(E)) {
174173
const Type *BaseType = ME->getBaseType().getTypePtrOrNull();
175174
if (ME->isArrow()) {
@@ -183,7 +182,7 @@ std::vector<const NamedDecl *> resolveDependentExprToDecls(const Expr *E) {
183182
// can get further by analyzing the depedent expression.
184183
Expr *Base = ME->isImplicitAccess() ? nullptr : ME->getBase();
185184
if (Base && BT->getKind() == BuiltinType::Dependent) {
186-
BaseType = resolveDependentExprToType(Base);
185+
BaseType = resolveExprToType(Base);
187186
}
188187
}
189188
return getMembersReferencedViaDependentName(
@@ -197,7 +196,7 @@ std::vector<const NamedDecl *> resolveDependentExprToDecls(const Expr *E) {
197196
/*IsNonstaticMember=*/false);
198197
}
199198
if (const auto *CE = dyn_cast<CallExpr>(E)) {
200-
const auto *CalleeType = resolveDependentExprToType(CE->getCallee());
199+
const auto *CalleeType = resolveExprToType(CE->getCallee());
201200
if (!CalleeType)
202201
return {};
203202
if (const auto *FnTypePtr = CalleeType->getAs<PointerType>())
@@ -209,15 +208,16 @@ std::vector<const NamedDecl *> resolveDependentExprToDecls(const Expr *E) {
209208
}
210209
}
211210
}
212-
if (const auto *ME = dyn_cast<MemberExpr>(E)) {
211+
if (const auto *ME = dyn_cast<MemberExpr>(E))
213212
return {ME->getMemberDecl()};
214-
}
213+
if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
214+
return {DRE->getFoundDecl()};
215215
return {};
216216
}
217217

218-
// Try to heuristically resolve the type of a dependent expression `E`.
219-
const Type *resolveDependentExprToType(const Expr *E) {
220-
std::vector<const NamedDecl *> Decls = resolveDependentExprToDecls(E);
218+
// Try to heuristically resolve the type of a possibly-dependent expression `E`.
219+
const Type *resolveExprToType(const Expr *E) {
220+
std::vector<const NamedDecl *> Decls = resolveExprToDecls(E);
221221
if (Decls.size() != 1) // Names an overload set -- just bail.
222222
return nullptr;
223223
if (const auto *TD = dyn_cast<TypeDecl>(Decls[0])) {
@@ -426,12 +426,12 @@ struct TargetFinder {
426426
}
427427
void
428428
VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
429-
for (const NamedDecl *D : resolveDependentExprToDecls(E)) {
429+
for (const NamedDecl *D : resolveExprToDecls(E)) {
430430
Outer.add(D, Flags);
431431
}
432432
}
433433
void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
434-
for (const NamedDecl *D : resolveDependentExprToDecls(E)) {
434+
for (const NamedDecl *D : resolveExprToDecls(E)) {
435435
Outer.add(D, Flags);
436436
}
437437
}

clang-tools-extra/clangd/unittests/FindTargetTests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,20 @@ TEST_F(TargetDeclTest, DependentExprs) {
627627
};
628628
)cpp";
629629
EXPECT_DECLS("CXXDependentScopeMemberExpr", "int aaaa");
630+
631+
Code = R"cpp(
632+
class Foo {
633+
public:
634+
static Foo k(int);
635+
template <typename T> T convert() const;
636+
};
637+
template <typename T>
638+
void test() {
639+
Foo::k(T()).template [[convert]]<T>();
640+
}
641+
)cpp";
642+
EXPECT_DECLS("CXXDependentScopeMemberExpr",
643+
"template <typename T> T convert() const");
630644
}
631645

632646
TEST_F(TargetDeclTest, ObjC) {

0 commit comments

Comments
 (0)