2012-11-14 23:08:31 +08:00
|
|
|
//===-- Transforms.h - Transformations to ARC mode --------------*- C++ -*-===//
|
2011-06-22 04:20:39 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-06-22 04:20:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_LIB_ARCMIGRATE_TRANSFORMS_H
|
|
|
|
#define LLVM_CLANG_LIB_ARCMIGRATE_TRANSFORMS_H
|
|
|
|
|
2011-11-04 23:58:08 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2011-06-22 04:20:39 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2012-06-07 08:44:06 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2011-06-22 04:20:39 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class Decl;
|
|
|
|
class Stmt;
|
|
|
|
class BlockDecl;
|
|
|
|
class ObjCMethodDecl;
|
|
|
|
class FunctionDecl;
|
|
|
|
|
|
|
|
namespace arcmt {
|
|
|
|
class MigrationPass;
|
|
|
|
|
|
|
|
namespace trans {
|
|
|
|
|
2011-11-04 23:58:08 +08:00
|
|
|
class MigrationContext;
|
|
|
|
|
2011-06-22 04:20:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transformations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void rewriteAutoreleasePool(MigrationPass &pass);
|
|
|
|
void rewriteUnbridgedCasts(MigrationPass &pass);
|
|
|
|
void makeAssignARCSafe(MigrationPass &pass);
|
2011-11-04 23:58:22 +08:00
|
|
|
void removeRetainReleaseDeallocFinalize(MigrationPass &pass);
|
|
|
|
void removeZeroOutPropsInDeallocFinalize(MigrationPass &pass);
|
2011-06-22 04:20:39 +08:00
|
|
|
void rewriteUnusedInitDelegate(MigrationPass &pass);
|
2011-07-18 15:44:45 +08:00
|
|
|
void checkAPIUses(MigrationPass &pass);
|
2011-06-22 04:20:39 +08:00
|
|
|
|
2011-11-04 23:58:22 +08:00
|
|
|
void removeEmptyStatementsAndDeallocFinalize(MigrationPass &pass);
|
2011-06-22 04:20:42 +08:00
|
|
|
|
2011-11-04 23:58:08 +08:00
|
|
|
class BodyContext {
|
|
|
|
MigrationContext &MigrateCtx;
|
|
|
|
ParentMap PMap;
|
|
|
|
Stmt *TopStmt;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BodyContext(MigrationContext &MigrateCtx, Stmt *S)
|
|
|
|
: MigrateCtx(MigrateCtx), PMap(S), TopStmt(S) {}
|
|
|
|
|
|
|
|
MigrationContext &getMigrationContext() { return MigrateCtx; }
|
|
|
|
ParentMap &getParentMap() { return PMap; }
|
|
|
|
Stmt *getTopStmt() { return TopStmt; }
|
|
|
|
};
|
|
|
|
|
2011-11-07 02:57:57 +08:00
|
|
|
class ObjCImplementationContext {
|
|
|
|
MigrationContext &MigrateCtx;
|
|
|
|
ObjCImplementationDecl *ImpD;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ObjCImplementationContext(MigrationContext &MigrateCtx,
|
|
|
|
ObjCImplementationDecl *D)
|
|
|
|
: MigrateCtx(MigrateCtx), ImpD(D) {}
|
|
|
|
|
|
|
|
MigrationContext &getMigrationContext() { return MigrateCtx; }
|
|
|
|
ObjCImplementationDecl *getImplementationDecl() { return ImpD; }
|
|
|
|
};
|
|
|
|
|
2011-11-04 23:58:08 +08:00
|
|
|
class ASTTraverser {
|
|
|
|
public:
|
|
|
|
virtual ~ASTTraverser();
|
2011-11-07 02:57:57 +08:00
|
|
|
virtual void traverseTU(MigrationContext &MigrateCtx) { }
|
2011-11-04 23:58:08 +08:00
|
|
|
virtual void traverseBody(BodyContext &BodyCtx) { }
|
2011-11-07 02:57:57 +08:00
|
|
|
virtual void traverseObjCImplementation(ObjCImplementationContext &ImplCtx) {}
|
2011-11-04 23:58:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class MigrationContext {
|
|
|
|
std::vector<ASTTraverser *> Traversers;
|
|
|
|
|
|
|
|
public:
|
2011-11-07 02:58:03 +08:00
|
|
|
MigrationPass &Pass;
|
|
|
|
|
|
|
|
struct GCAttrOccurrence {
|
|
|
|
enum AttrKind { Weak, Strong } Kind;
|
|
|
|
SourceLocation Loc;
|
|
|
|
QualType ModifiedType;
|
|
|
|
Decl *Dcl;
|
2018-05-09 09:00:01 +08:00
|
|
|
/// true if the attribute is owned, e.g. it is in a body and not just
|
2011-11-07 02:58:03 +08:00
|
|
|
/// in an interface.
|
|
|
|
bool FullyMigratable;
|
|
|
|
};
|
|
|
|
std::vector<GCAttrOccurrence> GCAttrs;
|
|
|
|
llvm::DenseSet<unsigned> AttrSet;
|
2011-11-08 10:02:38 +08:00
|
|
|
llvm::DenseSet<unsigned> RemovedAttrSet;
|
2011-11-07 02:58:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Set of raw '@' locations for 'assign' properties group that contain
|
2011-11-08 02:46:46 +08:00
|
|
|
/// GC __weak.
|
|
|
|
llvm::DenseSet<unsigned> AtPropsWeak;
|
2011-11-08 02:40:32 +08:00
|
|
|
|
2011-11-04 23:58:08 +08:00
|
|
|
explicit MigrationContext(MigrationPass &pass) : Pass(pass) {}
|
|
|
|
~MigrationContext();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-11-04 23:58:08 +08:00
|
|
|
typedef std::vector<ASTTraverser *>::iterator traverser_iterator;
|
|
|
|
traverser_iterator traversers_begin() { return Traversers.begin(); }
|
|
|
|
traverser_iterator traversers_end() { return Traversers.end(); }
|
|
|
|
|
|
|
|
void addTraverser(ASTTraverser *traverser) {
|
|
|
|
Traversers.push_back(traverser);
|
|
|
|
}
|
|
|
|
|
2011-11-05 07:43:03 +08:00
|
|
|
bool isGCOwnedNonObjC(QualType T);
|
2011-11-28 10:04:36 +08:00
|
|
|
bool removePropertyAttribute(StringRef fromAttr, SourceLocation atLoc) {
|
|
|
|
return rewritePropertyAttribute(fromAttr, StringRef(), atLoc);
|
|
|
|
}
|
2011-11-08 02:46:46 +08:00
|
|
|
bool rewritePropertyAttribute(StringRef fromAttr, StringRef toAttr,
|
|
|
|
SourceLocation atLoc);
|
2011-11-28 10:04:36 +08:00
|
|
|
bool addPropertyAttribute(StringRef attr, SourceLocation atLoc);
|
2011-11-05 07:43:03 +08:00
|
|
|
|
2011-11-04 23:58:08 +08:00
|
|
|
void traverse(TranslationUnitDecl *TU);
|
2011-11-07 02:58:17 +08:00
|
|
|
|
|
|
|
void dumpGCAttrs();
|
2011-11-04 23:58:08 +08:00
|
|
|
};
|
|
|
|
|
2011-11-07 02:57:57 +08:00
|
|
|
class PropertyRewriteTraverser : public ASTTraverser {
|
|
|
|
public:
|
2014-03-14 14:02:07 +08:00
|
|
|
void traverseObjCImplementation(ObjCImplementationContext &ImplCtx) override;
|
2011-11-07 02:57:57 +08:00
|
|
|
};
|
|
|
|
|
2012-03-05 16:46:24 +08:00
|
|
|
class BlockObjCVariableTraverser : public ASTTraverser {
|
|
|
|
public:
|
2014-03-14 14:02:07 +08:00
|
|
|
void traverseBody(BodyContext &BodyCtx) override;
|
2012-03-05 16:46:24 +08:00
|
|
|
};
|
|
|
|
|
2013-01-05 02:30:08 +08:00
|
|
|
class ProtectedScopeTraverser : public ASTTraverser {
|
|
|
|
public:
|
2014-03-14 14:02:07 +08:00
|
|
|
void traverseBody(BodyContext &BodyCtx) override;
|
2013-01-05 02:30:08 +08:00
|
|
|
};
|
|
|
|
|
2011-11-04 23:58:08 +08:00
|
|
|
// GC transformations
|
|
|
|
|
2011-11-07 02:58:03 +08:00
|
|
|
class GCAttrsTraverser : public ASTTraverser {
|
|
|
|
public:
|
2014-03-14 14:02:07 +08:00
|
|
|
void traverseTU(MigrationContext &MigrateCtx) override;
|
2011-11-07 02:58:03 +08:00
|
|
|
};
|
|
|
|
|
2011-11-04 23:58:08 +08:00
|
|
|
class GCCollectableCallsTraverser : public ASTTraverser {
|
|
|
|
public:
|
2014-03-14 14:02:07 +08:00
|
|
|
void traverseBody(BodyContext &BodyCtx) override;
|
2011-11-04 23:58:08 +08:00
|
|
|
};
|
|
|
|
|
2011-06-22 04:20:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helpers.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determine whether we can add weak to the given type.
|
2011-11-08 02:40:29 +08:00
|
|
|
bool canApplyWeak(ASTContext &Ctx, QualType type,
|
|
|
|
bool AllowOnUnknownClass = false);
|
2011-07-13 06:05:17 +08:00
|
|
|
|
2012-05-24 05:50:04 +08:00
|
|
|
bool isPlusOneAssign(const BinaryOperator *E);
|
2013-01-05 02:30:11 +08:00
|
|
|
bool isPlusOne(const Expr *E);
|
2012-05-24 05:50:04 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 'Loc' is the end of a statement range. This returns the location
|
2011-06-22 04:20:39 +08:00
|
|
|
/// immediately after the semicolon following the statement.
|
|
|
|
/// If no semicolon is found or the location is inside a macro, the returned
|
|
|
|
/// source location will be invalid.
|
2013-10-12 01:35:22 +08:00
|
|
|
SourceLocation findLocationAfterSemi(SourceLocation loc, ASTContext &Ctx,
|
|
|
|
bool IsDecl = false);
|
2011-06-22 04:20:39 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 'Loc' is the end of a statement range. This returns the location
|
2011-09-02 04:53:18 +08:00
|
|
|
/// of the semicolon following the statement.
|
|
|
|
/// If no semicolon is found or the location is inside a macro, the returned
|
|
|
|
/// source location will be invalid.
|
2013-10-12 01:35:22 +08:00
|
|
|
SourceLocation findSemiAfterLocation(SourceLocation loc, ASTContext &Ctx,
|
|
|
|
bool IsDecl = false);
|
2011-09-02 04:53:18 +08:00
|
|
|
|
2011-06-22 04:20:39 +08:00
|
|
|
bool hasSideEffects(Expr *E, ASTContext &Ctx);
|
2011-07-15 07:32:04 +08:00
|
|
|
bool isGlobalVar(Expr *E);
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns "nil" or "0" if 'nil' macro is not actually defined.
|
2015-04-30 07:20:19 +08:00
|
|
|
StringRef getNilString(MigrationPass &Pass);
|
2011-06-22 04:20:39 +08:00
|
|
|
|
|
|
|
template <typename BODY_TRANS>
|
|
|
|
class BodyTransform : public RecursiveASTVisitor<BodyTransform<BODY_TRANS> > {
|
|
|
|
MigrationPass &Pass;
|
2012-06-07 08:44:06 +08:00
|
|
|
Decl *ParentD;
|
2011-06-22 04:20:39 +08:00
|
|
|
|
2012-06-07 08:44:06 +08:00
|
|
|
typedef RecursiveASTVisitor<BodyTransform<BODY_TRANS> > base;
|
2011-06-22 04:20:39 +08:00
|
|
|
public:
|
2014-05-07 14:21:57 +08:00
|
|
|
BodyTransform(MigrationPass &pass) : Pass(pass), ParentD(nullptr) { }
|
2011-06-22 04:20:39 +08:00
|
|
|
|
2011-06-24 05:21:33 +08:00
|
|
|
bool TraverseStmt(Stmt *rootS) {
|
2011-07-18 15:44:45 +08:00
|
|
|
if (rootS)
|
2012-06-07 08:44:06 +08:00
|
|
|
BODY_TRANS(Pass).transformBody(rootS, ParentD);
|
2011-06-22 04:20:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-07 08:44:06 +08:00
|
|
|
|
|
|
|
bool TraverseObjCMethodDecl(ObjCMethodDecl *D) {
|
|
|
|
SaveAndRestore<Decl *> SetParent(ParentD, D);
|
|
|
|
return base::TraverseObjCMethodDecl(D);
|
|
|
|
}
|
2011-06-22 04:20:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef llvm::DenseSet<Expr *> ExprSet;
|
|
|
|
|
|
|
|
void clearRefsIn(Stmt *S, ExprSet &refs);
|
|
|
|
template <typename iterator>
|
|
|
|
void clearRefsIn(iterator begin, iterator end, ExprSet &refs) {
|
|
|
|
for (; begin != end; ++begin)
|
|
|
|
clearRefsIn(*begin, refs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs);
|
|
|
|
|
|
|
|
void collectRemovables(Stmt *S, ExprSet &exprs);
|
|
|
|
|
|
|
|
} // end namespace trans
|
|
|
|
|
|
|
|
} // end namespace arcmt
|
|
|
|
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|