clang  6.0.0svn
TransUnusedInitDelegate.cpp
Go to the documentation of this file.
1 //===--- TransUnusedInitDelegate.cpp - Transformations 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/AST/ASTContext.h"
27 
28 using namespace clang;
29 using namespace arcmt;
30 using namespace trans;
31 
32 namespace {
33 
34 class UnusedInitRewriter : public RecursiveASTVisitor<UnusedInitRewriter> {
35  Stmt *Body;
36  MigrationPass &Pass;
37 
38  ExprSet Removables;
39 
40 public:
41  UnusedInitRewriter(MigrationPass &pass)
42  : Body(nullptr), Pass(pass) { }
43 
44  void transformBody(Stmt *body, Decl *ParentD) {
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  SourceRange ExprRange = ME->getSourceRange();
59  Pass.TA.insert(ExprRange.getBegin(), "if (!(self = ");
60  std::string retStr = ")) return ";
61  retStr += getNilString(Pass);
62  Pass.TA.insertAfterToken(ExprRange.getEnd(), retStr);
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 
78 }
Defines the clang::ASTContext interface.
Stmt - This represents one statement.
Definition: Stmt.h:66
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument&#39;s dynamic ty...
void rewriteUnusedInitDelegate(MigrationPass &pass)
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
StringRef getNilString(MigrationPass &Pass)
Returns "nil" or "0" if &#39;nil&#39; macro is not actually defined.
Definition: Transforms.cpp:209
bool clearDiagnostic(ArrayRef< unsigned > IDs, SourceRange range)
void collectRemovables(Stmt *S, ExprSet &exprs)
Definition: Transforms.cpp:308
Expr - This represents one expression.
Definition: Expr.h:106
void insertAfterToken(SourceLocation loc, StringRef text)
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:903
SourceLocation getEnd() const
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:216
Dataflow Directional Tag Classes.
bool isDelegateInitCall() const
isDelegateInitCall - Answers whether this message send has been tagged as a "delegate init call"...
Definition: ExprObjC.h:1359
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:989
TransformActions & TA
Definition: Internals.h:151
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:265
void insert(SourceLocation loc, StringRef text)
A trivial tuple used to represent a source range.
bool hasDiagnostic(unsigned ID, SourceRange range)
Definition: Internals.h:88
SourceLocation getBegin() const