Home | History | Annotate | Download | only in ARCMigrate
      1 //===--- 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 
     28 using namespace clang;
     29 using namespace arcmt;
     30 using namespace trans;
     31 
     32 namespace {
     33 
     34 class ARCAssignChecker : public RecursiveASTVisitor<ARCAssignChecker> {
     35   MigrationPass &Pass;
     36   llvm::DenseSet<VarDecl *> ModifiedVars;
     37 
     38 public:
     39   ARCAssignChecker(MigrationPass &pass) : Pass(pass) { }
     40 
     41   bool VisitBinaryOperator(BinaryOperator *Exp) {
     42     Expr *E = Exp->getLHS();
     43     SourceLocation OrigLoc = E->getExprLoc();
     44     SourceLocation Loc = OrigLoc;
     45     DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
     46     if (declRef && isa<VarDecl>(declRef->getDecl())) {
     47       ASTContext &Ctx = Pass.Ctx;
     48       Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(Ctx, &Loc);
     49       if (IsLV != Expr::MLV_ConstQualified)
     50         return true;
     51       VarDecl *var = cast<VarDecl>(declRef->getDecl());
     52       if (var->isARCPseudoStrong()) {
     53         Transaction Trans(Pass.TA);
     54         if (Pass.TA.clearDiagnostic(diag::err_typecheck_arr_assign_enumeration,
     55                                     Exp->getOperatorLoc())) {
     56           if (!ModifiedVars.count(var)) {
     57             TypeLoc TLoc = var->getTypeSourceInfo()->getTypeLoc();
     58             Pass.TA.insert(TLoc.getBeginLoc(), "__strong ");
     59             ModifiedVars.insert(var);
     60           }
     61         }
     62       }
     63     }
     64 
     65     return true;
     66   }
     67 };
     68 
     69 } // anonymous namespace
     70 
     71 void trans::makeAssignARCSafe(MigrationPass &pass) {
     72   ARCAssignChecker assignCheck(pass);
     73   assignCheck.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
     74 }
     75