From 843dfccdc5095687856c7f8cd3e85372d38a2ea2 Mon Sep 17 00:00:00 2001 From: John McCall Date: Tue, 29 Nov 2016 21:57:00 +0000 Subject: [PATCH] getObjCEncodingForMethodDecl cannot fail. Simplify. NFC. llvm-svn: 288203 --- clang/include/clang/AST/ASTContext.h | 12 ++++---- clang/lib/AST/ASTContext.cpp | 28 +++++++++--------- clang/lib/CodeGen/CGObjCGNU.cpp | 37 +++++++++--------------- clang/lib/CodeGen/CGObjCMac.cpp | 21 +++----------- clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp | 13 +++++---- clang/lib/Frontend/Rewrite/RewriteObjC.cpp | 14 ++++----- clang/tools/libclang/CXType.cpp | 7 ++--- 7 files changed, 53 insertions(+), 79 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index cfd744a..1310c43 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -1617,16 +1617,15 @@ public: /// /// \returns true if an error occurred (e.g., because one of the parameter /// types is incomplete), false otherwise. - bool getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S); + std::string getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const; /// \brief Emit the encoded type for the method declaration \p Decl into /// \p S. /// /// \returns true if an error occurred (e.g., because one of the parameter /// types is incomplete), false otherwise. - bool getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S, - bool Extended = false) - const; + std::string getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, + bool Extended = false) const; /// \brief Return the encoded type for this block declaration. std::string getObjCEncodingForBlock(const BlockExpr *blockExpr) const; @@ -1635,9 +1634,8 @@ public: /// this method declaration. If non-NULL, Container must be either /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should /// only be NULL when getting encodings for protocol properties. - void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, - const Decl *Container, - std::string &S) const; + std::string getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, + const Decl *Container) const; bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, ObjCProtocolDecl *rProto) const; diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 62d9b65..ed08558 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -5578,8 +5578,9 @@ std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { return S; } -bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, - std::string& S) { +std::string +ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const { + std::string S; // Encode result type. getObjCEncodingForType(Decl->getReturnType(), S); CharUnits ParmOffset; @@ -5590,8 +5591,8 @@ bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, if (sz.isZero()) continue; - assert (sz.isPositive() && - "getObjCEncodingForFunctionDecl - Incomplete param type"); + assert(sz.isPositive() && + "getObjCEncodingForFunctionDecl - Incomplete param type"); ParmOffset += sz; } S += charUnitsToString(ParmOffset); @@ -5613,7 +5614,7 @@ bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, ParmOffset += getObjCEncodingTypeSize(PType); } - return false; + return S; } /// getObjCEncodingForMethodParameter - Return the encoded type for a single @@ -5635,11 +5636,11 @@ void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, /// getObjCEncodingForMethodDecl - Return the encoded type for this method /// declaration. -bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, - std::string& S, - bool Extended) const { +std::string ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, + bool Extended) const { // FIXME: This is not very efficient. // Encode return type. + std::string S; getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), Decl->getReturnType(), S, Extended); // Compute size of all parameters. @@ -5685,7 +5686,7 @@ bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, ParmOffset += getObjCEncodingTypeSize(PType); } - return false; + return S; } ObjCPropertyImplDecl * @@ -5733,9 +5734,9 @@ ASTContext::getObjCPropertyImplDeclForPropertyDecl( /// kPropertyNonAtomic = 'N' // property non-atomic /// }; /// @endcode -void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, - const Decl *Container, - std::string& S) const { +std::string +ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, + const Decl *Container) const { // Collect information from the property implementation decl(s). bool Dynamic = false; ObjCPropertyImplDecl *SynthesizePID = nullptr; @@ -5749,7 +5750,7 @@ void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, } // FIXME: This is not very efficient. - S = "T"; + std::string S = "T"; // Encode result type. // GCC has some special rules regarding encoding of properties which @@ -5798,6 +5799,7 @@ void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, } // FIXME: OBJCGC: weak & strong + return S; } /// getLegacyIntegralTypeEncoding - diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp index 8f98452..98295d8 100644 --- a/clang/lib/CodeGen/CGObjCGNU.cpp +++ b/clang/lib/CodeGen/CGObjCGNU.cpp @@ -209,8 +209,8 @@ protected: if ((R.getKind() == ObjCRuntime::GNUstep) && (R.getVersion() >= VersionTuple(1, 6))) { std::string NameAndAttributes; - std::string TypeStr; - CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); + std::string TypeStr = + CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container); NameAndAttributes += '\0'; NameAndAttributes += TypeStr.length() + 3; NameAndAttributes += TypeStr; @@ -1123,8 +1123,7 @@ llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel) { llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, const ObjCMethodDecl *Method) { - std::string SelTypes; - CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes); + std::string SelTypes = CGM.getContext().getObjCEncodingForMethodDecl(Method); return GetSelector(CGF, Method->getSelector(), SelTypes); } @@ -1804,8 +1803,7 @@ void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { SmallVector OptionalInstanceMethodNames; SmallVector OptionalInstanceMethodTypes; for (const auto *I : PD->instance_methods()) { - std::string TypeStr; - Context.getObjCEncodingForMethodDecl(I, TypeStr); + std::string TypeStr = Context.getObjCEncodingForMethodDecl(I); if (I->getImplementationControl() == ObjCMethodDecl::Optional) { OptionalInstanceMethodNames.push_back( MakeConstantString(I->getSelector().getAsString())); @@ -1822,8 +1820,7 @@ void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { SmallVector OptionalClassMethodNames; SmallVector OptionalClassMethodTypes; for (const auto *I : PD->class_methods()) { - std::string TypeStr; - Context.getObjCEncodingForMethodDecl(I,TypeStr); + std::string TypeStr = Context.getObjCEncodingForMethodDecl(I); if (I->getImplementationControl() == ObjCMethodDecl::Optional) { OptionalClassMethodNames.push_back( MakeConstantString(I->getSelector().getAsString())); @@ -1892,8 +1889,7 @@ void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { PushPropertyAttributes(fields, property); if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { - std::string typeStr; - Context.getObjCEncodingForMethodDecl(getter, typeStr); + std::string typeStr = Context.getObjCEncodingForMethodDecl(getter); llvm::Constant *typeEncoding = MakeConstantString(typeStr); InstanceMethodTypes.push_back(typeEncoding); fields.add(MakeConstantString(getter->getSelector().getAsString())); @@ -1903,8 +1899,7 @@ void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { fields.add(NULLPtr); } if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { - std::string typeStr; - Context.getObjCEncodingForMethodDecl(setter, typeStr); + std::string typeStr = Context.getObjCEncodingForMethodDecl(setter); llvm::Constant *typeEncoding = MakeConstantString(typeStr); InstanceMethodTypes.push_back(typeEncoding); fields.add(MakeConstantString(setter->getSelector().getAsString())); @@ -2045,8 +2040,7 @@ void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { SmallVector InstanceMethodTypes; for (const auto *I : OCD->instance_methods()) { InstanceMethodSels.push_back(I->getSelector()); - std::string TypeStr; - CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr); + std::string TypeStr = CGM.getContext().getObjCEncodingForMethodDecl(I); InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); } @@ -2055,8 +2049,7 @@ void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { SmallVector ClassMethodTypes; for (const auto *I : OCD->class_methods()) { ClassMethodSels.push_back(I->getSelector()); - std::string TypeStr; - CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr); + std::string TypeStr = CGM.getContext().getObjCEncodingForMethodDecl(I); ClassMethodTypes.push_back(MakeConstantString(TypeStr)); } @@ -2126,8 +2119,7 @@ llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OI fields.add(MakePropertyEncodingString(property, OID)); PushPropertyAttributes(fields, property, isSynthesized, isDynamic); if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { - std::string TypeStr; - Context.getObjCEncodingForMethodDecl(getter,TypeStr); + std::string TypeStr = Context.getObjCEncodingForMethodDecl(getter); llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); if (isSynthesized) { InstanceMethodTypes.push_back(TypeEncoding); @@ -2140,8 +2132,7 @@ llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OI fields.add(NULLPtr); } if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { - std::string TypeStr; - Context.getObjCEncodingForMethodDecl(setter,TypeStr); + std::string TypeStr = Context.getObjCEncodingForMethodDecl(setter); llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); if (isSynthesized) { InstanceMethodTypes.push_back(TypeEncoding); @@ -2279,8 +2270,7 @@ void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { SmallVector InstanceMethodTypes; for (const auto *I : OID->instance_methods()) { InstanceMethodSels.push_back(I->getSelector()); - std::string TypeStr; - Context.getObjCEncodingForMethodDecl(I,TypeStr); + std::string TypeStr = Context.getObjCEncodingForMethodDecl(I); InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); } @@ -2292,8 +2282,7 @@ void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { SmallVector ClassMethodTypes; for (const auto *I : OID->class_methods()) { ClassMethodSels.push_back(I->getSelector()); - std::string TypeStr; - Context.getObjCEncodingForMethodDecl(I,TypeStr); + std::string TypeStr = Context.getObjCEncodingForMethodDecl(I); ClassMethodTypes.push_back(MakeConstantString(TypeStr)); } // Collect the names of referenced protocols diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp index 06b77a0..1d25f3b 100644 --- a/clang/lib/CodeGen/CGObjCMac.cpp +++ b/clang/lib/CodeGen/CGObjCMac.cpp @@ -2760,8 +2760,6 @@ llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { std::vector MethodTypesExt, OptMethodTypesExt; for (const auto *MD : PD->instance_methods()) { llvm::Constant *C = GetMethodDescriptionConstant(MD); - if (!C) - return GetOrEmitProtocolRef(PD); if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { OptInstanceMethods.push_back(C); @@ -2774,8 +2772,6 @@ llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { for (const auto *MD : PD->class_methods()) { llvm::Constant *C = GetMethodDescriptionConstant(MD); - if (!C) - return GetOrEmitProtocolRef(PD); if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { OptClassMethods.push_back(C); @@ -3076,8 +3072,6 @@ CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { ObjCTypes.SelectorPtrTy), GetMethodVarType(MD) }; - if (!Desc[1]) - return nullptr; return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, Desc); @@ -5178,9 +5172,8 @@ llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, bool Extended) { - std::string TypeStr; - if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended)) - return nullptr; + std::string TypeStr = + CGM.getContext().getObjCEncodingForMethodDecl(D, Extended); llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; if (!Entry) @@ -5201,8 +5194,8 @@ llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { llvm::Constant * CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, const Decl *Container) { - std::string TypeStr; - CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); + std::string TypeStr = + CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container); return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); } @@ -6633,8 +6626,6 @@ llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( std::vector MethodTypesExt, OptMethodTypesExt; for (const auto *MD : PD->instance_methods()) { llvm::Constant *C = GetMethodDescriptionConstant(MD); - if (!C) - return GetOrEmitProtocolRef(PD); if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { OptInstanceMethods.push_back(C); @@ -6647,8 +6638,6 @@ llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( for (const auto *MD : PD->class_methods()) { llvm::Constant *C = GetMethodDescriptionConstant(MD); - if (!C) - return GetOrEmitProtocolRef(PD); if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { OptClassMethods.push_back(C); @@ -6814,8 +6803,6 @@ CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), ObjCTypes.SelectorPtrTy); Desc[1] = GetMethodVarType(MD); - if (!Desc[1]) - return nullptr; // Protocol methods have no implementation. So, this entry is always NULL. Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); diff --git a/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp b/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp index c481e1c..e7bfced 100644 --- a/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp +++ b/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp @@ -6348,8 +6348,7 @@ static void Write_method_list_t_initializer(RewriteModernObjC &RewriteObj, Result += "\t{(struct objc_selector *)\""; Result += (MD)->getSelector().getAsString(); Result += "\""; Result += ", "; - std::string MethodTypeString; - Context->getObjCEncodingForMethodDecl(MD, MethodTypeString); + std::string MethodTypeString = Context->getObjCEncodingForMethodDecl(MD); Result += "\""; Result += MethodTypeString; Result += "\""; Result += ", "; if (!MethodImpl) @@ -6388,8 +6387,9 @@ static void Write_prop_list_t_initializer(RewriteModernObjC &RewriteObj, else Result += "\t{\""; Result += PropDecl->getName(); Result += "\","; - std::string PropertyTypeString, QuotePropertyTypeString; - Context->getObjCEncodingForPropertyDecl(PropDecl, Container, PropertyTypeString); + std::string PropertyTypeString = + Context->getObjCEncodingForPropertyDecl(PropDecl, Container); + std::string QuotePropertyTypeString; RewriteObj.QuoteDoublequotes(PropertyTypeString, QuotePropertyTypeString); Result += "\""; Result += QuotePropertyTypeString; Result += "\""; if (i == e-1) @@ -6718,8 +6718,9 @@ static void Write__extendedMethodTypes_initializer(RewriteModernObjC &RewriteObj Result += "{\n"; for (unsigned i = 0, e = Methods.size(); i < e; i++) { ObjCMethodDecl *MD = Methods[i]; - std::string MethodTypeString, QuoteMethodTypeString; - Context->getObjCEncodingForMethodDecl(MD, MethodTypeString, true); + std::string MethodTypeString = + Context->getObjCEncodingForMethodDecl(MD, true); + std::string QuoteMethodTypeString; RewriteObj.QuoteDoublequotes(MethodTypeString, QuoteMethodTypeString); Result += "\t\""; Result += QuoteMethodTypeString; Result += "\""; if (i == e-1) diff --git a/clang/lib/Frontend/Rewrite/RewriteObjC.cpp b/clang/lib/Frontend/Rewrite/RewriteObjC.cpp index 00fc83f1..e842e59 100644 --- a/clang/lib/Frontend/Rewrite/RewriteObjC.cpp +++ b/clang/lib/Frontend/Rewrite/RewriteObjC.cpp @@ -5124,8 +5124,7 @@ void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( else Result += "\t ,{(struct objc_selector *)\""; Result += (*I)->getSelector().getAsString(); - std::string MethodTypeString; - Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); + std::string MethodTypeString = Context->getObjCEncodingForMethodDecl(*I); Result += "\", \""; Result += MethodTypeString; Result += "\"}\n"; @@ -5162,8 +5161,7 @@ void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( else Result += "\t ,{(struct objc_selector *)\""; Result += (*I)->getSelector().getAsString(); - std::string MethodTypeString; - Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); + std::string MethodTypeString = Context->getObjCEncodingForMethodDecl(*I); Result += "\", \""; Result += MethodTypeString; Result += "\"}\n"; @@ -5773,8 +5771,8 @@ void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegi Result += "\t,{{(SEL)\""; Result += (*MethodBegin)->getSelector().getAsString(); - std::string MethodTypeString; - Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); + std::string MethodTypeString = + Context->getObjCEncodingForMethodDecl(*MethodBegin); Result += "\", \""; Result += MethodTypeString; Result += "\", (void *)"; @@ -5783,8 +5781,8 @@ void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegi for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { Result += "\t ,{(SEL)\""; Result += (*MethodBegin)->getSelector().getAsString(); - std::string MethodTypeString; - Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); + std::string MethodTypeString = + Context->getObjCEncodingForMethodDecl(*MethodBegin); Result += "\", \""; Result += MethodTypeString; Result += "\", (void *)"; diff --git a/clang/tools/libclang/CXType.cpp b/clang/tools/libclang/CXType.cpp index 106fa7e..a07bbe08 100644 --- a/clang/tools/libclang/CXType.cpp +++ b/clang/tools/libclang/CXType.cpp @@ -902,12 +902,11 @@ CXString clang_getDeclObjCTypeEncoding(CXCursor C) { std::string encoding; if (const ObjCMethodDecl *OMD = dyn_cast(D)) { - if (Ctx.getObjCEncodingForMethodDecl(OMD, encoding)) - return cxstring::createRef("?"); + encoding = Ctx.getObjCEncodingForMethodDecl(OMD); } else if (const ObjCPropertyDecl *OPD = dyn_cast(D)) - Ctx.getObjCEncodingForPropertyDecl(OPD, nullptr, encoding); + encoding = Ctx.getObjCEncodingForPropertyDecl(OPD, nullptr); else if (const FunctionDecl *FD = dyn_cast(D)) - Ctx.getObjCEncodingForFunctionDecl(FD, encoding); + encoding = Ctx.getObjCEncodingForFunctionDecl(FD); else { QualType Ty; if (const TypeDecl *TD = dyn_cast(D)) -- 2.7.4