blob: cfa6da1f99f92a57a71ad9e159141489ef39a433 [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;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000031
32namespace {
33
34class ARCAssignChecker : public RecursiveASTVisitor<ARCAssignChecker> {
35 MigrationPass &Pass;
36 llvm::DenseSet<VarDecl *> ModifiedVars;
37
38public:
39 ARCAssignChecker(MigrationPass &pass) : Pass(pass) { }
40
41 bool VisitBinaryOperator(BinaryOperator *Exp) {
Argyrios Kyrtzidisfcf28b22011-11-05 00:02:26 +000042 if (Exp->getType()->isDependentType())
43 return true;
44
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000045 Expr *E = Exp->getLHS();
46 SourceLocation OrigLoc = E->getExprLoc();
47 SourceLocation Loc = OrigLoc;
48 DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
49 if (declRef && isa<VarDecl>(declRef->getDecl())) {
50 ASTContext &Ctx = Pass.Ctx;
51 Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(Ctx, &Loc);
52 if (IsLV != Expr::MLV_ConstQualified)
53 return true;
54 VarDecl *var = cast<VarDecl>(declRef->getDecl());
55 if (var->isARCPseudoStrong()) {
56 Transaction Trans(Pass.TA);
57 if (Pass.TA.clearDiagnostic(diag::err_typecheck_arr_assign_enumeration,
58 Exp->getOperatorLoc())) {
59 if (!ModifiedVars.count(var)) {
60 TypeLoc TLoc = var->getTypeSourceInfo()->getTypeLoc();
61 Pass.TA.insert(TLoc.getBeginLoc(), "__strong ");
62 ModifiedVars.insert(var);
63 }
64 }
65 }
66 }
67
68 return true;
69 }
70};
71
72} // anonymous namespace
73
74void trans::makeAssignARCSafe(MigrationPass &pass) {
75 ARCAssignChecker assignCheck(pass);
76 assignCheck.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
77}