diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -281,9 +281,32 @@ void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) { Outer.add(SE->getPack(), Flags); } + void VisitCXXNewExpr(const CXXNewExpr *CNE) { + Outer.add(CNE->getOperatorNew(), Flags); + // Also consider `new` a reference to any constructor that is called. + if (auto *CCE = CNE->getConstructExpr()) + Outer.add(CCE, Flags); + // FIXME: heuristically resolve constructors in dependent code. + } + void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE) { + Outer.add(CDE->getOperatorDelete(), Flags); + // Also consider `delete` a reference to any destructor that is called. + QualType T = CDE->getDestroyedType(); + if (T.isNull()) + return; + if (const auto *CRD = T->getAsCXXRecordDecl()) { + if (auto *CDD = CRD->getDestructor()) + Outer.add(CDD, Flags); + } + // FIXME: heuristically resolve destructors in dependent code. + } void VisitCXXConstructExpr(const CXXConstructExpr *CCE) { Outer.add(CCE->getConstructor(), Flags); } + void + VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *CCE) { + // FIXME: heuristically resolve constructors in dependent code. + } void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) { for (const DesignatedInitExpr::Designator &D : llvm::reverse(DIE->designators())) @@ -341,12 +364,6 @@ void VisitPseudoObjectExpr(const PseudoObjectExpr *POE) { Outer.add(POE->getSyntacticForm(), Flags); } - void VisitCXXNewExpr(const CXXNewExpr *CNE) { - Outer.add(CNE->getOperatorNew(), Flags); - } - void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE) { - Outer.add(CDE->getOperatorDelete(), Flags); - } }; Visitor(*this, Flags).Visit(S); } diff --git a/clang-tools-extra/clangd/HeuristicResolver.h b/clang-tools-extra/clangd/HeuristicResolver.h --- a/clang-tools-extra/clangd/HeuristicResolver.h +++ b/clang-tools-extra/clangd/HeuristicResolver.h @@ -17,6 +17,7 @@ class ASTContext; class CallExpr; class CXXDependentScopeMemberExpr; +class CXXDestructorDecl; class DeclarationName; class DependentScopeDeclRefExpr; class NamedDecl; diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -756,11 +756,12 @@ Code = R"cpp( struct X { + X(); static void *operator new(unsigned long); }; auto* k = [[new]] X(); )cpp"; - EXPECT_DECLS("CXXNewExpr", "static void *operator new(unsigned long)"); + EXPECT_DECLS("CXXNewExpr", "static void *operator new(unsigned long)", "X()"); Code = R"cpp( void *operator new(unsigned long); auto* k = [[new]] int(); @@ -769,13 +770,15 @@ Code = R"cpp( struct X { + ~X(); static void operator delete(void *) noexcept; }; void k(X* x) { [[delete]] x; } )cpp"; - EXPECT_DECLS("CXXDeleteExpr", "static void operator delete(void *) noexcept"); + EXPECT_DECLS("CXXDeleteExpr", "static void operator delete(void *) noexcept", + "~X() noexcept"); Code = R"cpp( void operator delete(void *) noexcept; void k(int* x) {