Author: mren Date: Thu Apr 7 14:30:20 2016 New Revision: 265711 URL: http://llvm.org/viewvc/llvm-project?rev=265711&view=rev Log: NFC: simplify code in BuildInstanceMessage.
Instead of searching the global pool multiple times: in LookupFactoryMethodInGlobalPool, LookupInstanceMethodInGlobalPool, CollectMultipleMethodsInGlobalPool, and AreMultipleMethodsInGlobalPool, we now collect the method candidates in CollectMultipleMethodsInGlobalPool only, and other functions will use the collected method set. This commit adds parameter "Methods" to AreMultipleMethodsInGlobalPool, and SelectBestMethod. It also changes the implementation of CollectMultipleMethodsInGlobalPool to collect the desired kind first, if none is found, to collect the other kind. This avoids the need to call both LookupFactoryMethodInGlobalPool and LookupInstanceMethodInGlobalPool. Modified: cfe/trunk/include/clang/Sema/Sema.h cfe/trunk/lib/Sema/SemaDeclObjC.cpp cfe/trunk/lib/Sema/SemaExprObjC.cpp cfe/trunk/lib/Sema/SemaOverload.cpp Modified: cfe/trunk/include/clang/Sema/Sema.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=265711&r1=265710&r2=265711&view=diff ============================================================================== --- cfe/trunk/include/clang/Sema/Sema.h (original) +++ cfe/trunk/include/clang/Sema/Sema.h Thu Apr 7 14:30:20 2016 @@ -3155,25 +3155,31 @@ private: public: /// \brief - Returns instance or factory methods in global method pool for - /// given selector. If no such method or only one method found, function returns - /// false; otherwise, it returns true - bool CollectMultipleMethodsInGlobalPool(Selector Sel, - SmallVectorImpl<ObjCMethodDecl*>& Methods, - bool instance); + /// given selector. It checks the desired kind first, if none is found, and + /// parameter checkTheOther is set, it then checks the other kind. If no such + /// method or only one method is found, function returns false; otherwise, it + /// returns true. + bool + CollectMultipleMethodsInGlobalPool(Selector Sel, + SmallVectorImpl<ObjCMethodDecl*>& Methods, + bool InstanceFirst, bool CheckTheOther); - bool AreMultipleMethodsInGlobalPool(Selector Sel, ObjCMethodDecl *BestMethod, - SourceRange R, - bool receiverIdOrClass); + bool + AreMultipleMethodsInGlobalPool(Selector Sel, ObjCMethodDecl *BestMethod, + SourceRange R, bool receiverIdOrClass, + SmallVectorImpl<ObjCMethodDecl*>& Methods); - void DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods, - Selector Sel, SourceRange R, - bool receiverIdOrClass); + void + DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods, + Selector Sel, SourceRange R, + bool receiverIdOrClass); private: /// \brief - Returns a selector which best matches given argument list or /// nullptr if none could be found ObjCMethodDecl *SelectBestMethod(Selector Sel, MultiExprArg Args, - bool IsInstance); + bool IsInstance, + SmallVectorImpl<ObjCMethodDecl*>& Methods); /// \brief Record the typo correction failure and return an empty correction. Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=265711&r1=265710&r2=265711&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original) +++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Thu Apr 7 14:30:20 2016 @@ -3281,25 +3281,57 @@ static bool isAcceptableMethodMismatch(O return (chosen->getReturnType()->isIntegerType()); } +/// We first select the type of the method: Instance or Factory, then collect +/// all methods with that type. bool Sema::CollectMultipleMethodsInGlobalPool( - Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, bool instance) { + Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, + bool InstanceFirst, bool CheckTheOther) { if (ExternalSource) ReadMethodPool(Sel); GlobalMethodPool::iterator Pos = MethodPool.find(Sel); if (Pos == MethodPool.end()) return false; + // Gather the non-hidden methods. - ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; + ObjCMethodList &MethList = InstanceFirst ? Pos->second.first : + Pos->second.second; for (ObjCMethodList *M = &MethList; M; M = M->getNext()) if (M->getMethod() && !M->getMethod()->isHidden()) Methods.push_back(M->getMethod()); + + // Return if we find any method with the desired kind. + if (!Methods.empty()) + return Methods.size() > 1; + + if (!CheckTheOther) + return false; + + // Gather the other kind. + ObjCMethodList &MethList2 = InstanceFirst ? Pos->second.second : + Pos->second.first; + for (ObjCMethodList *M = &MethList2; M; M = M->getNext()) + if (M->getMethod() && !M->getMethod()->isHidden()) + Methods.push_back(M->getMethod()); + return Methods.size() > 1; } -bool Sema::AreMultipleMethodsInGlobalPool(Selector Sel, ObjCMethodDecl *BestMethod, - SourceRange R, - bool receiverIdOrClass) { +bool Sema::AreMultipleMethodsInGlobalPool( + Selector Sel, ObjCMethodDecl *BestMethod, SourceRange R, + bool receiverIdOrClass, SmallVectorImpl<ObjCMethodDecl *> &Methods) { + // Diagnose finding more than one method in global pool. + SmallVector<ObjCMethodDecl *, 4> FilteredMethods; + FilteredMethods.push_back(BestMethod); + + for (auto *M : Methods) + if (M != BestMethod && !M->hasAttr<UnavailableAttr>()) + FilteredMethods.push_back(M); + + if (FilteredMethods.size() > 1) + DiagnoseMultipleMethodInGlobalPool(FilteredMethods, Sel, R, + receiverIdOrClass); + GlobalMethodPool::iterator Pos = MethodPool.find(Sel); // Test for no method in the pool which should not trigger any warning by // caller. @@ -3307,17 +3339,6 @@ bool Sema::AreMultipleMethodsInGlobalPoo return true; ObjCMethodList &MethList = BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second; - - // Diagnose finding more than one method in global pool - SmallVector<ObjCMethodDecl *, 4> Methods; - Methods.push_back(BestMethod); - for (ObjCMethodList *ML = &MethList; ML; ML = ML->getNext()) - if (ObjCMethodDecl *M = ML->getMethod()) - if (!M->isHidden() && M != BestMethod && !M->hasAttr<UnavailableAttr>()) - Methods.push_back(M); - if (Methods.size() > 1) - DiagnoseMultipleMethodInGlobalPool(Methods, Sel, R, receiverIdOrClass); - return MethList.hasMoreThanOneDecl(); } Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=265711&r1=265710&r2=265711&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original) +++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Thu Apr 7 14:30:20 2016 @@ -2625,22 +2625,22 @@ ExprResult Sema::BuildInstanceMessage(Ex typeBound); if (receiverIsIdLike || ReceiverType->isBlockPointerType() || (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) { - Method = LookupInstanceMethodInGlobalPool(Sel, - SourceRange(LBracLoc, RBracLoc), - receiverIsIdLike); - if (!Method) - Method = LookupFactoryMethodInGlobalPool(Sel, - SourceRange(LBracLoc,RBracLoc), - receiverIsIdLike); - if (Method) { + SmallVector<ObjCMethodDecl*, 4> Methods; + CollectMultipleMethodsInGlobalPool(Sel, Methods, true/*InstanceFirst*/, + true/*CheckTheOther*/); + if (!Methods.empty()) { + // We chose the first method as the initial condidate, then try to + // select a better one. + Method = Methods[0]; + if (ObjCMethodDecl *BestMethod = - SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod())) + SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod(), Methods)) Method = BestMethod; + if (!AreMultipleMethodsInGlobalPool(Sel, Method, SourceRange(LBracLoc, RBracLoc), - receiverIsIdLike)) { - DiagnoseUseOfDecl(Method, SelLoc); - } + receiverIsIdLike, Methods)) + DiagnoseUseOfDecl(Method, SelLoc); } } else if (ReceiverType->isObjCClassOrClassKindOfType() || ReceiverType->isObjCQualifiedClassType()) { @@ -2678,25 +2678,32 @@ ExprResult Sema::BuildInstanceMessage(Ex if (!Method) { // If not messaging 'self', look for any factory method named 'Sel'. if (!Receiver || !isSelfExpr(Receiver)) { - Method = LookupFactoryMethodInGlobalPool(Sel, - SourceRange(LBracLoc, RBracLoc)); - if (!Method) { - // If no class (factory) method was found, check if an _instance_ - // method of the same name exists in the root class only. - Method = LookupInstanceMethodInGlobalPool(Sel, - SourceRange(LBracLoc, RBracLoc)); - if (Method) - if (const ObjCInterfaceDecl *ID = - dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { - if (ID->getSuperClass()) - Diag(SelLoc, diag::warn_root_inst_method_not_found) - << Sel << SourceRange(LBracLoc, RBracLoc); - } + // If no class (factory) method was found, check if an _instance_ + // method of the same name exists in the root class only. + SmallVector<ObjCMethodDecl*, 4> Methods; + CollectMultipleMethodsInGlobalPool(Sel, Methods, + false/*InstanceFirst*/, + true/*CheckTheOther*/); + if (!Methods.empty()) { + // We chose the first method as the initial condidate, then try + // to select a better one. + Method = Methods[0]; + + // If we find an instance method, emit waring. + if (Method->isInstanceMethod()) { + if (const ObjCInterfaceDecl *ID = + dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { + if (ID->getSuperClass()) + Diag(SelLoc, diag::warn_root_inst_method_not_found) + << Sel << SourceRange(LBracLoc, RBracLoc); + } + } + + if (ObjCMethodDecl *BestMethod = + SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod(), + Methods)) + Method = BestMethod; } - if (Method) - if (ObjCMethodDecl *BestMethod = - SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod())) - Method = BestMethod; } } } @@ -2761,15 +2768,24 @@ ExprResult Sema::BuildInstanceMessage(Ex // behavior isn't very desirable, however we need it for GCC // compatibility. FIXME: should we deviate?? if (OCIType->qual_empty()) { - Method = LookupInstanceMethodInGlobalPool(Sel, - SourceRange(LBracLoc, RBracLoc)); - if (Method) { - if (auto BestMethod = - SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod())) + SmallVector<ObjCMethodDecl*, 4> Methods; + CollectMultipleMethodsInGlobalPool(Sel, Methods, + true/*InstanceFirst*/, + false/*CheckTheOther*/); + if (!Methods.empty()) { + // We chose the first method as the initial condidate, then try + // to select a better one. + Method = Methods[0]; + + if (ObjCMethodDecl *BestMethod = + SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod(), + Methods)) Method = BestMethod; + AreMultipleMethodsInGlobalPool(Sel, Method, SourceRange(LBracLoc, RBracLoc), - true); + true/*receiverIdOrClass*/, + Methods); } if (Method && !forwardClass) Diag(SelLoc, diag::warn_maynot_respond) Modified: cfe/trunk/lib/Sema/SemaOverload.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=265711&r1=265710&r2=265711&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaOverload.cpp (original) +++ cfe/trunk/lib/Sema/SemaOverload.cpp Thu Apr 7 14:30:20 2016 @@ -5856,12 +5856,12 @@ Sema::AddOverloadCandidate(FunctionDecl } } -ObjCMethodDecl *Sema::SelectBestMethod(Selector Sel, MultiExprArg Args, - bool IsInstance) { - SmallVector<ObjCMethodDecl*, 4> Methods; - if (!CollectMultipleMethodsInGlobalPool(Sel, Methods, IsInstance)) +ObjCMethodDecl * +Sema::SelectBestMethod(Selector Sel, MultiExprArg Args, bool IsInstance, + SmallVectorImpl<ObjCMethodDecl *> &Methods) { + if (Methods.size() <= 1) return nullptr; - + for (unsigned b = 0, e = Methods.size(); b < e; b++) { bool Match = true; ObjCMethodDecl *Method = Methods[b]; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits