[cfe-commits] r135382 - in /cfe/trunk: lib/ARCMigrate/TransAPIUses.cpp test/ARCMT/nsdata.m test/ARCMT/nsdata.m.result
Argyrios Kyrtzidis
akyrtzi at gmail.com
Mon Jul 18 00:44:50 PDT 2011
Author: akirtzidis
Date: Mon Jul 18 02:44:50 2011
New Revision: 135382
URL: http://llvm.org/viewvc/llvm-project?rev=135382&view=rev
Log:
[arcmt] When a NSData's 'bytes' family of methods are used on a local var,
add __attribute__((objc_precise_lifetime)) to make sure that the object
(and its data) will not get released before the var goes out-of-scope.
rdar://9206226
Added:
cfe/trunk/test/ARCMT/nsdata.m
cfe/trunk/test/ARCMT/nsdata.m.result
Modified:
cfe/trunk/lib/ARCMigrate/TransAPIUses.cpp
Modified: cfe/trunk/lib/ARCMigrate/TransAPIUses.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransAPIUses.cpp?rev=135382&r1=135381&r2=135382&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransAPIUses.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransAPIUses.cpp Mon Jul 18 02:44:50 2011
@@ -13,6 +13,8 @@
//
// - 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.
//
//===----------------------------------------------------------------------===//
@@ -28,9 +30,13 @@
class APIChecker : public RecursiveASTVisitor<APIChecker> {
MigrationPass &Pass;
+
Selector getReturnValueSel, setReturnValueSel;
Selector getArgumentSel, setArgumentSel;
+ Selector bytesSel, getBytesSel, getBytesLengthSel, getBytesRangeSel;
+
+ llvm::DenseSet<VarDecl *> ChangedNSDataVars;
public:
APIChecker(MigrationPass &pass) : Pass(pass) {
SelectorTable &sels = Pass.Ctx.Selectors;
@@ -44,6 +50,14 @@
getArgumentSel = sels.getSelector(2, selIds);
selIds[0] = &ids.get("setArgument");
setArgumentSel = sels.getSelector(2, selIds);
+
+ bytesSel = sels.getNullarySelector(&ids.get("bytes"));
+ getBytesSel = sels.getUnarySelector(&ids.get("getBytes"));
+ selIds[0] = &ids.get("getBytes");
+ selIds[1] = &ids.get("length");
+ getBytesLengthSel = sels.getSelector(2, selIds);
+ selIds[1] = &ids.get("range");
+ getBytesRangeSel = sels.getSelector(2, selIds);
}
bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
@@ -78,6 +92,26 @@
return true;
}
+ if (E->isInstanceMessage() &&
+ E->getReceiverInterface() &&
+ E->getReceiverInterface()->getName() == "NSData" &&
+ E->getInstanceReceiver() &&
+ (E->getSelector() == bytesSel ||
+ E->getSelector() == getBytesSel ||
+ E->getSelector() == getBytesLengthSel ||
+ E->getSelector() == getBytesRangeSel)) {
+ Expr *rec = E->getInstanceReceiver();
+ rec = rec->IgnoreParenCasts();
+ if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(rec))
+ if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
+ if (VD->hasLocalStorage() && !ChangedNSDataVars.count(VD)) {
+ Transaction Trans(Pass.TA);
+ Pass.TA.insertAfterToken(VD->getLocation(),
+ " __attribute__((objc_precise_lifetime))");
+ ChangedNSDataVars.insert(VD);
+ }
+ }
+
return true;
}
};
Added: cfe/trunk/test/ARCMT/nsdata.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/nsdata.m?rev=135382&view=auto
==============================================================================
--- cfe/trunk/test/ARCMT/nsdata.m (added)
+++ cfe/trunk/test/ARCMT/nsdata.m Mon Jul 18 02:44:50 2011
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fsyntax-only -fobjc-arc -x objective-c %s.result
+// RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fsyntax-only -x objective-c %s > %t
+// RUN: diff %t %s.result
+
+#include "Common.h"
+
+ at interface NSData : NSObject
+- (const void *)bytes;
+ at end
+
+typedef struct _NSRange {
+ NSUInteger location;
+ NSUInteger length;
+} NSRange;
+
+ at interface NSData (NSExtendedData)
+- (void)getBytes:(void *)buffer length:(NSUInteger)length;
+- (void)getBytes:(void *)buffer range:(NSRange)range;
+ at end
+
+ at interface NSData (NSDeprecated)
+- (void)getBytes:(void *)buffer;
+ at end
+
+void test(NSData* parmdata) {
+ NSData *data, *data2 = parmdata;
+ void *p = [data bytes];
+ p = [data bytes];
+
+ [data2 getBytes:&p length:sizeof(p)];
+ p = [parmdata bytes];
+}
Added: cfe/trunk/test/ARCMT/nsdata.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/nsdata.m.result?rev=135382&view=auto
==============================================================================
--- cfe/trunk/test/ARCMT/nsdata.m.result (added)
+++ cfe/trunk/test/ARCMT/nsdata.m.result Mon Jul 18 02:44:50 2011
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fsyntax-only -fobjc-arc -x objective-c %s.result
+// RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fsyntax-only -x objective-c %s > %t
+// RUN: diff %t %s.result
+
+#include "Common.h"
+
+ at interface NSData : NSObject
+- (const void *)bytes;
+ at end
+
+typedef struct _NSRange {
+ NSUInteger location;
+ NSUInteger length;
+} NSRange;
+
+ at interface NSData (NSExtendedData)
+- (void)getBytes:(void *)buffer length:(NSUInteger)length;
+- (void)getBytes:(void *)buffer range:(NSRange)range;
+ at end
+
+ at interface NSData (NSDeprecated)
+- (void)getBytes:(void *)buffer;
+ at end
+
+void test(NSData* parmdata __attribute__((objc_precise_lifetime))) {
+ NSData *data __attribute__((objc_precise_lifetime)), *data2 __attribute__((objc_precise_lifetime)) = parmdata;
+ void *p = [data bytes];
+ p = [data bytes];
+
+ [data2 getBytes:&p length:sizeof(p)];
+ p = [parmdata bytes];
+}
More information about the cfe-commits
mailing list