[clang-tools-extra] 7a70968 - [clangd] Handle DependentNameType in HeuristicResolver::resolveTypeToRecordDecl()
Nathan Ridge via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 16 12:21:01 PDT 2023
Author: Nathan Ridge
Date: 2023-06-16T15:19:50-04:00
New Revision: 7a709689bc1755a569864a97d93b96d22f988eb4
URL: https://github.com/llvm/llvm-project/commit/7a709689bc1755a569864a97d93b96d22f988eb4
DIFF: https://github.com/llvm/llvm-project/commit/7a709689bc1755a569864a97d93b96d22f988eb4.diff
LOG: [clangd] Handle DependentNameType in HeuristicResolver::resolveTypeToRecordDecl()
Fixes https://github.com/clangd/clangd/issues/1671
Differential Revision: https://reviews.llvm.org/D152645
Added:
Modified:
clang-tools-extra/clangd/HeuristicResolver.cpp
clang-tools-extra/clangd/HeuristicResolver.h
clang-tools-extra/clangd/unittests/FindTargetTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/HeuristicResolver.cpp b/clang-tools-extra/clangd/HeuristicResolver.cpp
index e91bd0e87a8b7..7960ab92ba9e6 100644
--- a/clang-tools-extra/clangd/HeuristicResolver.cpp
+++ b/clang-tools-extra/clangd/HeuristicResolver.cpp
@@ -10,6 +10,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/ExprCXX.h"
+#include "clang/AST/Type.h"
namespace clang {
namespace clangd {
@@ -29,15 +30,39 @@ const auto TemplateFilter = [](const NamedDecl *D) {
return isa<TemplateDecl>(D);
};
+namespace {
+
+const Type *resolveDeclsToType(const std::vector<const NamedDecl *> &Decls,
+ ASTContext &Ctx) {
+ if (Decls.size() != 1) // Names an overload set -- just bail.
+ return nullptr;
+ if (const auto *TD = dyn_cast<TypeDecl>(Decls[0])) {
+ return Ctx.getTypeDeclType(TD).getTypePtr();
+ }
+ if (const auto *VD = dyn_cast<ValueDecl>(Decls[0])) {
+ return VD->getType().getTypePtrOrNull();
+ }
+ return nullptr;
+}
+
+} // namespace
+
// Helper function for HeuristicResolver::resolveDependentMember()
// which takes a possibly-dependent type `T` and heuristically
// resolves it to a CXXRecordDecl in which we can try name lookup.
-CXXRecordDecl *resolveTypeToRecordDecl(const Type *T) {
+CXXRecordDecl *HeuristicResolver::resolveTypeToRecordDecl(const Type *T) const {
assert(T);
// Unwrap type sugar such as type aliases.
T = T->getCanonicalTypeInternal().getTypePtr();
+ if (const auto *DNT = T->getAs<DependentNameType>()) {
+ T = resolveDeclsToType(resolveDependentNameType(DNT), Ctx);
+ if (!T)
+ return nullptr;
+ T = T->getCanonicalTypeInternal().getTypePtr();
+ }
+
if (const auto *RT = T->getAs<RecordType>())
return dyn_cast<CXXRecordDecl>(RT->getDecl());
@@ -185,18 +210,6 @@ HeuristicResolver::resolveTemplateSpecializationType(
DTST->getIdentifier(), TemplateFilter);
}
-const Type *resolveDeclsToType(const std::vector<const NamedDecl *> &Decls) {
- if (Decls.size() != 1) // Names an overload set -- just bail.
- return nullptr;
- if (const auto *TD = dyn_cast<TypeDecl>(Decls[0])) {
- return TD->getTypeForDecl();
- }
- if (const auto *VD = dyn_cast<ValueDecl>(Decls[0])) {
- return VD->getType().getTypePtrOrNull();
- }
- return nullptr;
-}
-
std::vector<const NamedDecl *>
HeuristicResolver::resolveExprToDecls(const Expr *E) const {
if (const auto *ME = dyn_cast<CXXDependentScopeMemberExpr>(E)) {
@@ -220,7 +233,7 @@ HeuristicResolver::resolveExprToDecls(const Expr *E) const {
const Type *HeuristicResolver::resolveExprToType(const Expr *E) const {
std::vector<const NamedDecl *> Decls = resolveExprToDecls(E);
if (!Decls.empty())
- return resolveDeclsToType(Decls);
+ return resolveDeclsToType(Decls, Ctx);
return E->getType().getTypePtr();
}
@@ -239,9 +252,11 @@ const Type *HeuristicResolver::resolveNestedNameSpecifierToType(
case NestedNameSpecifier::TypeSpecWithTemplate:
return NNS->getAsType();
case NestedNameSpecifier::Identifier: {
- return resolveDeclsToType(resolveDependentMember(
- resolveNestedNameSpecifierToType(NNS->getPrefix()),
- NNS->getAsIdentifier(), TypeFilter));
+ return resolveDeclsToType(
+ resolveDependentMember(
+ resolveNestedNameSpecifierToType(NNS->getPrefix()),
+ NNS->getAsIdentifier(), TypeFilter),
+ Ctx);
}
default:
break;
diff --git a/clang-tools-extra/clangd/HeuristicResolver.h b/clang-tools-extra/clangd/HeuristicResolver.h
index 2b66e4bd9b8fe..6e3a8349a8606 100644
--- a/clang-tools-extra/clangd/HeuristicResolver.h
+++ b/clang-tools-extra/clangd/HeuristicResolver.h
@@ -94,6 +94,11 @@ class HeuristicResolver {
// `E`.
const Type *resolveExprToType(const Expr *E) const;
std::vector<const NamedDecl *> resolveExprToDecls(const Expr *E) const;
+
+ // Helper function for HeuristicResolver::resolveDependentMember()
+ // which takes a possibly-dependent type `T` and heuristically
+ // resolves it to a CXXRecordDecl in which we can try name lookup.
+ CXXRecordDecl *resolveTypeToRecordDecl(const Type *T) const;
};
} // namespace clangd
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index d54539b53badd..8a69d4f9ed4a7 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -877,6 +877,22 @@ TEST_F(TargetDeclTest, DependentExprs) {
}
)cpp";
EXPECT_DECLS("CXXDependentScopeMemberExpr", "void find()");
+
+ Code = R"cpp(
+ template <typename T>
+ struct Waldo {
+ void find();
+ };
+ template <typename T>
+ struct MetaWaldo {
+ using Type = Waldo<T>;
+ };
+ template <typename T>
+ void foo(typename MetaWaldo<T>::Type w) {
+ w.[[find]]();
+ }
+ )cpp";
+ EXPECT_DECLS("CXXDependentScopeMemberExpr", "void find()");
}
TEST_F(TargetDeclTest, DependentTypes) {
More information about the cfe-commits
mailing list