diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/ARCMigrate/ObjCMT.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/ARCMigrate/ObjCMT.cpp | 101 |
1 files changed, 58 insertions, 43 deletions
diff --git a/contrib/llvm/tools/clang/lib/ARCMigrate/ObjCMT.cpp b/contrib/llvm/tools/clang/lib/ARCMigrate/ObjCMT.cpp index 52c424c..8c2e0f4 100644 --- a/contrib/llvm/tools/clang/lib/ARCMigrate/ObjCMT.cpp +++ b/contrib/llvm/tools/clang/lib/ARCMigrate/ObjCMT.cpp @@ -48,7 +48,7 @@ class ObjCMigrateASTConsumer : public ASTConsumer { }; void migrateDecl(Decl *D); - void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCContainerDecl *D); + void migrateObjCContainerDecl(ASTContext &Ctx, ObjCContainerDecl *D); void migrateProtocolConformance(ASTContext &Ctx, const ObjCImplementationDecl *ImpDecl); void CacheObjCNSIntegerTypedefed(const TypedefDecl *TypedefDcl); @@ -245,17 +245,16 @@ namespace { (Msg->getReceiverKind() != ObjCMessageExpr::Instance && Msg->getReceiverKind() != ObjCMessageExpr::SuperInstance)) return false; + if (const Expr *Receiver = Msg->getInstanceReceiver()) + if (Receiver->getType()->isObjCBuiltinType()) + return false; + const ObjCMethodDecl *Method = Msg->getMethodDecl(); if (!Method) return false; if (!Method->isPropertyAccessor()) return false; - const ObjCInterfaceDecl *IFace = - NS.getASTContext().getObjContainingInterface(Method); - if (!IFace) - return false; - const ObjCPropertyDecl *Prop = Method->findPropertyDecl(); if (!Prop) return false; @@ -305,6 +304,10 @@ namespace { BegLoc = PP.getLocForEndOfToken(BegLoc); SourceLocation EndLoc = RHS->getLocStart(); EndLoc = EndLoc.getLocWithOffset(-1); + const char *colon = PP.getSourceManager().getCharacterData(EndLoc); + // Add a space after '=' if there is no space between RHS and '=' + if (colon && colon[0] == ':') + PropertyDotString += " "; SourceRange Range(BegLoc, EndLoc); commit.replace(Range, PropertyDotString); // remove '[' ']' @@ -465,7 +468,7 @@ static void rewriteToObjCProperty(const ObjCMethodDecl *Getter, ASTContext &Context = NS.getASTContext(); bool LParenAdded = false; std::string PropertyString = "@property "; - if (UseNsIosOnlyMacro && Context.Idents.get("NS_NONATOMIC_IOSONLY").hasMacroDefinition()) { + if (UseNsIosOnlyMacro && NS.isMacroDefined("NS_NONATOMIC_IOSONLY")) { PropertyString += "(NS_NONATOMIC_IOSONLY"; LParenAdded = true; } else if (!Atomic) { @@ -575,7 +578,7 @@ static bool IsCategoryNameWithDeprecatedSuffix(ObjCContainerDecl *D) { return false; } -void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx, +void ObjCMigrateASTConsumer::migrateObjCContainerDecl(ASTContext &Ctx, ObjCContainerDecl *D) { if (D->isDeprecated() || IsCategoryNameWithDeprecatedSuffix(D)) return; @@ -616,7 +619,7 @@ ClassImplementsAllMethodsAndProperties(ASTContext &Ctx, if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional) continue; HasAtleastOneRequiredProperty = true; - DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName()); + DeclContext::lookup_result R = IDecl->lookup(Property->getDeclName()); if (R.size() == 0) { // Relax the rule and look into class's implementation for a synthesize // or dynamic declaration. Class is implementing a property coming from @@ -647,7 +650,7 @@ ClassImplementsAllMethodsAndProperties(ASTContext &Ctx, continue; if (MD->getImplementationControl() == ObjCMethodDecl::Optional) continue; - DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName()); + DeclContext::lookup_result R = ImpDecl->lookup(MD->getDeclName()); if (R.size() == 0) return false; bool match = false; @@ -768,19 +771,34 @@ static void rewriteToNSMacroDecl(ASTContext &Ctx, const TypedefDecl *TypedefDcl, const NSAPI &NS, edit::Commit &commit, bool IsNSIntegerType) { - QualType EnumUnderlyingT = EnumDcl->getPromotionType(); - assert(!EnumUnderlyingT.isNull() + QualType DesignatedEnumType = EnumDcl->getIntegerType(); + assert(!DesignatedEnumType.isNull() && "rewriteToNSMacroDecl - underlying enum type is null"); PrintingPolicy Policy(Ctx.getPrintingPolicy()); - std::string TypeString = EnumUnderlyingT.getAsString(Policy); + std::string TypeString = DesignatedEnumType.getAsString(Policy); std::string ClassString = IsNSIntegerType ? "NS_ENUM(" : "NS_OPTIONS("; ClassString += TypeString; ClassString += ", "; ClassString += TypedefDcl->getIdentifier()->getName(); ClassString += ')'; - SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart()); + SourceLocation EndLoc; + if (EnumDcl->getIntegerTypeSourceInfo()) { + TypeSourceInfo *TSourceInfo = EnumDcl->getIntegerTypeSourceInfo(); + TypeLoc TLoc = TSourceInfo->getTypeLoc(); + EndLoc = TLoc.getLocEnd(); + const char *lbrace = Ctx.getSourceManager().getCharacterData(EndLoc); + unsigned count = 0; + if (lbrace) + while (lbrace[count] != '{') + ++count; + if (count > 0) + EndLoc = EndLoc.getLocWithOffset(count-1); + } + else + EndLoc = EnumDcl->getLocStart(); + SourceRange R(EnumDcl->getLocStart(), EndLoc); commit.replace(R, ClassString); // This is to remove spaces between '}' and typedef name. SourceLocation StartTypedefLoc = EnumDcl->getLocEnd(); @@ -902,7 +920,7 @@ bool ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl, const TypedefDecl *TypedefDcl) { if (!EnumDcl->isCompleteDefinition() || EnumDcl->getIdentifier() || - EnumDcl->isDeprecated() || EnumDcl->getIntegerTypeSourceInfo()) + EnumDcl->isDeprecated()) return false; if (!TypedefDcl) { if (NSIntegerTypedefed) { @@ -1259,7 +1277,7 @@ void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx, QualType RT = OM->getReturnType(); if (!TypeIsInnerPointer(RT) || - !Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) + !NSAPIObj->isMacroDefined("NS_RETURNS_INNER_POINTER")) return; edit::Commit commit(*Editor); @@ -1270,9 +1288,9 @@ void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx, void ObjCMigrateASTConsumer::migratePropertyNsReturnsInnerPointer(ASTContext &Ctx, ObjCPropertyDecl *P) { QualType T = P->getType(); - + if (!TypeIsInnerPointer(T) || - !Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) + !NSAPIObj->isMacroDefined("NS_RETURNS_INNER_POINTER")) return; edit::Commit commit(*Editor); commit.insertBefore(P->getLocEnd(), " NS_RETURNS_INNER_POINTER "); @@ -1390,7 +1408,7 @@ static bool AuditedType (QualType AT) { void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) { if (CFFunctionIBCandidates.empty()) return; - if (!Ctx.Idents.get("CF_IMPLICIT_BRIDGING_ENABLED").hasMacroDefinition()) { + if (!NSAPIObj->isMacroDefined("CF_IMPLICIT_BRIDGING_ENABLED")) { CFFunctionIBCandidates.clear(); FileId = FileID(); return; @@ -1465,16 +1483,14 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, RetEffect Ret = CE.getReturnValue(); const char *AnnotationString = nullptr; if (Ret.getObjKind() == RetEffect::CF) { - if (Ret.isOwned() && - Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) + if (Ret.isOwned() && NSAPIObj->isMacroDefined("CF_RETURNS_RETAINED")) AnnotationString = " CF_RETURNS_RETAINED"; else if (Ret.notOwned() && - Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) + NSAPIObj->isMacroDefined("CF_RETURNS_NOT_RETAINED")) AnnotationString = " CF_RETURNS_NOT_RETAINED"; } else if (Ret.getObjKind() == RetEffect::ObjC) { - if (Ret.isOwned() && - Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) + if (Ret.isOwned() && NSAPIObj->isMacroDefined("NS_RETURNS_RETAINED")) AnnotationString = " NS_RETURNS_RETAINED"; } @@ -1491,13 +1507,13 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, const ParmVarDecl *pd = *pi; ArgEffect AE = AEArgs[i]; if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() && - Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { + NSAPIObj->isMacroDefined("CF_CONSUMED")) { edit::Commit commit(*Editor); commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); Editor->commit(commit); } else if (AE == DecRefMsg && !pd->hasAttr<NSConsumedAttr>() && - Ctx.Idents.get("NS_CONSUMED").hasMacroDefinition()) { + NSAPIObj->isMacroDefined("NS_CONSUMED")) { edit::Commit commit(*Editor); commit.insertBefore(pd->getLocation(), "NS_CONSUMED "); Editor->commit(commit); @@ -1582,11 +1598,10 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, RetEffect Ret = CE.getReturnValue(); const char *AnnotationString = nullptr; if (Ret.getObjKind() == RetEffect::CF) { - if (Ret.isOwned() && - Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) + if (Ret.isOwned() && NSAPIObj->isMacroDefined("CF_RETURNS_RETAINED")) AnnotationString = " CF_RETURNS_RETAINED"; else if (Ret.notOwned() && - Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) + NSAPIObj->isMacroDefined("CF_RETURNS_NOT_RETAINED")) AnnotationString = " CF_RETURNS_NOT_RETAINED"; } else if (Ret.getObjKind() == RetEffect::ObjC) { @@ -1600,8 +1615,7 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, break; default: - if (Ret.isOwned() && - Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) + if (Ret.isOwned() && NSAPIObj->isMacroDefined("NS_RETURNS_RETAINED")) AnnotationString = " NS_RETURNS_RETAINED"; break; } @@ -1620,7 +1634,7 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, const ParmVarDecl *pd = *pi; ArgEffect AE = AEArgs[i]; if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() && - Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { + NSAPIObj->isMacroDefined("CF_CONSUMED")) { edit::Commit commit(*Editor); commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); Editor->commit(commit); @@ -1640,12 +1654,12 @@ void ObjCMigrateASTConsumer::migrateAddMethodAnnotation( MethodDecl->hasAttr<NSReturnsRetainedAttr>() || MethodDecl->hasAttr<NSReturnsNotRetainedAttr>() || MethodDecl->hasAttr<NSReturnsAutoreleasedAttr>()); - - if (CE.getReceiver() == DecRefMsg && + + if (CE.getReceiver() == DecRefMsg && !MethodDecl->hasAttr<NSConsumesSelfAttr>() && MethodDecl->getMethodFamily() != OMF_init && MethodDecl->getMethodFamily() != OMF_release && - Ctx.Idents.get("NS_CONSUMES_SELF").hasMacroDefinition()) { + NSAPIObj->isMacroDefined("NS_CONSUMES_SELF")) { edit::Commit commit(*Editor); commit.insertBefore(MethodDecl->getLocEnd(), " NS_CONSUMES_SELF"); Editor->commit(commit); @@ -1711,7 +1725,7 @@ void ObjCMigrateASTConsumer::inferDesignatedInitializers( const ObjCInterfaceDecl *IFace = ImplD->getClassInterface(); if (!IFace || IFace->hasDesignatedInitializers()) return; - if (!Ctx.Idents.get("NS_DESIGNATED_INITIALIZER").hasMacroDefinition()) + if (!NSAPIObj->isMacroDefined("NS_DESIGNATED_INITIALIZER")) return; for (const auto *MD : ImplD->instance_methods()) { @@ -1772,9 +1786,7 @@ public: : SourceMgr(SM), OS(OS) { OS << "[\n"; } - ~JSONEditWriter() { - OS << "]\n"; - } + ~JSONEditWriter() override { OS << "]\n"; } private: struct EntryWriter { @@ -1858,13 +1870,16 @@ void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D)) if (canModify(CDecl)) - migrateObjCInterfaceDecl(Ctx, CDecl); + migrateObjCContainerDecl(Ctx, CDecl); if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(*D)) { if (canModify(CatDecl)) - migrateObjCInterfaceDecl(Ctx, CatDecl); + migrateObjCContainerDecl(Ctx, CatDecl); } - else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) + else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) { ObjCProtocolDecls.insert(PDecl->getCanonicalDecl()); + if (canModify(PDecl)) + migrateObjCContainerDecl(Ctx, PDecl); + } else if (const ObjCImplementationDecl *ImpDecl = dyn_cast<ObjCImplementationDecl>(*D)) { if ((ASTMigrateActions & FrontendOptions::ObjCMT_ProtocolConformance) && @@ -2268,7 +2283,7 @@ bool arcmt::getFileRemappingsFromFileList( continue; } - remap.push_back(std::make_pair(I->first->getName(), TempFile)); + remap.emplace_back(I->first->getName(), TempFile); } return hasErrorOccurred; |