blob: 8c00df5daa03093de907ac27901406ca5124ff1d [file] [log] [blame]
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +00001//===--- TransARCAssign.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//
10// makeAssignARCSafe:
11//
12// Add '__strong' where appropriate.
13//
14// for (id x in collection) {
15// x = 0;
16// }
17// ---->
18// for (__strong id x in collection) {
19// x = 0;
20// }
21//
22//===----------------------------------------------------------------------===//
23
24#include "Transforms.h"
25#include "Internals.h"
26#include "clang/Sema/SemaDiagnostic.h"
27
28using namespace clang;
29using namespace arcmt;
30using namespace trans;
31using llvm::StringRef;
32
33namespace {
34
35class ARCAssignChecker : public RecursiveASTVisitor<ARCAssignChecker> {
36 MigrationPass &Pass;
37 llvm::DenseSet<VarDecl *> ModifiedVars;
38
39public:
40 ARCAssignChecker(MigrationPass &pass) : Pass(pass) { }
41
42 bool VisitBinaryOperator(BinaryOperator *Exp) {
43 Expr *E = Exp->getLHS();
44 SourceLocation OrigLoc = E->getExprLoc();
45 SourceLocation Loc = OrigLoc;
46 DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
47 if (declRef && isa<VarDecl>(declRef->getDecl())) {
48 ASTContext &Ctx = Pass.Ctx;
49 Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(Ctx, &Loc);
50 if (IsLV != Expr::MLV_ConstQualified)
51 return true;
52 VarDecl *var = cast<VarDecl>(declRef->getDecl());
53 if (var->isARCPseudoStrong()) {
54 Transaction Trans(Pass.TA);
55 if (Pass.TA.clearDiagnostic(diag::err_typecheck_arr_assign_enumeration,
56 Exp->getOperatorLoc())) {
57 if (!ModifiedVars.count(var)) {
58 TypeLoc TLoc = var->getTypeSourceInfo()->getTypeLoc();
59 Pass.TA.insert(TLoc.getBeginLoc(), "__strong ");
60 ModifiedVars.insert(var);
61 }
62 }
63 }
64 }
65
66 return true;
67 }
68};
69
70} // anonymous namespace
71
72void trans::makeAssignARCSafe(MigrationPass &pass) {
73 ARCAssignChecker assignCheck(pass);
74 assignCheck.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
75}