1 //===--- Transforms.cpp - Transformations to ARC mode ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/Lex/Preprocessor.h"
20 #include "clang/Sema/Sema.h"
21 #include "clang/Sema/SemaDiagnostic.h"
22 #include "llvm/ADT/DenseSet.h"
23 #include "llvm/ADT/StringSwitch.h"
26 using namespace clang;
27 using namespace arcmt;
28 using namespace trans;
30 ASTTraverser::~ASTTraverser() { }
32 bool MigrationPass::CFBridgingFunctionsDefined() {
33 if (!EnableCFBridgeFns.hasValue())
34 EnableCFBridgeFns = SemaRef.isKnownName("CFBridgingRetain") &&
35 SemaRef.isKnownName("CFBridgingRelease");
36 return *EnableCFBridgeFns;
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
43 bool trans::canApplyWeak(ASTContext &Ctx, QualType type,
44 bool AllowOnUnknownClass) {
45 if (!Ctx.getLangOpts().ObjCARCWeak)
52 // iOS is always safe to use 'weak'.
53 if (Ctx.getTargetInfo().getTriple().isiOS())
54 AllowOnUnknownClass = true;
56 while (const PointerType *ptr = T->getAs<PointerType>())
57 T = ptr->getPointeeType();
58 if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
59 ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
60 if (!AllowOnUnknownClass && (!Class || Class->getName() == "NSObject"))
61 return false; // id/NSObject is not safe for weak.
62 if (!AllowOnUnknownClass && !Class->hasDefinition())
63 return false; // forward classes are not verifiable, therefore not safe.
64 if (Class && Class->isArcWeakrefUnavailable())
71 bool trans::isPlusOneAssign(const BinaryOperator *E) {
72 if (E->getOpcode() != BO_Assign)
75 return isPlusOne(E->getRHS());
78 bool trans::isPlusOne(const Expr *E) {
81 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(E))
82 E = EWC->getSubExpr();
84 if (const ObjCMessageExpr *
85 ME = dyn_cast<ObjCMessageExpr>(E->IgnoreParenCasts()))
86 if (ME->getMethodFamily() == OMF_retain)
90 callE = dyn_cast<CallExpr>(E->IgnoreParenCasts())) {
91 if (const FunctionDecl *FD = callE->getDirectCallee()) {
92 if (FD->hasAttr<CFReturnsRetainedAttr>())
96 FD->getIdentifier() &&
97 FD->getParent()->isTranslationUnit() &&
98 FD->isExternallyVisible() &&
99 ento::cocoa::isRefType(callE->getType(), "CF",
100 FD->getIdentifier()->getName())) {
101 StringRef fname = FD->getIdentifier()->getName();
102 if (fname.endswith("Retain") ||
103 fname.find("Create") != StringRef::npos ||
104 fname.find("Copy") != StringRef::npos) {
111 const ImplicitCastExpr *implCE = dyn_cast<ImplicitCastExpr>(E);
112 while (implCE && implCE->getCastKind() == CK_BitCast)
113 implCE = dyn_cast<ImplicitCastExpr>(implCE->getSubExpr());
115 if (implCE && implCE->getCastKind() == CK_ARCConsumeObject)
121 /// \brief 'Loc' is the end of a statement range. This returns the location
122 /// immediately after the semicolon following the statement.
123 /// If no semicolon is found or the location is inside a macro, the returned
124 /// source location will be invalid.
125 SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
126 ASTContext &Ctx, bool IsDecl) {
127 SourceLocation SemiLoc = findSemiAfterLocation(loc, Ctx, IsDecl);
128 if (SemiLoc.isInvalid())
129 return SourceLocation();
130 return SemiLoc.getLocWithOffset(1);
133 /// \brief \arg Loc is the end of a statement range. This returns the location
134 /// of the semicolon following the statement.
135 /// If no semicolon is found or the location is inside a macro, the returned
136 /// source location will be invalid.
137 SourceLocation trans::findSemiAfterLocation(SourceLocation loc,
140 SourceManager &SM = Ctx.getSourceManager();
141 if (loc.isMacroID()) {
142 if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOpts(), &loc))
143 return SourceLocation();
145 loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx.getLangOpts());
147 // Break down the source location.
148 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
150 // Try to load the file buffer.
151 bool invalidTemp = false;
152 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
154 return SourceLocation();
156 const char *tokenBegin = file.data() + locInfo.second;
158 // Lex from the start of the given location.
159 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
161 file.begin(), tokenBegin, file.end());
163 lexer.LexFromRawLexer(tok);
164 if (tok.isNot(tok::semi)) {
166 return SourceLocation();
167 // Declaration may be followed with other tokens; such as an __attribute,
168 // before ending with a semicolon.
169 return findSemiAfterLocation(tok.getLocation(), Ctx, /*IsDecl*/true);
172 return tok.getLocation();
175 bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {
176 if (!E || !E->HasSideEffects(Ctx))
179 E = E->IgnoreParenCasts();
180 ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E);
183 switch (ME->getMethodFamily()) {
184 case OMF_autorelease:
188 switch (ME->getReceiverKind()) {
189 case ObjCMessageExpr::SuperInstance:
191 case ObjCMessageExpr::Instance:
192 return hasSideEffects(ME->getInstanceReceiver(), Ctx);
204 bool trans::isGlobalVar(Expr *E) {
205 E = E->IgnoreParenCasts();
206 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
207 return DRE->getDecl()->getDeclContext()->isFileContext() &&
208 DRE->getDecl()->isExternallyVisible();
209 if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E))
210 return isGlobalVar(condOp->getTrueExpr()) &&
211 isGlobalVar(condOp->getFalseExpr());
216 StringRef trans::getNilString(MigrationPass &Pass) {
217 return Pass.SemaRef.PP.isMacroDefined("nil") ? "nil" : "0";
222 class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
225 ReferenceClear(ExprSet &refs) : Refs(refs) { }
226 bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; }
229 class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> {
234 ReferenceCollector(ValueDecl *D, ExprSet &refs)
235 : Dcl(D), Refs(refs) { }
237 bool VisitDeclRefExpr(DeclRefExpr *E) {
238 if (E->getDecl() == Dcl)
244 class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> {
248 RemovablesCollector(ExprSet &removables)
249 : Removables(removables) { }
251 bool shouldWalkTypesOfTypeLocs() const { return false; }
253 bool TraverseStmtExpr(StmtExpr *E) {
254 CompoundStmt *S = E->getSubStmt();
255 for (CompoundStmt::body_iterator
256 I = S->body_begin(), E = S->body_end(); I != E; ++I) {
264 bool VisitCompoundStmt(CompoundStmt *S) {
265 for (auto *I : S->body())
270 bool VisitIfStmt(IfStmt *S) {
276 bool VisitWhileStmt(WhileStmt *S) {
281 bool VisitDoStmt(DoStmt *S) {
286 bool VisitForStmt(ForStmt *S) {
297 while (LabelStmt *Label = dyn_cast<LabelStmt>(S))
298 S = Label->getSubStmt();
299 S = S->IgnoreImplicit();
300 if (Expr *E = dyn_cast<Expr>(S))
301 Removables.insert(E);
305 } // end anonymous namespace
307 void trans::clearRefsIn(Stmt *S, ExprSet &refs) {
308 ReferenceClear(refs).TraverseStmt(S);
311 void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) {
312 ReferenceCollector(D, refs).TraverseStmt(S);
315 void trans::collectRemovables(Stmt *S, ExprSet &exprs) {
316 RemovablesCollector(exprs).TraverseStmt(S);
319 //===----------------------------------------------------------------------===//
321 //===----------------------------------------------------------------------===//
325 class ASTTransform : public RecursiveASTVisitor<ASTTransform> {
326 MigrationContext &MigrateCtx;
327 typedef RecursiveASTVisitor<ASTTransform> base;
330 ASTTransform(MigrationContext &MigrateCtx) : MigrateCtx(MigrateCtx) { }
332 bool shouldWalkTypesOfTypeLocs() const { return false; }
334 bool TraverseObjCImplementationDecl(ObjCImplementationDecl *D) {
335 ObjCImplementationContext ImplCtx(MigrateCtx, D);
336 for (MigrationContext::traverser_iterator
337 I = MigrateCtx.traversers_begin(),
338 E = MigrateCtx.traversers_end(); I != E; ++I)
339 (*I)->traverseObjCImplementation(ImplCtx);
341 return base::TraverseObjCImplementationDecl(D);
344 bool TraverseStmt(Stmt *rootS) {
348 BodyContext BodyCtx(MigrateCtx, rootS);
349 for (MigrationContext::traverser_iterator
350 I = MigrateCtx.traversers_begin(),
351 E = MigrateCtx.traversers_end(); I != E; ++I)
352 (*I)->traverseBody(BodyCtx);
360 MigrationContext::~MigrationContext() {
361 for (traverser_iterator
362 I = traversers_begin(), E = traversers_end(); I != E; ++I)
366 bool MigrationContext::isGCOwnedNonObjC(QualType T) {
367 while (!T.isNull()) {
368 if (const AttributedType *AttrT = T->getAs<AttributedType>()) {
369 if (AttrT->getAttrKind() == AttributedType::attr_objc_ownership)
370 return !AttrT->getModifiedType()->isObjCRetainableType();
373 if (T->isArrayType())
374 T = Pass.Ctx.getBaseElementType(T);
375 else if (const PointerType *PT = T->getAs<PointerType>())
376 T = PT->getPointeeType();
377 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
378 T = RT->getPointeeType();
386 bool MigrationContext::rewritePropertyAttribute(StringRef fromAttr,
388 SourceLocation atLoc) {
389 if (atLoc.isMacroID())
392 SourceManager &SM = Pass.Ctx.getSourceManager();
394 // Break down the source location.
395 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(atLoc);
397 // Try to load the file buffer.
398 bool invalidTemp = false;
399 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
403 const char *tokenBegin = file.data() + locInfo.second;
405 // Lex from the start of the given location.
406 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
407 Pass.Ctx.getLangOpts(),
408 file.begin(), tokenBegin, file.end());
410 lexer.LexFromRawLexer(tok);
411 if (tok.isNot(tok::at)) return false;
412 lexer.LexFromRawLexer(tok);
413 if (tok.isNot(tok::raw_identifier)) return false;
414 if (tok.getRawIdentifier() != "property")
416 lexer.LexFromRawLexer(tok);
417 if (tok.isNot(tok::l_paren)) return false;
419 Token BeforeTok = tok;
421 AfterTok.startToken();
422 SourceLocation AttrLoc;
424 lexer.LexFromRawLexer(tok);
425 if (tok.is(tok::r_paren))
429 if (tok.isNot(tok::raw_identifier)) return false;
430 if (tok.getRawIdentifier() == fromAttr) {
431 if (!toAttr.empty()) {
432 Pass.TA.replaceText(tok.getLocation(), fromAttr, toAttr);
435 // We want to remove the attribute.
436 AttrLoc = tok.getLocation();
440 lexer.LexFromRawLexer(tok);
441 if (AttrLoc.isValid() && AfterTok.is(tok::unknown))
443 } while (tok.isNot(tok::comma) && tok.isNot(tok::r_paren));
444 if (tok.is(tok::r_paren))
446 if (AttrLoc.isInvalid())
448 lexer.LexFromRawLexer(tok);
451 if (toAttr.empty() && AttrLoc.isValid() && AfterTok.isNot(tok::unknown)) {
452 // We want to remove the attribute.
453 if (BeforeTok.is(tok::l_paren) && AfterTok.is(tok::r_paren)) {
454 Pass.TA.remove(SourceRange(BeforeTok.getLocation(),
455 AfterTok.getLocation()));
456 } else if (BeforeTok.is(tok::l_paren) && AfterTok.is(tok::comma)) {
457 Pass.TA.remove(SourceRange(AttrLoc, AfterTok.getLocation()));
459 Pass.TA.remove(SourceRange(BeforeTok.getLocation(), AttrLoc));
468 bool MigrationContext::addPropertyAttribute(StringRef attr,
469 SourceLocation atLoc) {
470 if (atLoc.isMacroID())
473 SourceManager &SM = Pass.Ctx.getSourceManager();
475 // Break down the source location.
476 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(atLoc);
478 // Try to load the file buffer.
479 bool invalidTemp = false;
480 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
484 const char *tokenBegin = file.data() + locInfo.second;
486 // Lex from the start of the given location.
487 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
488 Pass.Ctx.getLangOpts(),
489 file.begin(), tokenBegin, file.end());
491 lexer.LexFromRawLexer(tok);
492 if (tok.isNot(tok::at)) return false;
493 lexer.LexFromRawLexer(tok);
494 if (tok.isNot(tok::raw_identifier)) return false;
495 if (tok.getRawIdentifier() != "property")
497 lexer.LexFromRawLexer(tok);
499 if (tok.isNot(tok::l_paren)) {
500 Pass.TA.insert(tok.getLocation(), std::string("(") + attr.str() + ") ");
504 lexer.LexFromRawLexer(tok);
505 if (tok.is(tok::r_paren)) {
506 Pass.TA.insert(tok.getLocation(), attr);
510 if (tok.isNot(tok::raw_identifier)) return false;
512 Pass.TA.insert(tok.getLocation(), std::string(attr) + ", ");
516 void MigrationContext::traverse(TranslationUnitDecl *TU) {
517 for (traverser_iterator
518 I = traversers_begin(), E = traversers_end(); I != E; ++I)
519 (*I)->traverseTU(*this);
521 ASTTransform(*this).TraverseDecl(TU);
524 static void GCRewriteFinalize(MigrationPass &pass) {
525 ASTContext &Ctx = pass.Ctx;
526 TransformActions &TA = pass.TA;
527 DeclContext *DC = Ctx.getTranslationUnitDecl();
528 Selector FinalizeSel =
529 Ctx.Selectors.getNullarySelector(&pass.Ctx.Idents.get("finalize"));
531 typedef DeclContext::specific_decl_iterator<ObjCImplementationDecl>
533 for (impl_iterator I = impl_iterator(DC->decls_begin()),
534 E = impl_iterator(DC->decls_end()); I != E; ++I) {
535 for (const auto *MD : I->instance_methods()) {
539 if (MD->isInstanceMethod() && MD->getSelector() == FinalizeSel) {
540 const ObjCMethodDecl *FinalizeM = MD;
541 Transaction Trans(TA);
542 TA.insert(FinalizeM->getSourceRange().getBegin(),
543 "#if !__has_feature(objc_arc)\n");
544 CharSourceRange::getTokenRange(FinalizeM->getSourceRange());
545 const SourceManager &SM = pass.Ctx.getSourceManager();
546 const LangOptions &LangOpts = pass.Ctx.getLangOpts();
548 std::string str = "\n#endif\n";
549 str += Lexer::getSourceText(
550 CharSourceRange::getTokenRange(FinalizeM->getSourceRange()),
551 SM, LangOpts, &Invalid);
552 TA.insertAfterToken(FinalizeM->getSourceRange().getEnd(), str);
560 //===----------------------------------------------------------------------===//
561 // getAllTransformations.
562 //===----------------------------------------------------------------------===//
564 static void traverseAST(MigrationPass &pass) {
565 MigrationContext MigrateCtx(pass);
567 if (pass.isGCMigration()) {
568 MigrateCtx.addTraverser(new GCCollectableCallsTraverser);
569 MigrateCtx.addTraverser(new GCAttrsTraverser());
571 MigrateCtx.addTraverser(new PropertyRewriteTraverser());
572 MigrateCtx.addTraverser(new BlockObjCVariableTraverser());
573 MigrateCtx.addTraverser(new ProtectedScopeTraverser());
575 MigrateCtx.traverse(pass.Ctx.getTranslationUnitDecl());
578 static void independentTransforms(MigrationPass &pass) {
579 rewriteAutoreleasePool(pass);
580 removeRetainReleaseDeallocFinalize(pass);
581 rewriteUnusedInitDelegate(pass);
582 removeZeroOutPropsInDeallocFinalize(pass);
583 makeAssignARCSafe(pass);
584 rewriteUnbridgedCasts(pass);
589 std::vector<TransformFn> arcmt::getAllTransformations(
590 LangOptions::GCMode OrigGCMode,
591 bool NoFinalizeRemoval) {
592 std::vector<TransformFn> transforms;
594 if (OrigGCMode == LangOptions::GCOnly && NoFinalizeRemoval)
595 transforms.push_back(GCRewriteFinalize);
596 transforms.push_back(independentTransforms);
597 // This depends on previous transformations removing various expressions.
598 transforms.push_back(removeEmptyStatementsAndDeallocFinalize);