1 //===--- TransProperties.cpp - Transformations to ARC mode ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // - Adds strong/weak/unsafe_unretained ownership specifier to properties that
13 // - Migrates properties from (retain) to (strong) and (assign) to
14 // (unsafe_unretained/weak).
15 // - If a property is synthesized, adds the ownership specifier in the ivar
16 // backing the property.
18 // @interface Foo : NSObject {
21 // @property (assign) id x;
24 // @interface Foo : NSObject {
25 // NSObject *__weak x;
27 // @property (weak) id x;
30 //===----------------------------------------------------------------------===//
32 #include "Transforms.h"
33 #include "Internals.h"
34 #include "clang/Basic/SourceManager.h"
35 #include "clang/Lex/Lexer.h"
36 #include "clang/Sema/SemaDiagnostic.h"
39 using namespace clang;
40 using namespace arcmt;
41 using namespace trans;
45 class PropertiesRewriter {
46 MigrationContext &MigrateCtx;
48 ObjCImplementationDecl *CurImplD;
52 PropAction_RetainReplacedWithStrong,
53 PropAction_AssignRemoved,
54 PropAction_AssignRewritten,
55 PropAction_MaybeAddWeakOrUnsafe
59 ObjCPropertyDecl *PropD;
61 ObjCPropertyImplDecl *ImplD;
63 PropData(ObjCPropertyDecl *propD)
64 : PropD(propD), IvarD(nullptr), ImplD(nullptr) {}
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 = nullptr) {
78 for (auto *Prop : D->instance_properties()) {
79 if (Prop->getAtLoc().isInvalid())
81 unsigned RawLoc = Prop->getAtLoc().getRawEncoding();
83 if (PrevAtProps->find(RawLoc) != PrevAtProps->end())
85 PropsTy &props = AtProps[RawLoc];
86 props.push_back(Prop);
90 void doTransform(ObjCImplementationDecl *D) {
92 ObjCInterfaceDecl *iface = D->getClassInterface();
96 collectProperties(iface, AtProps);
98 // Look through extensions.
99 for (auto *Ext : iface->visible_extensions())
100 collectProperties(Ext, AtProps);
102 typedef DeclContext::specific_decl_iterator<ObjCPropertyImplDecl>
104 for (prop_impl_iterator
105 I = prop_impl_iterator(D->decls_begin()),
106 E = prop_impl_iterator(D->decls_end()); I != E; ++I) {
107 ObjCPropertyImplDecl *implD = *I;
108 if (implD->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
110 ObjCPropertyDecl *propD = implD->getPropertyDecl();
111 if (!propD || propD->isInvalidDecl())
113 ObjCIvarDecl *ivarD = implD->getPropertyIvarDecl();
114 if (!ivarD || ivarD->isInvalidDecl())
116 unsigned rawAtLoc = propD->getAtLoc().getRawEncoding();
117 AtPropDeclsTy::iterator findAtLoc = AtProps.find(rawAtLoc);
118 if (findAtLoc == AtProps.end())
121 PropsTy &props = findAtLoc->second;
122 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
123 if (I->PropD == propD) {
131 for (AtPropDeclsTy::iterator
132 I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
133 SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first);
134 PropsTy &props = I->second;
135 if (!getPropertyType(props)->isObjCRetainableType())
137 if (hasIvarWithExplicitARCOwnership(props))
140 Transaction Trans(Pass.TA);
141 rewriteProperty(props, atLoc);
146 void doPropAction(PropActionKind kind,
147 PropsTy &props, SourceLocation atLoc,
148 bool markAction = true) {
150 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
151 ActionOnProp[I->PropD->getIdentifier()] = kind;
154 case PropAction_None:
156 case PropAction_RetainReplacedWithStrong: {
157 StringRef toAttr = "strong";
158 MigrateCtx.rewritePropertyAttribute("retain", toAttr, atLoc);
161 case PropAction_AssignRemoved:
162 return removeAssignForDefaultStrong(props, atLoc);
163 case PropAction_AssignRewritten:
164 return rewriteAssign(props, atLoc);
165 case PropAction_MaybeAddWeakOrUnsafe:
166 return maybeAddWeakOrUnsafeUnretainedAttr(props, atLoc);
170 void rewriteProperty(PropsTy &props, SourceLocation atLoc) {
171 ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props);
173 if (propAttrs & (ObjCPropertyDecl::OBJC_PR_copy |
174 ObjCPropertyDecl::OBJC_PR_unsafe_unretained |
175 ObjCPropertyDecl::OBJC_PR_strong |
176 ObjCPropertyDecl::OBJC_PR_weak))
179 if (propAttrs & ObjCPropertyDecl::OBJC_PR_retain) {
180 // strong is the default.
181 return doPropAction(PropAction_RetainReplacedWithStrong, props, atLoc);
184 bool HasIvarAssignedAPlusOneObject = hasIvarAssignedAPlusOneObject(props);
186 if (propAttrs & ObjCPropertyDecl::OBJC_PR_assign) {
187 if (HasIvarAssignedAPlusOneObject)
188 return doPropAction(PropAction_AssignRemoved, props, atLoc);
189 return doPropAction(PropAction_AssignRewritten, props, atLoc);
192 if (HasIvarAssignedAPlusOneObject ||
193 (Pass.isGCMigration() && !hasGCWeak(props, atLoc)))
194 return; // 'strong' by default.
196 return doPropAction(PropAction_MaybeAddWeakOrUnsafe, props, atLoc);
199 void removeAssignForDefaultStrong(PropsTy &props,
200 SourceLocation atLoc) const {
201 removeAttribute("retain", atLoc);
202 if (!removeAttribute("assign", atLoc))
205 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
207 Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
208 diag::err_arc_assign_property_ownership,
209 diag::err_arc_inconsistent_property_ownership,
210 I->IvarD->getLocation());
214 void rewriteAssign(PropsTy &props, SourceLocation atLoc) const {
215 bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
216 /*AllowOnUnknownClass=*/Pass.isGCMigration());
217 const char *toWhich =
218 (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "strong" :
219 (canUseWeak ? "weak" : "unsafe_unretained");
221 bool rewroteAttr = rewriteAttribute("assign", toWhich, atLoc);
225 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
226 if (isUserDeclared(I->IvarD)) {
228 I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) {
229 const char *toWhich =
230 (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "__strong " :
231 (canUseWeak ? "__weak " : "__unsafe_unretained ");
232 Pass.TA.insert(I->IvarD->getLocation(), toWhich);
236 Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
237 diag::err_arc_assign_property_ownership,
238 diag::err_arc_inconsistent_property_ownership,
239 I->IvarD->getLocation());
243 void maybeAddWeakOrUnsafeUnretainedAttr(PropsTy &props,
244 SourceLocation atLoc) const {
245 bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
246 /*AllowOnUnknownClass=*/Pass.isGCMigration());
248 bool addedAttr = addAttribute(canUseWeak ? "weak" : "unsafe_unretained",
253 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
254 if (isUserDeclared(I->IvarD)) {
256 I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
257 Pass.TA.insert(I->IvarD->getLocation(),
258 canUseWeak ? "__weak " : "__unsafe_unretained ");
261 Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
262 diag::err_arc_assign_property_ownership,
263 diag::err_arc_inconsistent_property_ownership,
264 I->IvarD->getLocation());
265 Pass.TA.clearDiagnostic(
266 diag::err_arc_objc_property_default_assign_on_object,
267 I->ImplD->getLocation());
272 bool removeAttribute(StringRef fromAttr, SourceLocation atLoc) const {
273 return MigrateCtx.removePropertyAttribute(fromAttr, atLoc);
276 bool rewriteAttribute(StringRef fromAttr, StringRef toAttr,
277 SourceLocation atLoc) const {
278 return MigrateCtx.rewritePropertyAttribute(fromAttr, toAttr, atLoc);
281 bool addAttribute(StringRef attr, SourceLocation atLoc) const {
282 return MigrateCtx.addPropertyAttribute(attr, atLoc);
285 class PlusOneAssign : public RecursiveASTVisitor<PlusOneAssign> {
288 PlusOneAssign(ObjCIvarDecl *D) : Ivar(D) {}
290 bool VisitBinAssign(BinaryOperator *E) {
291 Expr *lhs = E->getLHS()->IgnoreParenImpCasts();
292 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(lhs)) {
293 if (RE->getDecl() != Ivar)
296 if (isPlusOneAssign(E))
304 bool hasIvarAssignedAPlusOneObject(PropsTy &props) const {
305 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
306 PlusOneAssign oneAssign(I->IvarD);
307 bool notFound = oneAssign.TraverseDecl(CurImplD);
315 bool hasIvarWithExplicitARCOwnership(PropsTy &props) const {
316 if (Pass.isGCMigration())
319 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
320 if (isUserDeclared(I->IvarD)) {
321 if (isa<AttributedType>(I->IvarD->getType()))
323 if (I->IvarD->getType().getLocalQualifiers().getObjCLifetime()
324 != Qualifiers::OCL_Strong)
332 // Returns true if all declarations in the @property have GC __weak.
333 bool hasGCWeak(PropsTy &props, SourceLocation atLoc) const {
334 if (!Pass.isGCMigration())
338 return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding());
341 bool isUserDeclared(ObjCIvarDecl *ivarD) const {
342 return ivarD && !ivarD->getSynthesize();
345 QualType getPropertyType(PropsTy &props) const {
346 assert(!props.empty());
347 QualType ty = props[0].PropD->getType().getUnqualifiedType();
350 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
351 assert(ty == I->PropD->getType().getUnqualifiedType());
357 ObjCPropertyDecl::PropertyAttributeKind
358 getPropertyAttrs(PropsTy &props) const {
359 assert(!props.empty());
360 ObjCPropertyDecl::PropertyAttributeKind
361 attrs = props[0].PropD->getPropertyAttributesAsWritten();
364 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
365 assert(attrs == I->PropD->getPropertyAttributesAsWritten());
372 } // anonymous namespace
374 void PropertyRewriteTraverser::traverseObjCImplementation(
375 ObjCImplementationContext &ImplCtx) {
376 PropertiesRewriter(ImplCtx.getMigrationContext())
377 .doTransform(ImplCtx.getImplementationDecl());