1 //===--- TransUnusedInitDelegate.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 //===----------------------------------------------------------------------===//
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/AST/ASTContext.h"
26 #include "clang/Sema/SemaDiagnostic.h"
28 using namespace clang;
29 using namespace arcmt;
30 using namespace trans;
34 class UnusedInitRewriter : public RecursiveASTVisitor<UnusedInitRewriter> {
41 UnusedInitRewriter(MigrationPass &pass)
42 : Body(0), Pass(pass) { }
44 void transformBody(Stmt *body, Decl *ParentD) {
46 collectRemovables(body, Removables);
50 bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {
51 if (ME->isDelegateInitCall() &&
53 Pass.TA.hasDiagnostic(diag::err_arc_unused_init_message,
55 Transaction Trans(Pass.TA);
56 Pass.TA.clearDiagnostic(diag::err_arc_unused_init_message,
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);
68 bool isRemovable(Expr *E) const {
69 return Removables.count(E);
73 } // anonymous namespace
75 void trans::rewriteUnusedInitDelegate(MigrationPass &pass) {
76 BodyTransform<UnusedInitRewriter> trans(pass);
77 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());