[cfe-commits] r158127 - in /cfe/trunk: lib/ARCMigrate/TransAutoreleasePool.cpp lib/ARCMigrate/TransRetainReleaseDealloc.cpp lib/ARCMigrate/TransUnbridgedCasts.cpp lib/ARCMigrate/TransUnusedInitDelegate.cpp lib/ARCMigrate/Transforms.h test/ARCMT/nonobjc-to-objc-cast-2.m test/ARCMT/nonobjc-to-objc-cast.m test/ARCMT/nonobjc-to-objc-cast.m.result
Argyrios Kyrtzidis
akyrtzi at gmail.com
Wed Jun 6 17:44:06 PDT 2012
Author: akirtzidis
Date: Wed Jun 6 19:44:06 2012
New Revision: 158127
URL: http://llvm.org/viewvc/llvm-project?rev=158127&view=rev
Log:
[arcmt] At an unbridged cast error, if we're returning a load-of-ivar from a +0 method,
automatically insert a __bridge cast.
radar://11560638
Modified:
cfe/trunk/lib/ARCMigrate/TransAutoreleasePool.cpp
cfe/trunk/lib/ARCMigrate/TransRetainReleaseDealloc.cpp
cfe/trunk/lib/ARCMigrate/TransUnbridgedCasts.cpp
cfe/trunk/lib/ARCMigrate/TransUnusedInitDelegate.cpp
cfe/trunk/lib/ARCMigrate/Transforms.h
cfe/trunk/test/ARCMT/nonobjc-to-objc-cast-2.m
cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m
cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m.result
Modified: cfe/trunk/lib/ARCMigrate/TransAutoreleasePool.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransAutoreleasePool.cpp?rev=158127&r1=158126&r2=158127&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransAutoreleasePool.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransAutoreleasePool.cpp Wed Jun 6 19:44:06 2012
@@ -75,7 +75,7 @@
&pass.Ctx.Idents.get("drain"));
}
- void transformBody(Stmt *body) {
+ void transformBody(Stmt *body, Decl *ParentD) {
Body = body;
TraverseStmt(body);
}
Modified: cfe/trunk/lib/ARCMigrate/TransRetainReleaseDealloc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransRetainReleaseDealloc.cpp?rev=158127&r1=158126&r2=158127&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransRetainReleaseDealloc.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransRetainReleaseDealloc.cpp Wed Jun 6 19:44:06 2012
@@ -49,7 +49,7 @@
Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("finalize"));
}
- void transformBody(Stmt *body) {
+ void transformBody(Stmt *body, Decl *ParentD) {
Body = body;
collectRemovables(body, Removables);
StmtMap.reset(new ParentMap(body));
Modified: cfe/trunk/lib/ARCMigrate/TransUnbridgedCasts.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransUnbridgedCasts.cpp?rev=158127&r1=158126&r2=158127&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransUnbridgedCasts.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransUnbridgedCasts.cpp Wed Jun 6 19:44:06 2012
@@ -50,13 +50,15 @@
MigrationPass &Pass;
IdentifierInfo *SelfII;
OwningPtr<ParentMap> StmtMap;
+ Decl *ParentD;
public:
- UnbridgedCastRewriter(MigrationPass &pass) : Pass(pass) {
+ UnbridgedCastRewriter(MigrationPass &pass) : Pass(pass), ParentD(0) {
SelfII = &Pass.Ctx.Idents.get("self");
}
- void transformBody(Stmt *body) {
+ void transformBody(Stmt *body, Decl *ParentD) {
+ this->ParentD = ParentD;
StmtMap.reset(new ParentMap(body));
TraverseStmt(body);
}
@@ -155,6 +157,21 @@
}
}
}
+
+ // If returning an ivar or a member of an ivar from a +0 method, use
+ // a __bridge cast.
+ Expr *base = inner->IgnoreParenImpCasts();
+ while (isa<MemberExpr>(base))
+ base = cast<MemberExpr>(base)->getBase()->IgnoreParenImpCasts();
+ if (isa<ObjCIvarRefExpr>(base) &&
+ isa<ReturnStmt>(StmtMap->getParentIgnoreParenCasts(E))) {
+ if (ObjCMethodDecl *method = dyn_cast_or_null<ObjCMethodDecl>(ParentD)) {
+ if (!method->hasAttr<NSReturnsRetainedAttr>()) {
+ castToObjCObject(E, /*retained=*/false);
+ return;
+ }
+ }
+ }
}
void castToObjCObject(CastExpr *E, bool retained) {
Modified: cfe/trunk/lib/ARCMigrate/TransUnusedInitDelegate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransUnusedInitDelegate.cpp?rev=158127&r1=158126&r2=158127&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransUnusedInitDelegate.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransUnusedInitDelegate.cpp Wed Jun 6 19:44:06 2012
@@ -40,7 +40,7 @@
UnusedInitRewriter(MigrationPass &pass)
: Body(0), Pass(pass) { }
- void transformBody(Stmt *body) {
+ void transformBody(Stmt *body, Decl *ParentD) {
Body = body;
collectRemovables(body, Removables);
TraverseStmt(body);
Modified: cfe/trunk/lib/ARCMigrate/Transforms.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Transforms.h?rev=158127&r1=158126&r2=158127&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Transforms.h (original)
+++ cfe/trunk/lib/ARCMigrate/Transforms.h Wed Jun 6 19:44:06 2012
@@ -13,6 +13,7 @@
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ParentMap.h"
#include "llvm/ADT/DenseSet.h"
+#include "llvm/Support/SaveAndRestore.h"
namespace clang {
class Decl;
@@ -176,15 +177,22 @@
template <typename BODY_TRANS>
class BodyTransform : public RecursiveASTVisitor<BodyTransform<BODY_TRANS> > {
MigrationPass &Pass;
+ Decl *ParentD;
+ typedef RecursiveASTVisitor<BodyTransform<BODY_TRANS> > base;
public:
- BodyTransform(MigrationPass &pass) : Pass(pass) { }
+ BodyTransform(MigrationPass &pass) : Pass(pass), ParentD(0) { }
bool TraverseStmt(Stmt *rootS) {
if (rootS)
- BODY_TRANS(Pass).transformBody(rootS);
+ BODY_TRANS(Pass).transformBody(rootS, ParentD);
return true;
}
+
+ bool TraverseObjCMethodDecl(ObjCMethodDecl *D) {
+ SaveAndRestore<Decl *> SetParent(ParentD, D);
+ return base::TraverseObjCMethodDecl(D);
+ }
};
typedef llvm::DenseSet<Expr *> ExprSet;
Modified: cfe/trunk/test/ARCMT/nonobjc-to-objc-cast-2.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/nonobjc-to-objc-cast-2.m?rev=158127&r1=158126&r2=158127&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/nonobjc-to-objc-cast-2.m (original)
+++ cfe/trunk/test/ARCMT/nonobjc-to-objc-cast-2.m Wed Jun 6 19:44:06 2012
@@ -3,16 +3,37 @@
#include "Common.h"
- at interface NSString : NSObject
--(id)string;
--(id)newString;
- at end
-
typedef const struct __CFString * CFStringRef;
typedef const void * CFTypeRef;
CFTypeRef CFBridgingRetain(id X);
id CFBridgingRelease(CFTypeRef);
+struct StrS {
+ CFStringRef sref_member;
+};
+
+ at interface NSString : NSObject {
+ CFStringRef sref;
+ struct StrS *strS;
+}
+-(id)string;
+-(id)newString;
+ at end
+
+ at implementation NSString
+-(id)string {
+ if (0)
+ return sref;
+ else
+ return strS->sref_member;
+}
+-(id)newString {
+ return sref; // expected-error {{implicit conversion of C pointer type 'CFStringRef' (aka 'const struct __CFString *') to Objective-C pointer type 'id' requires a bridged cast}} \
+ // expected-note{{use __bridge to convert directly (no change in ownership)}} \
+ // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}}
+}
+ at end
+
void f(BOOL b) {
CFStringRef cfstr;
NSString *str = (NSString *)cfstr; // expected-error {{cast of C pointer type 'CFStringRef' (aka 'const struct __CFString *') to Objective-C pointer type 'NSString *' requires a bridged cast}} \
Modified: cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m?rev=158127&r1=158126&r2=158127&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m (original)
+++ cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m Wed Jun 6 19:44:06 2012
@@ -5,11 +5,6 @@
#include "Common.h"
- at interface NSString : NSObject
--(id)string;
--(id)newString;
- at end
-
typedef const struct __CFString * CFStringRef;
extern const CFStringRef kUTTypePlainText;
extern const CFStringRef kUTTypeRTF;
@@ -21,6 +16,18 @@
extern CFStringRef CFUUIDCreateString(CFAllocatorRef alloc, CFUUIDRef uuid);
+struct StrS {
+ CFStringRef sref_member;
+};
+
+ at interface NSString : NSObject {
+ CFStringRef sref;
+ struct StrS *strS;
+}
+-(id)string;
+-(id)newString;
+ at end
+
void f(BOOL b, id p) {
NSString *str = (NSString *)kUTTypePlainText;
str = b ? kUTTypeRTF : kUTTypePlainText;
@@ -41,6 +48,16 @@
}
@end
+ at implementation NSString
+-(id)string {
+ if (0)
+ return sref;
+ else
+ return strS->sref_member;
+}
+-(id)newString { return 0; }
+ at end
+
extern void consumeParam(CFStringRef CF_CONSUMED p);
void f2(NSString *s) {
Modified: cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m.result?rev=158127&r1=158126&r2=158127&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m.result (original)
+++ cfe/trunk/test/ARCMT/nonobjc-to-objc-cast.m.result Wed Jun 6 19:44:06 2012
@@ -5,11 +5,6 @@
#include "Common.h"
- at interface NSString : NSObject
--(id)string;
--(id)newString;
- at end
-
typedef const struct __CFString * CFStringRef;
extern const CFStringRef kUTTypePlainText;
extern const CFStringRef kUTTypeRTF;
@@ -21,6 +16,18 @@
extern CFStringRef CFUUIDCreateString(CFAllocatorRef alloc, CFUUIDRef uuid);
+struct StrS {
+ CFStringRef sref_member;
+};
+
+ at interface NSString : NSObject {
+ CFStringRef sref;
+ struct StrS *strS;
+}
+-(id)string;
+-(id)newString;
+ at end
+
void f(BOOL b, id p) {
NSString *str = (__bridge NSString *)kUTTypePlainText;
str = (__bridge NSString *)(b ? kUTTypeRTF : kUTTypePlainText);
@@ -41,6 +48,16 @@
}
@end
+ at implementation NSString
+-(id)string {
+ if (0)
+ return (__bridge id)(sref);
+ else
+ return (__bridge id)(strS->sref_member);
+}
+-(id)newString { return 0; }
+ at end
+
extern void consumeParam(CFStringRef CF_CONSUMED p);
void f2(NSString *s) {
More information about the cfe-commits
mailing list