1 //===--- TransUnusedInitDelegate.cpp - Tranformations 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 //===----------------------------------------------------------------------===//
12 // rewriteUnusedInitDelegate:
14 // Rewrites an unused result of calling a delegate initialization, to assigning
15 // the result to self.
19 // self = [self init];
21 //===----------------------------------------------------------------------===//
23 #include "Transforms.h"
24 #include "Internals.h"
25 #include "clang/Sema/SemaDiagnostic.h"
27 using namespace clang;
28 using namespace arcmt;
29 using namespace trans;
33 class UnusedInitRewriter : public RecursiveASTVisitor<UnusedInitRewriter> {
40 UnusedInitRewriter(MigrationPass &pass)
41 : Body(0), Pass(pass) { }
43 void transformBody(Stmt *body) {
45 collectRemovables(body, Removables);
49 bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {
50 if (ME->isDelegateInitCall() &&
52 Pass.TA.hasDiagnostic(diag::err_arc_unused_init_message,
54 Transaction Trans(Pass.TA);
55 Pass.TA.clearDiagnostic(diag::err_arc_unused_init_message,
57 SourceRange ExprRange = ME->getSourceRange();
58 Pass.TA.insert(ExprRange.getBegin(), "if (!(self = ");
59 std::string retStr = ")) return ";
60 retStr += getNilString(Pass.Ctx);
61 Pass.TA.insertAfterToken(ExprRange.getEnd(), retStr);
67 bool isRemovable(Expr *E) const {
68 return Removables.count(E);
72 } // anonymous namespace
74 void trans::rewriteUnusedInitDelegate(MigrationPass &pass) {
75 BodyTransform<UnusedInitRewriter> trans(pass);
76 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());