1//===--- Transforms.cpp - Transformations to ARC mode ---------------------===//
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#include "Transforms.h"
11#include "Internals.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/RecursiveASTVisitor.h"
14#include "clang/AST/StmtVisitor.h"
15#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Basic/TargetInfo.h"
18#include "clang/Lex/Lexer.h"
19#include "clang/Sema/Sema.h"
20#include "clang/Sema/SemaDiagnostic.h"
21#include "llvm/ADT/DenseSet.h"
22#include "llvm/ADT/StringSwitch.h"
23#include <map>
24
25using namespace clang;
26using namespace arcmt;
27using namespace trans;
28
29ASTTraverser::~ASTTraverser() { }
30
31bool MigrationPass::CFBridgingFunctionsDefined() {
32  if (!EnableCFBridgeFns.hasValue())
33    EnableCFBridgeFns = SemaRef.isKnownName("CFBridgingRetain") &&
34                        SemaRef.isKnownName("CFBridgingRelease");
35  return *EnableCFBridgeFns;
36}
37
38//===----------------------------------------------------------------------===//
39// Helpers.
40//===----------------------------------------------------------------------===//
41
42bool trans::canApplyWeak(ASTContext &Ctx, QualType type,
43                         bool AllowOnUnknownClass) {
44  if (!Ctx.getLangOpts().ObjCARCWeak)
45    return false;
46
47  QualType T = type;
48  if (T.isNull())
49    return false;
50
51  // iOS is always safe to use 'weak'.
52  if (Ctx.getTargetInfo().getTriple().isiOS())
53    AllowOnUnknownClass = true;
54
55  while (const PointerType *ptr = T->getAs<PointerType>())
56    T = ptr->getPointeeType();
57  if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
58    ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
59    if (!AllowOnUnknownClass && (!Class || Class->getName() == "NSObject"))
60      return false; // id/NSObject is not safe for weak.
61    if (!AllowOnUnknownClass && !Class->hasDefinition())
62      return false; // forward classes are not verifiable, therefore not safe.
63    if (Class && Class->isArcWeakrefUnavailable())
64      return false;
65  }
66
67  return true;
68}
69
70bool trans::isPlusOneAssign(const BinaryOperator *E) {
71  if (E->getOpcode() != BO_Assign)
72    return false;
73
74  return isPlusOne(E->getRHS());
75}
76
77bool trans::isPlusOne(const Expr *E) {
78  if (!E)
79    return false;
80  if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(E))
81    E = EWC->getSubExpr();
82
83  if (const ObjCMessageExpr *
84        ME = dyn_cast<ObjCMessageExpr>(E->IgnoreParenCasts()))
85    if (ME->getMethodFamily() == OMF_retain)
86      return true;
87
88  if (const CallExpr *
89        callE = dyn_cast<CallExpr>(E->IgnoreParenCasts())) {
90    if (const FunctionDecl *FD = callE->getDirectCallee()) {
91      if (FD->hasAttr<CFReturnsRetainedAttr>())
92        return true;
93
94      if (FD->isGlobal() &&
95          FD->getIdentifier() &&
96          FD->getParent()->isTranslationUnit() &&
97          FD->isExternallyVisible() &&
98          ento::cocoa::isRefType(callE->getType(), "CF",
99                                 FD->getIdentifier()->getName())) {
100        StringRef fname = FD->getIdentifier()->getName();
101        if (fname.endswith("Retain") ||
102            fname.find("Create") != StringRef::npos ||
103            fname.find("Copy") != StringRef::npos) {
104          return true;
105        }
106      }
107    }
108  }
109
110  const ImplicitCastExpr *implCE = dyn_cast<ImplicitCastExpr>(E);
111  while (implCE && implCE->getCastKind() ==  CK_BitCast)
112    implCE = dyn_cast<ImplicitCastExpr>(implCE->getSubExpr());
113
114  if (implCE && implCE->getCastKind() == CK_ARCConsumeObject)
115    return true;
116
117  return false;
118}
119
120/// \brief 'Loc' is the end of a statement range. This returns the location
121/// immediately after the semicolon following the statement.
122/// If no semicolon is found or the location is inside a macro, the returned
123/// source location will be invalid.
124SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
125                                            ASTContext &Ctx, bool IsDecl) {
126  SourceLocation SemiLoc = findSemiAfterLocation(loc, Ctx, IsDecl);
127  if (SemiLoc.isInvalid())
128    return SourceLocation();
129  return SemiLoc.getLocWithOffset(1);
130}
131
132/// \brief \arg Loc is the end of a statement range. This returns the location
133/// of the semicolon following the statement.
134/// If no semicolon is found or the location is inside a macro, the returned
135/// source location will be invalid.
136SourceLocation trans::findSemiAfterLocation(SourceLocation loc,
137                                            ASTContext &Ctx,
138                                            bool IsDecl) {
139  SourceManager &SM = Ctx.getSourceManager();
140  if (loc.isMacroID()) {
141    if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOpts(), &loc))
142      return SourceLocation();
143  }
144  loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx.getLangOpts());
145
146  // Break down the source location.
147  std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
148
149  // Try to load the file buffer.
150  bool invalidTemp = false;
151  StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
152  if (invalidTemp)
153    return SourceLocation();
154
155  const char *tokenBegin = file.data() + locInfo.second;
156
157  // Lex from the start of the given location.
158  Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
159              Ctx.getLangOpts(),
160              file.begin(), tokenBegin, file.end());
161  Token tok;
162  lexer.LexFromRawLexer(tok);
163  if (tok.isNot(tok::semi)) {
164    if (!IsDecl)
165      return SourceLocation();
166    // Declaration may be followed with other tokens; such as an __attribute,
167    // before ending with a semicolon.
168    return findSemiAfterLocation(tok.getLocation(), Ctx, /*IsDecl*/true);
169  }
170
171  return tok.getLocation();
172}
173
174bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {
175  if (!E || !E->HasSideEffects(Ctx))
176    return false;
177
178  E = E->IgnoreParenCasts();
179  ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E);
180  if (!ME)
181    return true;
182  switch (ME->getMethodFamily()) {
183  case OMF_autorelease:
184  case OMF_dealloc:
185  case OMF_release:
186  case OMF_retain:
187    switch (ME->getReceiverKind()) {
188    case ObjCMessageExpr::SuperInstance:
189      return false;
190    case ObjCMessageExpr::Instance:
191      return hasSideEffects(ME->getInstanceReceiver(), Ctx);
192    default:
193      break;
194    }
195    break;
196  default:
197    break;
198  }
199
200  return true;
201}
202
203bool trans::isGlobalVar(Expr *E) {
204  E = E->IgnoreParenCasts();
205  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
206    return DRE->getDecl()->getDeclContext()->isFileContext() &&
207           DRE->getDecl()->isExternallyVisible();
208  if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E))
209    return isGlobalVar(condOp->getTrueExpr()) &&
210           isGlobalVar(condOp->getFalseExpr());
211
212  return false;
213}
214
215StringRef trans::getNilString(ASTContext &Ctx) {
216  if (Ctx.Idents.get("nil").hasMacroDefinition())
217    return "nil";
218  else
219    return "0";
220}
221
222namespace {
223
224class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
225  ExprSet &Refs;
226public:
227  ReferenceClear(ExprSet &refs) : Refs(refs) { }
228  bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; }
229};
230
231class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> {
232  ValueDecl *Dcl;
233  ExprSet &Refs;
234
235public:
236  ReferenceCollector(ValueDecl *D, ExprSet &refs)
237    : Dcl(D), Refs(refs) { }
238
239  bool VisitDeclRefExpr(DeclRefExpr *E) {
240    if (E->getDecl() == Dcl)
241      Refs.insert(E);
242    return true;
243  }
244};
245
246class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> {
247  ExprSet &Removables;
248
249public:
250  RemovablesCollector(ExprSet &removables)
251  : Removables(removables) { }
252
253  bool shouldWalkTypesOfTypeLocs() const { return false; }
254
255  bool TraverseStmtExpr(StmtExpr *E) {
256    CompoundStmt *S = E->getSubStmt();
257    for (CompoundStmt::body_iterator
258        I = S->body_begin(), E = S->body_end(); I != E; ++I) {
259      if (I != E - 1)
260        mark(*I);
261      TraverseStmt(*I);
262    }
263    return true;
264  }
265
266  bool VisitCompoundStmt(CompoundStmt *S) {
267    for (auto *I : S->body())
268      mark(I);
269    return true;
270  }
271
272  bool VisitIfStmt(IfStmt *S) {
273    mark(S->getThen());
274    mark(S->getElse());
275    return true;
276  }
277
278  bool VisitWhileStmt(WhileStmt *S) {
279    mark(S->getBody());
280    return true;
281  }
282
283  bool VisitDoStmt(DoStmt *S) {
284    mark(S->getBody());
285    return true;
286  }
287
288  bool VisitForStmt(ForStmt *S) {
289    mark(S->getInit());
290    mark(S->getInc());
291    mark(S->getBody());
292    return true;
293  }
294
295private:
296  void mark(Stmt *S) {
297    if (!S) return;
298
299    while (LabelStmt *Label = dyn_cast<LabelStmt>(S))
300      S = Label->getSubStmt();
301    S = S->IgnoreImplicit();
302    if (Expr *E = dyn_cast<Expr>(S))
303      Removables.insert(E);
304  }
305};
306
307} // end anonymous namespace
308
309void trans::clearRefsIn(Stmt *S, ExprSet &refs) {
310  ReferenceClear(refs).TraverseStmt(S);
311}
312
313void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) {
314  ReferenceCollector(D, refs).TraverseStmt(S);
315}
316
317void trans::collectRemovables(Stmt *S, ExprSet &exprs) {
318  RemovablesCollector(exprs).TraverseStmt(S);
319}
320
321//===----------------------------------------------------------------------===//
322// MigrationContext
323//===----------------------------------------------------------------------===//
324
325namespace {
326
327class ASTTransform : public RecursiveASTVisitor<ASTTransform> {
328  MigrationContext &MigrateCtx;
329  typedef RecursiveASTVisitor<ASTTransform> base;
330
331public:
332  ASTTransform(MigrationContext &MigrateCtx) : MigrateCtx(MigrateCtx) { }
333
334  bool shouldWalkTypesOfTypeLocs() const { return false; }
335
336  bool TraverseObjCImplementationDecl(ObjCImplementationDecl *D) {
337    ObjCImplementationContext ImplCtx(MigrateCtx, D);
338    for (MigrationContext::traverser_iterator
339           I = MigrateCtx.traversers_begin(),
340           E = MigrateCtx.traversers_end(); I != E; ++I)
341      (*I)->traverseObjCImplementation(ImplCtx);
342
343    return base::TraverseObjCImplementationDecl(D);
344  }
345
346  bool TraverseStmt(Stmt *rootS) {
347    if (!rootS)
348      return true;
349
350    BodyContext BodyCtx(MigrateCtx, rootS);
351    for (MigrationContext::traverser_iterator
352           I = MigrateCtx.traversers_begin(),
353           E = MigrateCtx.traversers_end(); I != E; ++I)
354      (*I)->traverseBody(BodyCtx);
355
356    return true;
357  }
358};
359
360}
361
362MigrationContext::~MigrationContext() {
363  for (traverser_iterator
364         I = traversers_begin(), E = traversers_end(); I != E; ++I)
365    delete *I;
366}
367
368bool MigrationContext::isGCOwnedNonObjC(QualType T) {
369  while (!T.isNull()) {
370    if (const AttributedType *AttrT = T->getAs<AttributedType>()) {
371      if (AttrT->getAttrKind() == AttributedType::attr_objc_ownership)
372        return !AttrT->getModifiedType()->isObjCRetainableType();
373    }
374
375    if (T->isArrayType())
376      T = Pass.Ctx.getBaseElementType(T);
377    else if (const PointerType *PT = T->getAs<PointerType>())
378      T = PT->getPointeeType();
379    else if (const ReferenceType *RT = T->getAs<ReferenceType>())
380      T = RT->getPointeeType();
381    else
382      break;
383  }
384
385  return false;
386}
387
388bool MigrationContext::rewritePropertyAttribute(StringRef fromAttr,
389                                                StringRef toAttr,
390                                                SourceLocation atLoc) {
391  if (atLoc.isMacroID())
392    return false;
393
394  SourceManager &SM = Pass.Ctx.getSourceManager();
395
396  // Break down the source location.
397  std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(atLoc);
398
399  // Try to load the file buffer.
400  bool invalidTemp = false;
401  StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
402  if (invalidTemp)
403    return false;
404
405  const char *tokenBegin = file.data() + locInfo.second;
406
407  // Lex from the start of the given location.
408  Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
409              Pass.Ctx.getLangOpts(),
410              file.begin(), tokenBegin, file.end());
411  Token tok;
412  lexer.LexFromRawLexer(tok);
413  if (tok.isNot(tok::at)) return false;
414  lexer.LexFromRawLexer(tok);
415  if (tok.isNot(tok::raw_identifier)) return false;
416  if (tok.getRawIdentifier() != "property")
417    return false;
418  lexer.LexFromRawLexer(tok);
419  if (tok.isNot(tok::l_paren)) return false;
420
421  Token BeforeTok = tok;
422  Token AfterTok;
423  AfterTok.startToken();
424  SourceLocation AttrLoc;
425
426  lexer.LexFromRawLexer(tok);
427  if (tok.is(tok::r_paren))
428    return false;
429
430  while (1) {
431    if (tok.isNot(tok::raw_identifier)) return false;
432    if (tok.getRawIdentifier() == fromAttr) {
433      if (!toAttr.empty()) {
434        Pass.TA.replaceText(tok.getLocation(), fromAttr, toAttr);
435        return true;
436      }
437      // We want to remove the attribute.
438      AttrLoc = tok.getLocation();
439    }
440
441    do {
442      lexer.LexFromRawLexer(tok);
443      if (AttrLoc.isValid() && AfterTok.is(tok::unknown))
444        AfterTok = tok;
445    } while (tok.isNot(tok::comma) && tok.isNot(tok::r_paren));
446    if (tok.is(tok::r_paren))
447      break;
448    if (AttrLoc.isInvalid())
449      BeforeTok = tok;
450    lexer.LexFromRawLexer(tok);
451  }
452
453  if (toAttr.empty() && AttrLoc.isValid() && AfterTok.isNot(tok::unknown)) {
454    // We want to remove the attribute.
455    if (BeforeTok.is(tok::l_paren) && AfterTok.is(tok::r_paren)) {
456      Pass.TA.remove(SourceRange(BeforeTok.getLocation(),
457                                 AfterTok.getLocation()));
458    } else if (BeforeTok.is(tok::l_paren) && AfterTok.is(tok::comma)) {
459      Pass.TA.remove(SourceRange(AttrLoc, AfterTok.getLocation()));
460    } else {
461      Pass.TA.remove(SourceRange(BeforeTok.getLocation(), AttrLoc));
462    }
463
464    return true;
465  }
466
467  return false;
468}
469
470bool MigrationContext::addPropertyAttribute(StringRef attr,
471                                            SourceLocation atLoc) {
472  if (atLoc.isMacroID())
473    return false;
474
475  SourceManager &SM = Pass.Ctx.getSourceManager();
476
477  // Break down the source location.
478  std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(atLoc);
479
480  // Try to load the file buffer.
481  bool invalidTemp = false;
482  StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
483  if (invalidTemp)
484    return false;
485
486  const char *tokenBegin = file.data() + locInfo.second;
487
488  // Lex from the start of the given location.
489  Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
490              Pass.Ctx.getLangOpts(),
491              file.begin(), tokenBegin, file.end());
492  Token tok;
493  lexer.LexFromRawLexer(tok);
494  if (tok.isNot(tok::at)) return false;
495  lexer.LexFromRawLexer(tok);
496  if (tok.isNot(tok::raw_identifier)) return false;
497  if (tok.getRawIdentifier() != "property")
498    return false;
499  lexer.LexFromRawLexer(tok);
500
501  if (tok.isNot(tok::l_paren)) {
502    Pass.TA.insert(tok.getLocation(), std::string("(") + attr.str() + ") ");
503    return true;
504  }
505
506  lexer.LexFromRawLexer(tok);
507  if (tok.is(tok::r_paren)) {
508    Pass.TA.insert(tok.getLocation(), attr);
509    return true;
510  }
511
512  if (tok.isNot(tok::raw_identifier)) return false;
513
514  Pass.TA.insert(tok.getLocation(), std::string(attr) + ", ");
515  return true;
516}
517
518void MigrationContext::traverse(TranslationUnitDecl *TU) {
519  for (traverser_iterator
520         I = traversers_begin(), E = traversers_end(); I != E; ++I)
521    (*I)->traverseTU(*this);
522
523  ASTTransform(*this).TraverseDecl(TU);
524}
525
526static void GCRewriteFinalize(MigrationPass &pass) {
527  ASTContext &Ctx = pass.Ctx;
528  TransformActions &TA = pass.TA;
529  DeclContext *DC = Ctx.getTranslationUnitDecl();
530  Selector FinalizeSel =
531   Ctx.Selectors.getNullarySelector(&pass.Ctx.Idents.get("finalize"));
532
533  typedef DeclContext::specific_decl_iterator<ObjCImplementationDecl>
534  impl_iterator;
535  for (impl_iterator I = impl_iterator(DC->decls_begin()),
536       E = impl_iterator(DC->decls_end()); I != E; ++I) {
537    for (const auto *MD : I->instance_methods()) {
538      if (!MD->hasBody())
539        continue;
540
541      if (MD->isInstanceMethod() && MD->getSelector() == FinalizeSel) {
542        const ObjCMethodDecl *FinalizeM = MD;
543        Transaction Trans(TA);
544        TA.insert(FinalizeM->getSourceRange().getBegin(),
545                  "#if !__has_feature(objc_arc)\n");
546        CharSourceRange::getTokenRange(FinalizeM->getSourceRange());
547        const SourceManager &SM = pass.Ctx.getSourceManager();
548        const LangOptions &LangOpts = pass.Ctx.getLangOpts();
549        bool Invalid;
550        std::string str = "\n#endif\n";
551        str += Lexer::getSourceText(
552                  CharSourceRange::getTokenRange(FinalizeM->getSourceRange()),
553                                    SM, LangOpts, &Invalid);
554        TA.insertAfterToken(FinalizeM->getSourceRange().getEnd(), str);
555
556        break;
557      }
558    }
559  }
560}
561
562//===----------------------------------------------------------------------===//
563// getAllTransformations.
564//===----------------------------------------------------------------------===//
565
566static void traverseAST(MigrationPass &pass) {
567  MigrationContext MigrateCtx(pass);
568
569  if (pass.isGCMigration()) {
570    MigrateCtx.addTraverser(new GCCollectableCallsTraverser);
571    MigrateCtx.addTraverser(new GCAttrsTraverser());
572  }
573  MigrateCtx.addTraverser(new PropertyRewriteTraverser());
574  MigrateCtx.addTraverser(new BlockObjCVariableTraverser());
575  MigrateCtx.addTraverser(new ProtectedScopeTraverser());
576
577  MigrateCtx.traverse(pass.Ctx.getTranslationUnitDecl());
578}
579
580static void independentTransforms(MigrationPass &pass) {
581  rewriteAutoreleasePool(pass);
582  removeRetainReleaseDeallocFinalize(pass);
583  rewriteUnusedInitDelegate(pass);
584  removeZeroOutPropsInDeallocFinalize(pass);
585  makeAssignARCSafe(pass);
586  rewriteUnbridgedCasts(pass);
587  checkAPIUses(pass);
588  traverseAST(pass);
589}
590
591std::vector<TransformFn> arcmt::getAllTransformations(
592                                               LangOptions::GCMode OrigGCMode,
593                                               bool NoFinalizeRemoval) {
594  std::vector<TransformFn> transforms;
595
596  if (OrigGCMode ==  LangOptions::GCOnly && NoFinalizeRemoval)
597    transforms.push_back(GCRewriteFinalize);
598  transforms.push_back(independentTransforms);
599  // This depends on previous transformations removing various expressions.
600  transforms.push_back(removeEmptyStatementsAndDeallocFinalize);
601
602  return transforms;
603}
604