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) { |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 79 | for (auto *Prop : D->instance_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 | |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 99 | // Look through extensions. |
| 100 | for (auto *Ext : iface->visible_extensions()) |
| 101 | collectProperties(Ext, AtProps); |
| 102 | |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 103 | typedef DeclContext::specific_decl_iterator<ObjCPropertyImplDecl> |
| 104 | prop_impl_iterator; |
| 105 | for (prop_impl_iterator |
| 106 | I = prop_impl_iterator(D->decls_begin()), |
| 107 | E = prop_impl_iterator(D->decls_end()); I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 108 | ObjCPropertyImplDecl *implD = *I; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 109 | if (implD->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 110 | continue; |
| 111 | ObjCPropertyDecl *propD = implD->getPropertyDecl(); |
| 112 | if (!propD || propD->isInvalidDecl()) |
| 113 | continue; |
| 114 | ObjCIvarDecl *ivarD = implD->getPropertyIvarDecl(); |
| 115 | if (!ivarD || ivarD->isInvalidDecl()) |
| 116 | continue; |
| 117 | unsigned rawAtLoc = propD->getAtLoc().getRawEncoding(); |
| 118 | AtPropDeclsTy::iterator findAtLoc = AtProps.find(rawAtLoc); |
| 119 | if (findAtLoc == AtProps.end()) |
| 120 | continue; |
| 121 | |
| 122 | PropsTy &props = findAtLoc->second; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 123 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 124 | if (I->PropD == propD) { |
| 125 | I->IvarD = ivarD; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 126 | I->ImplD = implD; |
| 127 | break; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 132 | for (AtPropDeclsTy::iterator |
| 133 | I = AtProps.begin(), E = AtProps.end(); I != E; ++I) { |
| 134 | SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first); |
| 135 | PropsTy &props = I->second; |
Argyrios Kyrtzidis | aa421ea | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 136 | if (!getPropertyType(props)->isObjCRetainableType()) |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 137 | continue; |
Argyrios Kyrtzidis | 0dd98a4 | 2011-11-07 18:40:32 +0000 | [diff] [blame] | 138 | if (hasIvarWithExplicitARCOwnership(props)) |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 139 | continue; |
| 140 | |
| 141 | Transaction Trans(Pass.TA); |
| 142 | rewriteProperty(props, atLoc); |
| 143 | } |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private: |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 147 | void doPropAction(PropActionKind kind, |
| 148 | PropsTy &props, SourceLocation atLoc, |
| 149 | bool markAction = true) { |
| 150 | if (markAction) |
| 151 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
| 152 | ActionOnProp[I->PropD->getIdentifier()] = kind; |
| 153 | |
| 154 | switch (kind) { |
| 155 | case PropAction_None: |
| 156 | return; |
Fariborz Jahanian | 9652adf | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 157 | case PropAction_RetainReplacedWithStrong: { |
| 158 | StringRef toAttr = "strong"; |
| 159 | MigrateCtx.rewritePropertyAttribute("retain", toAttr, atLoc); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 160 | return; |
Fariborz Jahanian | 9652adf | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 161 | } |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 162 | case PropAction_AssignRemoved: |
| 163 | return removeAssignForDefaultStrong(props, atLoc); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 164 | case PropAction_AssignRewritten: |
| 165 | return rewriteAssign(props, atLoc); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 166 | case PropAction_MaybeAddWeakOrUnsafe: |
| 167 | return maybeAddWeakOrUnsafeUnretainedAttr(props, atLoc); |
| 168 | } |
| 169 | } |
| 170 | |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 171 | void rewriteProperty(PropsTy &props, SourceLocation atLoc) { |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 172 | ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props); |
| 173 | |
| 174 | if (propAttrs & (ObjCPropertyDecl::OBJC_PR_copy | |
| 175 | ObjCPropertyDecl::OBJC_PR_unsafe_unretained | |
| 176 | ObjCPropertyDecl::OBJC_PR_strong | |
| 177 | ObjCPropertyDecl::OBJC_PR_weak)) |
| 178 | return; |
| 179 | |
| 180 | if (propAttrs & ObjCPropertyDecl::OBJC_PR_retain) { |
Argyrios Kyrtzidis | 485ecee | 2011-11-08 23:09:34 +0000 | [diff] [blame] | 181 | // strong is the default. |
Fariborz Jahanian | 9652adf | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 182 | return doPropAction(PropAction_RetainReplacedWithStrong, props, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 185 | bool HasIvarAssignedAPlusOneObject = hasIvarAssignedAPlusOneObject(props); |
| 186 | |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 187 | if (propAttrs & ObjCPropertyDecl::OBJC_PR_assign) { |
Fariborz Jahanian | 1630c15 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 188 | if (HasIvarAssignedAPlusOneObject) |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 189 | return doPropAction(PropAction_AssignRemoved, props, atLoc); |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 190 | return doPropAction(PropAction_AssignRewritten, props, atLoc); |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 193 | if (HasIvarAssignedAPlusOneObject || |
| 194 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc))) |
Argyrios Kyrtzidis | e4a38a7 | 2011-11-08 22:10:58 +0000 | [diff] [blame] | 195 | return; // 'strong' by default. |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 196 | |
Argyrios Kyrtzidis | 05c65fb | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 197 | return doPropAction(PropAction_MaybeAddWeakOrUnsafe, props, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 200 | void removeAssignForDefaultStrong(PropsTy &props, |
| 201 | SourceLocation atLoc) const { |
| 202 | removeAttribute("retain", atLoc); |
| 203 | if (!removeAttribute("assign", atLoc)) |
| 204 | return; |
| 205 | |
| 206 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 207 | if (I->ImplD) |
Argyrios Kyrtzidis | f5b993f | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 208 | Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership, |
| 209 | diag::err_arc_assign_property_ownership, |
| 210 | diag::err_arc_inconsistent_property_ownership, |
| 211 | I->IvarD->getLocation()); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 215 | void rewriteAssign(PropsTy &props, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 216 | bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props), |
| 217 | /*AllowOnUnknownClass=*/Pass.isGCMigration()); |
Fariborz Jahanian | 1630c15 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 218 | const char *toWhich = |
| 219 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "strong" : |
| 220 | (canUseWeak ? "weak" : "unsafe_unretained"); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 221 | |
Fariborz Jahanian | 1630c15 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 222 | bool rewroteAttr = rewriteAttribute("assign", toWhich, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 223 | if (!rewroteAttr) |
| 224 | canUseWeak = false; |
| 225 | |
| 226 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 3fc3dcd | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 227 | if (isUserDeclared(I->IvarD)) { |
| 228 | if (I->IvarD && |
Fariborz Jahanian | 1630c15 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 229 | I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) { |
| 230 | const char *toWhich = |
| 231 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "__strong " : |
| 232 | (canUseWeak ? "__weak " : "__unsafe_unretained "); |
| 233 | Pass.TA.insert(I->IvarD->getLocation(), toWhich); |
| 234 | } |
Argyrios Kyrtzidis | 3fc3dcd | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 235 | } |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 236 | if (I->ImplD) |
Argyrios Kyrtzidis | f5b993f | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 237 | Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership, |
| 238 | diag::err_arc_assign_property_ownership, |
| 239 | diag::err_arc_inconsistent_property_ownership, |
| 240 | I->IvarD->getLocation()); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | void maybeAddWeakOrUnsafeUnretainedAttr(PropsTy &props, |
| 245 | SourceLocation atLoc) const { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 246 | bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props), |
| 247 | /*AllowOnUnknownClass=*/Pass.isGCMigration()); |
Argyrios Kyrtzidis | 485ecee | 2011-11-08 23:09:34 +0000 | [diff] [blame] | 248 | |
| 249 | bool addedAttr = addAttribute(canUseWeak ? "weak" : "unsafe_unretained", |
| 250 | atLoc); |
| 251 | if (!addedAttr) |
| 252 | canUseWeak = false; |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 253 | |
| 254 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 3fc3dcd | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 255 | if (isUserDeclared(I->IvarD)) { |
| 256 | if (I->IvarD && |
| 257 | I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) |
| 258 | Pass.TA.insert(I->IvarD->getLocation(), |
| 259 | canUseWeak ? "__weak " : "__unsafe_unretained "); |
| 260 | } |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 261 | if (I->ImplD) { |
Argyrios Kyrtzidis | f5b993f | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 262 | Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership, |
| 263 | diag::err_arc_assign_property_ownership, |
| 264 | diag::err_arc_inconsistent_property_ownership, |
| 265 | I->IvarD->getLocation()); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 266 | Pass.TA.clearDiagnostic( |
| 267 | diag::err_arc_objc_property_default_assign_on_object, |
| 268 | I->ImplD->getLocation()); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
Argyrios Kyrtzidis | ffe8b1c | 2011-10-17 23:14:16 +0000 | [diff] [blame] | 273 | bool removeAttribute(StringRef fromAttr, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | eca1f36 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 274 | return MigrateCtx.removePropertyAttribute(fromAttr, atLoc); |
Argyrios Kyrtzidis | ffe8b1c | 2011-10-17 23:14:16 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 277 | bool rewriteAttribute(StringRef fromAttr, StringRef toAttr, |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 278 | SourceLocation atLoc) const { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 279 | return MigrateCtx.rewritePropertyAttribute(fromAttr, toAttr, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 282 | bool addAttribute(StringRef attr, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | eca1f36 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 283 | return MigrateCtx.addPropertyAttribute(attr, atLoc); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 286 | class PlusOneAssign : public RecursiveASTVisitor<PlusOneAssign> { |
| 287 | ObjCIvarDecl *Ivar; |
| 288 | public: |
| 289 | PlusOneAssign(ObjCIvarDecl *D) : Ivar(D) {} |
| 290 | |
| 291 | bool VisitBinAssign(BinaryOperator *E) { |
| 292 | Expr *lhs = E->getLHS()->IgnoreParenImpCasts(); |
| 293 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(lhs)) { |
| 294 | if (RE->getDecl() != Ivar) |
| 295 | return true; |
| 296 | |
Argyrios Kyrtzidis | 0b21d82 | 2012-05-23 21:50:04 +0000 | [diff] [blame] | 297 | if (isPlusOneAssign(E)) |
Argyrios Kyrtzidis | 93db227 | 2011-08-10 21:46:48 +0000 | [diff] [blame] | 298 | return false; |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | return true; |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | bool hasIvarAssignedAPlusOneObject(PropsTy &props) const { |
| 306 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 307 | PlusOneAssign oneAssign(I->IvarD); |
| 308 | bool notFound = oneAssign.TraverseDecl(CurImplD); |
| 309 | if (!notFound) |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | return false; |
| 314 | } |
| 315 | |
Argyrios Kyrtzidis | 0dd98a4 | 2011-11-07 18:40:32 +0000 | [diff] [blame] | 316 | bool hasIvarWithExplicitARCOwnership(PropsTy &props) const { |
| 317 | if (Pass.isGCMigration()) |
| 318 | return false; |
| 319 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 320 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 321 | if (isUserDeclared(I->IvarD)) { |
| 322 | if (isa<AttributedType>(I->IvarD->getType())) |
| 323 | return true; |
| 324 | if (I->IvarD->getType().getLocalQualifiers().getObjCLifetime() |
| 325 | != Qualifiers::OCL_Strong) |
| 326 | return true; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 333 | // \brief Returns true if all declarations in the @property have GC __weak. |
| 334 | bool hasGCWeak(PropsTy &props, SourceLocation atLoc) const { |
| 335 | if (!Pass.isGCMigration()) |
| 336 | return false; |
| 337 | if (props.empty()) |
| 338 | return false; |
| 339 | return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding()); |
| 340 | } |
| 341 | |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 342 | bool isUserDeclared(ObjCIvarDecl *ivarD) const { |
| 343 | return ivarD && !ivarD->getSynthesize(); |
| 344 | } |
| 345 | |
| 346 | QualType getPropertyType(PropsTy &props) const { |
| 347 | assert(!props.empty()); |
Argyrios Kyrtzidis | aa421ea | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 348 | QualType ty = props[0].PropD->getType().getUnqualifiedType(); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 349 | |
| 350 | #ifndef NDEBUG |
| 351 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
Argyrios Kyrtzidis | aa421ea | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 352 | assert(ty == I->PropD->getType().getUnqualifiedType()); |
Argyrios Kyrtzidis | c8b3619 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 353 | #endif |
| 354 | |
| 355 | return ty; |
| 356 | } |
| 357 | |
| 358 | ObjCPropertyDecl::PropertyAttributeKind |
| 359 | getPropertyAttrs(PropsTy &props) const { |
| 360 | assert(!props.empty()); |
| 361 | ObjCPropertyDecl::PropertyAttributeKind |
| 362 | attrs = props[0].PropD->getPropertyAttributesAsWritten(); |
| 363 | |
| 364 | #ifndef NDEBUG |
| 365 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
| 366 | assert(attrs == I->PropD->getPropertyAttributesAsWritten()); |
| 367 | #endif |
| 368 | |
| 369 | return attrs; |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 370 | } |
| 371 | }; |
| 372 | |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 373 | } // anonymous namespace |
| 374 | |
Argyrios Kyrtzidis | aaa9996 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 375 | void PropertyRewriteTraverser::traverseObjCImplementation( |
| 376 | ObjCImplementationContext &ImplCtx) { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 377 | PropertiesRewriter(ImplCtx.getMigrationContext()) |
Argyrios Kyrtzidis | aaa9996 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 378 | .doTransform(ImplCtx.getImplementationDecl()); |
Argyrios Kyrtzidis | e5b475c | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 379 | } |