Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | 471c8b4 | 2012-07-04 20:19:54 +0000 | [diff] [blame] | 25 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 26 | #include "clang/Sema/SemaDiagnostic.h" |
| 27 | |
| 28 | using namespace clang; |
| 29 | using namespace arcmt; |
| 30 | using namespace trans; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
| 33 | |
| 34 | class UnusedInitRewriter : public RecursiveASTVisitor<UnusedInitRewriter> { |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 35 | Stmt *Body; |
| 36 | MigrationPass &Pass; |
| 37 | |
| 38 | ExprSet Removables; |
| 39 | |
| 40 | public: |
Argyrios Kyrtzidis | b1094a0 | 2011-06-23 21:21:33 +0000 | [diff] [blame] | 41 | UnusedInitRewriter(MigrationPass &pass) |
| 42 | : Body(0), Pass(pass) { } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 43 | |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 44 | void transformBody(Stmt *body, Decl *ParentD) { |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 45 | Body = body; |
| 46 | collectRemovables(body, Removables); |
| 47 | TraverseStmt(body); |
| 48 | } |
| 49 | |
| 50 | bool VisitObjCMessageExpr(ObjCMessageExpr *ME) { |
| 51 | if (ME->isDelegateInitCall() && |
| 52 | isRemovable(ME) && |
| 53 | Pass.TA.hasDiagnostic(diag::err_arc_unused_init_message, |
| 54 | ME->getExprLoc())) { |
| 55 | Transaction Trans(Pass.TA); |
| 56 | Pass.TA.clearDiagnostic(diag::err_arc_unused_init_message, |
| 57 | ME->getExprLoc()); |
Argyrios Kyrtzidis | d76e1cd | 2012-03-31 01:34:06 +0000 | [diff] [blame] | 58 | SourceRange ExprRange = ME->getSourceRange(); |
| 59 | Pass.TA.insert(ExprRange.getBegin(), "if (!(self = "); |
| 60 | std::string retStr = ")) return "; |
| 61 | retStr += getNilString(Pass.Ctx); |
| 62 | Pass.TA.insertAfterToken(ExprRange.getEnd(), retStr); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | bool isRemovable(Expr *E) const { |
| 69 | return Removables.count(E); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | } // anonymous namespace |
| 74 | |
| 75 | void trans::rewriteUnusedInitDelegate(MigrationPass &pass) { |
| 76 | BodyTransform<UnusedInitRewriter> trans(pass); |
| 77 | trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl()); |
| 78 | } |