2012-11-14 23:08:31 +08:00
|
|
|
//===--- TransUnbridgedCasts.cpp - Transformations to ARC mode ------------===//
|
2011-06-22 04:20:39 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// rewriteUnbridgedCasts:
|
|
|
|
//
|
|
|
|
// A cast of non-objc pointer to an objc one is checked. If the non-objc pointer
|
|
|
|
// is from a file-level variable, __bridge cast is used to convert it.
|
|
|
|
// For the result of a function call that we know is +1/+0,
|
2012-06-01 08:10:47 +08:00
|
|
|
// __bridge/CFBridgingRelease is used.
|
2011-06-22 04:20:39 +08:00
|
|
|
//
|
|
|
|
// NSString *str = (NSString *)kUTTypePlainText;
|
|
|
|
// str = b ? kUTTypeRTF : kUTTypePlainText;
|
|
|
|
// NSString *_uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault,
|
|
|
|
// _uuid);
|
|
|
|
// ---->
|
|
|
|
// NSString *str = (__bridge NSString *)kUTTypePlainText;
|
|
|
|
// str = (__bridge NSString *)(b ? kUTTypeRTF : kUTTypePlainText);
|
2012-06-01 08:10:47 +08:00
|
|
|
// NSString *_uuidString = (NSString *)
|
|
|
|
// CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, _uuid));
|
2011-06-22 04:20:39 +08:00
|
|
|
//
|
|
|
|
// For a C pointer to ObjC, for casting 'self', __bridge is used.
|
|
|
|
//
|
|
|
|
// CFStringRef str = (CFStringRef)self;
|
|
|
|
// ---->
|
|
|
|
// CFStringRef str = (__bridge CFStringRef)self;
|
|
|
|
//
|
2013-01-03 11:17:17 +08:00
|
|
|
// Uses of Block_copy/Block_release macros are rewritten:
|
|
|
|
//
|
|
|
|
// c = Block_copy(b);
|
|
|
|
// Block_release(c);
|
|
|
|
// ---->
|
|
|
|
// c = [b copy];
|
|
|
|
// <removed>
|
|
|
|
//
|
2011-06-22 04:20:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Transforms.h"
|
|
|
|
#include "Internals.h"
|
2012-07-05 04:19:54 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2011-07-27 13:28:18 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
|
2011-06-22 04:20:39 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-07-05 04:19:54 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-06-22 04:20:39 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace arcmt;
|
|
|
|
using namespace trans;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class UnbridgedCastRewriter : public RecursiveASTVisitor<UnbridgedCastRewriter>{
|
|
|
|
MigrationPass &Pass;
|
|
|
|
IdentifierInfo *SelfII;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<ParentMap> StmtMap;
|
2012-06-07 08:44:06 +08:00
|
|
|
Decl *ParentD;
|
2013-01-03 11:17:17 +08:00
|
|
|
Stmt *Body;
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<ExprSet> Removables;
|
2011-07-27 13:28:18 +08:00
|
|
|
|
2011-06-22 04:20:39 +08:00
|
|
|
public:
|
2014-05-07 14:21:57 +08:00
|
|
|
UnbridgedCastRewriter(MigrationPass &pass)
|
|
|
|
: Pass(pass), ParentD(nullptr), Body(nullptr) {
|
2011-06-22 04:20:39 +08:00
|
|
|
SelfII = &Pass.Ctx.Idents.get("self");
|
|
|
|
}
|
|
|
|
|
2012-06-07 08:44:06 +08:00
|
|
|
void transformBody(Stmt *body, Decl *ParentD) {
|
|
|
|
this->ParentD = ParentD;
|
2013-01-03 11:17:17 +08:00
|
|
|
Body = body;
|
2011-07-27 13:28:18 +08:00
|
|
|
StmtMap.reset(new ParentMap(body));
|
|
|
|
TraverseStmt(body);
|
|
|
|
}
|
|
|
|
|
2013-08-10 04:20:25 +08:00
|
|
|
bool TraverseBlockDecl(BlockDecl *D) {
|
|
|
|
// ParentMap does not enter into a BlockDecl to record its stmts, so use a
|
|
|
|
// new UnbridgedCastRewriter to handle the block.
|
|
|
|
UnbridgedCastRewriter(Pass).transformBody(D->getBody(), D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-22 04:20:39 +08:00
|
|
|
bool VisitCastExpr(CastExpr *E) {
|
2013-01-03 11:17:17 +08:00
|
|
|
if (E->getCastKind() != CK_CPointerToObjCPointerCast &&
|
|
|
|
E->getCastKind() != CK_BitCast &&
|
|
|
|
E->getCastKind() != CK_AnyPointerToBlockPointerCast)
|
2011-06-22 04:20:39 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
QualType castType = E->getType();
|
|
|
|
Expr *castExpr = E->getSubExpr();
|
|
|
|
QualType castExprType = castExpr->getType();
|
|
|
|
|
2013-01-03 11:17:17 +08:00
|
|
|
if (castType->isObjCRetainableType() == castExprType->isObjCRetainableType())
|
2011-06-22 04:20:39 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
bool exprRetainable = castExprType->isObjCIndirectLifetimeType();
|
|
|
|
bool castRetainable = castType->isObjCIndirectLifetimeType();
|
|
|
|
if (exprRetainable == castRetainable) return true;
|
|
|
|
|
|
|
|
if (castExpr->isNullPointerConstant(Pass.Ctx,
|
|
|
|
Expr::NPC_ValueDependentIsNull))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
SourceLocation loc = castExpr->getExprLoc();
|
|
|
|
if (loc.isValid() && Pass.Ctx.getSourceManager().isInSystemHeader(loc))
|
|
|
|
return true;
|
|
|
|
|
2013-01-03 11:17:17 +08:00
|
|
|
if (castType->isObjCRetainableType())
|
2011-06-22 04:20:39 +08:00
|
|
|
transformNonObjCToObjCCast(E);
|
|
|
|
else
|
|
|
|
transformObjCToNonObjCCast(E);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void transformNonObjCToObjCCast(CastExpr *E) {
|
|
|
|
if (!E) return;
|
|
|
|
|
|
|
|
// Global vars are assumed that are cast as unretained.
|
|
|
|
if (isGlobalVar(E))
|
|
|
|
if (E->getSubExpr()->getType()->isPointerType()) {
|
|
|
|
castToObjCObject(E, /*retained=*/false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the cast is directly over the result of a Core Foundation function
|
|
|
|
// try to figure out whether it should be cast as retained or unretained.
|
|
|
|
Expr *inner = E->IgnoreParenCasts();
|
|
|
|
if (CallExpr *callE = dyn_cast<CallExpr>(inner)) {
|
|
|
|
if (FunctionDecl *FD = callE->getDirectCallee()) {
|
2013-12-19 10:39:40 +08:00
|
|
|
if (FD->hasAttr<CFReturnsRetainedAttr>()) {
|
2011-06-22 04:20:39 +08:00
|
|
|
castToObjCObject(E, /*retained=*/true);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-19 10:39:40 +08:00
|
|
|
if (FD->hasAttr<CFReturnsNotRetainedAttr>()) {
|
2011-06-22 04:20:39 +08:00
|
|
|
castToObjCObject(E, /*retained=*/false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (FD->isGlobal() &&
|
|
|
|
FD->getIdentifier() &&
|
|
|
|
ento::cocoa::isRefType(E->getSubExpr()->getType(), "CF",
|
|
|
|
FD->getIdentifier()->getName())) {
|
|
|
|
StringRef fname = FD->getIdentifier()->getName();
|
|
|
|
if (fname.endswith("Retain") ||
|
|
|
|
fname.find("Create") != StringRef::npos ||
|
|
|
|
fname.find("Copy") != StringRef::npos) {
|
2012-02-01 05:58:23 +08:00
|
|
|
// Do not migrate to couple of bridge transfer casts which
|
|
|
|
// cancel each other out. Leave it unchanged so error gets user
|
|
|
|
// attention instead.
|
|
|
|
if (FD->getName() == "CFRetain" &&
|
|
|
|
FD->getNumParams() == 1 &&
|
|
|
|
FD->getParent()->isTranslationUnit() &&
|
2013-05-13 08:12:11 +08:00
|
|
|
FD->isExternallyVisible()) {
|
2012-02-01 05:58:23 +08:00
|
|
|
Expr *Arg = callE->getArg(0);
|
2012-02-01 06:09:44 +08:00
|
|
|
if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
|
2012-02-01 05:58:23 +08:00
|
|
|
const Expr *sub = ICE->getSubExpr();
|
|
|
|
QualType T = sub->getType();
|
|
|
|
if (T->isObjCObjectPointerType())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-06-22 04:20:39 +08:00
|
|
|
castToObjCObject(E, /*retained=*/true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fname.find("Get") != StringRef::npos) {
|
|
|
|
castToObjCObject(E, /*retained=*/false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-07 08:44:06 +08:00
|
|
|
|
|
|
|
// If returning an ivar or a member of an ivar from a +0 method, use
|
|
|
|
// a __bridge cast.
|
|
|
|
Expr *base = inner->IgnoreParenImpCasts();
|
|
|
|
while (isa<MemberExpr>(base))
|
|
|
|
base = cast<MemberExpr>(base)->getBase()->IgnoreParenImpCasts();
|
|
|
|
if (isa<ObjCIvarRefExpr>(base) &&
|
|
|
|
isa<ReturnStmt>(StmtMap->getParentIgnoreParenCasts(E))) {
|
|
|
|
if (ObjCMethodDecl *method = dyn_cast_or_null<ObjCMethodDecl>(ParentD)) {
|
|
|
|
if (!method->hasAttr<NSReturnsRetainedAttr>()) {
|
|
|
|
castToObjCObject(E, /*retained=*/false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-22 04:20:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void castToObjCObject(CastExpr *E, bool retained) {
|
|
|
|
rewriteToBridgedCast(E, retained ? OBC_BridgeTransfer : OBC_Bridge);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewriteToBridgedCast(CastExpr *E, ObjCBridgeCastKind Kind) {
|
2011-07-27 13:28:18 +08:00
|
|
|
Transaction Trans(Pass.TA);
|
|
|
|
rewriteToBridgedCast(E, Kind, Trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewriteToBridgedCast(CastExpr *E, ObjCBridgeCastKind Kind,
|
|
|
|
Transaction &Trans) {
|
2011-06-22 04:20:39 +08:00
|
|
|
TransformActions &TA = Pass.TA;
|
|
|
|
|
|
|
|
// We will remove the compiler diagnostic.
|
|
|
|
if (!TA.hasDiagnostic(diag::err_arc_mismatched_cast,
|
|
|
|
diag::err_arc_cast_requires_bridge,
|
2011-07-27 13:28:18 +08:00
|
|
|
E->getLocStart())) {
|
|
|
|
Trans.abort();
|
2011-06-22 04:20:39 +08:00
|
|
|
return;
|
2011-07-27 13:28:18 +08:00
|
|
|
}
|
2011-06-22 04:20:39 +08:00
|
|
|
|
|
|
|
StringRef bridge;
|
|
|
|
switch(Kind) {
|
|
|
|
case OBC_Bridge:
|
|
|
|
bridge = "__bridge "; break;
|
|
|
|
case OBC_BridgeTransfer:
|
|
|
|
bridge = "__bridge_transfer "; break;
|
|
|
|
case OBC_BridgeRetained:
|
|
|
|
bridge = "__bridge_retained "; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TA.clearDiagnostic(diag::err_arc_mismatched_cast,
|
|
|
|
diag::err_arc_cast_requires_bridge,
|
|
|
|
E->getLocStart());
|
2012-06-01 08:10:47 +08:00
|
|
|
if (Kind == OBC_Bridge || !Pass.CFBridgingFunctionsDefined()) {
|
|
|
|
if (CStyleCastExpr *CCE = dyn_cast<CStyleCastExpr>(E)) {
|
|
|
|
TA.insertAfterToken(CCE->getLParenLoc(), bridge);
|
2011-06-22 04:20:39 +08:00
|
|
|
} else {
|
2012-06-01 08:10:47 +08:00
|
|
|
SourceLocation insertLoc = E->getSubExpr()->getLocStart();
|
|
|
|
SmallString<128> newCast;
|
2011-06-22 04:20:39 +08:00
|
|
|
newCast += '(';
|
2012-06-01 08:10:47 +08:00
|
|
|
newCast += bridge;
|
|
|
|
newCast += E->getType().getAsString(Pass.Ctx.getPrintingPolicy());
|
|
|
|
newCast += ')';
|
|
|
|
|
|
|
|
if (isa<ParenExpr>(E->getSubExpr())) {
|
|
|
|
TA.insert(insertLoc, newCast.str());
|
|
|
|
} else {
|
|
|
|
newCast += '(';
|
|
|
|
TA.insert(insertLoc, newCast.str());
|
|
|
|
TA.insertAfterToken(E->getLocEnd(), ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(Kind == OBC_BridgeTransfer || Kind == OBC_BridgeRetained);
|
2012-06-07 09:10:31 +08:00
|
|
|
SmallString<32> BridgeCall;
|
|
|
|
|
|
|
|
Expr *WrapE = E->getSubExpr();
|
|
|
|
SourceLocation InsertLoc = WrapE->getLocStart();
|
|
|
|
|
|
|
|
SourceManager &SM = Pass.Ctx.getSourceManager();
|
|
|
|
char PrevChar = *SM.getCharacterData(InsertLoc.getLocWithOffset(-1));
|
|
|
|
if (Lexer::isIdentifierBodyChar(PrevChar, Pass.Ctx.getLangOpts()))
|
|
|
|
BridgeCall += ' ';
|
|
|
|
|
2012-06-01 08:10:47 +08:00
|
|
|
if (Kind == OBC_BridgeTransfer)
|
2012-06-07 09:10:31 +08:00
|
|
|
BridgeCall += "CFBridgingRelease";
|
2012-06-01 08:10:47 +08:00
|
|
|
else
|
2012-06-07 09:10:31 +08:00
|
|
|
BridgeCall += "CFBridgingRetain";
|
2012-06-01 08:10:47 +08:00
|
|
|
|
|
|
|
if (isa<ParenExpr>(WrapE)) {
|
2012-06-07 09:10:31 +08:00
|
|
|
TA.insert(InsertLoc, BridgeCall);
|
2012-06-01 08:10:47 +08:00
|
|
|
} else {
|
2012-06-07 09:10:31 +08:00
|
|
|
BridgeCall += '(';
|
|
|
|
TA.insert(InsertLoc, BridgeCall);
|
2012-06-01 08:10:47 +08:00
|
|
|
TA.insertAfterToken(WrapE->getLocEnd(), ")");
|
2011-06-22 04:20:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 13:28:18 +08:00
|
|
|
void rewriteCastForCFRetain(CastExpr *castE, CallExpr *callE) {
|
|
|
|
Transaction Trans(Pass.TA);
|
|
|
|
Pass.TA.replace(callE->getSourceRange(), callE->getArg(0)->getSourceRange());
|
|
|
|
rewriteToBridgedCast(castE, OBC_BridgeRetained, Trans);
|
|
|
|
}
|
|
|
|
|
2013-01-03 11:17:17 +08:00
|
|
|
void getBlockMacroRanges(CastExpr *E, SourceRange &Outer, SourceRange &Inner) {
|
|
|
|
SourceManager &SM = Pass.Ctx.getSourceManager();
|
|
|
|
SourceLocation Loc = E->getExprLoc();
|
|
|
|
assert(Loc.isMacroID());
|
2018-04-30 13:25:48 +08:00
|
|
|
CharSourceRange MacroRange = SM.getImmediateExpansionRange(Loc);
|
2013-01-03 11:17:17 +08:00
|
|
|
SourceRange SubRange = E->getSubExpr()->IgnoreParenImpCasts()->getSourceRange();
|
|
|
|
SourceLocation InnerBegin = SM.getImmediateMacroCallerLoc(SubRange.getBegin());
|
|
|
|
SourceLocation InnerEnd = SM.getImmediateMacroCallerLoc(SubRange.getEnd());
|
|
|
|
|
2018-04-30 13:25:48 +08:00
|
|
|
Outer = MacroRange.getAsRange();
|
2013-01-03 11:17:17 +08:00
|
|
|
Inner = SourceRange(InnerBegin, InnerEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewriteBlockCopyMacro(CastExpr *E) {
|
|
|
|
SourceRange OuterRange, InnerRange;
|
|
|
|
getBlockMacroRanges(E, OuterRange, InnerRange);
|
|
|
|
|
|
|
|
Transaction Trans(Pass.TA);
|
|
|
|
Pass.TA.replace(OuterRange, InnerRange);
|
|
|
|
Pass.TA.insert(InnerRange.getBegin(), "[");
|
|
|
|
Pass.TA.insertAfterToken(InnerRange.getEnd(), " copy]");
|
|
|
|
Pass.TA.clearDiagnostic(diag::err_arc_mismatched_cast,
|
|
|
|
diag::err_arc_cast_requires_bridge,
|
|
|
|
OuterRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeBlockReleaseMacro(CastExpr *E) {
|
|
|
|
SourceRange OuterRange, InnerRange;
|
|
|
|
getBlockMacroRanges(E, OuterRange, InnerRange);
|
|
|
|
|
|
|
|
Transaction Trans(Pass.TA);
|
|
|
|
Pass.TA.clearDiagnostic(diag::err_arc_mismatched_cast,
|
|
|
|
diag::err_arc_cast_requires_bridge,
|
|
|
|
OuterRange);
|
|
|
|
if (!hasSideEffects(E, Pass.Ctx)) {
|
|
|
|
if (tryRemoving(cast<Expr>(StmtMap->getParentIgnoreParenCasts(E))))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Pass.TA.replace(OuterRange, InnerRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tryRemoving(Expr *E) const {
|
|
|
|
if (!Removables) {
|
|
|
|
Removables.reset(new ExprSet);
|
|
|
|
collectRemovables(Body, *Removables);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Removables->count(E)) {
|
|
|
|
Pass.TA.removeStmt(E);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-22 04:20:39 +08:00
|
|
|
void transformObjCToNonObjCCast(CastExpr *E) {
|
2013-01-03 11:17:17 +08:00
|
|
|
SourceLocation CastLoc = E->getExprLoc();
|
|
|
|
if (CastLoc.isMacroID()) {
|
|
|
|
StringRef MacroName = Lexer::getImmediateMacroName(CastLoc,
|
|
|
|
Pass.Ctx.getSourceManager(),
|
|
|
|
Pass.Ctx.getLangOpts());
|
|
|
|
if (MacroName == "Block_copy") {
|
|
|
|
rewriteBlockCopyMacro(E);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (MacroName == "Block_release") {
|
|
|
|
removeBlockReleaseMacro(E);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-22 04:20:39 +08:00
|
|
|
if (isSelf(E->getSubExpr()))
|
|
|
|
return rewriteToBridgedCast(E, OBC_Bridge);
|
2011-07-27 13:28:18 +08:00
|
|
|
|
|
|
|
CallExpr *callE;
|
|
|
|
if (isPassedToCFRetain(E, callE))
|
|
|
|
return rewriteCastForCFRetain(E, callE);
|
|
|
|
|
|
|
|
ObjCMethodFamily family = getFamilyOfMessage(E->getSubExpr());
|
|
|
|
if (family == OMF_retain)
|
|
|
|
return rewriteToBridgedCast(E, OBC_BridgeRetained);
|
|
|
|
|
|
|
|
if (family == OMF_autorelease || family == OMF_release) {
|
|
|
|
std::string err = "it is not safe to cast to '";
|
2011-09-28 06:38:19 +08:00
|
|
|
err += E->getType().getAsString(Pass.Ctx.getPrintingPolicy());
|
2011-07-27 13:28:18 +08:00
|
|
|
err += "' the result of '";
|
|
|
|
err += family == OMF_autorelease ? "autorelease" : "release";
|
|
|
|
err += "' message; a __bridge cast may result in a pointer to a "
|
|
|
|
"destroyed object and a __bridge_retained may leak the object";
|
|
|
|
Pass.TA.reportError(err, E->getLocStart(),
|
|
|
|
E->getSubExpr()->getSourceRange());
|
|
|
|
Stmt *parent = E;
|
|
|
|
do {
|
|
|
|
parent = StmtMap->getParentIgnoreParenImpCasts(parent);
|
|
|
|
} while (parent && isa<ExprWithCleanups>(parent));
|
|
|
|
|
|
|
|
if (ReturnStmt *retS = dyn_cast_or_null<ReturnStmt>(parent)) {
|
|
|
|
std::string note = "remove the cast and change return type of function "
|
|
|
|
"to '";
|
2011-09-28 06:38:19 +08:00
|
|
|
note += E->getSubExpr()->getType().getAsString(Pass.Ctx.getPrintingPolicy());
|
2011-07-27 13:28:18 +08:00
|
|
|
note += "' to have the object automatically autoreleased";
|
|
|
|
Pass.TA.reportNote(note, retS->getLocStart());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:30 +08:00
|
|
|
Expr *subExpr = E->getSubExpr();
|
|
|
|
|
|
|
|
// Look through pseudo-object expressions.
|
|
|
|
if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(subExpr)) {
|
|
|
|
subExpr = pseudo->getResultExpr();
|
|
|
|
assert(subExpr && "no result for pseudo-object of non-void type?");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImplicitCastExpr *implCE = dyn_cast<ImplicitCastExpr>(subExpr)) {
|
2011-09-10 14:18:15 +08:00
|
|
|
if (implCE->getCastKind() == CK_ARCConsumeObject)
|
2011-07-27 13:28:18 +08:00
|
|
|
return rewriteToBridgedCast(E, OBC_BridgeRetained);
|
2011-09-10 14:18:15 +08:00
|
|
|
if (implCE->getCastKind() == CK_ARCReclaimReturnedObject)
|
2011-07-27 13:28:18 +08:00
|
|
|
return rewriteToBridgedCast(E, OBC_Bridge);
|
|
|
|
}
|
2011-09-15 02:17:09 +08:00
|
|
|
|
|
|
|
bool isConsumed = false;
|
|
|
|
if (isPassedToCParamWithKnownOwnership(E, isConsumed))
|
|
|
|
return rewriteToBridgedCast(E, isConsumed ? OBC_BridgeRetained
|
|
|
|
: OBC_Bridge);
|
2011-07-27 13:28:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ObjCMethodFamily getFamilyOfMessage(Expr *E) {
|
|
|
|
E = E->IgnoreParenCasts();
|
|
|
|
if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E))
|
|
|
|
return ME->getMethodFamily();
|
|
|
|
|
|
|
|
return OMF_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isPassedToCFRetain(Expr *E, CallExpr *&callE) const {
|
|
|
|
if ((callE = dyn_cast_or_null<CallExpr>(
|
|
|
|
StmtMap->getParentIgnoreParenImpCasts(E))))
|
|
|
|
if (FunctionDecl *
|
|
|
|
FD = dyn_cast_or_null<FunctionDecl>(callE->getCalleeDecl()))
|
|
|
|
if (FD->getName() == "CFRetain" && FD->getNumParams() == 1 &&
|
|
|
|
FD->getParent()->isTranslationUnit() &&
|
2013-05-13 08:12:11 +08:00
|
|
|
FD->isExternallyVisible())
|
2011-07-27 13:28:18 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2011-06-22 04:20:39 +08:00
|
|
|
}
|
|
|
|
|
2011-09-15 02:17:09 +08:00
|
|
|
bool isPassedToCParamWithKnownOwnership(Expr *E, bool &isConsumed) const {
|
|
|
|
if (CallExpr *callE = dyn_cast_or_null<CallExpr>(
|
|
|
|
StmtMap->getParentIgnoreParenImpCasts(E)))
|
|
|
|
if (FunctionDecl *
|
|
|
|
FD = dyn_cast_or_null<FunctionDecl>(callE->getCalleeDecl())) {
|
|
|
|
unsigned i = 0;
|
|
|
|
for (unsigned e = callE->getNumArgs(); i != e; ++i) {
|
|
|
|
Expr *arg = callE->getArg(i);
|
|
|
|
if (arg == E || arg->IgnoreParenImpCasts() == E)
|
|
|
|
break;
|
|
|
|
}
|
2013-02-15 01:29:16 +08:00
|
|
|
if (i < callE->getNumArgs() && i < FD->getNumParams()) {
|
2011-09-15 02:17:09 +08:00
|
|
|
ParmVarDecl *PD = FD->getParamDecl(i);
|
2013-12-19 10:39:40 +08:00
|
|
|
if (PD->hasAttr<CFConsumedAttr>()) {
|
2011-09-15 02:17:09 +08:00
|
|
|
isConsumed = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-27 13:28:18 +08:00
|
|
|
bool isSelf(Expr *E) const {
|
2011-06-22 04:20:39 +08:00
|
|
|
E = E->IgnoreParenLValueCasts();
|
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
|
2011-07-27 13:28:18 +08:00
|
|
|
if (ImplicitParamDecl *IPD = dyn_cast<ImplicitParamDecl>(DRE->getDecl()))
|
|
|
|
if (IPD->getIdentifier() == SelfII)
|
|
|
|
return true;
|
|
|
|
|
2011-06-22 04:20:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void trans::rewriteUnbridgedCasts(MigrationPass &pass) {
|
2011-07-27 13:28:18 +08:00
|
|
|
BodyTransform<UnbridgedCastRewriter> trans(pass);
|
2011-06-22 04:20:39 +08:00
|
|
|
trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
|
|
|
|
}
|