blob: 2fa18a3d6256ccd3386f5fee8750cf9ebd5f83ee [file] [log] [blame]
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +00001//===--- 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
27using namespace clang;
28using namespace arcmt;
29using namespace trans;
30using llvm::StringRef;
31
32namespace {
33
34class UnusedInitRewriter : public RecursiveASTVisitor<UnusedInitRewriter> {
35 Decl *Dcl;
36 Stmt *Body;
37 MigrationPass &Pass;
38
39 ExprSet Removables;
40
41public:
42 UnusedInitRewriter(Decl *D, MigrationPass &pass)
43 : Dcl(D), Body(0), Pass(pass) { }
44
45 void transformBody(Stmt *body) {
46 Body = body;
47 collectRemovables(body, Removables);
48 TraverseStmt(body);
49 }
50
51 bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {
52 if (ME->isDelegateInitCall() &&
53 isRemovable(ME) &&
54 Pass.TA.hasDiagnostic(diag::err_arc_unused_init_message,
55 ME->getExprLoc())) {
56 Transaction Trans(Pass.TA);
57 Pass.TA.clearDiagnostic(diag::err_arc_unused_init_message,
58 ME->getExprLoc());
59 Pass.TA.insert(ME->getExprLoc(), "self = ");
60 }
61 return true;
62 }
63
64private:
65 bool isRemovable(Expr *E) const {
66 return Removables.count(E);
67 }
68};
69
70} // anonymous namespace
71
72void trans::rewriteUnusedInitDelegate(MigrationPass &pass) {
73 BodyTransform<UnusedInitRewriter> trans(pass);
74 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
75}