clang 8.0.0

Transforms.h
Go to the documentation of this file.
1 //===-- Transforms.h - Transformations to ARC mode --------------*- C++ -*-===//
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 #ifndef LLVM_CLANG_LIB_ARCMIGRATE_TRANSFORMS_H
11 #define LLVM_CLANG_LIB_ARCMIGRATE_TRANSFORMS_H
12
13 #include "clang/AST/ParentMap.h"
14 #include "clang/AST/RecursiveASTVisitor.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/Support/SaveAndRestore.h"
17
18 namespace clang {
19 class Decl;
20 class Stmt;
21 class BlockDecl;
22 class ObjCMethodDecl;
23 class FunctionDecl;
24
25 namespace arcmt {
26 class MigrationPass;
27
28 namespace trans {
29
30 class MigrationContext;
31
32 //===----------------------------------------------------------------------===//
33 // Transformations.
34 //===----------------------------------------------------------------------===//
35
36 void rewriteAutoreleasePool(MigrationPass &pass);
37 void rewriteUnbridgedCasts(MigrationPass &pass);
38 void makeAssignARCSafe(MigrationPass &pass);
39 void removeRetainReleaseDeallocFinalize(MigrationPass &pass);
40 void removeZeroOutPropsInDeallocFinalize(MigrationPass &pass);
41 void rewriteUnusedInitDelegate(MigrationPass &pass);
42 void checkAPIUses(MigrationPass &pass);
43
44 void removeEmptyStatementsAndDeallocFinalize(MigrationPass &pass);
45
46 class BodyContext {
47 MigrationContext &MigrateCtx;
48 ParentMap PMap;
49 Stmt *TopStmt;
50
51 public:
52 BodyContext(MigrationContext &MigrateCtx, Stmt *S)
53 : MigrateCtx(MigrateCtx), PMap(S), TopStmt(S) {}
54
55 MigrationContext &getMigrationContext() { return MigrateCtx; }
56 ParentMap &getParentMap() { return PMap; }
57 Stmt *getTopStmt() { return TopStmt; }
58 };
59
60 class ObjCImplementationContext {
61 MigrationContext &MigrateCtx;
62 ObjCImplementationDecl *ImpD;
63
64 public:
65 ObjCImplementationContext(MigrationContext &MigrateCtx,
66 ObjCImplementationDecl *D)
67 : MigrateCtx(MigrateCtx), ImpD(D) {}
68
69 MigrationContext &getMigrationContext() { return MigrateCtx; }
70 ObjCImplementationDecl *getImplementationDecl() { return ImpD; }
71 };
72
73 class ASTTraverser {
74 public:
75 virtual ~ASTTraverser();
76 virtual void traverseTU(MigrationContext &MigrateCtx) { }
77 virtual void traverseBody(BodyContext &BodyCtx) { }
78 virtual void traverseObjCImplementation(ObjCImplementationContext &ImplCtx) {}
79 };
80
81 class MigrationContext {
82 std::vector<ASTTraverser *> Traversers;
83
84 public:
85 MigrationPass &Pass;
86
87 struct GCAttrOccurrence {
88 enum AttrKind { Weak, Strong } Kind;
89 SourceLocation Loc;
90 QualType ModifiedType;
91 Decl *Dcl;
92 /// true if the attribute is owned, e.g. it is in a body and not just
93 /// in an interface.
94 bool FullyMigratable;
95 };
96 std::vector<GCAttrOccurrence> GCAttrs;
97 llvm::DenseSet<unsigned> AttrSet;
98 llvm::DenseSet<unsigned> RemovedAttrSet;
99
100 /// Set of raw '@' locations for 'assign' properties group that contain
101 /// GC __weak.
102 llvm::DenseSet<unsigned> AtPropsWeak;
103
104 explicit MigrationContext(MigrationPass &pass) : Pass(pass) {}
105 ~MigrationContext();
106
107 typedef std::vector<ASTTraverser *>::iterator traverser_iterator;
108 traverser_iterator traversers_begin() { return Traversers.begin(); }
109 traverser_iterator traversers_end() { return Traversers.end(); }
110
111 void addTraverser(ASTTraverser *traverser) {
112 Traversers.push_back(traverser);
113 }
114
115 bool isGCOwnedNonObjC(QualType T);
116 bool removePropertyAttribute(StringRef fromAttr, SourceLocation atLoc) {
117 return rewritePropertyAttribute(fromAttr, StringRef(), atLoc);
118 }
119 bool rewritePropertyAttribute(StringRef fromAttr, StringRef toAttr,
120 SourceLocation atLoc);
121 bool addPropertyAttribute(StringRef attr, SourceLocation atLoc);
122
123 void traverse(TranslationUnitDecl *TU);
124
125 void dumpGCAttrs();
126 };
127
128 class PropertyRewriteTraverser : public ASTTraverser {
129 public:
130 void traverseObjCImplementation(ObjCImplementationContext &ImplCtx) override;
131 };
132
133 class BlockObjCVariableTraverser : public ASTTraverser {
134 public:
135 void traverseBody(BodyContext &BodyCtx) override;
136 };
137
138 class ProtectedScopeTraverser : public ASTTraverser {
139 public:
140 void traverseBody(BodyContext &BodyCtx) override;
141 };
142
143 // GC transformations
144
145 class GCAttrsTraverser : public ASTTraverser {
146 public:
147 void traverseTU(MigrationContext &MigrateCtx) override;
148 };
149
150 class GCCollectableCallsTraverser : public ASTTraverser {
151 public:
152 void traverseBody(BodyContext &BodyCtx) override;
153 };
154
155 //===----------------------------------------------------------------------===//
156 // Helpers.
157 //===----------------------------------------------------------------------===//
158
159 /// Determine whether we can add weak to the given type.
160 bool canApplyWeak(ASTContext &Ctx, QualType type,
161 bool AllowOnUnknownClass = false);
162
163 bool isPlusOneAssign(const BinaryOperator *E);
164 bool isPlusOne(const Expr *E);
165
166 /// 'Loc' is the end of a statement range. This returns the location
167 /// immediately after the semicolon following the statement.
168 /// If no semicolon is found or the location is inside a macro, the returned
169 /// source location will be invalid.
170 SourceLocation findLocationAfterSemi(SourceLocation loc, ASTContext &Ctx,
171 bool IsDecl = false);
172
173 /// 'Loc' is the end of a statement range. This returns the location
174 /// of the semicolon following the statement.
175 /// If no semicolon is found or the location is inside a macro, the returned
176 /// source location will be invalid.
177 SourceLocation findSemiAfterLocation(SourceLocation loc, ASTContext &Ctx,
178 bool IsDecl = false);
179
180 bool hasSideEffects(Expr *E, ASTContext &Ctx);
181 bool isGlobalVar(Expr *E);
182 /// Returns "nil" or "0" if 'nil' macro is not actually defined.
183 StringRef getNilString(MigrationPass &Pass);
184
185 template <typename BODY_TRANS>
186 class BodyTransform : public RecursiveASTVisitor<BodyTransform<BODY_TRANS> > {
187 MigrationPass &Pass;
188 Decl *ParentD;
189
190 typedef RecursiveASTVisitor<BodyTransform<BODY_TRANS> > base;
191 public:
192 BodyTransform(MigrationPass &pass) : Pass(pass), ParentD(nullptr) { }
193
194 bool TraverseStmt(Stmt *rootS) {
195 if (rootS)
196 BODY_TRANS(Pass).transformBody(rootS, ParentD);
197 return true;
198 }
199
200 bool TraverseObjCMethodDecl(ObjCMethodDecl *D) {
201 SaveAndRestore<Decl *> SetParent(ParentD, D);
202 return base::TraverseObjCMethodDecl(D);
203 }
204 };
205
206 typedef llvm::DenseSet<Expr *> ExprSet;
207
208 void clearRefsIn(Stmt *S, ExprSet &refs);
209 template <typename iterator>
210 void clearRefsIn(iterator begin, iterator end, ExprSet &refs) {
211 for (; begin != end; ++begin)
212 clearRefsIn(*begin, refs);
213 }
214
215 void collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs);
216
217 void collectRemovables(Stmt *S, ExprSet &exprs);
218
219 } // end namespace trans
220
221 } // end namespace arcmt
222
223 } // end namespace clang
224
225 #endif
clang::arcmt::trans::BodyContext::BodyContext
BodyContext(MigrationContext &MigrateCtx, Stmt *S)
Definition: Transforms.h:52
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:638
clang::ParentMap
Definition: ParentMap.h:21
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
llvm::DenseSet< unsigned >
clang::arcmt::trans::BodyTransform::BodyTransform
BodyTransform(MigrationPass &pass)
Definition: Transforms.h:192
clang::arcmt::MigrationPass
Definition: Internals.h:145
clang::arcmt::trans::ObjCImplementationContext::getMigrationContext
MigrationContext & getMigrationContext()
Definition: Transforms.h:69
clang::CodeGen::AlignmentSource::Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
clang::arcmt::trans::ProtectedScopeTraverser
Definition: Transforms.h:138
clang::arcmt::trans::MigrationContext::traversers_begin
traverser_iterator traversers_begin()
Definition: Transforms.h:108
clang::arcmt::trans::ASTTraverser::traverseObjCImplementation
virtual void traverseObjCImplementation(ObjCImplementationContext &ImplCtx)
Definition: Transforms.h:78
clang::arcmt::trans::findSemiAfterLocation
SourceLocation findSemiAfterLocation(SourceLocation loc, ASTContext &Ctx, bool IsDecl=false)
&#39;Loc&#39; is the end of a statement range.
Definition: Transforms.cpp:130
clang::arcmt::trans::makeAssignARCSafe
void makeAssignARCSafe(MigrationPass &pass)
Definition: TransARCAssign.cpp:75
clang::arcmt::trans::MigrationContext::traverser_iterator
std::vector< ASTTraverser * >::iterator traverser_iterator
Definition: Transforms.h:107
clang::ObjCMethodDecl
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
clang::arcmt::trans::BlockObjCVariableTraverser
Definition: Transforms.h:133
clang::arcmt::trans::MigrationContext::GCAttrs
std::vector< GCAttrOccurrence > GCAttrs
Definition: Transforms.h:96
clang::arcmt::trans::clearRefsIn
void clearRefsIn(Stmt *S, ExprSet &refs)
Definition: Transforms.cpp:300
clang::arcmt::trans::BodyContext::getParentMap
ParentMap & getParentMap()
Definition: Transforms.h:56
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
clang::arcmt::trans::BodyTransform::TraverseObjCMethodDecl
bool TraverseObjCMethodDecl(ObjCMethodDecl *D)
Definition: Transforms.h:200
clang::arcmt::trans::isGlobalVar
bool isGlobalVar(Expr *E)
Definition: Transforms.cpp:197
clang::arcmt::trans::MigrationContext::GCAttrOccurrence::FullyMigratable
bool FullyMigratable
true if the attribute is owned, e.g.
Definition: Transforms.h:94
clang::arcmt::trans::MigrationContext::GCAttrOccurrence::Loc
SourceLocation Loc
Definition: Transforms.h:89
clang::arcmt::trans::findLocationAfterSemi
SourceLocation findLocationAfterSemi(SourceLocation loc, ASTContext &Ctx, bool IsDecl=false)
&#39;Loc&#39; is the end of a statement range.
Definition: Transforms.cpp:118
clang::arcmt::trans::removeRetainReleaseDeallocFinalize
void removeRetainReleaseDeallocFinalize(MigrationPass &pass)
Definition: TransRetainReleaseDealloc.cpp:457
clang::arcmt::trans::BodyContext::getMigrationContext
MigrationContext & getMigrationContext()
Definition: Transforms.h:55
clang::arcmt::trans::MigrationContext::GCAttrOccurrence::Weak
Definition: Transforms.h:88
clang::arcmt::trans::BodyTransform::TraverseStmt
bool TraverseStmt(Stmt *rootS)
Definition: Transforms.h:194
clang::BinaryOperator
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3287
clang::arcmt::trans::rewriteUnusedInitDelegate
void rewriteUnusedInitDelegate(MigrationPass &pass)
Definition: TransUnusedInitDelegate.cpp:75
clang::arcmt::trans::ASTTraverser::traverseBody
virtual void traverseBody(BodyContext &BodyCtx)
Definition: Transforms.h:77
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::trans::isPlusOneAssign
bool isPlusOneAssign(const BinaryOperator *E)
Definition: Transforms.cpp:67
clang::arcmt::trans::ASTTraverser::traverseTU
virtual void traverseTU(MigrationContext &MigrateCtx)
Definition: Transforms.h:76
clang::arcmt::trans::ASTTraverser
Definition: Transforms.h:73
clang::arcmt::trans::getNilString
StringRef getNilString(MigrationPass &Pass)
Returns "nil" or "0" if &#39;nil&#39; macro is not actually defined.
Definition: Transforms.cpp:209
clang::arcmt::trans::MigrationContext::RemovedAttrSet
llvm::DenseSet< unsigned > RemovedAttrSet
Definition: Transforms.h:98
clang::arcmt::trans::collectRemovables
void collectRemovables(Stmt *S, ExprSet &exprs)
Definition: Transforms.cpp:308
clang::arcmt::trans::ObjCImplementationContext
Definition: Transforms.h:60
clang::ValueDecl
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:637
clang::Expr
This represents one expression.
Definition: Expr.h:106
ParentMap.h
clang::arcmt::trans::GCCollectableCallsTraverser
Definition: Transforms.h:150
clang::arcmt::trans::GCAttrsTraverser
Definition: Transforms.h:145
clang::arcmt::trans::MigrationContext::GCAttrOccurrence
Definition: Transforms.h:87
clang::arcmt::trans::removeEmptyStatementsAndDeallocFinalize
void removeEmptyStatementsAndDeallocFinalize(MigrationPass &pass)
Definition: TransEmptyStatementsAndDealloc.cpp:242
clang::arcmt::trans::MigrationContext::GCAttrOccurrence::ModifiedType
QualType ModifiedType
Definition: Transforms.h:90
clang::arcmt::trans::ObjCImplementationContext::getImplementationDecl
ObjCImplementationDecl * getImplementationDecl()
Definition: Transforms.h:70
clang::arcmt::trans::checkAPIUses
void checkAPIUses(MigrationPass &pass)
Definition: TransAPIUses.cpp:106
clang::arcmt::trans::BodyContext
Definition: Transforms.h:46
RecursiveASTVisitor.h
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:38
clang::arcmt::trans::rewriteUnbridgedCasts
void rewriteUnbridgedCasts(MigrationPass &pass)
Definition: TransUnbridgedCasts.cpp:464
Kind
Kind
Definition: ChrootChecker.cpp:29
clang::arcmt::trans::MigrationContext::AttrSet
llvm::DenseSet< unsigned > AttrSet
Definition: Transforms.h:97
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::arcmt::trans::MigrationContext::traversers_end
traverser_iterator traversers_end()
Definition: Transforms.h:109
clang::arcmt::trans::MigrationContext::removePropertyAttribute
bool removePropertyAttribute(StringRef fromAttr, SourceLocation atLoc)
Definition: Transforms.h:116
clang::arcmt::trans::MigrationContext::Pass
MigrationPass & Pass
Definition: Transforms.h:85
clang::arcmt::trans::collectRefs
void collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs)
Definition: Transforms.cpp:304
clang::arcmt::trans::MigrationContext::addTraverser
void addTraverser(ASTTraverser *traverser)
Definition: Transforms.h:111
clang::arcmt::trans::ExprSet
llvm::DenseSet< Expr * > ExprSet
Definition: Transforms.h:206
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::arcmt::trans::MigrationContext::GCAttrOccurrence::AttrKind
AttrKind
Definition: Transforms.h:88
clang::arcmt::trans::ObjCImplementationContext::ObjCImplementationContext
ObjCImplementationContext(MigrationContext &MigrateCtx, ObjCImplementationDecl *D)
Definition: Transforms.h:65
clang::arcmt::trans::rewriteAutoreleasePool
void rewriteAutoreleasePool(MigrationPass &pass)
Definition: TransAutoreleasePool.cpp:432
clang::arcmt::trans::hasSideEffects
bool hasSideEffects(Expr *E, ASTContext &Ctx)
Definition: Transforms.cpp:168
llvm::SaveAndRestore
Definition: LLVM.h:42
type
clang::arcmt::trans::isPlusOne
bool isPlusOne(const Expr *E)
Definition: Transforms.cpp:74
clang::arcmt::trans::BodyTransform
Definition: Transforms.h:186
clang::arcmt::trans::MigrationContext
Definition: Transforms.h:81
clang::ObjCImplementationDecl
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
Definition: DeclObjC.h:2552
clang::arcmt::trans::MigrationContext::GCAttrOccurrence::Dcl
Decl * Dcl
Definition: Transforms.h:91
clang::arcmt::trans::removeZeroOutPropsInDeallocFinalize
void removeZeroOutPropsInDeallocFinalize(MigrationPass &pass)
Definition: TransZeroOutPropsInDealloc.cpp:224
clang::arcmt::trans::MigrationContext::MigrationContext
MigrationContext(MigrationPass &pass)
Definition: Transforms.h:104
clang::arcmt::trans::PropertyRewriteTraverser
Definition: Transforms.h:128
clang::TranslationUnitDecl
The top declaration context.
Definition: Decl.h:108
clang::arcmt::trans::BodyContext::getTopStmt
Stmt * getTopStmt()
Definition: Transforms.h:57
clang::arcmt::trans::MigrationContext::AtPropsWeak
llvm::DenseSet< unsigned > AtPropsWeak
Set of raw &#39;@&#39; locations for &#39;assign&#39; properties group that contain GC __weak.
Definition: Transforms.h:102