1 //===--- TransUnusedInitDelegate.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 // Transformations: 10 //===----------------------------------------------------------------------===// 11 // 12 // rewriteUnusedInitDelegate: 13 // 14 // Rewrites an unused result of calling a delegate initialization, to assigning 15 // the result to self. 16 // e.g 17 // [self init]; 18 // ----> 19 // self = [self init]; 20 // 21 //===----------------------------------------------------------------------===// 22 23 #include "Transforms.h" 24 #include "Internals.h" 25 #include "clang/Sema/SemaDiagnostic.h" 26 27 using namespace clang; 28 using namespace arcmt; 29 using namespace trans; 30 31 namespace { 32 33 class UnusedInitRewriter : public RecursiveASTVisitor<UnusedInitRewriter> { 34 Stmt *Body; 35 MigrationPass &Pass; 36 37 ExprSet Removables; 38 39 public: 40 UnusedInitRewriter(MigrationPass &pass) 41 : Body(0), Pass(pass) { } 42 43 void transformBody(Stmt *body) { 44 Body = body; 45 collectRemovables(body, Removables); 46 TraverseStmt(body); 47 } 48 49 bool VisitObjCMessageExpr(ObjCMessageExpr *ME) { 50 if (ME->isDelegateInitCall() && 51 isRemovable(ME) && 52 Pass.TA.hasDiagnostic(diag::err_arc_unused_init_message, 53 ME->getExprLoc())) { 54 Transaction Trans(Pass.TA); 55 Pass.TA.clearDiagnostic(diag::err_arc_unused_init_message, 56 ME->getExprLoc()); 57 Pass.TA.insert(ME->getExprLoc(), "self = "); 58 } 59 return true; 60 } 61 62 private: 63 bool isRemovable(Expr *E) const { 64 return Removables.count(E); 65 } 66 }; 67 68 } // anonymous namespace 69 70 void trans::rewriteUnusedInitDelegate(MigrationPass &pass) { 71 BodyTransform<UnusedInitRewriter> trans(pass); 72 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl()); 73 } 74