clang  6.0.0svn
TransARCAssign.cpp
Go to the documentation of this file.
1 //===--- TransARCAssign.cpp - Transformations 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/AST/ASTContext.h"
28 
29 using namespace clang;
30 using namespace arcmt;
31 using namespace trans;
32 
33 namespace {
34 
35 class ARCAssignChecker : public RecursiveASTVisitor<ARCAssignChecker> {
36  MigrationPass &Pass;
37  llvm::DenseSet<VarDecl *> ModifiedVars;
38 
39 public:
40  ARCAssignChecker(MigrationPass &pass) : Pass(pass) { }
41 
42  bool VisitBinaryOperator(BinaryOperator *Exp) {
43  if (Exp->getType()->isDependentType())
44  return true;
45 
46  Expr *E = Exp->getLHS();
47  SourceLocation OrigLoc = E->getExprLoc();
48  SourceLocation Loc = OrigLoc;
49  DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
50  if (declRef && isa<VarDecl>(declRef->getDecl())) {
51  ASTContext &Ctx = Pass.Ctx;
53  if (IsLV != Expr::MLV_ConstQualified)
54  return true;
55  VarDecl *var = cast<VarDecl>(declRef->getDecl());
56  if (var->isARCPseudoStrong()) {
57  Transaction Trans(Pass.TA);
58  if (Pass.TA.clearDiagnostic(diag::err_typecheck_arr_assign_enumeration,
59  Exp->getOperatorLoc())) {
60  if (!ModifiedVars.count(var)) {
61  TypeLoc TLoc = var->getTypeSourceInfo()->getTypeLoc();
62  Pass.TA.insert(TLoc.getBeginLoc(), "__strong ");
63  ModifiedVars.insert(var);
64  }
65  }
66  }
67  }
68 
69  return true;
70  }
71 };
72 
73 } // anonymous namespace
74 
76  ARCAssignChecker assignCheck(pass);
77  assignCheck.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
78 }
Defines the clang::ASTContext interface.
isModifiableLvalueResult
Definition: Expr.h:269
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:771
void makeAssignARCSafe(MigrationPass &pass)
bool isARCPseudoStrong() const
Determine whether this variable is an ARC pseudo-__strong variable.
Definition: Decl.h:1285
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:40
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2986
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
Definition: Expr.cpp:2462
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
bool clearDiagnostic(ArrayRef< unsigned > IDs, SourceRange range)
SourceLocation getBeginLoc() const
Get the begin source location.
Definition: TypeLoc.cpp:169
Expr - This represents one expression.
Definition: Expr.h:106
QualType getType() const
Definition: Expr.h:128
ValueDecl * getDecl()
Definition: Expr.h:1041
Encodes a location in the source.
SourceLocation getOperatorLoc() const
Definition: Expr.h:3024
isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc=nullptr) const
isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, does not have an incomplet...
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:216
Expr * getLHS() const
Definition: Expr.h:3030
Dataflow Directional Tag Classes.
TypeSourceInfo * getTypeSourceInfo() const
Definition: Decl.h:674
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition: TypeLoc.h:222
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:958
TransformActions & TA
Definition: Internals.h:151
void insert(SourceLocation loc, StringRef text)
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition: Type.h:1795
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:956