Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/ARCMigrate/TransUnusedInitDelegate.cpp
Line
Count
Source
1
//===--- TransUnusedInitDelegate.cpp - Transformations to ARC mode --------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
// Transformations:
9
//===----------------------------------------------------------------------===//
10
//
11
// rewriteUnusedInitDelegate:
12
//
13
// Rewrites an unused result of calling a delegate initialization, to assigning
14
// the result to self.
15
// e.g
16
//  [self init];
17
// ---->
18
//  self = [self init];
19
//
20
//===----------------------------------------------------------------------===//
21
22
#include "Transforms.h"
23
#include "Internals.h"
24
#include "clang/AST/ASTContext.h"
25
#include "clang/Sema/SemaDiagnostic.h"
26
27
using namespace clang;
28
using namespace arcmt;
29
using namespace trans;
30
31
namespace {
32
33
class UnusedInitRewriter : public RecursiveASTVisitor<UnusedInitRewriter> {
34
  Stmt *Body;
35
  MigrationPass &Pass;
36
37
  ExprSet Removables;
38
39
public:
40
  UnusedInitRewriter(MigrationPass &pass)
41
403
    : Body(nullptr), Pass(pass) { }
42
43
403
  void transformBody(Stmt *body, Decl *ParentD) {
44
403
    Body = body;
45
403
    collectRemovables(body, Removables);
46
403
    TraverseStmt(body);
47
403
  }
48
49
495
  bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {
50
495
    if (ME->isDelegateInitCall() &&
51
495
        
isRemovable(ME)4
&&
52
495
        Pass.TA.hasDiagnostic(diag::err_arc_unused_init_message,
53
4
                              ME->getExprLoc())) {
54
4
      Transaction Trans(Pass.TA);
55
4
      Pass.TA.clearDiagnostic(diag::err_arc_unused_init_message,
56
4
                              ME->getExprLoc());
57
4
      SourceRange ExprRange = ME->getSourceRange();
58
4
      Pass.TA.insert(ExprRange.getBegin(), "if (!(self = ");
59
4
      std::string retStr = ")) return ";
60
4
      retStr += getNilString(Pass);
61
4
      Pass.TA.insertAfterToken(ExprRange.getEnd(), retStr);
62
4
    }
63
495
    return true;
64
495
  }
65
66
private:
67
4
  bool isRemovable(Expr *E) const {
68
4
    return Removables.count(E);
69
4
  }
70
};
71
72
} // anonymous namespace
73
74
85
void trans::rewriteUnusedInitDelegate(MigrationPass &pass) {
75
85
  BodyTransform<UnusedInitRewriter> trans(pass);
76
85
  trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
77
85
}