TransBlockObjCVariable.cpp 4.5 KB

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