TransProperties.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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<unsigned, 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. if (Prop->getAtLoc().isInvalid())
  70. continue;
  71. unsigned RawLoc = Prop->getAtLoc().getRawEncoding();
  72. if (PrevAtProps)
  73. if (PrevAtProps->find(RawLoc) != PrevAtProps->end())
  74. continue;
  75. PropsTy &props = AtProps[RawLoc];
  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. unsigned rawAtLoc = propD->getAtLoc().getRawEncoding();
  103. AtPropDeclsTy::iterator findAtLoc = AtProps.find(rawAtLoc);
  104. if (findAtLoc == AtProps.end())
  105. continue;
  106. PropsTy &props = findAtLoc->second;
  107. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  108. if (I->PropD == propD) {
  109. I->IvarD = ivarD;
  110. I->ImplD = implD;
  111. break;
  112. }
  113. }
  114. }
  115. for (AtPropDeclsTy::iterator
  116. I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
  117. SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first);
  118. PropsTy &props = I->second;
  119. if (!getPropertyType(props)->isObjCRetainableType())
  120. continue;
  121. if (hasIvarWithExplicitARCOwnership(props))
  122. continue;
  123. Transaction Trans(Pass.TA);
  124. rewriteProperty(props, atLoc);
  125. }
  126. }
  127. private:
  128. void doPropAction(PropActionKind kind,
  129. PropsTy &props, SourceLocation atLoc,
  130. bool markAction = true) {
  131. if (markAction)
  132. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
  133. ActionOnProp[I->PropD->getIdentifier()] = kind;
  134. switch (kind) {
  135. case PropAction_None:
  136. return;
  137. case PropAction_RetainReplacedWithStrong: {
  138. StringRef toAttr = "strong";
  139. MigrateCtx.rewritePropertyAttribute("retain", toAttr, atLoc);
  140. return;
  141. }
  142. case PropAction_AssignRemoved:
  143. return removeAssignForDefaultStrong(props, atLoc);
  144. case PropAction_AssignRewritten:
  145. return rewriteAssign(props, atLoc);
  146. case PropAction_MaybeAddWeakOrUnsafe:
  147. return maybeAddWeakOrUnsafeUnretainedAttr(props, atLoc);
  148. }
  149. }
  150. void rewriteProperty(PropsTy &props, SourceLocation atLoc) {
  151. ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props);
  152. if (propAttrs & (ObjCPropertyDecl::OBJC_PR_copy |
  153. ObjCPropertyDecl::OBJC_PR_unsafe_unretained |
  154. ObjCPropertyDecl::OBJC_PR_strong |
  155. ObjCPropertyDecl::OBJC_PR_weak))
  156. return;
  157. if (propAttrs & ObjCPropertyDecl::OBJC_PR_retain) {
  158. // strong is the default.
  159. return doPropAction(PropAction_RetainReplacedWithStrong, props, atLoc);
  160. }
  161. bool HasIvarAssignedAPlusOneObject = hasIvarAssignedAPlusOneObject(props);
  162. if (propAttrs & ObjCPropertyDecl::OBJC_PR_assign) {
  163. if (HasIvarAssignedAPlusOneObject)
  164. return doPropAction(PropAction_AssignRemoved, props, atLoc);
  165. return doPropAction(PropAction_AssignRewritten, props, atLoc);
  166. }
  167. if (HasIvarAssignedAPlusOneObject ||
  168. (Pass.isGCMigration() && !hasGCWeak(props, atLoc)))
  169. return; // 'strong' by default.
  170. return doPropAction(PropAction_MaybeAddWeakOrUnsafe, props, atLoc);
  171. }
  172. void removeAssignForDefaultStrong(PropsTy &props,
  173. SourceLocation atLoc) const {
  174. removeAttribute("retain", atLoc);
  175. if (!removeAttribute("assign", atLoc))
  176. return;
  177. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  178. if (I->ImplD)
  179. Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
  180. diag::err_arc_assign_property_ownership,
  181. diag::err_arc_inconsistent_property_ownership,
  182. I->IvarD->getLocation());
  183. }
  184. }
  185. void rewriteAssign(PropsTy &props, SourceLocation atLoc) const {
  186. bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
  187. /*AllowOnUnknownClass=*/Pass.isGCMigration());
  188. const char *toWhich =
  189. (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "strong" :
  190. (canUseWeak ? "weak" : "unsafe_unretained");
  191. bool rewroteAttr = rewriteAttribute("assign", toWhich, atLoc);
  192. if (!rewroteAttr)
  193. canUseWeak = false;
  194. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  195. if (isUserDeclared(I->IvarD)) {
  196. if (I->IvarD &&
  197. I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) {
  198. const char *toWhich =
  199. (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "__strong " :
  200. (canUseWeak ? "__weak " : "__unsafe_unretained ");
  201. Pass.TA.insert(I->IvarD->getLocation(), toWhich);
  202. }
  203. }
  204. if (I->ImplD)
  205. Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
  206. diag::err_arc_assign_property_ownership,
  207. diag::err_arc_inconsistent_property_ownership,
  208. I->IvarD->getLocation());
  209. }
  210. }
  211. void maybeAddWeakOrUnsafeUnretainedAttr(PropsTy &props,
  212. SourceLocation atLoc) const {
  213. bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
  214. /*AllowOnUnknownClass=*/Pass.isGCMigration());
  215. bool addedAttr = addAttribute(canUseWeak ? "weak" : "unsafe_unretained",
  216. atLoc);
  217. if (!addedAttr)
  218. canUseWeak = false;
  219. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  220. if (isUserDeclared(I->IvarD)) {
  221. if (I->IvarD &&
  222. I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
  223. Pass.TA.insert(I->IvarD->getLocation(),
  224. canUseWeak ? "__weak " : "__unsafe_unretained ");
  225. }
  226. if (I->ImplD) {
  227. Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
  228. diag::err_arc_assign_property_ownership,
  229. diag::err_arc_inconsistent_property_ownership,
  230. I->IvarD->getLocation());
  231. Pass.TA.clearDiagnostic(
  232. diag::err_arc_objc_property_default_assign_on_object,
  233. I->ImplD->getLocation());
  234. }
  235. }
  236. }
  237. bool removeAttribute(StringRef fromAttr, SourceLocation atLoc) const {
  238. return MigrateCtx.removePropertyAttribute(fromAttr, atLoc);
  239. }
  240. bool rewriteAttribute(StringRef fromAttr, StringRef toAttr,
  241. SourceLocation atLoc) const {
  242. return MigrateCtx.rewritePropertyAttribute(fromAttr, toAttr, atLoc);
  243. }
  244. bool addAttribute(StringRef attr, SourceLocation atLoc) const {
  245. return MigrateCtx.addPropertyAttribute(attr, atLoc);
  246. }
  247. class PlusOneAssign : public RecursiveASTVisitor<PlusOneAssign> {
  248. ObjCIvarDecl *Ivar;
  249. public:
  250. PlusOneAssign(ObjCIvarDecl *D) : Ivar(D) {}
  251. bool VisitBinAssign(BinaryOperator *E) {
  252. Expr *lhs = E->getLHS()->IgnoreParenImpCasts();
  253. if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(lhs)) {
  254. if (RE->getDecl() != Ivar)
  255. return true;
  256. if (isPlusOneAssign(E))
  257. return false;
  258. }
  259. return true;
  260. }
  261. };
  262. bool hasIvarAssignedAPlusOneObject(PropsTy &props) const {
  263. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  264. PlusOneAssign oneAssign(I->IvarD);
  265. bool notFound = oneAssign.TraverseDecl(CurImplD);
  266. if (!notFound)
  267. return true;
  268. }
  269. return false;
  270. }
  271. bool hasIvarWithExplicitARCOwnership(PropsTy &props) const {
  272. if (Pass.isGCMigration())
  273. return false;
  274. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
  275. if (isUserDeclared(I->IvarD)) {
  276. if (isa<AttributedType>(I->IvarD->getType()))
  277. return true;
  278. if (I->IvarD->getType().getLocalQualifiers().getObjCLifetime()
  279. != Qualifiers::OCL_Strong)
  280. return true;
  281. }
  282. }
  283. return false;
  284. }
  285. // Returns true if all declarations in the @property have GC __weak.
  286. bool hasGCWeak(PropsTy &props, SourceLocation atLoc) const {
  287. if (!Pass.isGCMigration())
  288. return false;
  289. if (props.empty())
  290. return false;
  291. return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding());
  292. }
  293. bool isUserDeclared(ObjCIvarDecl *ivarD) const {
  294. return ivarD && !ivarD->getSynthesize();
  295. }
  296. QualType getPropertyType(PropsTy &props) const {
  297. assert(!props.empty());
  298. QualType ty = props[0].PropD->getType().getUnqualifiedType();
  299. #ifndef NDEBUG
  300. for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
  301. assert(ty == I->PropD->getType().getUnqualifiedType());
  302. #endif
  303. return ty;
  304. }
  305. ObjCPropertyDecl::PropertyAttributeKind
  306. getPropertyAttrs(PropsTy &props) const {
  307. assert(!props.empty());
  308. ObjCPropertyDecl::PropertyAttributeKind
  309. attrs = 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. }