TransBlockObjCVariable.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //===--- TransBlockObjCVariable.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. // rewriteBlockObjCVariable:
  10. //
  11. // Adding __block to an obj-c variable could be either because the variable
  12. // is used for output storage or the user wanted to break a retain cycle.
  13. // This transformation checks whether a reference of the variable for the block
  14. // is actually needed (it is assigned to or its address is taken) or not.
  15. // If the reference is not needed it will assume __block was added to break a
  16. // cycle so it will remove '__block' and add __weak/__unsafe_unretained.
  17. // e.g
  18. //
  19. // __block Foo *x;
  20. // bar(^ { [x cake]; });
  21. // ---->
  22. // __weak Foo *x;
  23. // bar(^ { [x cake]; });
  24. //
  25. //===----------------------------------------------------------------------===//
  26. #include "Transforms.h"
  27. #include "Internals.h"
  28. #include "clang/AST/ASTContext.h"
  29. #include "clang/AST/Attr.h"
  30. #include "clang/Basic/SourceManager.h"
  31. using namespace clang;
  32. using namespace arcmt;
  33. using namespace trans;
  34. namespace {
  35. class RootBlockObjCVarRewriter :
  36. public RecursiveASTVisitor<RootBlockObjCVarRewriter> {
  37. llvm::DenseSet<VarDecl *> &VarsToChange;
  38. class BlockVarChecker : public RecursiveASTVisitor<BlockVarChecker> {
  39. VarDecl *Var;
  40. typedef RecursiveASTVisitor<BlockVarChecker> base;
  41. public:
  42. BlockVarChecker(VarDecl *var) : Var(var) { }
  43. bool TraverseImplicitCastExpr(ImplicitCastExpr *castE) {
  44. if (DeclRefExpr *
  45. ref = dyn_cast<DeclRefExpr>(castE->getSubExpr())) {
  46. if (ref->getDecl() == Var) {
  47. if (castE->getCastKind() == CK_LValueToRValue)
  48. return true; // Using the value of the variable.
  49. if (castE->getCastKind() == CK_NoOp && castE->isLValue() &&
  50. Var->getASTContext().getLangOpts().CPlusPlus)
  51. return true; // Binding to const C++ reference.
  52. }
  53. }
  54. return base::TraverseImplicitCastExpr(castE);
  55. }
  56. bool VisitDeclRefExpr(DeclRefExpr *E) {
  57. if (E->getDecl() == Var)
  58. return false; // The reference of the variable, and not just its value,
  59. // is needed.
  60. return true;
  61. }
  62. };
  63. public:
  64. RootBlockObjCVarRewriter(llvm::DenseSet<VarDecl *> &VarsToChange)
  65. : VarsToChange(VarsToChange) { }
  66. bool VisitBlockDecl(BlockDecl *block) {
  67. SmallVector<VarDecl *, 4> BlockVars;
  68. for (const auto &I : block->captures()) {
  69. VarDecl *var = I.getVariable();
  70. if (I.isByRef() &&
  71. var->getType()->isObjCObjectPointerType() &&
  72. isImplicitStrong(var->getType())) {
  73. BlockVars.push_back(var);
  74. }
  75. }
  76. for (unsigned i = 0, e = BlockVars.size(); i != e; ++i) {
  77. VarDecl *var = BlockVars[i];
  78. BlockVarChecker checker(var);
  79. bool onlyValueOfVarIsNeeded = checker.TraverseStmt(block->getBody());
  80. if (onlyValueOfVarIsNeeded)
  81. VarsToChange.insert(var);
  82. else
  83. VarsToChange.erase(var);
  84. }
  85. return true;
  86. }
  87. private:
  88. bool isImplicitStrong(QualType ty) {
  89. if (isa<AttributedType>(ty.getTypePtr()))
  90. return false;
  91. return ty.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong;
  92. }
  93. };
  94. class BlockObjCVarRewriter : public RecursiveASTVisitor<BlockObjCVarRewriter> {
  95. llvm::DenseSet<VarDecl *> &VarsToChange;
  96. public:
  97. BlockObjCVarRewriter(llvm::DenseSet<VarDecl *> &VarsToChange)
  98. : VarsToChange(VarsToChange) { }
  99. bool TraverseBlockDecl(BlockDecl *block) {
  100. RootBlockObjCVarRewriter(VarsToChange).TraverseDecl(block);
  101. return true;
  102. }
  103. };
  104. } // anonymous namespace
  105. void BlockObjCVariableTraverser::traverseBody(BodyContext &BodyCtx) {
  106. MigrationPass &Pass = BodyCtx.getMigrationContext().Pass;
  107. llvm::DenseSet<VarDecl *> VarsToChange;
  108. BlockObjCVarRewriter trans(VarsToChange);
  109. trans.TraverseStmt(BodyCtx.getTopStmt());
  110. for (llvm::DenseSet<VarDecl *>::iterator
  111. I = VarsToChange.begin(), E = VarsToChange.end(); I != E; ++I) {
  112. VarDecl *var = *I;
  113. BlocksAttr *attr = var->getAttr<BlocksAttr>();
  114. if(!attr)
  115. continue;
  116. bool useWeak = canApplyWeak(Pass.Ctx, var->getType());
  117. SourceManager &SM = Pass.Ctx.getSourceManager();
  118. Transaction Trans(Pass.TA);
  119. Pass.TA.replaceText(SM.getExpansionLoc(attr->getLocation()),
  120. "__block",
  121. useWeak ? "__weak" : "__unsafe_unretained");
  122. }
  123. }