blob: 1019ab4ff1f6ea9d423039300c808d99445b09bf [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> {
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000035 Stmt *Body;
36 MigrationPass &Pass;
37
38 ExprSet Removables;
39
40public:
Argyrios Kyrtzidisb1094a02011-06-23 21:21:33 +000041 UnusedInitRewriter(MigrationPass &pass)
42 : Body(0), Pass(pass) { }
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000043
44 void transformBody(Stmt *body) {
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());
58 Pass.TA.insert(ME->getExprLoc(), "self = ");
59 }
60 return true;
61 }
62
63private:
64 bool isRemovable(Expr *E) const {
65 return Removables.count(E);
66 }
67};
68
69} // anonymous namespace
70
71void trans::rewriteUnusedInitDelegate(MigrationPass &pass) {
72 BodyTransform<UnusedInitRewriter> trans(pass);
73 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
74}