2012-11-14 23:08:31 +08:00
|
|
|
//===--- TransUnusedInitDelegate.cpp - Transformations to ARC mode --------===//
|
2011-06-22 04:20:39 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-06-22 04:20:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transformations:
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// rewriteUnusedInitDelegate:
|
|
|
|
//
|
|
|
|
// Rewrites an unused result of calling a delegate initialization, to assigning
|
|
|
|
// the result to self.
|
|
|
|
// e.g
|
|
|
|
// [self init];
|
|
|
|
// ---->
|
|
|
|
// self = [self init];
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Transforms.h"
|
|
|
|
#include "Internals.h"
|
2012-07-05 04:19:54 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2011-06-22 04:20:39 +08:00
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace arcmt;
|
|
|
|
using namespace trans;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class UnusedInitRewriter : public RecursiveASTVisitor<UnusedInitRewriter> {
|
|
|
|
Stmt *Body;
|
|
|
|
MigrationPass &Pass;
|
|
|
|
|
|
|
|
ExprSet Removables;
|
|
|
|
|
|
|
|
public:
|
2011-06-24 05:21:33 +08:00
|
|
|
UnusedInitRewriter(MigrationPass &pass)
|
2014-05-07 14:21:57 +08:00
|
|
|
: Body(nullptr), Pass(pass) { }
|
2011-06-22 04:20:39 +08:00
|
|
|
|
2012-06-07 08:44:06 +08:00
|
|
|
void transformBody(Stmt *body, Decl *ParentD) {
|
2011-06-22 04:20:39 +08:00
|
|
|
Body = body;
|
|
|
|
collectRemovables(body, Removables);
|
|
|
|
TraverseStmt(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {
|
|
|
|
if (ME->isDelegateInitCall() &&
|
|
|
|
isRemovable(ME) &&
|
|
|
|
Pass.TA.hasDiagnostic(diag::err_arc_unused_init_message,
|
|
|
|
ME->getExprLoc())) {
|
|
|
|
Transaction Trans(Pass.TA);
|
|
|
|
Pass.TA.clearDiagnostic(diag::err_arc_unused_init_message,
|
|
|
|
ME->getExprLoc());
|
2012-03-31 09:34:06 +08:00
|
|
|
SourceRange ExprRange = ME->getSourceRange();
|
|
|
|
Pass.TA.insert(ExprRange.getBegin(), "if (!(self = ");
|
|
|
|
std::string retStr = ")) return ";
|
2015-04-30 07:20:19 +08:00
|
|
|
retStr += getNilString(Pass);
|
2012-03-31 09:34:06 +08:00
|
|
|
Pass.TA.insertAfterToken(ExprRange.getEnd(), retStr);
|
2011-06-22 04:20:39 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool isRemovable(Expr *E) const {
|
|
|
|
return Removables.count(E);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
void trans::rewriteUnusedInitDelegate(MigrationPass &pass) {
|
|
|
|
BodyTransform<UnusedInitRewriter> trans(pass);
|
|
|
|
trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
|
|
|
|
}
|