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/Sema/Sema.h"
20 #include "clang/Sema/SemaDiagnostic.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/StringSwitch.h"
25 using namespace clang;
26 using namespace arcmt;
27 using namespace trans;
29 ASTTraverser::~ASTTraverser() { }
31 bool MigrationPass::CFBridgingFunctionsDefined() {
32 if (!EnableCFBridgeFns.hasValue())
33 EnableCFBridgeFns = SemaRef.isKnownName("CFBridgingRetain") &&
34 SemaRef.isKnownName("CFBridgingRelease");
35 return *EnableCFBridgeFns;
38 //===----------------------------------------------------------------------===//
40 //===----------------------------------------------------------------------===//
42 bool trans::canApplyWeak(ASTContext &Ctx, QualType type,
43 bool AllowOnUnknownClass) {
44 if (!Ctx.getLangOpts().ObjCARCWeak)
51 // iOS is always safe to use 'weak'.
52 if (Ctx.getTargetInfo().getTriple().isiOS())
53 AllowOnUnknownClass = true;
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())
70 bool trans::isPlusOneAssign(const BinaryOperator *E) {
71 if (E->getOpcode() != BO_Assign)
74 return isPlusOne(E->getRHS());
77 bool trans::isPlusOne(const Expr *E) {
80 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(E))
81 E = EWC->getSubExpr();
83 if (const ObjCMessageExpr *
84 ME = dyn_cast<ObjCMessageExpr>(E->IgnoreParenCasts()))
85 if (ME->getMethodFamily() == OMF_retain)
89 callE = dyn_cast<CallExpr>(E->IgnoreParenCasts())) {
90 if (const FunctionDecl *FD = callE->getDirectCallee()) {
91 if (FD->hasAttr<CFReturnsRetainedAttr>())
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) {
110 const ImplicitCastExpr *implCE = dyn_cast<ImplicitCastExpr>(E);
111 while (implCE && implCE->getCastKind() == CK_BitCast)
112 implCE = dyn_cast<ImplicitCastExpr>(implCE->getSubExpr());
114 if (implCE && implCE->getCastKind() == CK_ARCConsumeObject)
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.
124 SourceLocation 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);
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.
136 SourceLocation trans::findSemiAfterLocation(SourceLocation loc,
139 SourceManager &SM = Ctx.getSourceManager();
140 if (loc.isMacroID()) {
141 if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOpts(), &loc))
142 return SourceLocation();
144 loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx.getLangOpts());
146 // Break down the source location.
147 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
149 // Try to load the file buffer.
150 bool invalidTemp = false;
151 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
153 return SourceLocation();
155 const char *tokenBegin = file.data() + locInfo.second;
157 // Lex from the start of the given location.
158 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
160 file.begin(), tokenBegin, file.end());
162 lexer.LexFromRawLexer(tok);
163 if (tok.isNot(tok::semi)) {
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);
171 return tok.getLocation();
174 bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {
175 if (!E || !E->HasSideEffects(Ctx))
178 E = E->IgnoreParenCasts();
179 ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E);
182 switch (ME->getMethodFamily()) {
183 case OMF_autorelease:
187 switch (ME->getReceiverKind()) {
188 case ObjCMessageExpr::SuperInstance:
190 case ObjCMessageExpr::Instance:
191 return hasSideEffects(ME->getInstanceReceiver(), Ctx);
203 bool 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());
215 StringRef trans::getNilString(ASTContext &Ctx) {
216 if (Ctx.Idents.get("nil").hasMacroDefinition())
224 class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
227 ReferenceClear(ExprSet &refs) : Refs(refs) { }
228 bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; }
231 class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> {
236 ReferenceCollector(ValueDecl *D, ExprSet &refs)
237 : Dcl(D), Refs(refs) { }
239 bool VisitDeclRefExpr(DeclRefExpr *E) {
240 if (E->getDecl() == Dcl)
246 class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> {
250 RemovablesCollector(ExprSet &removables)
251 : Removables(removables) { }
253 bool shouldWalkTypesOfTypeLocs() const { return false; }
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) {
266 bool VisitCompoundStmt(CompoundStmt *S) {
267 for (auto *I : S->body())
272 bool VisitIfStmt(IfStmt *S) {
278 bool VisitWhileStmt(WhileStmt *S) {
283 bool VisitDoStmt(DoStmt *S) {
288 bool VisitForStmt(ForStmt *S) {
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);
307 } // end anonymous namespace
309 void trans::clearRefsIn(Stmt *S, ExprSet &refs) {
310 ReferenceClear(refs).TraverseStmt(S);
313 void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) {
314 ReferenceCollector(D, refs).TraverseStmt(S);
317 void trans::collectRemovables(Stmt *S, ExprSet &exprs) {
318 RemovablesCollector(exprs).TraverseStmt(S);
321 //===----------------------------------------------------------------------===//
323 //===----------------------------------------------------------------------===//
327 class ASTTransform : public RecursiveASTVisitor<ASTTransform> {
328 MigrationContext &MigrateCtx;
329 typedef RecursiveASTVisitor<ASTTransform> base;
332 ASTTransform(MigrationContext &MigrateCtx) : MigrateCtx(MigrateCtx) { }
334 bool shouldWalkTypesOfTypeLocs() const { return false; }
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);
343 return base::TraverseObjCImplementationDecl(D);
346 bool TraverseStmt(Stmt *rootS) {
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);
362 MigrationContext::~MigrationContext() {
363 for (traverser_iterator
364 I = traversers_begin(), E = traversers_end(); I != E; ++I)
368 bool 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();
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();
388 bool MigrationContext::rewritePropertyAttribute(StringRef fromAttr,
390 SourceLocation atLoc) {
391 if (atLoc.isMacroID())
394 SourceManager &SM = Pass.Ctx.getSourceManager();
396 // Break down the source location.
397 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(atLoc);
399 // Try to load the file buffer.
400 bool invalidTemp = false;
401 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
405 const char *tokenBegin = file.data() + locInfo.second;
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());
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")
418 lexer.LexFromRawLexer(tok);
419 if (tok.isNot(tok::l_paren)) return false;
421 Token BeforeTok = tok;
423 AfterTok.startToken();
424 SourceLocation AttrLoc;
426 lexer.LexFromRawLexer(tok);
427 if (tok.is(tok::r_paren))
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);
437 // We want to remove the attribute.
438 AttrLoc = tok.getLocation();
442 lexer.LexFromRawLexer(tok);
443 if (AttrLoc.isValid() && AfterTok.is(tok::unknown))
445 } while (tok.isNot(tok::comma) && tok.isNot(tok::r_paren));
446 if (tok.is(tok::r_paren))
448 if (AttrLoc.isInvalid())
450 lexer.LexFromRawLexer(tok);
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()));
461 Pass.TA.remove(SourceRange(BeforeTok.getLocation(), AttrLoc));
470 bool MigrationContext::addPropertyAttribute(StringRef attr,
471 SourceLocation atLoc) {
472 if (atLoc.isMacroID())
475 SourceManager &SM = Pass.Ctx.getSourceManager();
477 // Break down the source location.
478 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(atLoc);
480 // Try to load the file buffer.
481 bool invalidTemp = false;
482 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
486 const char *tokenBegin = file.data() + locInfo.second;
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());
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")
499 lexer.LexFromRawLexer(tok);
501 if (tok.isNot(tok::l_paren)) {
502 Pass.TA.insert(tok.getLocation(), std::string("(") + attr.str() + ") ");
506 lexer.LexFromRawLexer(tok);
507 if (tok.is(tok::r_paren)) {
508 Pass.TA.insert(tok.getLocation(), attr);
512 if (tok.isNot(tok::raw_identifier)) return false;
514 Pass.TA.insert(tok.getLocation(), std::string(attr) + ", ");
518 void MigrationContext::traverse(TranslationUnitDecl *TU) {
519 for (traverser_iterator
520 I = traversers_begin(), E = traversers_end(); I != E; ++I)
521 (*I)->traverseTU(*this);
523 ASTTransform(*this).TraverseDecl(TU);
526 static 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"));
533 typedef DeclContext::specific_decl_iterator<ObjCImplementationDecl>
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()) {
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();
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);
562 //===----------------------------------------------------------------------===//
563 // getAllTransformations.
564 //===----------------------------------------------------------------------===//
566 static void traverseAST(MigrationPass &pass) {
567 MigrationContext MigrateCtx(pass);
569 if (pass.isGCMigration()) {
570 MigrateCtx.addTraverser(new GCCollectableCallsTraverser);
571 MigrateCtx.addTraverser(new GCAttrsTraverser());
573 MigrateCtx.addTraverser(new PropertyRewriteTraverser());
574 MigrateCtx.addTraverser(new BlockObjCVariableTraverser());
575 MigrateCtx.addTraverser(new ProtectedScopeTraverser());
577 MigrateCtx.traverse(pass.Ctx.getTranslationUnitDecl());
580 static void independentTransforms(MigrationPass &pass) {
581 rewriteAutoreleasePool(pass);
582 removeRetainReleaseDeallocFinalize(pass);
583 rewriteUnusedInitDelegate(pass);
584 removeZeroOutPropsInDeallocFinalize(pass);
585 makeAssignARCSafe(pass);
586 rewriteUnbridgedCasts(pass);
591 std::vector<TransformFn> arcmt::getAllTransformations(
592 LangOptions::GCMode OrigGCMode,
593 bool NoFinalizeRemoval) {
594 std::vector<TransformFn> transforms;
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);