1 //===--- TransProperties.cpp - Transformations to ARC mode ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // - Adds strong/weak/unsafe_unretained ownership specifier to properties that
14 // - Migrates properties from (retain) to (strong) and (assign) to
15 // (unsafe_unretained/weak).
16 // - If a property is synthesized, adds the ownership specifier in the ivar
17 // backing the property.
19 // @interface Foo : NSObject {
22 // @property (assign) id x;
25 // @interface Foo : NSObject {
26 // NSObject *__weak x;
28 // @property (weak) id x;
31 //===----------------------------------------------------------------------===//
33 #include "Transforms.h"
34 #include "Internals.h"
35 #include "clang/Basic/SourceManager.h"
36 #include "clang/Lex/Lexer.h"
37 #include "clang/Sema/SemaDiagnostic.h"
40 using namespace clang;
41 using namespace arcmt;
42 using namespace trans;
46 class PropertiesRewriter {
47 MigrationContext &MigrateCtx;
49 ObjCImplementationDecl *CurImplD;
53 PropAction_RetainReplacedWithStrong,
54 PropAction_AssignRemoved,
55 PropAction_AssignRewritten,
56 PropAction_MaybeAddWeakOrUnsafe
60 ObjCPropertyDecl *PropD;
62 ObjCPropertyImplDecl *ImplD;
64 PropData(ObjCPropertyDecl *propD) : PropD(propD), IvarD(0), ImplD(0) { }
67 typedef SmallVector<PropData, 2> PropsTy;
68 typedef std::map<unsigned, PropsTy> AtPropDeclsTy;
69 AtPropDeclsTy AtProps;
70 llvm::DenseMap<IdentifierInfo *, PropActionKind> ActionOnProp;
73 explicit PropertiesRewriter(MigrationContext &MigrateCtx)
74 : MigrateCtx(MigrateCtx), Pass(MigrateCtx.Pass) { }
76 static void collectProperties(ObjCContainerDecl *D, AtPropDeclsTy &AtProps,
77 AtPropDeclsTy *PrevAtProps = 0) {
78 for (ObjCInterfaceDecl::prop_iterator
79 propI = D->prop_begin(),
80 propE = D->prop_end(); propI != propE; ++propI) {
81 if (propI->getAtLoc().isInvalid())
83 unsigned RawLoc = propI->getAtLoc().getRawEncoding();
85 if (PrevAtProps->find(RawLoc) != PrevAtProps->end())
87 PropsTy &props = AtProps[RawLoc];
88 props.push_back(*propI);
92 void doTransform(ObjCImplementationDecl *D) {
94 ObjCInterfaceDecl *iface = D->getClassInterface();
98 collectProperties(iface, AtProps);
100 typedef DeclContext::specific_decl_iterator<ObjCPropertyImplDecl>
102 for (prop_impl_iterator
103 I = prop_impl_iterator(D->decls_begin()),
104 E = prop_impl_iterator(D->decls_end()); I != E; ++I) {
105 ObjCPropertyImplDecl *implD = *I;
106 if (implD->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
108 ObjCPropertyDecl *propD = implD->getPropertyDecl();
109 if (!propD || propD->isInvalidDecl())
111 ObjCIvarDecl *ivarD = implD->getPropertyIvarDecl();
112 if (!ivarD || ivarD->isInvalidDecl())
114 unsigned rawAtLoc = propD->getAtLoc().getRawEncoding();
115 AtPropDeclsTy::iterator findAtLoc = AtProps.find(rawAtLoc);
116 if (findAtLoc == AtProps.end())
119 PropsTy &props = findAtLoc->second;
120 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
121 if (I->PropD == propD) {
129 for (AtPropDeclsTy::iterator
130 I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
131 SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first);
132 PropsTy &props = I->second;
133 if (!getPropertyType(props)->isObjCRetainableType())
135 if (hasIvarWithExplicitARCOwnership(props))
138 Transaction Trans(Pass.TA);
139 rewriteProperty(props, atLoc);
142 AtPropDeclsTy AtExtProps;
143 // Look through extensions.
144 for (ObjCInterfaceDecl::visible_extensions_iterator
145 ext = iface->visible_extensions_begin(),
146 extEnd = iface->visible_extensions_end();
147 ext != extEnd; ++ext) {
148 collectProperties(*ext, AtExtProps, &AtProps);
151 for (AtPropDeclsTy::iterator
152 I = AtExtProps.begin(), E = AtExtProps.end(); I != E; ++I) {
153 SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first);
154 PropsTy &props = I->second;
155 Transaction Trans(Pass.TA);
156 doActionForExtensionProp(props, atLoc);
161 void doPropAction(PropActionKind kind,
162 PropsTy &props, SourceLocation atLoc,
163 bool markAction = true) {
165 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
166 ActionOnProp[I->PropD->getIdentifier()] = kind;
169 case PropAction_None:
171 case PropAction_RetainReplacedWithStrong: {
172 StringRef toAttr = "strong";
173 MigrateCtx.rewritePropertyAttribute("retain", toAttr, atLoc);
176 case PropAction_AssignRemoved:
177 return removeAssignForDefaultStrong(props, atLoc);
178 case PropAction_AssignRewritten:
179 return rewriteAssign(props, atLoc);
180 case PropAction_MaybeAddWeakOrUnsafe:
181 return maybeAddWeakOrUnsafeUnretainedAttr(props, atLoc);
185 void doActionForExtensionProp(PropsTy &props, SourceLocation atLoc) {
186 llvm::DenseMap<IdentifierInfo *, PropActionKind>::iterator I;
187 I = ActionOnProp.find(props[0].PropD->getIdentifier());
188 if (I == ActionOnProp.end())
191 doPropAction(I->second, props, atLoc, false);
194 void rewriteProperty(PropsTy &props, SourceLocation atLoc) {
195 ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props);
197 if (propAttrs & (ObjCPropertyDecl::OBJC_PR_copy |
198 ObjCPropertyDecl::OBJC_PR_unsafe_unretained |
199 ObjCPropertyDecl::OBJC_PR_strong |
200 ObjCPropertyDecl::OBJC_PR_weak))
203 if (propAttrs & ObjCPropertyDecl::OBJC_PR_retain) {
204 // strong is the default.
205 return doPropAction(PropAction_RetainReplacedWithStrong, props, atLoc);
208 bool HasIvarAssignedAPlusOneObject = hasIvarAssignedAPlusOneObject(props);
210 if (propAttrs & ObjCPropertyDecl::OBJC_PR_assign) {
211 if (HasIvarAssignedAPlusOneObject)
212 return doPropAction(PropAction_AssignRemoved, props, atLoc);
213 return doPropAction(PropAction_AssignRewritten, props, atLoc);
216 if (HasIvarAssignedAPlusOneObject ||
217 (Pass.isGCMigration() && !hasGCWeak(props, atLoc)))
218 return; // 'strong' by default.
220 return doPropAction(PropAction_MaybeAddWeakOrUnsafe, props, atLoc);
223 void removeAssignForDefaultStrong(PropsTy &props,
224 SourceLocation atLoc) const {
225 removeAttribute("retain", atLoc);
226 if (!removeAttribute("assign", atLoc))
229 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
231 Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
232 diag::err_arc_assign_property_ownership,
233 diag::err_arc_inconsistent_property_ownership,
234 I->IvarD->getLocation());
238 void rewriteAssign(PropsTy &props, SourceLocation atLoc) const {
239 bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
240 /*AllowOnUnknownClass=*/Pass.isGCMigration());
241 const char *toWhich =
242 (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "strong" :
243 (canUseWeak ? "weak" : "unsafe_unretained");
245 bool rewroteAttr = rewriteAttribute("assign", toWhich, atLoc);
249 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
250 if (isUserDeclared(I->IvarD)) {
252 I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) {
253 const char *toWhich =
254 (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "__strong " :
255 (canUseWeak ? "__weak " : "__unsafe_unretained ");
256 Pass.TA.insert(I->IvarD->getLocation(), toWhich);
260 Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
261 diag::err_arc_assign_property_ownership,
262 diag::err_arc_inconsistent_property_ownership,
263 I->IvarD->getLocation());
267 void maybeAddWeakOrUnsafeUnretainedAttr(PropsTy &props,
268 SourceLocation atLoc) const {
269 bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
270 /*AllowOnUnknownClass=*/Pass.isGCMigration());
272 bool addedAttr = addAttribute(canUseWeak ? "weak" : "unsafe_unretained",
277 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
278 if (isUserDeclared(I->IvarD)) {
280 I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
281 Pass.TA.insert(I->IvarD->getLocation(),
282 canUseWeak ? "__weak " : "__unsafe_unretained ");
285 Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
286 diag::err_arc_assign_property_ownership,
287 diag::err_arc_inconsistent_property_ownership,
288 I->IvarD->getLocation());
289 Pass.TA.clearDiagnostic(
290 diag::err_arc_objc_property_default_assign_on_object,
291 I->ImplD->getLocation());
296 bool removeAttribute(StringRef fromAttr, SourceLocation atLoc) const {
297 return MigrateCtx.removePropertyAttribute(fromAttr, atLoc);
300 bool rewriteAttribute(StringRef fromAttr, StringRef toAttr,
301 SourceLocation atLoc) const {
302 return MigrateCtx.rewritePropertyAttribute(fromAttr, toAttr, atLoc);
305 bool addAttribute(StringRef attr, SourceLocation atLoc) const {
306 return MigrateCtx.addPropertyAttribute(attr, atLoc);
309 class PlusOneAssign : public RecursiveASTVisitor<PlusOneAssign> {
312 PlusOneAssign(ObjCIvarDecl *D) : Ivar(D) {}
314 bool VisitBinAssign(BinaryOperator *E) {
315 Expr *lhs = E->getLHS()->IgnoreParenImpCasts();
316 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(lhs)) {
317 if (RE->getDecl() != Ivar)
320 if (isPlusOneAssign(E))
328 bool hasIvarAssignedAPlusOneObject(PropsTy &props) const {
329 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
330 PlusOneAssign oneAssign(I->IvarD);
331 bool notFound = oneAssign.TraverseDecl(CurImplD);
339 bool hasIvarWithExplicitARCOwnership(PropsTy &props) const {
340 if (Pass.isGCMigration())
343 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
344 if (isUserDeclared(I->IvarD)) {
345 if (isa<AttributedType>(I->IvarD->getType()))
347 if (I->IvarD->getType().getLocalQualifiers().getObjCLifetime()
348 != Qualifiers::OCL_Strong)
356 bool hasAllIvarsBacked(PropsTy &props) const {
357 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
358 if (!isUserDeclared(I->IvarD))
364 // \brief Returns true if all declarations in the @property have GC __weak.
365 bool hasGCWeak(PropsTy &props, SourceLocation atLoc) const {
366 if (!Pass.isGCMigration())
370 return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding());
373 bool isUserDeclared(ObjCIvarDecl *ivarD) const {
374 return ivarD && !ivarD->getSynthesize();
377 QualType getPropertyType(PropsTy &props) const {
378 assert(!props.empty());
379 QualType ty = props[0].PropD->getType().getUnqualifiedType();
382 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
383 assert(ty == I->PropD->getType().getUnqualifiedType());
389 ObjCPropertyDecl::PropertyAttributeKind
390 getPropertyAttrs(PropsTy &props) const {
391 assert(!props.empty());
392 ObjCPropertyDecl::PropertyAttributeKind
393 attrs = props[0].PropD->getPropertyAttributesAsWritten();
396 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
397 assert(attrs == I->PropD->getPropertyAttributesAsWritten());
404 } // anonymous namespace
406 void PropertyRewriteTraverser::traverseObjCImplementation(
407 ObjCImplementationContext &ImplCtx) {
408 PropertiesRewriter(ImplCtx.getMigrationContext())
409 .doTransform(ImplCtx.getImplementationDecl());