@@ -978,9 +978,10 @@ namespace Cpp {
978978 // encompassed in an anonymous namespace as follows.
979979 namespace {
980980 bool IsTemplatedFunction (Decl *D) {
981- if ( llvm::isa_and_nonnull<FunctionTemplateDecl>(D))
982- return true ;
981+ return llvm::isa_and_nonnull<FunctionTemplateDecl>(D);
982+ }
983983
984+ bool IsTemplateInstantiationOrSpecialization (Decl* D) {
984985 if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D)) {
985986 auto TK = FD->getTemplatedKind ();
986987 return TK == FunctionDecl::TemplatedKind::
@@ -1003,9 +1004,12 @@ namespace Cpp {
10031004 bool IsTemplatedFunction (TCppFunction_t func)
10041005 {
10051006 auto *D = (Decl *) func;
1006- return IsTemplatedFunction (D);
1007+ return IsTemplatedFunction (D) || IsTemplateInstantiationOrSpecialization (D) ;
10071008 }
10081009
1010+ // FIXME: This lookup is broken, and should no longer be used in favour of
1011+ // `GetClassTemplatedMethods` If the candidate set returned is =1, that means
1012+ // the template function exists and >1 means overloads
10091013 bool ExistsFunctionTemplate (const std::string& name,
10101014 TCppScope_t parent)
10111015 {
@@ -1021,38 +1025,69 @@ namespace Cpp {
10211025 return false ;
10221026
10231027 if ((intptr_t ) ND != (intptr_t ) -1 )
1024- return IsTemplatedFunction (ND);
1028+ return IsTemplatedFunction (ND) ||
1029+ IsTemplateInstantiationOrSpecialization (ND);
10251030
10261031 // FIXME: Cycle through the Decls and check if there is a templated function
10271032 return true ;
10281033 }
10291034
1030- void GetClassTemplatedMethods ( const std::string& name, TCppScope_t parent,
1031- std::vector<TCppFunction_t>& funcs) {
1032-
1035+ // Looks up all constructors in the current DeclContext
1036+ void LookupConstructors ( const std::string& name, TCppScope_t parent,
1037+ std::vector<TCppFunction_t>& funcs) {
10331038 auto * D = (Decl*)parent;
10341039
1035- if (!parent || name.empty ())
1036- return ;
1040+ if (auto * CXXRD = llvm::dyn_cast_or_null<CXXRecordDecl>(D)) {
1041+ getSema ().ForceDeclarationOfImplicitMembers (CXXRD);
1042+ DeclContextLookupResult Result = getSema ().LookupConstructors (CXXRD);
1043+ // Obtaining all constructors when we intend to lookup a method under a
1044+ // scope can lead to crashes. We avoid that by accumulating constructors
1045+ // only if the Decl matches the lookup name.
1046+ if (!Result.empty ())
1047+ for (auto * i : Result)
1048+ if (GetName (i) == name)
1049+ funcs.push_back (i);
1050+ }
1051+ }
10371052
1038- D = GetUnderlyingScope (D);
1053+ bool GetClassTemplatedMethods (const std::string& name, TCppScope_t parent,
1054+ std::vector<TCppFunction_t>& funcs) {
1055+ auto * D = (Decl*)parent;
1056+ if (!D && name.empty ())
1057+ return false ;
10391058
1040- llvm::StringRef Name (name);
1059+ // Accumulate constructors
1060+ LookupConstructors (name, parent, funcs);
10411061 auto & S = getSema ();
1062+ D = GetUnderlyingScope (D);
1063+ llvm::StringRef Name (name);
10421064 DeclarationName DName = &getASTContext ().Idents .get (name);
10431065 clang::LookupResult R (S, DName, SourceLocation (), Sema::LookupOrdinaryName,
10441066 For_Visible_Redeclaration);
1067+ auto * DC = clang::Decl::castToDeclContext (D);
1068+ Cpp_utils::Lookup::Named (&S, R, DC);
10451069
1046- Cpp_utils::Lookup::Named (&S, R, Decl::castToDeclContext (D));
1047-
1048- if (R.empty ())
1049- return ;
1070+ // Distinct match, single Decl
1071+ if (R.getResultKind () == clang::LookupResult::Found) {
1072+ funcs.push_back (R.getFoundDecl ());
1073+ }
1074+ // Loop over overload set
1075+ else if (R.getResultKind () == clang::LookupResult::FoundOverloaded) {
1076+ for (auto * Found : R)
1077+ if (IsTemplatedFunction (Found))
1078+ funcs.push_back (Found);
1079+ }
10501080
1051- R.resolveKind ();
1081+ // Ambiguously found
1082+ // FIXME : if result not empty, add them anyway?
1083+ else {
1084+ if (!R.empty ()) {
1085+ for (auto * Found : R)
1086+ funcs.push_back (Found);
1087+ }
1088+ }
10521089
1053- for (auto * Found : R)
1054- if (llvm::isa<FunctionTemplateDecl>(Found))
1055- funcs.push_back (Found);
1090+ return !funcs.empty ();
10561091 }
10571092
10581093 // Adapted from inner workings of Sema::BuildCallExpr
@@ -1175,6 +1210,8 @@ namespace Cpp {
11751210 bool IsConstructor (TCppConstFunction_t method)
11761211 {
11771212 const auto * D = static_cast <const Decl*>(method);
1213+ if (const auto * FTD = dyn_cast<FunctionTemplateDecl>(D))
1214+ return IsConstructor (FTD->getTemplatedDecl ());
11781215 return llvm::isa_and_nonnull<CXXConstructorDecl>(D);
11791216 }
11801217
0 commit comments