Benjamin Kramer | d81108f | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 1 | //===--- TransProperties.cpp - Transformations to ARC mode ----------------===// |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 10 | // rewriteProperties: |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 11 | // |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 12 | // - Adds strong/weak/unsafe_unretained ownership specifier to properties that |
| 13 | // are missing one. |
| 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. |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 18 | // |
| 19 | // @interface Foo : NSObject { |
| 20 | // NSObject *x; |
| 21 | // } |
| 22 | // @property (assign) id x; |
| 23 | // @end |
| 24 | // ----> |
| 25 | // @interface Foo : NSObject { |
| 26 | // NSObject *__weak x; |
| 27 | // } |
| 28 | // @property (weak) id x; |
| 29 | // @end |
| 30 | // |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | #include "Transforms.h" |
| 34 | #include "Internals.h" |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 35 | #include "clang/Basic/SourceManager.h" |
| 36 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 37 | #include "clang/Sema/SemaDiagnostic.h" |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 38 | #include <map> |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace clang; |
| 41 | using namespace arcmt; |
| 42 | using namespace trans; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 43 | |
| 44 | namespace { |
| 45 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 46 | class PropertiesRewriter { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 47 | MigrationContext &MigrateCtx; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 48 | MigrationPass &Pass; |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 49 | ObjCImplementationDecl *CurImplD; |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 50 | |
| 51 | enum PropActionKind { |
| 52 | PropAction_None, |
Fariborz Jahanian | 9652adf | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 53 | PropAction_RetainReplacedWithStrong, |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 54 | PropAction_AssignRemoved, |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 55 | PropAction_AssignRewritten, |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 56 | PropAction_MaybeAddWeakOrUnsafe |
| 57 | }; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 58 | |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 59 | struct PropData { |
| 60 | ObjCPropertyDecl *PropD; |
| 61 | ObjCIvarDecl *IvarD; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 62 | ObjCPropertyImplDecl *ImplD; |
| 63 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 64 | PropData(ObjCPropertyDecl *propD) |
| 65 | : PropD(propD), IvarD(nullptr), ImplD(nullptr) {} |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | typedef SmallVector<PropData, 2> PropsTy; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 69 | typedef std::map<unsigned, PropsTy> AtPropDeclsTy; |
| 70 | AtPropDeclsTy AtProps; |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 71 | llvm::DenseMap<IdentifierInfo *, PropActionKind> ActionOnProp; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 72 | |
| 73 | public: |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 74 | explicit PropertiesRewriter(MigrationContext &MigrateCtx) |
| 75 | : MigrateCtx(MigrateCtx), Pass(MigrateCtx.Pass) { } |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 76 | |
Argyrios Kyrtzidis | 28e0a7f | 2012-03-29 01:10:31 +0000 | [diff] [blame] | 77 | static void collectProperties(ObjCContainerDecl *D, AtPropDeclsTy &AtProps, |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 78 | AtPropDeclsTy *PrevAtProps = nullptr) { |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 79 | for (auto *Prop : D->properties()) { |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 80 | if (Prop->getAtLoc().isInvalid()) |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 81 | continue; |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 82 | unsigned RawLoc = Prop->getAtLoc().getRawEncoding(); |
Argyrios Kyrtzidis | 28e0a7f | 2012-03-29 01:10:31 +0000 | [diff] [blame] | 83 | if (PrevAtProps) |
| 84 | if (PrevAtProps->find(RawLoc) != PrevAtProps->end()) |
| 85 | continue; |
| 86 | PropsTy &props = AtProps[RawLoc]; |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 87 | props.push_back(Prop); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 91 | void doTransform(ObjCImplementationDecl *D) { |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 92 | CurImplD = D; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 93 | ObjCInterfaceDecl *iface = D->getClassInterface(); |
| 94 | if (!iface) |
| 95 | return; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 96 | |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 97 | collectProperties(iface, AtProps); |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 98 | |
| 99 | typedef DeclContext::specific_decl_iterator<ObjCPropertyImplDecl> |
| 100 | prop_impl_iterator; |
| 101 | for (prop_impl_iterator |
| 102 | I = prop_impl_iterator(D->decls_begin()), |
| 103 | E = prop_impl_iterator(D->decls_end()); I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 104 | ObjCPropertyImplDecl *implD = *I; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 105 | if (implD->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 106 | continue; |
| 107 | ObjCPropertyDecl *propD = implD->getPropertyDecl(); |
| 108 | if (!propD || propD->isInvalidDecl()) |
| 109 | continue; |
| 110 | ObjCIvarDecl *ivarD = implD->getPropertyIvarDecl(); |
| 111 | if (!ivarD || ivarD->isInvalidDecl()) |
| 112 | continue; |
| 113 | unsigned rawAtLoc = propD->getAtLoc().getRawEncoding(); |
| 114 | AtPropDeclsTy::iterator findAtLoc = AtProps.find(rawAtLoc); |
| 115 | if (findAtLoc == AtProps.end()) |
| 116 | continue; |
| 117 | |
| 118 | PropsTy &props = findAtLoc->second; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 119 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 120 | if (I->PropD == propD) { |
| 121 | I->IvarD = ivarD; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 122 | I->ImplD = implD; |
| 123 | break; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 128 | for (AtPropDeclsTy::iterator |
| 129 | I = AtProps.begin(), E = AtProps.end(); I != E; ++I) { |
| 130 | SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first); |
| 131 | PropsTy &props = I->second; |
Argyrios Kyrtzidis | aa421ea | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 132 | if (!getPropertyType(props)->isObjCRetainableType()) |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 133 | continue; |
Argyrios Kyrtzidis | 0dd98a4 | 2011-11-07 18:40:32 +0000 | [diff] [blame] | 134 | if (hasIvarWithExplicitARCOwnership(props)) |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 135 | continue; |
| 136 | |
| 137 | Transaction Trans(Pass.TA); |
| 138 | rewriteProperty(props, atLoc); |
| 139 | } |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 140 | |
| 141 | AtPropDeclsTy AtExtProps; |
| 142 | // Look through extensions. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 143 | for (auto *Ext : iface->visible_extensions()) |
| 144 | collectProperties(Ext, AtExtProps, &AtProps); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 145 | |
| 146 | for (AtPropDeclsTy::iterator |
| 147 | I = AtExtProps.begin(), E = AtExtProps.end(); I != E; ++I) { |
| 148 | SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first); |
| 149 | PropsTy &props = I->second; |
| 150 | Transaction Trans(Pass.TA); |
| 151 | doActionForExtensionProp(props, atLoc); |
| 152 | } |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | private: |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 156 | void doPropAction(PropActionKind kind, |
| 157 | PropsTy &props, SourceLocation atLoc, |
| 158 | bool markAction = true) { |
| 159 | if (markAction) |
| 160 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
| 161 | ActionOnProp[I->PropD->getIdentifier()] = kind; |
| 162 | |
| 163 | switch (kind) { |
| 164 | case PropAction_None: |
| 165 | return; |
Fariborz Jahanian | 9652adf | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 166 | case PropAction_RetainReplacedWithStrong: { |
| 167 | StringRef toAttr = "strong"; |
| 168 | MigrateCtx.rewritePropertyAttribute("retain", toAttr, atLoc); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 169 | return; |
Fariborz Jahanian | 9652adf | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 170 | } |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 171 | case PropAction_AssignRemoved: |
| 172 | return removeAssignForDefaultStrong(props, atLoc); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 173 | case PropAction_AssignRewritten: |
| 174 | return rewriteAssign(props, atLoc); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 175 | case PropAction_MaybeAddWeakOrUnsafe: |
| 176 | return maybeAddWeakOrUnsafeUnretainedAttr(props, atLoc); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void doActionForExtensionProp(PropsTy &props, SourceLocation atLoc) { |
| 181 | llvm::DenseMap<IdentifierInfo *, PropActionKind>::iterator I; |
| 182 | I = ActionOnProp.find(props[0].PropD->getIdentifier()); |
| 183 | if (I == ActionOnProp.end()) |
| 184 | return; |
| 185 | |
| 186 | doPropAction(I->second, props, atLoc, false); |
| 187 | } |
| 188 | |
| 189 | void rewriteProperty(PropsTy &props, SourceLocation atLoc) { |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 190 | ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props); |
| 191 | |
| 192 | if (propAttrs & (ObjCPropertyDecl::OBJC_PR_copy | |
| 193 | ObjCPropertyDecl::OBJC_PR_unsafe_unretained | |
| 194 | ObjCPropertyDecl::OBJC_PR_strong | |
| 195 | ObjCPropertyDecl::OBJC_PR_weak)) |
| 196 | return; |
| 197 | |
| 198 | if (propAttrs & ObjCPropertyDecl::OBJC_PR_retain) { |
Argyrios Kyrtzidis | 485ecee | 2011-11-08 23:09:34 +0000 | [diff] [blame] | 199 | // strong is the default. |
Fariborz Jahanian | 9652adf | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 200 | return doPropAction(PropAction_RetainReplacedWithStrong, props, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 203 | bool HasIvarAssignedAPlusOneObject = hasIvarAssignedAPlusOneObject(props); |
| 204 | |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 205 | if (propAttrs & ObjCPropertyDecl::OBJC_PR_assign) { |
Fariborz Jahanian | 1630c15 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 206 | if (HasIvarAssignedAPlusOneObject) |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 207 | return doPropAction(PropAction_AssignRemoved, props, atLoc); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 208 | return doPropAction(PropAction_AssignRewritten, props, atLoc); |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 211 | if (HasIvarAssignedAPlusOneObject || |
| 212 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc))) |
Argyrios Kyrtzidis | e4a38a7 | 2011-11-08 22:10:58 +0000 | [diff] [blame] | 213 | return; // 'strong' by default. |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 214 | |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 215 | return doPropAction(PropAction_MaybeAddWeakOrUnsafe, props, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 218 | void removeAssignForDefaultStrong(PropsTy &props, |
| 219 | SourceLocation atLoc) const { |
| 220 | removeAttribute("retain", atLoc); |
| 221 | if (!removeAttribute("assign", atLoc)) |
| 222 | return; |
| 223 | |
| 224 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 225 | if (I->ImplD) |
Argyrios Kyrtzidis | f5b993f | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 226 | Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership, |
| 227 | diag::err_arc_assign_property_ownership, |
| 228 | diag::err_arc_inconsistent_property_ownership, |
| 229 | I->IvarD->getLocation()); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 233 | void rewriteAssign(PropsTy &props, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 234 | bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props), |
| 235 | /*AllowOnUnknownClass=*/Pass.isGCMigration()); |
Fariborz Jahanian | 1630c15 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 236 | const char *toWhich = |
| 237 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "strong" : |
| 238 | (canUseWeak ? "weak" : "unsafe_unretained"); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 239 | |
Fariborz Jahanian | 1630c15 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 240 | bool rewroteAttr = rewriteAttribute("assign", toWhich, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 241 | if (!rewroteAttr) |
| 242 | canUseWeak = false; |
| 243 | |
| 244 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 3fc3dcd | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 245 | if (isUserDeclared(I->IvarD)) { |
| 246 | if (I->IvarD && |
Fariborz Jahanian | 1630c15 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 247 | I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) { |
| 248 | const char *toWhich = |
| 249 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "__strong " : |
| 250 | (canUseWeak ? "__weak " : "__unsafe_unretained "); |
| 251 | Pass.TA.insert(I->IvarD->getLocation(), toWhich); |
| 252 | } |
Argyrios Kyrtzidis | 3fc3dcd | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 253 | } |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 254 | if (I->ImplD) |
Argyrios Kyrtzidis | f5b993f | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 255 | Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership, |
| 256 | diag::err_arc_assign_property_ownership, |
| 257 | diag::err_arc_inconsistent_property_ownership, |
| 258 | I->IvarD->getLocation()); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | void maybeAddWeakOrUnsafeUnretainedAttr(PropsTy &props, |
| 263 | SourceLocation atLoc) const { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 264 | bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props), |
| 265 | /*AllowOnUnknownClass=*/Pass.isGCMigration()); |
Argyrios Kyrtzidis | 485ecee | 2011-11-08 23:09:34 +0000 | [diff] [blame] | 266 | |
| 267 | bool addedAttr = addAttribute(canUseWeak ? "weak" : "unsafe_unretained", |
| 268 | atLoc); |
| 269 | if (!addedAttr) |
| 270 | canUseWeak = false; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 271 | |
| 272 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 3fc3dcd | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 273 | if (isUserDeclared(I->IvarD)) { |
| 274 | if (I->IvarD && |
| 275 | I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) |
| 276 | Pass.TA.insert(I->IvarD->getLocation(), |
| 277 | canUseWeak ? "__weak " : "__unsafe_unretained "); |
| 278 | } |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 279 | if (I->ImplD) { |
Argyrios Kyrtzidis | f5b993f | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 280 | Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership, |
| 281 | diag::err_arc_assign_property_ownership, |
| 282 | diag::err_arc_inconsistent_property_ownership, |
| 283 | I->IvarD->getLocation()); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 284 | Pass.TA.clearDiagnostic( |
| 285 | diag::err_arc_objc_property_default_assign_on_object, |
| 286 | I->ImplD->getLocation()); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Argyrios Kyrtzidis | ffe8b1c | 2011-10-17 23:14:16 +0000 | [diff] [blame] | 291 | bool removeAttribute(StringRef fromAttr, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | eca1f36 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 292 | return MigrateCtx.removePropertyAttribute(fromAttr, atLoc); |
Argyrios Kyrtzidis | ffe8b1c | 2011-10-17 23:14:16 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 295 | bool rewriteAttribute(StringRef fromAttr, StringRef toAttr, |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 296 | SourceLocation atLoc) const { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 297 | return MigrateCtx.rewritePropertyAttribute(fromAttr, toAttr, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 300 | bool addAttribute(StringRef attr, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | eca1f36 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 301 | return MigrateCtx.addPropertyAttribute(attr, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 304 | class PlusOneAssign : public RecursiveASTVisitor<PlusOneAssign> { |
| 305 | ObjCIvarDecl *Ivar; |
| 306 | public: |
| 307 | PlusOneAssign(ObjCIvarDecl *D) : Ivar(D) {} |
| 308 | |
| 309 | bool VisitBinAssign(BinaryOperator *E) { |
| 310 | Expr *lhs = E->getLHS()->IgnoreParenImpCasts(); |
| 311 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(lhs)) { |
| 312 | if (RE->getDecl() != Ivar) |
| 313 | return true; |
| 314 | |
Argyrios Kyrtzidis | 0b21d82 | 2012-05-23 21:50:04 +0000 | [diff] [blame] | 315 | if (isPlusOneAssign(E)) |
Argyrios Kyrtzidis | 93db227 | 2011-08-10 21:46:48 +0000 | [diff] [blame] | 316 | return false; |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | return true; |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | bool hasIvarAssignedAPlusOneObject(PropsTy &props) const { |
| 324 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 325 | PlusOneAssign oneAssign(I->IvarD); |
| 326 | bool notFound = oneAssign.TraverseDecl(CurImplD); |
| 327 | if (!notFound) |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | return false; |
| 332 | } |
| 333 | |
Argyrios Kyrtzidis | 0dd98a4 | 2011-11-07 18:40:32 +0000 | [diff] [blame] | 334 | bool hasIvarWithExplicitARCOwnership(PropsTy &props) const { |
| 335 | if (Pass.isGCMigration()) |
| 336 | return false; |
| 337 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 338 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 339 | if (isUserDeclared(I->IvarD)) { |
| 340 | if (isa<AttributedType>(I->IvarD->getType())) |
| 341 | return true; |
| 342 | if (I->IvarD->getType().getLocalQualifiers().getObjCLifetime() |
| 343 | != Qualifiers::OCL_Strong) |
| 344 | return true; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 351 | // \brief Returns true if all declarations in the @property have GC __weak. |
| 352 | bool hasGCWeak(PropsTy &props, SourceLocation atLoc) const { |
| 353 | if (!Pass.isGCMigration()) |
| 354 | return false; |
| 355 | if (props.empty()) |
| 356 | return false; |
| 357 | return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding()); |
| 358 | } |
| 359 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 360 | bool isUserDeclared(ObjCIvarDecl *ivarD) const { |
| 361 | return ivarD && !ivarD->getSynthesize(); |
| 362 | } |
| 363 | |
| 364 | QualType getPropertyType(PropsTy &props) const { |
| 365 | assert(!props.empty()); |
Argyrios Kyrtzidis | aa421ea | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 366 | QualType ty = props[0].PropD->getType().getUnqualifiedType(); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 367 | |
| 368 | #ifndef NDEBUG |
| 369 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
Argyrios Kyrtzidis | aa421ea | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 370 | assert(ty == I->PropD->getType().getUnqualifiedType()); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 371 | #endif |
| 372 | |
| 373 | return ty; |
| 374 | } |
| 375 | |
| 376 | ObjCPropertyDecl::PropertyAttributeKind |
| 377 | getPropertyAttrs(PropsTy &props) const { |
| 378 | assert(!props.empty()); |
| 379 | ObjCPropertyDecl::PropertyAttributeKind |
| 380 | attrs = props[0].PropD->getPropertyAttributesAsWritten(); |
| 381 | |
| 382 | #ifndef NDEBUG |
| 383 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
| 384 | assert(attrs == I->PropD->getPropertyAttributesAsWritten()); |
| 385 | #endif |
| 386 | |
| 387 | return attrs; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 388 | } |
| 389 | }; |
| 390 | |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 391 | } // anonymous namespace |
| 392 | |
Argyrios Kyrtzidis | aaa9996 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 393 | void PropertyRewriteTraverser::traverseObjCImplementation( |
| 394 | ObjCImplementationContext &ImplCtx) { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 395 | PropertiesRewriter(ImplCtx.getMigrationContext()) |
Argyrios Kyrtzidis | aaa9996 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 396 | .doTransform(ImplCtx.getImplementationDecl()); |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 397 | } |