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