[clang-tools-extra] cd4e8d7 - [clangd] Fix an assertion failure in TargetFinder's heuristic resolution of dependent type.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 30 00:09:56 PDT 2020


Author: Haojian Wu
Date: 2020-07-30T08:54:22+02:00
New Revision: cd4e8d7f6f5ef108919f9f53db35ac73d1edea3d

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

LOG: [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

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 c6022b2463e8..a346d6b662e9 100644
--- a/clang-tools-extra/clangd/FindTarget.cpp
+++ b/clang-tools-extra/clangd/FindTarget.cpp
@@ -163,13 +163,12 @@ const Type *getPointeeType(const Type *T) {
 }
 
 // Forward declaration, needed as this function is mutually recursive
-// with resolveDependentExprToDecls.
-const Type *resolveDependentExprToType(const Expr *E);
+// with resolveExprToDecls.
+const Type *resolveExprToType(const Expr *E);
 
-// Try to heuristically resolve a dependent expression `E` to one
+// Try to heuristically resolve a possibly-dependent expression `E` to one
 // or more declarations that it likely references.
-std::vector<const NamedDecl *> resolveDependentExprToDecls(const Expr *E) {
-  assert(E->isTypeDependent());
+std::vector<const NamedDecl *> resolveExprToDecls(const Expr *E) {
   if (const auto *ME = dyn_cast<CXXDependentScopeMemberExpr>(E)) {
     const Type *BaseType = ME->getBaseType().getTypePtrOrNull();
     if (ME->isArrow()) {
@@ -183,7 +182,7 @@ std::vector<const NamedDecl *> resolveDependentExprToDecls(const Expr *E) {
       // can get further by analyzing the depedent expression.
       Expr *Base = ME->isImplicitAccess() ? nullptr : ME->getBase();
       if (Base && BT->getKind() == BuiltinType::Dependent) {
-        BaseType = resolveDependentExprToType(Base);
+        BaseType = resolveExprToType(Base);
       }
     }
     return getMembersReferencedViaDependentName(
@@ -197,7 +196,7 @@ std::vector<const NamedDecl *> resolveDependentExprToDecls(const Expr *E) {
         /*IsNonstaticMember=*/false);
   }
   if (const auto *CE = dyn_cast<CallExpr>(E)) {
-    const auto *CalleeType = resolveDependentExprToType(CE->getCallee());
+    const auto *CalleeType = resolveExprToType(CE->getCallee());
     if (!CalleeType)
       return {};
     if (const auto *FnTypePtr = CalleeType->getAs<PointerType>())
@@ -209,15 +208,16 @@ std::vector<const NamedDecl *> resolveDependentExprToDecls(const Expr *E) {
       }
     }
   }
-  if (const auto *ME = dyn_cast<MemberExpr>(E)) {
+  if (const auto *ME = dyn_cast<MemberExpr>(E))
     return {ME->getMemberDecl()};
-  }
+  if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
+    return {DRE->getFoundDecl()};
   return {};
 }
 
-// Try to heuristically resolve the type of a dependent expression `E`.
-const Type *resolveDependentExprToType(const Expr *E) {
-  std::vector<const NamedDecl *> Decls = resolveDependentExprToDecls(E);
+// Try to heuristically resolve the type of a possibly-dependent expression `E`.
+const Type *resolveExprToType(const Expr *E) {
+  std::vector<const NamedDecl *> Decls = resolveExprToDecls(E);
   if (Decls.size() != 1) // Names an overload set -- just bail.
     return nullptr;
   if (const auto *TD = dyn_cast<TypeDecl>(Decls[0])) {
@@ -426,12 +426,12 @@ struct TargetFinder {
       }
       void
       VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
-        for (const NamedDecl *D : resolveDependentExprToDecls(E)) {
+        for (const NamedDecl *D : resolveExprToDecls(E)) {
           Outer.add(D, Flags);
         }
       }
       void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
-        for (const NamedDecl *D : resolveDependentExprToDecls(E)) {
+        for (const NamedDecl *D : resolveExprToDecls(E)) {
           Outer.add(D, Flags);
         }
       }

diff  --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index f7af77e8b57b..92095e871e20 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -627,6 +627,20 @@ TEST_F(TargetDeclTest, DependentExprs) {
         };
       )cpp";
   EXPECT_DECLS("CXXDependentScopeMemberExpr", "int aaaa");
+
+  Code = R"cpp(
+        class Foo {
+        public:
+          static Foo k(int);
+          template <typename T> T convert() const;
+        };
+        template <typename T>
+        void test() {
+          Foo::k(T()).template [[convert]]<T>();
+        }
+      )cpp";
+  EXPECT_DECLS("CXXDependentScopeMemberExpr",
+               "template <typename T> T convert() const");
 }
 
 TEST_F(TargetDeclTest, ObjC) {


        


More information about the cfe-commits mailing list