diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2011-07-27 05:28:18 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2011-07-27 05:28:18 +0000 |
commit | 18fd0c6915b45c4daafe18e3cd324c13306f913f (patch) | |
tree | fb1e7ed5e727808a7c7e10189aced11902c070a5 /lib/ARCMigrate/TransAPIUses.cpp | |
parent | 3ef1ad2d28ef5a9b6ac7ec0bd4b2360a4ae3ee8b (diff) |
[arcmt] More automatic transformations and safety improvements; rdar://9615812 :
- Replace calling -zone with 'nil'. -zone is obsolete in ARC.
- Allow removing retain/release on a static global var.
- Fix assertion hit when scanning for name references outside a NSAutoreleasePool scope.
- Automatically add bridged casts for results of objc method calls and when calling CFRetain, for example:
NSString *s;
CFStringRef ref = [s string]; -> CFStringRef ref = (__bridge CFStringRef)([s string]);
ref = s.string; -> ref = (__bridge CFStringRef)(s.string);
ref = [NSString new]; -> ref = (__bridge_retained CFStringRef)([NSString new]);
ref = [s newString]; -> ref = (__bridge_retained CFStringRef)([s newString]);
ref = [[NSString alloc] init]; -> ref = (__bridge_retained CFStringRef)([[NSString alloc] init]);
ref = [[s string] retain]; -> ref = (__bridge_retained CFStringRef)([s string]);
ref = CFRetain(s); -> ref = (__bridge_retained CFTypeRef)(s);
ref = [s retain]; -> ref = (__bridge_retained CFStringRef)(s);
- Emit migrator error when trying to cast to CF type the result of autorelease/release:
for
CFStringRef f3() {
return (CFStringRef)[[[NSString alloc] init] autorelease];
}
emits:
t.m:12:10: error: [rewriter] it is not safe to cast to 'CFStringRef' the result of 'autorelease' message; a __bridge cast may result in a pointer to a destroyed object and a __bridge_retained may leak the object
return (CFStringRef)[[[NSString alloc] init] autorelease];
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.m:12:3: note: [rewriter] remove the cast and change return type of function to 'NSString *' to have the object automatically autoreleased
return (CFStringRef)[[[NSString alloc] init] autorelease];
^
- Before changing attributes to weak/unsafe_unretained, check if the backing ivar
is set to a +1 object, in which case use 'strong' instead.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@136208 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/ARCMigrate/TransAPIUses.cpp')
-rw-r--r-- | lib/ARCMigrate/TransAPIUses.cpp | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/lib/ARCMigrate/TransAPIUses.cpp b/lib/ARCMigrate/TransAPIUses.cpp index 228ca3e091..781ad7742d 100644 --- a/lib/ARCMigrate/TransAPIUses.cpp +++ b/lib/ARCMigrate/TransAPIUses.cpp @@ -9,17 +9,19 @@ // // checkAPIUses: // -// Emits error with some API uses that are not safe in ARC mode: +// Emits error/fix with some API uses that are obsolete or not safe in ARC mode: // // - NSInvocation's [get/set]ReturnValue and [get/set]Argument are only safe // with __unsafe_unretained objects. // - When a NSData's 'bytes' family of methods are used on a local var, // add __attribute__((objc_precise_lifetime)) to make it safer. +// - Calling -zone gets replaced with 'nil'. // //===----------------------------------------------------------------------===// #include "Transforms.h" #include "Internals.h" +#include "clang/Sema/SemaDiagnostic.h" using namespace clang; using namespace arcmt; @@ -35,6 +37,8 @@ class APIChecker : public RecursiveASTVisitor<APIChecker> { Selector bytesSel, getBytesSel, getBytesLengthSel, getBytesRangeSel; + Selector zoneSel; + llvm::DenseSet<VarDecl *> ChangedNSDataVars; public: APIChecker(MigrationPass &pass) : Pass(pass) { @@ -57,9 +61,12 @@ public: getBytesLengthSel = sels.getSelector(2, selIds); selIds[1] = &ids.get("range"); getBytesRangeSel = sels.getSelector(2, selIds); + + zoneSel = sels.getNullarySelector(&ids.get("zone")); } bool VisitObjCMessageExpr(ObjCMessageExpr *E) { + // NSInvocation. if (E->isInstanceMessage() && E->getReceiverInterface() && E->getReceiverInterface()->getName() == "NSInvocation") { @@ -91,6 +98,7 @@ public: return true; } + // NSData. if (E->isInstanceMessage() && E->getReceiverInterface() && E->getReceiverInterface()->getName() == "NSData" && @@ -111,6 +119,20 @@ public: } } + // -zone. + if (E->isInstanceMessage() && + E->getInstanceReceiver() && + E->getSelector() == zoneSel && + Pass.TA.hasDiagnostic(diag::err_unavailable, + diag::err_unavailable_message, + E->getInstanceReceiver()->getExprLoc())) { + // Calling -zone is meaningless in ARC, change it to nil. + Transaction Trans(Pass.TA); + Pass.TA.clearDiagnostic(diag::err_unavailable, + diag::err_unavailable_message, + E->getInstanceReceiver()->getExprLoc()); + Pass.TA.replace(E->getSourceRange(), getNilString(Pass.Ctx)); + } return true; } }; |