clang  10.0.0svn
TransEmptyStatementsAndDealloc.cpp
Go to the documentation of this file.
1 //===-- TransEmptyStatementsAndDealloc.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 // removeEmptyStatementsAndDealloc:
10 //
11 // Removes empty statements that are leftovers from previous transformations.
12 // e.g for
13 //
14 // [x retain];
15 //
16 // removeRetainReleaseDealloc will leave an empty ";" that removeEmptyStatements
17 // will remove.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #include "Transforms.h"
22 #include "Internals.h"
23 #include "clang/AST/ASTContext.h"
24 #include "clang/AST/StmtVisitor.h"
26 
27 using namespace clang;
28 using namespace arcmt;
29 using namespace trans;
30 
32  std::vector<SourceLocation> &MacroLocs,
33  ASTContext &Ctx) {
34  if (!S->hasLeadingEmptyMacro())
35  return false;
36 
37  SourceLocation SemiLoc = S->getSemiLoc();
38  if (SemiLoc.isInvalid() || SemiLoc.isMacroID())
39  return false;
40 
41  if (MacroLocs.empty())
42  return false;
43 
45  std::vector<SourceLocation>::iterator I = llvm::upper_bound(
46  MacroLocs, SemiLoc, BeforeThanCompare<SourceLocation>(SM));
47  --I;
49  AfterMacroLoc = I->getLocWithOffset(getARCMTMacroName().size());
50  assert(AfterMacroLoc.isFileID());
51 
52  if (AfterMacroLoc == SemiLoc)
53  return true;
54 
55  int RelOffs = 0;
56  if (!SM.isInSameSLocAddrSpace(AfterMacroLoc, SemiLoc, &RelOffs))
57  return false;
58  if (RelOffs < 0)
59  return false;
60 
61  // We make the reasonable assumption that a semicolon after 100 characters
62  // means that it is not the next token after our macro. If this assumption
63  // fails it is not critical, we will just fail to clear out, e.g., an empty
64  // 'if'.
65  if (RelOffs - getARCMTMacroName().size() > 100)
66  return false;
67 
68  SourceLocation AfterMacroSemiLoc = findSemiAfterLocation(AfterMacroLoc, Ctx);
69  return AfterMacroSemiLoc == SemiLoc;
70 }
71 
72 namespace {
73 
74 /// Returns true if the statement became empty due to previous
75 /// transformations.
76 class EmptyChecker : public StmtVisitor<EmptyChecker, bool> {
77  ASTContext &Ctx;
78  std::vector<SourceLocation> &MacroLocs;
79 
80 public:
81  EmptyChecker(ASTContext &ctx, std::vector<SourceLocation> &macroLocs)
82  : Ctx(ctx), MacroLocs(macroLocs) { }
83 
84  bool VisitNullStmt(NullStmt *S) {
85  return isEmptyARCMTMacroStatement(S, MacroLocs, Ctx);
86  }
87  bool VisitCompoundStmt(CompoundStmt *S) {
88  if (S->body_empty())
89  return false; // was already empty, not because of transformations.
90  for (auto *I : S->body())
91  if (!Visit(I))
92  return false;
93  return true;
94  }
95  bool VisitIfStmt(IfStmt *S) {
96  if (S->getConditionVariable())
97  return false;
98  Expr *condE = S->getCond();
99  if (!condE)
100  return false;
101  if (hasSideEffects(condE, Ctx))
102  return false;
103  if (!S->getThen() || !Visit(S->getThen()))
104  return false;
105  return !S->getElse() || Visit(S->getElse());
106  }
107  bool VisitWhileStmt(WhileStmt *S) {
108  if (S->getConditionVariable())
109  return false;
110  Expr *condE = S->getCond();
111  if (!condE)
112  return false;
113  if (hasSideEffects(condE, Ctx))
114  return false;
115  if (!S->getBody())
116  return false;
117  return Visit(S->getBody());
118  }
119  bool VisitDoStmt(DoStmt *S) {
120  Expr *condE = S->getCond();
121  if (!condE)
122  return false;
123  if (hasSideEffects(condE, Ctx))
124  return false;
125  if (!S->getBody())
126  return false;
127  return Visit(S->getBody());
128  }
129  bool VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
130  Expr *Exp = S->getCollection();
131  if (!Exp)
132  return false;
133  if (hasSideEffects(Exp, Ctx))
134  return false;
135  if (!S->getBody())
136  return false;
137  return Visit(S->getBody());
138  }
139  bool VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
140  if (!S->getSubStmt())
141  return false;
142  return Visit(S->getSubStmt());
143  }
144 };
145 
146 class EmptyStatementsRemover :
147  public RecursiveASTVisitor<EmptyStatementsRemover> {
148  MigrationPass &Pass;
149 
150 public:
151  EmptyStatementsRemover(MigrationPass &pass) : Pass(pass) { }
152 
153  bool TraverseStmtExpr(StmtExpr *E) {
154  CompoundStmt *S = E->getSubStmt();
156  I = S->body_begin(), E = S->body_end(); I != E; ++I) {
157  if (I != E - 1)
158  check(*I);
159  TraverseStmt(*I);
160  }
161  return true;
162  }
163 
164  bool VisitCompoundStmt(CompoundStmt *S) {
165  for (auto *I : S->body())
166  check(I);
167  return true;
168  }
169 
170  ASTContext &getContext() { return Pass.Ctx; }
171 
172 private:
173  void check(Stmt *S) {
174  if (!S) return;
175  if (EmptyChecker(Pass.Ctx, Pass.ARCMTMacroLocs).Visit(S)) {
176  Transaction Trans(Pass.TA);
177  Pass.TA.removeStmt(S);
178  }
179  }
180 };
181 
182 } // anonymous namespace
183 
184 static bool isBodyEmpty(CompoundStmt *body, ASTContext &Ctx,
185  std::vector<SourceLocation> &MacroLocs) {
186  for (auto *I : body->body())
187  if (!EmptyChecker(Ctx, MacroLocs).Visit(I))
188  return false;
189 
190  return true;
191 }
192 
194  ASTContext &Ctx = pass.Ctx;
195  TransformActions &TA = pass.TA;
197  Selector FinalizeSel =
198  Ctx.Selectors.getNullarySelector(&pass.Ctx.Idents.get("finalize"));
199 
201  impl_iterator;
202  for (impl_iterator I = impl_iterator(DC->decls_begin()),
203  E = impl_iterator(DC->decls_end()); I != E; ++I) {
204  ObjCMethodDecl *DeallocM = nullptr;
205  ObjCMethodDecl *FinalizeM = nullptr;
206  for (auto *MD : I->instance_methods()) {
207  if (!MD->hasBody())
208  continue;
209 
210  if (MD->getMethodFamily() == OMF_dealloc) {
211  DeallocM = MD;
212  } else if (MD->isInstanceMethod() && MD->getSelector() == FinalizeSel) {
213  FinalizeM = MD;
214  }
215  }
216 
217  if (DeallocM) {
218  if (isBodyEmpty(DeallocM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
219  Transaction Trans(TA);
220  TA.remove(DeallocM->getSourceRange());
221  }
222 
223  if (FinalizeM) {
224  Transaction Trans(TA);
225  TA.remove(FinalizeM->getSourceRange());
226  }
227 
228  } else if (FinalizeM) {
229  if (isBodyEmpty(FinalizeM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
230  Transaction Trans(TA);
231  TA.remove(FinalizeM->getSourceRange());
232  } else {
233  Transaction Trans(TA);
234  TA.replaceText(FinalizeM->getSelectorStartLoc(), "finalize", "dealloc");
235  }
236  }
237  }
238 }
239 
241  EmptyStatementsRemover(pass).TraverseDecl(pass.Ctx.getTranslationUnitDecl());
242 
244 
245  for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i) {
246  Transaction Trans(pass.TA);
247  pass.TA.remove(pass.ARCMTMacroLocs[i]);
248  }
249 }
Defines the clang::ASTContext interface.
body_iterator body_end()
Definition: Stmt.h:1345
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
Smart pointer class that efficiently represents Objective-C method names.
Expr * getCond()
Definition: Stmt.h:2249
CompoundStmt * getSubStmt()
Definition: Expr.h:3938
Stmt - This represents one statement.
Definition: Stmt.h:66
IfStmt - This represents an if/then/else.
Definition: Stmt.h:1812
Defines the SourceManager interface.
static StringRef getARCMTMacroName()
Definition: Internals.h:172
bool hasLeadingEmptyMacro() const
Definition: Stmt.h:1289
SourceLocation findSemiAfterLocation(SourceLocation loc, ASTContext &Ctx, bool IsDecl=false)
&#39;Loc&#39; is the end of a statement range.
Definition: Transforms.cpp:129
bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS, int *RelativeOffset) const
Return true if both LHS and RHS are in the local source location address space or the loaded one...
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:138
Stmt * getThen()
Definition: Stmt.h:1899
long i
Definition: xmmintrin.h:1456
const Stmt * getSubStmt() const
Definition: StmtObjC.h:379
Stmt * getBody()
Definition: Stmt.h:2353
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
IdentifierTable & Idents
Definition: ASTContext.h:569
Selector getNullarySelector(IdentifierInfo *ID)
static bool isBodyEmpty(CompoundStmt *body, ASTContext &Ctx, std::vector< SourceLocation > &MacroLocs)
CompoundStmt * getCompoundBody()
Definition: DeclObjC.h:510
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1310
This represents one expression.
Definition: Expr.h:108
void removeEmptyStatementsAndDeallocFinalize(MigrationPass &pass)
SourceLocation getSelectorStartLoc() const
Definition: DeclObjC.h:288
Stmt * getBody()
Definition: Stmt.h:2261
Compare two source locations.
Expr * getCond()
Definition: Stmt.h:1887
const SourceManager & SM
Definition: Format.cpp:1572
DoStmt - This represents a &#39;do/while&#39; stmt.
Definition: Stmt.h:2328
SelectorTable & Selectors
Definition: ASTContext.h:570
Encodes a location in the source.
body_range body()
Definition: Stmt.h:1343
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
decl_iterator decls_begin() const
Definition: DeclBase.cpp:1378
Stmt * getElse()
Definition: Stmt.h:1908
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:182
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:3922
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: DeclObjC.h:284
NullStmt - This is the null statement ";": C99 6.8.3p3.
Definition: Stmt.h:1275
static void cleanupDeallocOrFinalize(MigrationPass &pass)
Dataflow Directional Tag Classes.
VarDecl * getConditionVariable()
Retrieve the variable declared in this "while" statement, if any.
Definition: Stmt.cpp:1032
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
bool hasSideEffects(Expr *E, ASTContext &Ctx)
Definition: Transforms.cpp:167
SourceLocation getSemiLoc() const
Definition: Stmt.h:1286
void replaceText(SourceLocation loc, StringRef text, StringRef replacementText)
body_iterator body_begin()
Definition: Stmt.h:1344
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext, providing only those that are of type SpecificDecl (or a class derived from it).
Definition: DeclBase.h:2048
bool isMacroID() const
bool body_empty() const
Definition: Stmt.h:1337
Represents Objective-C&#39;s collection statement.
Definition: StmtObjC.h:23
std::vector< SourceLocation > & ARCMTMacroLocs
Definition: Internals.h:152
SourceManager & getSourceManager()
Definition: ASTContext.h:665
Expr * getCond()
Definition: Stmt.h:2346
VarDecl * getConditionVariable()
Retrieve the variable declared in this "if" statement, if any.
Definition: Stmt.cpp:875
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1007
TransformActions & TA
Definition: Internals.h:150
void remove(SourceRange range)
WhileStmt - This represents a &#39;while&#39; stmt.
Definition: Stmt.h:2200
static bool isEmptyARCMTMacroStatement(NullStmt *S, std::vector< SourceLocation > &MacroLocs, ASTContext &Ctx)
Represents Objective-C&#39;s @autoreleasepool Statement.
Definition: StmtObjC.h:368
This class handles loading and caching of source files into memory.
decl_iterator decls_end() const
Definition: DeclBase.h:2030