Path: blob/main/contrib/llvm-project/clang/lib/ARCMigrate/TransUnusedInitDelegate.cpp
35236 views
//===--- TransUnusedInitDelegate.cpp - Transformations to ARC mode --------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// Transformations:8//===----------------------------------------------------------------------===//9//10// rewriteUnusedInitDelegate:11//12// Rewrites an unused result of calling a delegate initialization, to assigning13// the result to self.14// e.g15// [self init];16// ---->17// self = [self init];18//19//===----------------------------------------------------------------------===//2021#include "Transforms.h"22#include "Internals.h"23#include "clang/AST/ASTContext.h"24#include "clang/Sema/SemaDiagnostic.h"2526using namespace clang;27using namespace arcmt;28using namespace trans;2930namespace {3132class UnusedInitRewriter : public RecursiveASTVisitor<UnusedInitRewriter> {33Stmt *Body;34MigrationPass &Pass;3536ExprSet Removables;3738public:39UnusedInitRewriter(MigrationPass &pass)40: Body(nullptr), Pass(pass) { }4142void transformBody(Stmt *body, Decl *ParentD) {43Body = body;44collectRemovables(body, Removables);45TraverseStmt(body);46}4748bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {49if (ME->isDelegateInitCall() &&50isRemovable(ME) &&51Pass.TA.hasDiagnostic(diag::err_arc_unused_init_message,52ME->getExprLoc())) {53Transaction Trans(Pass.TA);54Pass.TA.clearDiagnostic(diag::err_arc_unused_init_message,55ME->getExprLoc());56SourceRange ExprRange = ME->getSourceRange();57Pass.TA.insert(ExprRange.getBegin(), "if (!(self = ");58std::string retStr = ")) return ";59retStr += getNilString(Pass);60Pass.TA.insertAfterToken(ExprRange.getEnd(), retStr);61}62return true;63}6465private:66bool isRemovable(Expr *E) const {67return Removables.count(E);68}69};7071} // anonymous namespace7273void trans::rewriteUnusedInitDelegate(MigrationPass &pass) {74BodyTransform<UnusedInitRewriter> trans(pass);75trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());76}777879