2012-11-14 23:08:31 +08:00
|
|
|
//===--- TransBlockObjCVariable.cpp - Transformations to ARC mode ---------===//
|
2011-06-22 04:20:39 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// rewriteBlockObjCVariable:
|
|
|
|
//
|
2012-07-23 16:59:39 +08:00
|
|
|
// Adding __block to an obj-c variable could be either because the variable
|
2011-06-22 04:20:39 +08:00
|
|
|
// is used for output storage or the user wanted to break a retain cycle.
|
|
|
|
// This transformation checks whether a reference of the variable for the block
|
|
|
|
// is actually needed (it is assigned to or its address is taken) or not.
|
|
|
|
// If the reference is not needed it will assume __block was added to break a
|
|
|
|
// cycle so it will remove '__block' and add __weak/__unsafe_unretained.
|
|
|
|
// e.g
|
|
|
|
//
|
|
|
|
// __block Foo *x;
|
|
|
|
// bar(^ { [x cake]; });
|
|
|
|
// ---->
|
|
|
|
// __weak Foo *x;
|
|
|
|
// bar(^ { [x cake]; });
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Transforms.h"
|
|
|
|
#include "Internals.h"
|
2012-07-05 04:19:54 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2011-06-22 04:20:39 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace arcmt;
|
|
|
|
using namespace trans;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class RootBlockObjCVarRewriter :
|
|
|
|
public RecursiveASTVisitor<RootBlockObjCVarRewriter> {
|
2012-03-05 16:46:24 +08:00
|
|
|
llvm::DenseSet<VarDecl *> &VarsToChange;
|
2011-06-22 04:20:39 +08:00
|
|
|
|
|
|
|
class BlockVarChecker : public RecursiveASTVisitor<BlockVarChecker> {
|
|
|
|
VarDecl *Var;
|
|
|
|
|
|
|
|
typedef RecursiveASTVisitor<BlockVarChecker> base;
|
|
|
|
public:
|
|
|
|
BlockVarChecker(VarDecl *var) : Var(var) { }
|
|
|
|
|
|
|
|
bool TraverseImplicitCastExpr(ImplicitCastExpr *castE) {
|
2012-03-10 17:33:50 +08:00
|
|
|
if (DeclRefExpr *
|
|
|
|
ref = dyn_cast<DeclRefExpr>(castE->getSubExpr())) {
|
2011-06-22 04:20:39 +08:00
|
|
|
if (ref->getDecl() == Var) {
|
|
|
|
if (castE->getCastKind() == CK_LValueToRValue)
|
|
|
|
return true; // Using the value of the variable.
|
|
|
|
if (castE->getCastKind() == CK_NoOp && castE->isLValue() &&
|
2012-03-11 15:00:24 +08:00
|
|
|
Var->getASTContext().getLangOpts().CPlusPlus)
|
2011-06-22 04:20:39 +08:00
|
|
|
return true; // Binding to const C++ reference.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return base::TraverseImplicitCastExpr(castE);
|
|
|
|
}
|
|
|
|
|
2012-03-10 17:33:50 +08:00
|
|
|
bool VisitDeclRefExpr(DeclRefExpr *E) {
|
2011-06-22 04:20:39 +08:00
|
|
|
if (E->getDecl() == Var)
|
|
|
|
return false; // The reference of the variable, and not just its value,
|
|
|
|
// is needed.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2012-06-07 01:32:50 +08:00
|
|
|
RootBlockObjCVarRewriter(llvm::DenseSet<VarDecl *> &VarsToChange)
|
|
|
|
: VarsToChange(VarsToChange) { }
|
2011-06-22 04:20:39 +08:00
|
|
|
|
|
|
|
bool VisitBlockDecl(BlockDecl *block) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<VarDecl *, 4> BlockVars;
|
2011-06-22 04:20:39 +08:00
|
|
|
|
2014-03-15 02:34:04 +08:00
|
|
|
for (const auto &I : block->captures()) {
|
|
|
|
VarDecl *var = I.getVariable();
|
|
|
|
if (I.isByRef() &&
|
2011-06-22 04:20:39 +08:00
|
|
|
var->getType()->isObjCObjectPointerType() &&
|
|
|
|
isImplicitStrong(var->getType())) {
|
|
|
|
BlockVars.push_back(var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = BlockVars.size(); i != e; ++i) {
|
|
|
|
VarDecl *var = BlockVars[i];
|
|
|
|
|
|
|
|
BlockVarChecker checker(var);
|
|
|
|
bool onlyValueOfVarIsNeeded = checker.TraverseStmt(block->getBody());
|
2012-03-05 16:46:24 +08:00
|
|
|
if (onlyValueOfVarIsNeeded)
|
|
|
|
VarsToChange.insert(var);
|
|
|
|
else
|
|
|
|
VarsToChange.erase(var);
|
2011-06-22 04:20:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool isImplicitStrong(QualType ty) {
|
|
|
|
if (isa<AttributedType>(ty.getTypePtr()))
|
|
|
|
return false;
|
|
|
|
return ty.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BlockObjCVarRewriter : public RecursiveASTVisitor<BlockObjCVarRewriter> {
|
2012-03-05 16:46:24 +08:00
|
|
|
llvm::DenseSet<VarDecl *> &VarsToChange;
|
2011-06-22 04:20:39 +08:00
|
|
|
|
|
|
|
public:
|
2012-06-07 04:15:08 +08:00
|
|
|
BlockObjCVarRewriter(llvm::DenseSet<VarDecl *> &VarsToChange)
|
|
|
|
: VarsToChange(VarsToChange) { }
|
2011-06-22 04:20:39 +08:00
|
|
|
|
|
|
|
bool TraverseBlockDecl(BlockDecl *block) {
|
2012-06-07 01:32:50 +08:00
|
|
|
RootBlockObjCVarRewriter(VarsToChange).TraverseDecl(block);
|
2011-06-22 04:20:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-03-05 16:46:24 +08:00
|
|
|
void BlockObjCVariableTraverser::traverseBody(BodyContext &BodyCtx) {
|
|
|
|
MigrationPass &Pass = BodyCtx.getMigrationContext().Pass;
|
|
|
|
llvm::DenseSet<VarDecl *> VarsToChange;
|
|
|
|
|
2012-06-07 04:15:08 +08:00
|
|
|
BlockObjCVarRewriter trans(VarsToChange);
|
2012-03-05 16:46:24 +08:00
|
|
|
trans.TraverseStmt(BodyCtx.getTopStmt());
|
|
|
|
|
|
|
|
for (llvm::DenseSet<VarDecl *>::iterator
|
|
|
|
I = VarsToChange.begin(), E = VarsToChange.end(); I != E; ++I) {
|
|
|
|
VarDecl *var = *I;
|
|
|
|
BlocksAttr *attr = var->getAttr<BlocksAttr>();
|
|
|
|
if(!attr)
|
|
|
|
continue;
|
|
|
|
bool useWeak = canApplyWeak(Pass.Ctx, var->getType());
|
|
|
|
SourceManager &SM = Pass.Ctx.getSourceManager();
|
|
|
|
Transaction Trans(Pass.TA);
|
|
|
|
Pass.TA.replaceText(SM.getExpansionLoc(attr->getLocation()),
|
|
|
|
"__block",
|
|
|
|
useWeak ? "__weak" : "__unsafe_unretained");
|
|
|
|
}
|
2011-06-22 04:20:39 +08:00
|
|
|
}
|