From: Fariborz Jahanian Date: Thu, 5 Sep 2013 23:04:33 +0000 (+0000) Subject: ObjectiveC migrator: tighten the rules for when X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7fd8fb3a513330c9e754a6eec162170d5a979859;p=clang ObjectiveC migrator: tighten the rules for when inferring NS_RETURNS_RETAINED, etc., return annotations. Do not infer if these annotations are implicit from the naming convention. Also add inference for NS_CONSUMES_SELF annotation. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190106 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index 251cbaf221..8490d00e12 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -996,7 +996,8 @@ ObjCMigrateASTConsumer::CF_BRIDGING_KIND bool FuncIsReturnAnnotated = (FuncDecl->getAttr() || FuncDecl->getAttr() || FuncDecl->getAttr() || - FuncDecl->getAttr()); + FuncDecl->getAttr() || + FuncDecl->getAttr()); // Trivial case of when funciton is annotated and has no argument. if (FuncIsReturnAnnotated && FuncDecl->getNumParams() == 0) @@ -1072,12 +1073,24 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, AnnotationString = " CF_RETURNS_NOT_RETAINED"; } else if (Ret.getObjKind() == RetEffect::ObjC) { - if (Ret.isOwned() && - Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) - AnnotationString = " NS_RETURNS_RETAINED"; - else if (Ret.notOwned() && - Ctx.Idents.get("NS_RETURNS_NOT_RETAINED").hasMacroDefinition()) - AnnotationString = " NS_RETURNS_NOT_RETAINED"; + ObjCMethodFamily OMF = MethodDecl->getMethodFamily(); + switch (OMF) { + case clang::OMF_alloc: + case clang::OMF_new: + case clang::OMF_copy: + case clang::OMF_init: + case clang::OMF_mutableCopy: + break; + + default: + if (Ret.isOwned() && + Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) + AnnotationString = " NS_RETURNS_RETAINED"; + else if (Ret.notOwned() && + Ctx.Idents.get("NS_RETURNS_NOT_RETAINED").hasMacroDefinition()) + AnnotationString = " NS_RETURNS_NOT_RETAINED"; + break; + } } if (AnnotationString) { @@ -1111,7 +1124,18 @@ void ObjCMigrateASTConsumer::migrateAddMethodAnnotation( bool MethodIsReturnAnnotated = (MethodDecl->getAttr() || MethodDecl->getAttr() || MethodDecl->getAttr() || - MethodDecl->getAttr()); + MethodDecl->getAttr() || + MethodDecl->getAttr()); + + if (CE.getReceiver() == DecRefMsg && + !MethodDecl->getAttr() && + MethodDecl->getMethodFamily() != OMF_init && + MethodDecl->getMethodFamily() != OMF_release && + Ctx.Idents.get("NS_CONSUMES_SELF").hasMacroDefinition()) { + edit::Commit commit(*Editor); + commit.insertBefore(MethodDecl->getLocEnd(), " NS_CONSUMES_SELF"); + Editor->commit(commit); + } // Trivial case of when funciton is annotated and has no argument. if (MethodIsReturnAnnotated && diff --git a/test/ARCMT/objcmt-arc-cf-annotations.m.result b/test/ARCMT/objcmt-arc-cf-annotations.m.result index 8478ff51c0..08574de4e4 100644 --- a/test/ARCMT/objcmt-arc-cf-annotations.m.result +++ b/test/ARCMT/objcmt-arc-cf-annotations.m.result @@ -1368,7 +1368,7 @@ typedef NSString* MyStringTy; - (NSString*) returnsAnOwnedCFString CF_RETURNS_RETAINED; // no-warning - (MyStringTy) returnsAnOwnedTypedString NS_RETURNS_RETAINED; // no-warning - (NSString*) newString NS_RETURNS_NOT_RETAINED; // no-warning -- (NSString*) newString_auto NS_RETURNS_AUTORELEASED NS_RETURNS_NOT_RETAINED; // no-warning +- (NSString*) newString_auto NS_RETURNS_AUTORELEASED; // no-warning - (NSString*) newStringNoAttr; - (int) returnsAnOwnedInt NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to methods that return an Objective-C object}} - (id) pseudoInit NS_CONSUMES_SELF NS_RETURNS_RETAINED;