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