TransProperties.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //===--- TransProperties.cpp - Transformations to ARC mode ----------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // rewriteProperties:
  10. //
  11. // - Adds strong/weak/unsafe_unretained ownership specifier to properties that
  12. // are missing one.
  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.
  17. //
  18. // @interface Foo : NSObject {
  19. // NSObject *x;
  20. // }
  21. // @property (assign) id x;
  22. // @end
  23. // ---->
  24. // @interface Foo : NSObject {
  25. // NSObject *__weak x;
  26. // }
  27. // @property (weak) id x;
  28. // @end
  29. //
  30. //===----------------------------------------------------------------------===//
  31. #include "Transforms.h"
  32. #include "Internals.h"
  33. #include "clang/Basic/SourceManager.h"
  34. #include "clang/Lex/Lexer.h"
  35. #include "clang/Sema/SemaDiagnostic.h"
  36. #include <map>
  37. using namespace clang;
  38. using namespace arcmt;
  39. using namespace trans;
  40. namespace {
  41. class PropertiesRewriter {
  42. MigrationContext &MigrateCtx;
  43. MigrationPass &Pass;
  44. ObjCImplementationDecl *CurImplD;
  45. enum PropActionKind {
  46. PropAction_None,
  47. PropAction_RetainReplacedWithStrong,
  48. PropAction_AssignRemoved,
  49. PropAction_AssignRewritten,
  50. PropAction_MaybeAddWeakOrUnsafe
  51. };
  52. struct PropData {
  53. ObjCPropertyDecl *PropD;
  54. ObjCIvarDecl *IvarD;
  55. ObjCPropertyImplDecl *ImplD;
  56. PropData(ObjCPropertyDecl *propD)
  57. : PropD(propD), IvarD(nullptr), ImplD(nullptr) {}
  58. };
  59. typedef SmallVector<PropData, 2> PropsTy;
  60. typedef std::map<SourceLocation, PropsTy> AtPropDeclsTy;
  61. AtPropDeclsTy AtProps;
  62. llvm::DenseMap<IdentifierInfo *, PropActionKind> ActionOnProp;
  63. public:
  64. explicit PropertiesRewriter(MigrationContext &MigrateCtx)
  65. : MigrateCtx(MigrateCtx), Pass(MigrateCtx.Pass) { }
  66. static void collectProperties(ObjCContainerDecl *D, AtPropDeclsTy &AtProps,
  67. AtPropDeclsTy *PrevAtProps = nullptr) {
  68. for (auto *Prop : D->instance_properties()) {
  69. SourceLocation Loc = Prop->getAtLoc();
  70. if (Loc.isInvalid())
  71. continue;
  72. if (PrevAtProps)
  73. if (PrevAtProps->find(Loc) != PrevAtProps->end())
  74. continue;
  75. PropsTy &props = AtProps[Loc];
  76. props.push_back(Prop);
  77. }
  78. }
  79. void doTransform(ObjCImplementationDecl *D) {
  80. CurImplD = D;
  81. ObjCInterfaceDecl *iface = D->getClassInterface();
  82. if (!iface)
  83. return;
  84. collectProperties(iface, AtProps);
  85. // Look through extensions.
  86. for (auto *Ext : iface->visible_extensions())
  87. collectProperties(Ext, AtProps);
  88. typedef DeclContext::specific_decl_iterator<ObjCPropertyImplDecl>
  89. prop_impl_iterator;
  90. for (prop_impl_iterator
  91. I = prop_impl_iterator(D->decls_begin()),
  92. E = prop_impl_iterator(D->decls_end()); I != E; ++I) {
  93. ObjCPropertyImplDecl *implD = *I;
  94. if (implD->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
  95. continue;
  96. ObjCPropertyDecl *propD = implD->getPropertyDecl();
  97. if (!propD || propD->isInvalidDecl())
  98. continue;
  99. ObjCIvarDecl *ivarD = implD->getPropertyIvarDecl();
  100. if (!ivarD || ivarD->isInvalidDecl())
  101. continue;
  102. AtPropDeclsTy::iterator findAtLoc = AtProps.find(propD->getAtLoc());
  103. if (findAtLoc == AtProps.end())
  104. continue;
  105. PropsTy &props = findAtLoc->second;
  106. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  107. if (I->PropD == propD) {
  108. I->IvarD = ivarD;
  109. I->ImplD = implD;
  110. break;
  111. }
  112. }
  113. }
  114. for (AtPropDeclsTy::iterator
  115. I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
  116. SourceLocation atLoc = I->first;
  117. PropsTy &props = I->second;
  118. if (!getPropertyType(props)->isObjCRetainableType())
  119. continue;
  120. if (hasIvarWithExplicitARCOwnership(props))
  121. continue;
  122. Transaction Trans(Pass.TA);
  123. rewriteProperty(props, atLoc);
  124. }
  125. }
  126. private:
  127. void doPropAction(PropActionKind kind,
  128. PropsTy &props, SourceLocation atLoc,
  129. bool markAction = true) {
  130. if (markAction)
  131. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
  132. ActionOnProp[I->PropD->getIdentifier()] = kind;
  133. switch (kind) {
  134. case PropAction_None:
  135. return;
  136. case PropAction_RetainReplacedWithStrong: {
  137. StringRef toAttr = "strong";
  138. MigrateCtx.rewritePropertyAttribute("retain", toAttr, atLoc);
  139. return;
  140. }
  141. case PropAction_AssignRemoved:
  142. return removeAssignForDefaultStrong(props, atLoc);
  143. case PropAction_AssignRewritten:
  144. return rewriteAssign(props, atLoc);
  145. case PropAction_MaybeAddWeakOrUnsafe:
  146. return maybeAddWeakOrUnsafeUnretainedAttr(props, atLoc);
  147. }
  148. }
  149. void rewriteProperty(PropsTy &props, SourceLocation atLoc) {
  150. ObjCPropertyAttribute::Kind propAttrs = getPropertyAttrs(props);
  151. if (propAttrs &
  152. (ObjCPropertyAttribute::kind_copy |
  153. ObjCPropertyAttribute::kind_unsafe_unretained |
  154. ObjCPropertyAttribute::kind_strong | ObjCPropertyAttribute::kind_weak))
  155. return;
  156. if (propAttrs & ObjCPropertyAttribute::kind_retain) {
  157. // strong is the default.
  158. return doPropAction(PropAction_RetainReplacedWithStrong, props, atLoc);
  159. }
  160. bool HasIvarAssignedAPlusOneObject = hasIvarAssignedAPlusOneObject(props);
  161. if (propAttrs & ObjCPropertyAttribute::kind_assign) {
  162. if (HasIvarAssignedAPlusOneObject)
  163. return doPropAction(PropAction_AssignRemoved, props, atLoc);
  164. return doPropAction(PropAction_AssignRewritten, props, atLoc);
  165. }
  166. if (HasIvarAssignedAPlusOneObject ||
  167. (Pass.isGCMigration() && !hasGCWeak(props, atLoc)))
  168. return; // 'strong' by default.
  169. return doPropAction(PropAction_MaybeAddWeakOrUnsafe, props, atLoc);
  170. }
  171. void removeAssignForDefaultStrong(PropsTy &props,
  172. SourceLocation atLoc) const {
  173. removeAttribute("retain", atLoc);
  174. if (!removeAttribute("assign", atLoc))
  175. return;
  176. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  177. if (I->ImplD)
  178. Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
  179. diag::err_arc_assign_property_ownership,
  180. diag::err_arc_inconsistent_property_ownership,
  181. I->IvarD->getLocation());
  182. }
  183. }
  184. void rewriteAssign(PropsTy &props, SourceLocation atLoc) const {
  185. bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
  186. /*AllowOnUnknownClass=*/Pass.isGCMigration());
  187. const char *toWhich =
  188. (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "strong" :
  189. (canUseWeak ? "weak" : "unsafe_unretained");
  190. bool rewroteAttr = rewriteAttribute("assign", toWhich, atLoc);
  191. if (!rewroteAttr)
  192. canUseWeak = false;
  193. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  194. if (isUserDeclared(I->IvarD)) {
  195. if (I->IvarD &&
  196. I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) {
  197. const char *toWhich =
  198. (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "__strong " :
  199. (canUseWeak ? "__weak " : "__unsafe_unretained ");
  200. Pass.TA.insert(I->IvarD->getLocation(), toWhich);
  201. }
  202. }
  203. if (I->ImplD)
  204. Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
  205. diag::err_arc_assign_property_ownership,
  206. diag::err_arc_inconsistent_property_ownership,
  207. I->IvarD->getLocation());
  208. }
  209. }
  210. void maybeAddWeakOrUnsafeUnretainedAttr(PropsTy &props,
  211. SourceLocation atLoc) const {
  212. bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
  213. /*AllowOnUnknownClass=*/Pass.isGCMigration());
  214. bool addedAttr = addAttribute(canUseWeak ? "weak" : "unsafe_unretained",
  215. atLoc);
  216. if (!addedAttr)
  217. canUseWeak = false;
  218. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  219. if (isUserDeclared(I->IvarD)) {
  220. if (I->IvarD &&
  221. I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
  222. Pass.TA.insert(I->IvarD->getLocation(),
  223. canUseWeak ? "__weak " : "__unsafe_unretained ");
  224. }
  225. if (I->ImplD) {
  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());
  230. Pass.TA.clearDiagnostic(
  231. diag::err_arc_objc_property_default_assign_on_object,
  232. I->ImplD->getLocation());
  233. }
  234. }
  235. }
  236. bool removeAttribute(StringRef fromAttr, SourceLocation atLoc) const {
  237. return MigrateCtx.removePropertyAttribute(fromAttr, atLoc);
  238. }
  239. bool rewriteAttribute(StringRef fromAttr, StringRef toAttr,
  240. SourceLocation atLoc) const {
  241. return MigrateCtx.rewritePropertyAttribute(fromAttr, toAttr, atLoc);
  242. }
  243. bool addAttribute(StringRef attr, SourceLocation atLoc) const {
  244. return MigrateCtx.addPropertyAttribute(attr, atLoc);
  245. }
  246. class PlusOneAssign : public RecursiveASTVisitor<PlusOneAssign> {
  247. ObjCIvarDecl *Ivar;
  248. public:
  249. PlusOneAssign(ObjCIvarDecl *D) : Ivar(D) {}
  250. bool VisitBinaryOperator(BinaryOperator *E) {
  251. if (E->getOpcode() != BO_Assign)
  252. return true;
  253. Expr *lhs = E->getLHS()->IgnoreParenImpCasts();
  254. if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(lhs)) {
  255. if (RE->getDecl() != Ivar)
  256. return true;
  257. if (isPlusOneAssign(E))
  258. return false;
  259. }
  260. return true;
  261. }
  262. };
  263. bool hasIvarAssignedAPlusOneObject(PropsTy &props) const {
  264. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  265. PlusOneAssign oneAssign(I->IvarD);
  266. bool notFound = oneAssign.TraverseDecl(CurImplD);
  267. if (!notFound)
  268. return true;
  269. }
  270. return false;
  271. }
  272. bool hasIvarWithExplicitARCOwnership(PropsTy &props) const {
  273. if (Pass.isGCMigration())
  274. return false;
  275. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  276. if (isUserDeclared(I->IvarD)) {
  277. if (isa<AttributedType>(I->IvarD->getType()))
  278. return true;
  279. if (I->IvarD->getType().getLocalQualifiers().getObjCLifetime()
  280. != Qualifiers::OCL_Strong)
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. // Returns true if all declarations in the @property have GC __weak.
  287. bool hasGCWeak(PropsTy &props, SourceLocation atLoc) const {
  288. if (!Pass.isGCMigration())
  289. return false;
  290. if (props.empty())
  291. return false;
  292. return MigrateCtx.AtPropsWeak.count(atLoc);
  293. }
  294. bool isUserDeclared(ObjCIvarDecl *ivarD) const {
  295. return ivarD && !ivarD->getSynthesize();
  296. }
  297. QualType getPropertyType(PropsTy &props) const {
  298. assert(!props.empty());
  299. QualType ty = props[0].PropD->getType().getUnqualifiedType();
  300. #ifndef NDEBUG
  301. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
  302. assert(ty == I->PropD->getType().getUnqualifiedType());
  303. #endif
  304. return ty;
  305. }
  306. ObjCPropertyAttribute::Kind getPropertyAttrs(PropsTy &props) const {
  307. assert(!props.empty());
  308. ObjCPropertyAttribute::Kind attrs =
  309. props[0].PropD->getPropertyAttributesAsWritten();
  310. #ifndef NDEBUG
  311. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
  312. assert(attrs == I->PropD->getPropertyAttributesAsWritten());
  313. #endif
  314. return attrs;
  315. }
  316. };
  317. } // anonymous namespace
  318. void PropertyRewriteTraverser::traverseObjCImplementation(
  319. ObjCImplementationContext &ImplCtx) {
  320. PropertiesRewriter(ImplCtx.getMigrationContext())
  321. .doTransform(ImplCtx.getImplementationDecl());
  322. }