clang 10.0.0git

TransBlockObjCVariable.cpp
Go to the documentation of this file.
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
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
33 using namespace clang;
34 using namespace arcmt;
35 using namespace trans;
36
37 namespace {
38
39 class RootBlockObjCVarRewriter :
40 public RecursiveASTVisitor<RootBlockObjCVarRewriter> {
41 llvm::DenseSet<VarDecl *> &VarsToChange;
42
43 class BlockVarChecker : public RecursiveASTVisitor<BlockVarChecker> {
44 VarDecl *Var;
45
46 typedef RecursiveASTVisitor<BlockVarChecker> base;
47 public:
48 BlockVarChecker(VarDecl *var) : Var(var) { }
49
50 bool TraverseImplicitCastExpr(ImplicitCastExpr *castE) {
51 if (DeclRefExpr *
52 ref = dyn_cast<DeclRefExpr>(castE->getSubExpr())) {
53 if (ref->getDecl() == Var) {
54 if (castE->getCastKind() == CK_LValueToRValue)
55 return true; // Using the value of the variable.
56 if (castE->getCastKind() == CK_NoOp && castE->isLValue() &&
57 Var->getASTContext().getLangOpts().CPlusPlus)
58 return true; // Binding to const C++ reference.
59 }
60 }
61
62 return base::TraverseImplicitCastExpr(castE);
63 }
64
65 bool VisitDeclRefExpr(DeclRefExpr *E) {
66 if (E->getDecl() == Var)
67 return false; // The reference of the variable, and not just its value,
68 // is needed.
69 return true;
70 }
71 };
72
73 public:
74 RootBlockObjCVarRewriter(llvm::DenseSet<VarDecl *> &VarsToChange)
75 : VarsToChange(VarsToChange) { }
76
77 bool VisitBlockDecl(BlockDecl *block) {
78 SmallVector<VarDecl *, 4> BlockVars;
79
80 for (const auto &I : block->captures()) {
81 VarDecl *var = I.getVariable();
82 if (I.isByRef() &&
83 var->getType()->isObjCObjectPointerType() &&
84 isImplicitStrong(var->getType())) {
85 BlockVars.push_back(var);
86 }
87 }
88
89 for (unsigned i = 0, e = BlockVars.size(); i != e; ++i) {
90 VarDecl *var = BlockVars[i];
91
92 BlockVarChecker checker(var);
93 bool onlyValueOfVarIsNeeded = checker.TraverseStmt(block->getBody());
94 if (onlyValueOfVarIsNeeded)
95 VarsToChange.insert(var);
96 else
97 VarsToChange.erase(var);
98 }
99
100 return true;
101 }
102
103 private:
104 bool isImplicitStrong(QualType ty) {
105 if (isa<AttributedType>(ty.getTypePtr()))
106 return false;
107 return ty.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong;
108 }
109 };
110
111 class BlockObjCVarRewriter : public RecursiveASTVisitor<BlockObjCVarRewriter> {
112 llvm::DenseSet<VarDecl *> &VarsToChange;
113
114 public:
115 BlockObjCVarRewriter(llvm::DenseSet<VarDecl *> &VarsToChange)
116 : VarsToChange(VarsToChange) { }
117
118 bool TraverseBlockDecl(BlockDecl *block) {
119 RootBlockObjCVarRewriter(VarsToChange).TraverseDecl(block);
120 return true;
121 }
122 };
123
124 } // anonymous namespace
125
126 void BlockObjCVariableTraverser::traverseBody(BodyContext &BodyCtx) {
127 MigrationPass &Pass = BodyCtx.getMigrationContext().Pass;
128 llvm::DenseSet<VarDecl *> VarsToChange;
129
130 BlockObjCVarRewriter trans(VarsToChange);
131 trans.TraverseStmt(BodyCtx.getTopStmt());
132
133 for (llvm::DenseSet<VarDecl *>::iterator
134 I = VarsToChange.begin(), E = VarsToChange.end(); I != E; ++I) {
135 VarDecl *var = *I;
136 BlocksAttr *attr = var->getAttr<BlocksAttr>();
137 if(!attr)
138 continue;
139 bool useWeak = canApplyWeak(Pass.Ctx, var->getType());
140 SourceManager &SM = Pass.Ctx.getSourceManager();
141 Transaction Trans(Pass.TA);
142 Pass.TA.replaceText(SM.getExpansionLoc(attr->getLocation()),
143 "__block",
144 useWeak ? "__weak" : "__unsafe_unretained");
145 }
146 }
ASTContext.h
Defines the clang::ASTContext interface.
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:654
SourceManager.h
Defines the SourceManager interface.
llvm::DenseSet
Definition: Sema.h:71
clang::arcmt::MigrationPass
Definition: Internals.h:144
Internals.h
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:820
clang::BlockDecl::getBody
Stmt * getBody() const override
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition: Decl.h:4116
clang::CastExpr::getSubExpr
Expr * getSubExpr()
Definition: Expr.h:3202
clang::QualType::getLocalQualifiers
Qualifiers getLocalQualifiers() const
Retrieve the set of qualifiers local to this particular QualType instance, not including any qualifie...
Definition: Type.h:6275
clang::arcmt::trans::BodyContext::getMigrationContext
MigrationContext & getMigrationContext()
Definition: Transforms.h:54
clang::arcmt::MigrationPass::Ctx
ASTContext & Ctx
Definition: Internals.h:146
clang::QualType::getTypePtr
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:6256
clang::RecursiveASTVisitor
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Definition: RecursiveASTVisitor.h:151
clang::arcmt::Transaction
Definition: Internals.h:122
clang::BlockDecl
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4037
clang::Qualifiers::getObjCLifetime
ObjCLifetime getObjCLifetime() const
Definition: Type.h:333
clang::arcmt::trans::BodyContext
Definition: Transforms.h:45
clang::DeclRefExpr::getDecl
ValueDecl * getDecl()
Definition: Expr.h:1247
Transforms.h
SM
const SourceManager & SM
Definition: Format.cpp:1685
clang::arcmt::trans::canApplyWeak
bool canApplyWeak(ASTContext &Ctx, QualType type, bool AllowOnUnknownClass=false)
Determine whether we can add weak to the given type.
Definition: Transforms.cpp:37
clang::arcmt::trans::BlockObjCVariableTraverser::traverseBody
void traverseBody(BodyContext &BodyCtx) override
Definition: TransBlockObjCVariable.cpp:126
clang::Qualifiers::OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained...
Definition: Type.h:171
Attr.h
clang::CastExpr::getCastKind
CastKind getCastKind() const
Definition: Expr.h:3196
clang::Decl::getASTContext
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:377
llvm::SmallVector
Definition: LLVM.h:35
clang::Type::isObjCObjectPointerType
bool isObjCObjectPointerType() const
Definition: Type.h:6618
clang::ImplicitCastExpr
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition: Expr.h:3274
clang::Expr::isLValue
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language...
Definition: Expr.h:258
clang::arcmt::trans::MigrationContext::Pass
MigrationPass & Pass
Definition: Transforms.h:84
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::BlockDecl::captures
ArrayRef< Capture > captures() const
Definition: Decl.h:4164
clang::arcmt::TransformActions::replaceText
void replaceText(SourceLocation loc, StringRef text, StringRef replacementText)
Definition: TransformActions.cpp:659
clang::Decl::getAttr
T * getAttr() const
Definition: DeclBase.h:538
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:679
clang::arcmt::MigrationPass::TA
TransformActions & TA
Definition: Internals.h:150
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1171
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:630
clang::arcmt::trans::BodyContext::getTopStmt
Stmt * getTopStmt()
Definition: Transforms.h:56
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:724
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:596