2012-03-07 04:06:33 +08:00
|
|
|
//===--- ObjCMT.cpp - ObjC Migrate Tool -----------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-07-19 06:17:33 +08:00
|
|
|
#include "Transforms.h"
|
2013-12-12 10:24:20 +08:00
|
|
|
#include "clang/ARCMigrate/ARCMT.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
#include "clang/ARCMigrate/ARCMTActions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2012-07-05 04:19:54 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
#include "clang/AST/NSAPI.h"
|
2013-01-17 07:54:48 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
#include "clang/Edit/Commit.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Edit/EditedSource.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
#include "clang/Edit/EditsReceiver.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Edit/Rewriters.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/MultiplexConsumer.h"
|
2012-12-04 15:27:05 +08:00
|
|
|
#include "clang/Lex/PPConditionalDirectiveRecord.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
2013-08-16 05:44:38 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/ObjCRetainCount.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2014-11-19 11:06:06 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2013-11-15 00:33:29 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2013-12-12 10:24:20 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLParser.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace arcmt;
|
2013-08-16 05:44:38 +08:00
|
|
|
using namespace ento::objc_retain;
|
2012-03-07 04:06:33 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ObjCMigrateASTConsumer : public ASTConsumer {
|
2013-08-28 06:42:30 +08:00
|
|
|
enum CF_BRIDGING_KIND {
|
|
|
|
CF_BRIDGING_NONE,
|
|
|
|
CF_BRIDGING_ENABLE,
|
|
|
|
CF_BRIDGING_MAY_INCLUDE
|
|
|
|
};
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
void migrateDecl(Decl *D);
|
2013-09-18 03:00:30 +08:00
|
|
|
void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCContainerDecl *D);
|
2013-07-13 06:32:19 +08:00
|
|
|
void migrateProtocolConformance(ASTContext &Ctx,
|
|
|
|
const ObjCImplementationDecl *ImpDecl);
|
2013-10-15 08:00:28 +08:00
|
|
|
void CacheObjCNSIntegerTypedefed(const TypedefDecl *TypedefDcl);
|
2013-10-12 05:34:56 +08:00
|
|
|
bool migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl,
|
2013-07-19 04:11:45 +08:00
|
|
|
const TypedefDecl *TypedefDcl);
|
2013-10-03 06:49:59 +08:00
|
|
|
void migrateAllMethodInstaceType(ASTContext &Ctx, ObjCContainerDecl *CDecl);
|
2013-07-24 07:34:42 +08:00
|
|
|
void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl,
|
|
|
|
ObjCMethodDecl *OM);
|
2013-09-18 03:00:30 +08:00
|
|
|
bool migrateProperty(ASTContext &Ctx, ObjCContainerDecl *D, ObjCMethodDecl *OM);
|
2013-08-31 07:52:08 +08:00
|
|
|
void migrateNsReturnsInnerPointer(ASTContext &Ctx, ObjCMethodDecl *OM);
|
2013-09-25 04:20:52 +08:00
|
|
|
void migratePropertyNsReturnsInnerPointer(ASTContext &Ctx, ObjCPropertyDecl *P);
|
2013-08-03 02:00:53 +08:00
|
|
|
void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl,
|
2013-08-03 04:54:18 +08:00
|
|
|
ObjCMethodDecl *OM,
|
|
|
|
ObjCInstanceTypeFamily OIT_Family = OIT_None);
|
2013-08-13 07:17:13 +08:00
|
|
|
|
2013-08-23 05:40:15 +08:00
|
|
|
void migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl);
|
2013-08-28 06:42:30 +08:00
|
|
|
void AddCFAnnotations(ASTContext &Ctx, const CallEffects &CE,
|
2013-08-28 07:56:54 +08:00
|
|
|
const FunctionDecl *FuncDecl, bool ResultAnnotated);
|
2013-08-28 06:42:30 +08:00
|
|
|
void AddCFAnnotations(ASTContext &Ctx, const CallEffects &CE,
|
2013-08-28 07:56:54 +08:00
|
|
|
const ObjCMethodDecl *MethodDecl, bool ResultAnnotated);
|
2013-08-17 07:35:05 +08:00
|
|
|
|
2013-08-21 06:42:13 +08:00
|
|
|
void AnnotateImplicitBridging(ASTContext &Ctx);
|
|
|
|
|
2013-08-28 06:42:30 +08:00
|
|
|
CF_BRIDGING_KIND migrateAddFunctionAnnotation(ASTContext &Ctx,
|
|
|
|
const FunctionDecl *FuncDecl);
|
2013-08-13 07:17:13 +08:00
|
|
|
|
2013-08-23 02:35:27 +08:00
|
|
|
void migrateARCSafeAnnotation(ASTContext &Ctx, ObjCContainerDecl *CDecl);
|
|
|
|
|
2013-09-04 08:10:06 +08:00
|
|
|
void migrateAddMethodAnnotation(ASTContext &Ctx,
|
|
|
|
const ObjCMethodDecl *MethodDecl);
|
2013-12-11 02:36:49 +08:00
|
|
|
|
|
|
|
void inferDesignatedInitializers(ASTContext &Ctx,
|
|
|
|
const ObjCImplementationDecl *ImplD);
|
2014-10-08 03:01:46 +08:00
|
|
|
|
|
|
|
bool InsertFoundation(ASTContext &Ctx, SourceLocation Loc);
|
2013-12-11 02:36:49 +08:00
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
public:
|
|
|
|
std::string MigrateDir;
|
2013-10-03 01:08:12 +08:00
|
|
|
unsigned ASTMigrateActions;
|
2013-11-14 02:20:31 +08:00
|
|
|
FileID FileId;
|
2013-10-15 08:00:28 +08:00
|
|
|
const TypedefDecl *NSIntegerTypedefed;
|
|
|
|
const TypedefDecl *NSUIntegerTypedefed;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<NSAPI> NSAPIObj;
|
|
|
|
std::unique_ptr<edit::EditedSource> Editor;
|
2012-03-07 04:06:33 +08:00
|
|
|
FileRemapper &Remapper;
|
|
|
|
FileManager &FileMgr;
|
2012-12-04 15:27:05 +08:00
|
|
|
const PPConditionalDirectiveRecord *PPRec;
|
2013-07-04 07:05:00 +08:00
|
|
|
Preprocessor &PP;
|
2012-03-07 04:06:33 +08:00
|
|
|
bool IsOutputFile;
|
2014-10-08 03:01:46 +08:00
|
|
|
bool FoundationIncluded;
|
2013-07-13 06:32:19 +08:00
|
|
|
llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls;
|
2013-08-23 02:35:27 +08:00
|
|
|
llvm::SmallVector<const Decl *, 8> CFFunctionIBCandidates;
|
2014-11-19 11:06:06 +08:00
|
|
|
llvm::StringSet<> WhiteListFilenames;
|
2014-11-19 13:48:40 +08:00
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
ObjCMigrateASTConsumer(StringRef migrateDir,
|
2013-10-03 01:08:12 +08:00
|
|
|
unsigned astMigrateActions,
|
2012-03-07 04:06:33 +08:00
|
|
|
FileRemapper &remapper,
|
|
|
|
FileManager &fileMgr,
|
2012-12-04 15:27:05 +08:00
|
|
|
const PPConditionalDirectiveRecord *PPRec,
|
2013-07-04 07:05:00 +08:00
|
|
|
Preprocessor &PP,
|
2013-11-15 00:33:29 +08:00
|
|
|
bool isOutputFile,
|
|
|
|
ArrayRef<std::string> WhiteList)
|
2012-03-07 04:06:33 +08:00
|
|
|
: MigrateDir(migrateDir),
|
2013-10-03 01:08:12 +08:00
|
|
|
ASTMigrateActions(astMigrateActions),
|
2014-05-07 14:21:57 +08:00
|
|
|
NSIntegerTypedefed(nullptr), NSUIntegerTypedefed(nullptr),
|
2013-10-15 08:00:28 +08:00
|
|
|
Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP),
|
2014-10-08 03:01:46 +08:00
|
|
|
IsOutputFile(isOutputFile),
|
|
|
|
FoundationIncluded(false){
|
2013-11-15 00:33:29 +08:00
|
|
|
|
2014-11-19 11:06:06 +08:00
|
|
|
// FIXME: StringSet should have insert(iter, iter) to use here.
|
|
|
|
for (const std::string &Val : WhiteList)
|
|
|
|
WhiteListFilenames.insert(Val);
|
2013-11-15 00:33:29 +08:00
|
|
|
}
|
2012-03-07 04:06:33 +08:00
|
|
|
|
|
|
|
protected:
|
2014-03-14 14:02:07 +08:00
|
|
|
void Initialize(ASTContext &Context) override {
|
2012-03-07 04:06:33 +08:00
|
|
|
NSAPIObj.reset(new NSAPI(Context));
|
|
|
|
Editor.reset(new edit::EditedSource(Context.getSourceManager(),
|
2012-03-11 15:00:24 +08:00
|
|
|
Context.getLangOpts(),
|
2014-04-28 10:38:51 +08:00
|
|
|
PPRec));
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
bool HandleTopLevelDecl(DeclGroupRef DG) override {
|
2012-03-07 04:06:33 +08:00
|
|
|
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
|
|
|
|
migrateDecl(*I);
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-14 14:02:07 +08:00
|
|
|
void HandleInterestingDecl(DeclGroupRef DG) override {
|
2012-03-07 04:06:33 +08:00
|
|
|
// Ignore decls from the PCH.
|
|
|
|
}
|
2014-03-14 14:02:07 +08:00
|
|
|
void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override {
|
2012-03-07 04:06:33 +08:00
|
|
|
ObjCMigrateASTConsumer::HandleTopLevelDecl(DG);
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void HandleTranslationUnit(ASTContext &Ctx) override;
|
2013-11-15 00:33:29 +08:00
|
|
|
|
|
|
|
bool canModifyFile(StringRef Path) {
|
|
|
|
if (WhiteListFilenames.empty())
|
|
|
|
return true;
|
|
|
|
return WhiteListFilenames.find(llvm::sys::path::filename(Path))
|
|
|
|
!= WhiteListFilenames.end();
|
|
|
|
}
|
2013-12-12 05:39:00 +08:00
|
|
|
bool canModifyFile(const FileEntry *FE) {
|
|
|
|
if (!FE)
|
|
|
|
return false;
|
|
|
|
return canModifyFile(FE->getName());
|
|
|
|
}
|
|
|
|
bool canModifyFile(FileID FID) {
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return false;
|
|
|
|
return canModifyFile(PP.getSourceManager().getFileEntryForID(FID));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool canModify(const Decl *D) {
|
|
|
|
if (!D)
|
|
|
|
return false;
|
|
|
|
if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(D))
|
|
|
|
return canModify(CatImpl->getCategoryDecl());
|
|
|
|
if (const ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D))
|
|
|
|
return canModify(Impl->getClassInterface());
|
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
return canModify(cast<Decl>(MD->getDeclContext()));
|
|
|
|
|
|
|
|
FileID FID = PP.getSourceManager().getFileID(D->getLocation());
|
|
|
|
return canModifyFile(FID);
|
|
|
|
}
|
2012-03-07 04:06:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction,
|
2013-10-03 01:08:12 +08:00
|
|
|
StringRef migrateDir,
|
|
|
|
unsigned migrateAction)
|
2012-03-07 04:06:33 +08:00
|
|
|
: WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir),
|
2013-10-03 01:08:12 +08:00
|
|
|
ObjCMigAction(migrateAction),
|
2014-05-07 14:21:57 +08:00
|
|
|
CompInst(nullptr) {
|
2012-03-07 04:06:33 +08:00
|
|
|
if (MigrateDir.empty())
|
|
|
|
MigrateDir = "."; // user current directory if none is given.
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2012-12-04 15:27:05 +08:00
|
|
|
PPConditionalDirectiveRecord *
|
|
|
|
PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager());
|
2014-09-10 12:53:53 +08:00
|
|
|
CI.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
|
2014-08-11 03:56:51 +08:00
|
|
|
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
|
|
|
|
Consumers.push_back(WrapperFrontendAction::CreateASTConsumer(CI, InFile));
|
|
|
|
Consumers.push_back(llvm::make_unique<ObjCMigrateASTConsumer>(
|
|
|
|
MigrateDir, ObjCMigAction, Remapper, CompInst->getFileManager(), PPRec,
|
2014-08-27 14:28:36 +08:00
|
|
|
CompInst->getPreprocessor(), false, None));
|
2014-08-11 03:56:51 +08:00
|
|
|
return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) {
|
|
|
|
Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(),
|
|
|
|
/*ignoreIfFilesChanges=*/true);
|
|
|
|
CompInst = &CI;
|
|
|
|
CI.getDiagnostics().setIgnoreAllWarnings(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2014-10-10 02:30:56 +08:00
|
|
|
// FIXME. This duplicates one in RewriteObjCFoundationAPI.cpp
|
|
|
|
bool subscriptOperatorNeedsParens(const Expr *FullExpr) {
|
|
|
|
const Expr* Expr = FullExpr->IgnoreImpCasts();
|
|
|
|
if (isa<ArraySubscriptExpr>(Expr) ||
|
|
|
|
isa<CallExpr>(Expr) ||
|
|
|
|
isa<DeclRefExpr>(Expr) ||
|
|
|
|
isa<CXXNamedCastExpr>(Expr) ||
|
|
|
|
isa<CXXConstructExpr>(Expr) ||
|
|
|
|
isa<CXXThisExpr>(Expr) ||
|
|
|
|
isa<CXXTypeidExpr>(Expr) ||
|
|
|
|
isa<CXXUnresolvedConstructExpr>(Expr) ||
|
|
|
|
isa<ObjCMessageExpr>(Expr) ||
|
|
|
|
isa<ObjCPropertyRefExpr>(Expr) ||
|
|
|
|
isa<ObjCProtocolExpr>(Expr) ||
|
|
|
|
isa<MemberExpr>(Expr) ||
|
|
|
|
isa<ObjCIvarRefExpr>(Expr) ||
|
|
|
|
isa<ParenExpr>(FullExpr) ||
|
|
|
|
isa<ParenListExpr>(Expr) ||
|
|
|
|
isa<SizeOfPackExpr>(Expr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief - Rewrite message expression for Objective-C setter and getters into
|
|
|
|
/// property-dot syntax.
|
|
|
|
bool rewriteToPropertyDotSyntax(const ObjCMessageExpr *Msg,
|
|
|
|
Preprocessor &PP,
|
|
|
|
const NSAPI &NS, edit::Commit &commit,
|
|
|
|
const ParentMap *PMap) {
|
|
|
|
if (!Msg || Msg->isImplicit() ||
|
2014-12-10 06:36:47 +08:00
|
|
|
(Msg->getReceiverKind() != ObjCMessageExpr::Instance &&
|
|
|
|
Msg->getReceiverKind() != ObjCMessageExpr::SuperInstance))
|
2014-10-10 02:30:56 +08:00
|
|
|
return false;
|
|
|
|
const ObjCMethodDecl *Method = Msg->getMethodDecl();
|
|
|
|
if (!Method)
|
|
|
|
return false;
|
|
|
|
if (!Method->isPropertyAccessor())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ObjCInterfaceDecl *IFace =
|
|
|
|
NS.getASTContext().getObjContainingInterface(Method);
|
|
|
|
if (!IFace)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ObjCPropertyDecl *Prop = Method->findPropertyDecl();
|
|
|
|
if (!Prop)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceRange MsgRange = Msg->getSourceRange();
|
2014-12-10 06:36:47 +08:00
|
|
|
bool ReceiverIsSuper =
|
|
|
|
(Msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
|
|
|
|
// for 'super' receiver is nullptr.
|
2014-10-10 02:30:56 +08:00
|
|
|
const Expr *receiver = Msg->getInstanceReceiver();
|
2014-12-10 06:36:47 +08:00
|
|
|
bool NeedsParen =
|
|
|
|
ReceiverIsSuper ? false : subscriptOperatorNeedsParens(receiver);
|
2014-10-10 02:30:56 +08:00
|
|
|
bool IsGetter = (Msg->getNumArgs() == 0);
|
|
|
|
if (IsGetter) {
|
|
|
|
// Find space location range between receiver expression and getter method.
|
2014-12-10 06:36:47 +08:00
|
|
|
SourceLocation BegLoc =
|
|
|
|
ReceiverIsSuper ? Msg->getSuperLoc() : receiver->getLocEnd();
|
2014-10-10 02:30:56 +08:00
|
|
|
BegLoc = PP.getLocForEndOfToken(BegLoc);
|
|
|
|
SourceLocation EndLoc = Msg->getSelectorLoc(0);
|
|
|
|
SourceRange SpaceRange(BegLoc, EndLoc);
|
|
|
|
std::string PropertyDotString;
|
|
|
|
// rewrite getter method expression into: receiver.property or
|
|
|
|
// (receiver).property
|
|
|
|
if (NeedsParen) {
|
|
|
|
commit.insertBefore(receiver->getLocStart(), "(");
|
|
|
|
PropertyDotString = ").";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PropertyDotString = ".";
|
|
|
|
PropertyDotString += Prop->getName();
|
|
|
|
commit.replace(SpaceRange, PropertyDotString);
|
|
|
|
|
|
|
|
// remove '[' ']'
|
|
|
|
commit.replace(SourceRange(MsgRange.getBegin(), MsgRange.getBegin()), "");
|
|
|
|
commit.replace(SourceRange(MsgRange.getEnd(), MsgRange.getEnd()), "");
|
|
|
|
} else {
|
|
|
|
if (NeedsParen)
|
2014-12-10 06:36:47 +08:00
|
|
|
commit.insertWrap("(", receiver->getSourceRange(), ")");
|
2014-10-10 02:30:56 +08:00
|
|
|
std::string PropertyDotString = ".";
|
|
|
|
PropertyDotString += Prop->getName();
|
|
|
|
PropertyDotString += " =";
|
|
|
|
const Expr*const* Args = Msg->getArgs();
|
|
|
|
const Expr *RHS = Args[0];
|
|
|
|
if (!RHS)
|
|
|
|
return false;
|
2014-12-10 06:36:47 +08:00
|
|
|
SourceLocation BegLoc =
|
|
|
|
ReceiverIsSuper ? Msg->getSuperLoc() : receiver->getLocEnd();
|
2014-10-10 02:30:56 +08:00
|
|
|
BegLoc = PP.getLocForEndOfToken(BegLoc);
|
|
|
|
SourceLocation EndLoc = RHS->getLocStart();
|
|
|
|
EndLoc = EndLoc.getLocWithOffset(-1);
|
|
|
|
SourceRange Range(BegLoc, EndLoc);
|
|
|
|
commit.replace(Range, PropertyDotString);
|
|
|
|
// remove '[' ']'
|
|
|
|
commit.replace(SourceRange(MsgRange.getBegin(), MsgRange.getBegin()), "");
|
|
|
|
commit.replace(SourceRange(MsgRange.getEnd(), MsgRange.getEnd()), "");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> {
|
|
|
|
ObjCMigrateASTConsumer &Consumer;
|
2013-01-17 07:54:48 +08:00
|
|
|
ParentMap &PMap;
|
2012-03-07 04:06:33 +08:00
|
|
|
|
|
|
|
public:
|
2013-01-17 07:54:48 +08:00
|
|
|
ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap)
|
|
|
|
: Consumer(consumer), PMap(PMap) { }
|
2012-03-07 04:06:33 +08:00
|
|
|
|
|
|
|
bool shouldVisitTemplateInstantiations() const { return false; }
|
|
|
|
bool shouldWalkTypesOfTypeLocs() const { return false; }
|
|
|
|
|
|
|
|
bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
|
2013-10-03 01:08:12 +08:00
|
|
|
if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Literals) {
|
2012-03-07 04:06:33 +08:00
|
|
|
edit::Commit commit(*Consumer.Editor);
|
2013-01-17 07:54:48 +08:00
|
|
|
edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap);
|
2012-03-07 04:06:33 +08:00
|
|
|
Consumer.Editor->commit(commit);
|
|
|
|
}
|
|
|
|
|
2013-10-03 01:08:12 +08:00
|
|
|
if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Subscripting) {
|
2012-03-07 04:06:33 +08:00
|
|
|
edit::Commit commit(*Consumer.Editor);
|
|
|
|
edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit);
|
|
|
|
Consumer.Editor->commit(commit);
|
|
|
|
}
|
|
|
|
|
2014-11-01 05:19:45 +08:00
|
|
|
if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_PropertyDotSyntax) {
|
2014-10-10 02:30:56 +08:00
|
|
|
edit::Commit commit(*Consumer.Editor);
|
|
|
|
rewriteToPropertyDotSyntax(E, Consumer.PP, *Consumer.NSAPIObj,
|
|
|
|
commit, &PMap);
|
|
|
|
Consumer.Editor->commit(commit);
|
|
|
|
}
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TraverseObjCMessageExpr(ObjCMessageExpr *E) {
|
|
|
|
// Do depth first; we want to rewrite the subexpressions first so that if
|
|
|
|
// we have to move expressions we will move them already rewritten.
|
|
|
|
for (Stmt::child_range range = E->children(); range; ++range)
|
|
|
|
if (!TraverseStmt(*range))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return WalkUpFromObjCMessageExpr(E);
|
|
|
|
}
|
|
|
|
};
|
2013-01-17 07:54:48 +08:00
|
|
|
|
|
|
|
class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> {
|
|
|
|
ObjCMigrateASTConsumer &Consumer;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<ParentMap> PMap;
|
2013-01-17 07:54:48 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { }
|
|
|
|
|
|
|
|
bool shouldVisitTemplateInstantiations() const { return false; }
|
|
|
|
bool shouldWalkTypesOfTypeLocs() const { return false; }
|
|
|
|
|
|
|
|
bool TraverseStmt(Stmt *S) {
|
|
|
|
PMap.reset(new ParentMap(S));
|
|
|
|
ObjCMigrator(Consumer, *PMap).TraverseStmt(S);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCMigrateASTConsumer::migrateDecl(Decl *D) {
|
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
if (isa<ObjCMethodDecl>(D))
|
|
|
|
return; // Wait for the ObjC container declaration.
|
|
|
|
|
2013-01-17 07:54:48 +08:00
|
|
|
BodyMigrator(*this).TraverseDecl(D);
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
|
|
|
|
2013-10-17 03:48:23 +08:00
|
|
|
static void append_attr(std::string &PropertyString, const char *attr,
|
|
|
|
bool &LParenAdded) {
|
|
|
|
if (!LParenAdded) {
|
|
|
|
PropertyString += "(";
|
|
|
|
LParenAdded = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PropertyString += ", ";
|
2013-08-09 04:51:58 +08:00
|
|
|
PropertyString += attr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 04:14:24 +08:00
|
|
|
static
|
|
|
|
void MigrateBlockOrFunctionPointerTypeVariable(std::string & PropertyString,
|
|
|
|
const std::string& TypeString,
|
|
|
|
const char *name) {
|
|
|
|
const char *argPtr = TypeString.c_str();
|
|
|
|
int paren = 0;
|
|
|
|
while (*argPtr) {
|
|
|
|
switch (*argPtr) {
|
|
|
|
case '(':
|
|
|
|
PropertyString += *argPtr;
|
|
|
|
paren++;
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
PropertyString += *argPtr;
|
|
|
|
paren--;
|
|
|
|
break;
|
|
|
|
case '^':
|
2013-10-09 05:32:16 +08:00
|
|
|
case '*':
|
|
|
|
PropertyString += (*argPtr);
|
|
|
|
if (paren == 1) {
|
2013-10-09 04:14:24 +08:00
|
|
|
PropertyString += name;
|
2013-10-09 05:32:16 +08:00
|
|
|
name = "";
|
|
|
|
}
|
2013-10-09 04:14:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PropertyString += *argPtr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
argPtr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 02:28:58 +08:00
|
|
|
static const char *PropertyMemoryAttribute(ASTContext &Context, QualType ArgType) {
|
|
|
|
Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime();
|
|
|
|
bool RetainableObject = ArgType->isObjCRetainableType();
|
2013-12-12 09:02:00 +08:00
|
|
|
if (RetainableObject &&
|
|
|
|
(propertyLifetime == Qualifiers::OCL_Strong
|
|
|
|
|| propertyLifetime == Qualifiers::OCL_None)) {
|
2013-11-15 02:28:58 +08:00
|
|
|
if (const ObjCObjectPointerType *ObjPtrTy =
|
|
|
|
ArgType->getAs<ObjCObjectPointerType>()) {
|
|
|
|
ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
|
|
|
|
if (IDecl &&
|
|
|
|
IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying")))
|
|
|
|
return "copy";
|
|
|
|
else
|
2013-11-21 08:58:17 +08:00
|
|
|
return "strong";
|
2013-11-15 02:28:58 +08:00
|
|
|
}
|
|
|
|
else if (ArgType->isBlockPointerType())
|
|
|
|
return "copy";
|
|
|
|
} else if (propertyLifetime == Qualifiers::OCL_Weak)
|
|
|
|
// TODO. More precise determination of 'weak' attribute requires
|
|
|
|
// looking into setter's implementation for backing weak ivar.
|
|
|
|
return "weak";
|
|
|
|
else if (RetainableObject)
|
2013-11-21 08:58:17 +08:00
|
|
|
return ArgType->isBlockPointerType() ? "copy" : "strong";
|
2014-05-07 14:21:57 +08:00
|
|
|
return nullptr;
|
2013-11-15 02:28:58 +08:00
|
|
|
}
|
2013-10-09 04:14:24 +08:00
|
|
|
|
2013-10-17 06:35:19 +08:00
|
|
|
static void rewriteToObjCProperty(const ObjCMethodDecl *Getter,
|
2013-07-19 06:17:33 +08:00
|
|
|
const ObjCMethodDecl *Setter,
|
2013-08-09 04:51:58 +08:00
|
|
|
const NSAPI &NS, edit::Commit &commit,
|
2013-10-10 03:06:08 +08:00
|
|
|
unsigned LengthOfPrefix,
|
2013-11-13 08:08:36 +08:00
|
|
|
bool Atomic, bool UseNsIosOnlyMacro,
|
|
|
|
bool AvailabilityArgsMatch) {
|
2013-07-19 06:17:33 +08:00
|
|
|
ASTContext &Context = NS.getASTContext();
|
2013-10-17 03:48:23 +08:00
|
|
|
bool LParenAdded = false;
|
|
|
|
std::string PropertyString = "@property ";
|
2013-11-13 08:08:36 +08:00
|
|
|
if (UseNsIosOnlyMacro && Context.Idents.get("NS_NONATOMIC_IOSONLY").hasMacroDefinition()) {
|
2013-11-13 03:25:50 +08:00
|
|
|
PropertyString += "(NS_NONATOMIC_IOSONLY";
|
|
|
|
LParenAdded = true;
|
|
|
|
} else if (!Atomic) {
|
2013-10-17 03:48:23 +08:00
|
|
|
PropertyString += "(nonatomic";
|
|
|
|
LParenAdded = true;
|
|
|
|
}
|
|
|
|
|
2013-08-07 02:06:23 +08:00
|
|
|
std::string PropertyNameString = Getter->getNameAsString();
|
|
|
|
StringRef PropertyName(PropertyNameString);
|
2013-09-12 01:05:15 +08:00
|
|
|
if (LengthOfPrefix > 0) {
|
2013-10-17 03:48:23 +08:00
|
|
|
if (!LParenAdded) {
|
|
|
|
PropertyString += "(getter=";
|
|
|
|
LParenAdded = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PropertyString += ", getter=";
|
2013-08-09 04:51:58 +08:00
|
|
|
PropertyString += PropertyNameString;
|
|
|
|
}
|
2013-08-29 07:22:46 +08:00
|
|
|
// Property with no setter may be suggested as a 'readonly' property.
|
2013-11-22 01:49:34 +08:00
|
|
|
if (!Setter)
|
2013-11-15 02:28:58 +08:00
|
|
|
append_attr(PropertyString, "readonly", LParenAdded);
|
2013-11-22 01:49:34 +08:00
|
|
|
|
2013-10-09 04:14:24 +08:00
|
|
|
|
2013-10-17 02:52:17 +08:00
|
|
|
// Short circuit 'delegate' properties that contain the name "delegate" or
|
|
|
|
// "dataSource", or have exact name "target" to have 'assign' attribute.
|
2013-08-07 02:06:23 +08:00
|
|
|
if (PropertyName.equals("target") ||
|
|
|
|
(PropertyName.find("delegate") != StringRef::npos) ||
|
2013-10-17 04:44:26 +08:00
|
|
|
(PropertyName.find("dataSource") != StringRef::npos)) {
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType QT = Getter->getReturnType();
|
2013-10-17 04:44:26 +08:00
|
|
|
if (!QT->isRealType())
|
|
|
|
append_attr(PropertyString, "assign", LParenAdded);
|
2013-11-22 01:49:34 +08:00
|
|
|
} else if (!Setter) {
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType ResType = Context.getCanonicalType(Getter->getReturnType());
|
2013-11-22 01:49:34 +08:00
|
|
|
if (const char *MemoryManagementAttr = PropertyMemoryAttribute(Context, ResType))
|
|
|
|
append_attr(PropertyString, MemoryManagementAttr, LParenAdded);
|
|
|
|
} else {
|
2013-08-07 02:06:23 +08:00
|
|
|
const ParmVarDecl *argDecl = *Setter->param_begin();
|
|
|
|
QualType ArgType = Context.getCanonicalType(argDecl->getType());
|
2013-11-15 02:28:58 +08:00
|
|
|
if (const char *MemoryManagementAttr = PropertyMemoryAttribute(Context, ArgType))
|
|
|
|
append_attr(PropertyString, MemoryManagementAttr, LParenAdded);
|
2013-08-07 02:06:23 +08:00
|
|
|
}
|
2013-10-17 03:48:23 +08:00
|
|
|
if (LParenAdded)
|
|
|
|
PropertyString += ')';
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType RT = Getter->getReturnType();
|
2013-08-09 04:51:58 +08:00
|
|
|
if (!isa<TypedefType>(RT)) {
|
|
|
|
// strip off any ARC lifetime qualifier.
|
|
|
|
QualType CanResultTy = Context.getCanonicalType(RT);
|
|
|
|
if (CanResultTy.getQualifiers().hasObjCLifetime()) {
|
|
|
|
Qualifiers Qs = CanResultTy.getQualifiers();
|
|
|
|
Qs.removeObjCLifetime();
|
|
|
|
RT = Context.getQualifiedType(CanResultTy.getUnqualifiedType(), Qs);
|
|
|
|
}
|
2013-07-19 06:17:33 +08:00
|
|
|
}
|
|
|
|
PropertyString += " ";
|
2013-10-09 04:14:24 +08:00
|
|
|
PrintingPolicy SubPolicy(Context.getPrintingPolicy());
|
|
|
|
SubPolicy.SuppressStrongLifetime = true;
|
2013-10-10 01:37:28 +08:00
|
|
|
SubPolicy.SuppressLifetimeQualifiers = true;
|
2013-10-09 04:14:24 +08:00
|
|
|
std::string TypeString = RT.getAsString(SubPolicy);
|
2013-09-12 01:05:15 +08:00
|
|
|
if (LengthOfPrefix > 0) {
|
2013-08-09 04:51:58 +08:00
|
|
|
// property name must strip off "is" and lower case the first character
|
|
|
|
// after that; e.g. isContinuous will become continuous.
|
|
|
|
StringRef PropertyNameStringRef(PropertyNameString);
|
2013-09-12 01:05:15 +08:00
|
|
|
PropertyNameStringRef = PropertyNameStringRef.drop_front(LengthOfPrefix);
|
2013-08-09 04:51:58 +08:00
|
|
|
PropertyNameString = PropertyNameStringRef;
|
2013-10-09 04:14:24 +08:00
|
|
|
bool NoLowering = (isUppercase(PropertyNameString[0]) &&
|
|
|
|
PropertyNameString.size() > 1 &&
|
|
|
|
isUppercase(PropertyNameString[1]));
|
2013-09-12 02:27:16 +08:00
|
|
|
if (!NoLowering)
|
2013-10-09 04:14:24 +08:00
|
|
|
PropertyNameString[0] = toLowercase(PropertyNameString[0]);
|
2013-08-09 04:51:58 +08:00
|
|
|
}
|
2013-10-09 04:14:24 +08:00
|
|
|
if (RT->isBlockPointerType() || RT->isFunctionPointerType())
|
|
|
|
MigrateBlockOrFunctionPointerTypeVariable(PropertyString,
|
|
|
|
TypeString,
|
|
|
|
PropertyNameString.c_str());
|
|
|
|
else {
|
|
|
|
char LastChar = TypeString[TypeString.size()-1];
|
|
|
|
PropertyString += TypeString;
|
|
|
|
if (LastChar != '*')
|
|
|
|
PropertyString += ' ';
|
2013-08-09 04:51:58 +08:00
|
|
|
PropertyString += PropertyNameString;
|
2013-10-09 04:14:24 +08:00
|
|
|
}
|
2013-09-07 07:45:20 +08:00
|
|
|
SourceLocation StartGetterSelectorLoc = Getter->getSelectorStartLoc();
|
|
|
|
Selector GetterSelector = Getter->getSelector();
|
|
|
|
|
|
|
|
SourceLocation EndGetterSelectorLoc =
|
|
|
|
StartGetterSelectorLoc.getLocWithOffset(GetterSelector.getNameForSlot(0).size());
|
2013-07-19 06:17:33 +08:00
|
|
|
commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(),
|
2013-09-07 07:45:20 +08:00
|
|
|
EndGetterSelectorLoc),
|
2013-07-19 06:17:33 +08:00
|
|
|
PropertyString);
|
2013-11-01 08:26:48 +08:00
|
|
|
if (Setter && AvailabilityArgsMatch) {
|
2013-08-29 07:22:46 +08:00
|
|
|
SourceLocation EndLoc = Setter->getDeclaratorEndLoc();
|
|
|
|
// Get location past ';'
|
|
|
|
EndLoc = EndLoc.getLocWithOffset(1);
|
2013-10-17 06:35:19 +08:00
|
|
|
SourceLocation BeginOfSetterDclLoc = Setter->getLocStart();
|
2013-12-06 00:25:25 +08:00
|
|
|
// FIXME. This assumes that setter decl; is immediately preceded by eoln.
|
2013-10-17 06:35:19 +08:00
|
|
|
// It is trying to remove the setter method decl. line entirely.
|
|
|
|
BeginOfSetterDclLoc = BeginOfSetterDclLoc.getLocWithOffset(-1);
|
|
|
|
commit.remove(SourceRange(BeginOfSetterDclLoc, EndLoc));
|
2013-08-29 07:22:46 +08:00
|
|
|
}
|
2013-07-19 06:17:33 +08:00
|
|
|
}
|
|
|
|
|
2013-11-20 02:17:31 +08:00
|
|
|
static bool IsCategoryNameWithDeprecatedSuffix(ObjCContainerDecl *D) {
|
|
|
|
if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D)) {
|
|
|
|
StringRef Name = CatDecl->getName();
|
|
|
|
return Name.endswith("Deprecated");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-04 07:05:00 +08:00
|
|
|
void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx,
|
2013-09-18 03:00:30 +08:00
|
|
|
ObjCContainerDecl *D) {
|
2013-11-20 02:17:31 +08:00
|
|
|
if (D->isDeprecated() || IsCategoryNameWithDeprecatedSuffix(D))
|
2013-09-18 05:56:04 +08:00
|
|
|
return;
|
2013-11-20 02:17:31 +08:00
|
|
|
|
2014-03-14 03:03:34 +08:00
|
|
|
for (auto *Method : D->methods()) {
|
2013-09-18 05:56:04 +08:00
|
|
|
if (Method->isDeprecated())
|
|
|
|
continue;
|
2013-11-08 09:15:17 +08:00
|
|
|
bool PropertyInferred = migrateProperty(Ctx, D, Method);
|
|
|
|
// If a property is inferred, do not attempt to attach NS_RETURNS_INNER_POINTER to
|
|
|
|
// the getter method as it ends up on the property itself which we don't want
|
|
|
|
// to do unless -objcmt-returns-innerpointer-property option is on.
|
|
|
|
if (!PropertyInferred ||
|
|
|
|
(ASTMigrateActions & FrontendOptions::ObjCMT_ReturnsInnerPointerProperty))
|
2013-11-08 10:00:22 +08:00
|
|
|
if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation)
|
|
|
|
migrateNsReturnsInnerPointer(Ctx, Method);
|
2013-09-25 04:20:52 +08:00
|
|
|
}
|
2013-11-06 06:28:30 +08:00
|
|
|
if (!(ASTMigrateActions & FrontendOptions::ObjCMT_ReturnsInnerPointerProperty))
|
|
|
|
return;
|
|
|
|
|
2014-03-14 03:11:50 +08:00
|
|
|
for (auto *Prop : D->properties()) {
|
2013-10-03 06:49:59 +08:00
|
|
|
if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) &&
|
2013-10-31 08:06:58 +08:00
|
|
|
!Prop->isDeprecated())
|
2013-09-25 04:20:52 +08:00
|
|
|
migratePropertyNsReturnsInnerPointer(Ctx, Prop);
|
2013-07-04 07:05:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 08:06:58 +08:00
|
|
|
static bool
|
2013-07-14 01:16:41 +08:00
|
|
|
ClassImplementsAllMethodsAndProperties(ASTContext &Ctx,
|
2013-07-13 08:04:20 +08:00
|
|
|
const ObjCImplementationDecl *ImpDecl,
|
2013-07-16 05:22:08 +08:00
|
|
|
const ObjCInterfaceDecl *IDecl,
|
2013-07-13 08:04:20 +08:00
|
|
|
ObjCProtocolDecl *Protocol) {
|
2013-07-16 05:22:08 +08:00
|
|
|
// In auto-synthesis, protocol properties are not synthesized. So,
|
|
|
|
// a conforming protocol must have its required properties declared
|
|
|
|
// in class interface.
|
2013-07-23 07:50:04 +08:00
|
|
|
bool HasAtleastOneRequiredProperty = false;
|
2013-07-16 05:22:08 +08:00
|
|
|
if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition())
|
2014-03-14 03:11:50 +08:00
|
|
|
for (const auto *Property : PDecl->properties()) {
|
2013-07-16 05:22:08 +08:00
|
|
|
if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
|
|
|
|
continue;
|
2013-07-23 07:50:04 +08:00
|
|
|
HasAtleastOneRequiredProperty = true;
|
2013-07-16 05:22:08 +08:00
|
|
|
DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName());
|
2013-07-17 05:59:42 +08:00
|
|
|
if (R.size() == 0) {
|
|
|
|
// Relax the rule and look into class's implementation for a synthesize
|
|
|
|
// or dynamic declaration. Class is implementing a property coming from
|
|
|
|
// another protocol. This still makes the target protocol as conforming.
|
|
|
|
if (!ImpDecl->FindPropertyImplDecl(
|
|
|
|
Property->getDeclName().getAsIdentifierInfo()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (ObjCPropertyDecl *ClassProperty = dyn_cast<ObjCPropertyDecl>(R[0])) {
|
|
|
|
if ((ClassProperty->getPropertyAttributes()
|
|
|
|
!= Property->getPropertyAttributes()) ||
|
|
|
|
!Ctx.hasSameType(ClassProperty->getType(), Property->getType()))
|
2013-07-16 05:22:08 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 05:59:42 +08:00
|
|
|
else
|
|
|
|
return false;
|
2013-07-16 05:22:08 +08:00
|
|
|
}
|
2013-07-23 07:50:04 +08:00
|
|
|
|
2013-07-16 05:22:08 +08:00
|
|
|
// At this point, all required properties in this protocol conform to those
|
|
|
|
// declared in the class.
|
|
|
|
// Check that class implements the required methods of the protocol too.
|
2013-07-23 07:50:04 +08:00
|
|
|
bool HasAtleastOneRequiredMethod = false;
|
2013-07-16 08:20:21 +08:00
|
|
|
if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) {
|
|
|
|
if (PDecl->meth_begin() == PDecl->meth_end())
|
2013-07-23 07:50:04 +08:00
|
|
|
return HasAtleastOneRequiredProperty;
|
2014-03-14 03:03:34 +08:00
|
|
|
for (const auto *MD : PDecl->methods()) {
|
2013-07-16 08:20:21 +08:00
|
|
|
if (MD->isImplicit())
|
|
|
|
continue;
|
2013-07-16 05:22:08 +08:00
|
|
|
if (MD->getImplementationControl() == ObjCMethodDecl::Optional)
|
|
|
|
continue;
|
|
|
|
DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName());
|
2013-07-16 08:20:21 +08:00
|
|
|
if (R.size() == 0)
|
|
|
|
return false;
|
2013-07-23 07:50:04 +08:00
|
|
|
bool match = false;
|
|
|
|
HasAtleastOneRequiredMethod = true;
|
2013-07-16 05:22:08 +08:00
|
|
|
for (unsigned I = 0, N = R.size(); I != N; ++I)
|
|
|
|
if (ObjCMethodDecl *ImpMD = dyn_cast<ObjCMethodDecl>(R[0]))
|
|
|
|
if (Ctx.ObjCMethodsAreEqual(MD, ImpMD)) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!match)
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-16 08:20:21 +08:00
|
|
|
}
|
2013-07-23 07:50:04 +08:00
|
|
|
if (HasAtleastOneRequiredProperty || HasAtleastOneRequiredMethod)
|
|
|
|
return true;
|
|
|
|
return false;
|
2013-07-13 08:04:20 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:17:33 +08:00
|
|
|
static bool rewriteToObjCInterfaceDecl(const ObjCInterfaceDecl *IDecl,
|
|
|
|
llvm::SmallVectorImpl<ObjCProtocolDecl*> &ConformingProtocols,
|
|
|
|
const NSAPI &NS, edit::Commit &commit) {
|
|
|
|
const ObjCList<ObjCProtocolDecl> &Protocols = IDecl->getReferencedProtocols();
|
|
|
|
std::string ClassString;
|
|
|
|
SourceLocation EndLoc =
|
|
|
|
IDecl->getSuperClass() ? IDecl->getSuperClassLoc() : IDecl->getLocation();
|
|
|
|
|
|
|
|
if (Protocols.empty()) {
|
|
|
|
ClassString = '<';
|
|
|
|
for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
|
|
|
|
ClassString += ConformingProtocols[i]->getNameAsString();
|
|
|
|
if (i != (e-1))
|
|
|
|
ClassString += ", ";
|
|
|
|
}
|
|
|
|
ClassString += "> ";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ClassString = ", ";
|
|
|
|
for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
|
|
|
|
ClassString += ConformingProtocols[i]->getNameAsString();
|
|
|
|
if (i != (e-1))
|
|
|
|
ClassString += ", ";
|
|
|
|
}
|
|
|
|
ObjCInterfaceDecl::protocol_loc_iterator PL = IDecl->protocol_loc_end() - 1;
|
|
|
|
EndLoc = *PL;
|
|
|
|
}
|
|
|
|
|
|
|
|
commit.insertAfterToken(EndLoc, ClassString);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-07 07:50:37 +08:00
|
|
|
static StringRef GetUnsignedName(StringRef NSIntegerName) {
|
|
|
|
StringRef UnsignedName = llvm::StringSwitch<StringRef>(NSIntegerName)
|
|
|
|
.Case("int8_t", "uint8_t")
|
|
|
|
.Case("int16_t", "uint16_t")
|
|
|
|
.Case("int32_t", "uint32_t")
|
|
|
|
.Case("NSInteger", "NSUInteger")
|
|
|
|
.Case("int64_t", "uint64_t")
|
|
|
|
.Default(NSIntegerName);
|
|
|
|
return UnsignedName;
|
|
|
|
}
|
|
|
|
|
2013-07-19 06:17:33 +08:00
|
|
|
static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl,
|
|
|
|
const TypedefDecl *TypedefDcl,
|
2013-07-19 09:05:49 +08:00
|
|
|
const NSAPI &NS, edit::Commit &commit,
|
2014-10-07 07:50:37 +08:00
|
|
|
StringRef NSIntegerName,
|
2013-08-31 01:46:01 +08:00
|
|
|
bool NSOptions) {
|
|
|
|
std::string ClassString;
|
2014-10-07 07:50:37 +08:00
|
|
|
if (NSOptions) {
|
|
|
|
ClassString = "typedef NS_OPTIONS(";
|
|
|
|
ClassString += GetUnsignedName(NSIntegerName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ClassString = "typedef NS_ENUM(";
|
|
|
|
ClassString += NSIntegerName;
|
|
|
|
}
|
|
|
|
ClassString += ", ";
|
2013-08-31 01:46:01 +08:00
|
|
|
|
2013-07-19 06:17:33 +08:00
|
|
|
ClassString += TypedefDcl->getIdentifier()->getName();
|
|
|
|
ClassString += ')';
|
|
|
|
SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart());
|
|
|
|
commit.replace(R, ClassString);
|
2013-10-15 08:00:28 +08:00
|
|
|
SourceLocation EndOfEnumDclLoc = EnumDcl->getLocEnd();
|
|
|
|
EndOfEnumDclLoc = trans::findSemiAfterLocation(EndOfEnumDclLoc,
|
|
|
|
NS.getASTContext(), /*IsDecl*/true);
|
|
|
|
if (!EndOfEnumDclLoc.isInvalid()) {
|
|
|
|
SourceRange EnumDclRange(EnumDcl->getLocStart(), EndOfEnumDclLoc);
|
|
|
|
commit.insertFromRange(TypedefDcl->getLocStart(), EnumDclRange);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceLocation EndTypedefDclLoc = TypedefDcl->getLocEnd();
|
|
|
|
EndTypedefDclLoc = trans::findSemiAfterLocation(EndTypedefDclLoc,
|
|
|
|
NS.getASTContext(), /*IsDecl*/true);
|
|
|
|
if (!EndTypedefDclLoc.isInvalid()) {
|
|
|
|
SourceRange TDRange(TypedefDcl->getLocStart(), EndTypedefDclLoc);
|
|
|
|
commit.remove(TDRange);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
EndOfEnumDclLoc = trans::findLocationAfterSemi(EnumDcl->getLocEnd(), NS.getASTContext(),
|
2013-10-12 01:35:22 +08:00
|
|
|
/*IsDecl*/true);
|
2013-10-15 08:00:28 +08:00
|
|
|
if (!EndOfEnumDclLoc.isInvalid()) {
|
|
|
|
SourceLocation BeginOfEnumDclLoc = EnumDcl->getLocStart();
|
2013-12-06 00:25:25 +08:00
|
|
|
// FIXME. This assumes that enum decl; is immediately preceded by eoln.
|
2013-10-15 08:00:28 +08:00
|
|
|
// It is trying to remove the enum decl. lines entirely.
|
|
|
|
BeginOfEnumDclLoc = BeginOfEnumDclLoc.getLocWithOffset(-1);
|
|
|
|
commit.remove(SourceRange(BeginOfEnumDclLoc, EndOfEnumDclLoc));
|
2013-07-19 06:17:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-11 02:25:24 +08:00
|
|
|
static void rewriteToNSMacroDecl(ASTContext &Ctx,
|
|
|
|
const EnumDecl *EnumDcl,
|
2013-07-20 04:18:36 +08:00
|
|
|
const TypedefDecl *TypedefDcl,
|
2013-07-23 02:53:45 +08:00
|
|
|
const NSAPI &NS, edit::Commit &commit,
|
|
|
|
bool IsNSIntegerType) {
|
2014-12-11 02:25:24 +08:00
|
|
|
QualType EnumUnderlyingT = EnumDcl->getPromotionType();
|
|
|
|
assert(!EnumUnderlyingT.isNull()
|
|
|
|
&& "rewriteToNSMacroDecl - underlying enum type is null");
|
|
|
|
|
|
|
|
PrintingPolicy Policy(Ctx.getPrintingPolicy());
|
|
|
|
std::string TypeString = EnumUnderlyingT.getAsString(Policy);
|
|
|
|
std::string ClassString = IsNSIntegerType ? "NS_ENUM(" : "NS_OPTIONS(";
|
|
|
|
ClassString += TypeString;
|
|
|
|
ClassString += ", ";
|
|
|
|
|
2013-07-20 04:18:36 +08:00
|
|
|
ClassString += TypedefDcl->getIdentifier()->getName();
|
|
|
|
ClassString += ')';
|
|
|
|
SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart());
|
|
|
|
commit.replace(R, ClassString);
|
2014-10-02 04:46:32 +08:00
|
|
|
// This is to remove spaces between '}' and typedef name.
|
|
|
|
SourceLocation StartTypedefLoc = EnumDcl->getLocEnd();
|
|
|
|
StartTypedefLoc = StartTypedefLoc.getLocWithOffset(+1);
|
|
|
|
SourceLocation EndTypedefLoc = TypedefDcl->getLocEnd();
|
|
|
|
|
|
|
|
commit.remove(SourceRange(StartTypedefLoc, EndTypedefLoc));
|
2013-07-20 04:18:36 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 08:10:37 +08:00
|
|
|
static bool UseNSOptionsMacro(Preprocessor &PP, ASTContext &Ctx,
|
2013-07-23 02:53:45 +08:00
|
|
|
const EnumDecl *EnumDcl) {
|
|
|
|
bool PowerOfTwo = true;
|
2013-09-24 04:27:06 +08:00
|
|
|
bool AllHexdecimalEnumerator = true;
|
2013-08-16 02:46:37 +08:00
|
|
|
uint64_t MaxPowerOfTwoVal = 0;
|
2014-03-09 02:45:14 +08:00
|
|
|
for (auto Enumerator : EnumDcl->enumerators()) {
|
2013-07-23 02:53:45 +08:00
|
|
|
const Expr *InitExpr = Enumerator->getInitExpr();
|
|
|
|
if (!InitExpr) {
|
|
|
|
PowerOfTwo = false;
|
2013-09-24 04:27:06 +08:00
|
|
|
AllHexdecimalEnumerator = false;
|
2013-07-23 02:53:45 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-09-18 07:32:51 +08:00
|
|
|
InitExpr = InitExpr->IgnoreParenCasts();
|
2013-07-23 02:53:45 +08:00
|
|
|
if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr))
|
|
|
|
if (BO->isShiftOp() || BO->isBitwiseOp())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
uint64_t EnumVal = Enumerator->getInitVal().getZExtValue();
|
2013-08-16 02:46:37 +08:00
|
|
|
if (PowerOfTwo && EnumVal) {
|
|
|
|
if (!llvm::isPowerOf2_64(EnumVal))
|
|
|
|
PowerOfTwo = false;
|
|
|
|
else if (EnumVal > MaxPowerOfTwoVal)
|
|
|
|
MaxPowerOfTwoVal = EnumVal;
|
|
|
|
}
|
2013-09-24 04:27:06 +08:00
|
|
|
if (AllHexdecimalEnumerator && EnumVal) {
|
|
|
|
bool FoundHexdecimalEnumerator = false;
|
2013-08-30 08:10:37 +08:00
|
|
|
SourceLocation EndLoc = Enumerator->getLocEnd();
|
|
|
|
Token Tok;
|
|
|
|
if (!PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true))
|
|
|
|
if (Tok.isLiteral() && Tok.getLength() > 2) {
|
|
|
|
if (const char *StringLit = Tok.getLiteralData())
|
|
|
|
FoundHexdecimalEnumerator =
|
|
|
|
(StringLit[0] == '0' && (toLowercase(StringLit[1]) == 'x'));
|
|
|
|
}
|
2013-09-24 04:27:06 +08:00
|
|
|
if (!FoundHexdecimalEnumerator)
|
|
|
|
AllHexdecimalEnumerator = false;
|
2013-08-30 08:10:37 +08:00
|
|
|
}
|
2013-07-23 02:53:45 +08:00
|
|
|
}
|
2013-09-24 04:27:06 +08:00
|
|
|
return AllHexdecimalEnumerator || (PowerOfTwo && (MaxPowerOfTwoVal > 2));
|
2013-07-23 02:53:45 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 01:44:32 +08:00
|
|
|
void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx,
|
2013-07-13 06:32:19 +08:00
|
|
|
const ObjCImplementationDecl *ImpDecl) {
|
|
|
|
const ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface();
|
2013-09-18 05:56:04 +08:00
|
|
|
if (!IDecl || ObjCProtocolDecls.empty() || IDecl->isDeprecated())
|
2013-07-13 06:32:19 +08:00
|
|
|
return;
|
|
|
|
// Find all implicit conforming protocols for this class
|
|
|
|
// and make them explicit.
|
|
|
|
llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ExplicitProtocols;
|
|
|
|
Ctx.CollectInheritedProtocols(IDecl, ExplicitProtocols);
|
2013-07-13 08:04:20 +08:00
|
|
|
llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols;
|
2013-07-13 06:32:19 +08:00
|
|
|
|
2014-08-25 12:15:02 +08:00
|
|
|
for (ObjCProtocolDecl *ProtDecl : ObjCProtocolDecls)
|
|
|
|
if (!ExplicitProtocols.count(ProtDecl))
|
|
|
|
PotentialImplicitProtocols.push_back(ProtDecl);
|
2013-07-13 06:32:19 +08:00
|
|
|
|
|
|
|
if (PotentialImplicitProtocols.empty())
|
|
|
|
return;
|
2013-07-13 08:04:20 +08:00
|
|
|
|
|
|
|
// go through list of non-optional methods and properties in each protocol
|
|
|
|
// in the PotentialImplicitProtocols list. If class implements every one of the
|
|
|
|
// methods and properties, then this class conforms to this protocol.
|
|
|
|
llvm::SmallVector<ObjCProtocolDecl*, 8> ConformingProtocols;
|
|
|
|
for (unsigned i = 0, e = PotentialImplicitProtocols.size(); i != e; i++)
|
2013-07-16 05:22:08 +08:00
|
|
|
if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl,
|
2013-07-13 08:04:20 +08:00
|
|
|
PotentialImplicitProtocols[i]))
|
|
|
|
ConformingProtocols.push_back(PotentialImplicitProtocols[i]);
|
2013-07-16 08:20:21 +08:00
|
|
|
|
|
|
|
if (ConformingProtocols.empty())
|
|
|
|
return;
|
2013-07-17 08:02:22 +08:00
|
|
|
|
|
|
|
// Further reduce number of conforming protocols. If protocol P1 is in the list
|
|
|
|
// protocol P2 (P2<P1>), No need to include P1.
|
|
|
|
llvm::SmallVector<ObjCProtocolDecl*, 8> MinimalConformingProtocols;
|
|
|
|
for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
|
|
|
|
bool DropIt = false;
|
|
|
|
ObjCProtocolDecl *TargetPDecl = ConformingProtocols[i];
|
|
|
|
for (unsigned i1 = 0, e1 = ConformingProtocols.size(); i1 != e1; i1++) {
|
|
|
|
ObjCProtocolDecl *PDecl = ConformingProtocols[i1];
|
|
|
|
if (PDecl == TargetPDecl)
|
|
|
|
continue;
|
|
|
|
if (PDecl->lookupProtocolNamed(
|
|
|
|
TargetPDecl->getDeclName().getAsIdentifierInfo())) {
|
|
|
|
DropIt = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!DropIt)
|
|
|
|
MinimalConformingProtocols.push_back(TargetPDecl);
|
|
|
|
}
|
2013-12-17 09:01:33 +08:00
|
|
|
if (MinimalConformingProtocols.empty())
|
|
|
|
return;
|
2013-07-16 08:20:21 +08:00
|
|
|
edit::Commit commit(*Editor);
|
2013-07-19 06:17:33 +08:00
|
|
|
rewriteToObjCInterfaceDecl(IDecl, MinimalConformingProtocols,
|
|
|
|
*NSAPIObj, commit);
|
2013-07-16 08:20:21 +08:00
|
|
|
Editor->commit(commit);
|
2013-07-13 06:32:19 +08:00
|
|
|
}
|
|
|
|
|
2013-10-15 08:00:28 +08:00
|
|
|
void ObjCMigrateASTConsumer::CacheObjCNSIntegerTypedefed(
|
|
|
|
const TypedefDecl *TypedefDcl) {
|
|
|
|
|
|
|
|
QualType qt = TypedefDcl->getTypeSourceInfo()->getType();
|
|
|
|
if (NSAPIObj->isObjCNSIntegerType(qt))
|
|
|
|
NSIntegerTypedefed = TypedefDcl;
|
|
|
|
else if (NSAPIObj->isObjCNSUIntegerType(qt))
|
|
|
|
NSUIntegerTypedefed = TypedefDcl;
|
|
|
|
}
|
|
|
|
|
2013-10-12 05:34:56 +08:00
|
|
|
bool ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx,
|
2013-07-19 04:11:45 +08:00
|
|
|
const EnumDecl *EnumDcl,
|
|
|
|
const TypedefDecl *TypedefDcl) {
|
|
|
|
if (!EnumDcl->isCompleteDefinition() || EnumDcl->getIdentifier() ||
|
2014-09-16 00:41:47 +08:00
|
|
|
EnumDcl->isDeprecated() || EnumDcl->getIntegerTypeSourceInfo())
|
2013-10-15 08:00:28 +08:00
|
|
|
return false;
|
|
|
|
if (!TypedefDcl) {
|
|
|
|
if (NSIntegerTypedefed) {
|
|
|
|
TypedefDcl = NSIntegerTypedefed;
|
2014-05-07 14:21:57 +08:00
|
|
|
NSIntegerTypedefed = nullptr;
|
2013-10-15 08:00:28 +08:00
|
|
|
}
|
|
|
|
else if (NSUIntegerTypedefed) {
|
|
|
|
TypedefDcl = NSUIntegerTypedefed;
|
2014-05-07 14:21:57 +08:00
|
|
|
NSUIntegerTypedefed = nullptr;
|
2013-10-15 08:00:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2013-11-14 02:20:31 +08:00
|
|
|
FileID FileIdOfTypedefDcl =
|
|
|
|
PP.getSourceManager().getFileID(TypedefDcl->getLocation());
|
|
|
|
FileID FileIdOfEnumDcl =
|
|
|
|
PP.getSourceManager().getFileID(EnumDcl->getLocation());
|
2013-10-15 08:00:28 +08:00
|
|
|
if (FileIdOfTypedefDcl != FileIdOfEnumDcl)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (TypedefDcl->isDeprecated())
|
2013-10-12 05:34:56 +08:00
|
|
|
return false;
|
2013-07-19 04:11:45 +08:00
|
|
|
|
|
|
|
QualType qt = TypedefDcl->getTypeSourceInfo()->getType();
|
2014-10-07 07:50:37 +08:00
|
|
|
StringRef NSIntegerName = NSAPIObj->GetNSIntegralKind(qt);
|
2013-07-23 02:53:45 +08:00
|
|
|
|
2014-10-07 07:50:37 +08:00
|
|
|
if (NSIntegerName.empty()) {
|
2013-07-20 04:18:36 +08:00
|
|
|
// Also check for typedef enum {...} TD;
|
|
|
|
if (const EnumType *EnumTy = qt->getAs<EnumType>()) {
|
|
|
|
if (EnumTy->getDecl() == EnumDcl) {
|
2013-08-30 08:10:37 +08:00
|
|
|
bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl);
|
2014-10-10 06:04:27 +08:00
|
|
|
if (!InsertFoundation(Ctx, TypedefDcl->getLocStart()))
|
2013-10-12 05:34:56 +08:00
|
|
|
return false;
|
2013-07-20 04:18:36 +08:00
|
|
|
edit::Commit commit(*Editor);
|
2014-12-11 02:25:24 +08:00
|
|
|
rewriteToNSMacroDecl(Ctx, EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions);
|
2013-07-20 04:18:36 +08:00
|
|
|
Editor->commit(commit);
|
2013-10-18 06:23:32 +08:00
|
|
|
return true;
|
2013-07-20 04:18:36 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-12 05:34:56 +08:00
|
|
|
return false;
|
2013-07-23 02:53:45 +08:00
|
|
|
}
|
2013-07-19 04:11:45 +08:00
|
|
|
|
2013-08-31 01:46:01 +08:00
|
|
|
// We may still use NS_OPTIONS based on what we find in the enumertor list.
|
|
|
|
bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl);
|
2014-10-10 06:04:27 +08:00
|
|
|
if (!InsertFoundation(Ctx, TypedefDcl->getLocStart()))
|
2013-10-12 05:34:56 +08:00
|
|
|
return false;
|
2013-07-19 04:11:45 +08:00
|
|
|
edit::Commit commit(*Editor);
|
2013-10-15 08:00:28 +08:00
|
|
|
bool Res = rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj,
|
2014-10-07 07:50:37 +08:00
|
|
|
commit, NSIntegerName, NSOptions);
|
2013-07-19 04:11:45 +08:00
|
|
|
Editor->commit(commit);
|
2013-10-15 08:00:28 +08:00
|
|
|
return Res;
|
2013-07-19 04:11:45 +08:00
|
|
|
}
|
|
|
|
|
2014-05-21 08:24:20 +08:00
|
|
|
static void ReplaceWithInstancetype(ASTContext &Ctx,
|
|
|
|
const ObjCMigrateASTConsumer &ASTC,
|
2013-08-03 02:00:53 +08:00
|
|
|
ObjCMethodDecl *OM) {
|
2014-05-21 08:24:20 +08:00
|
|
|
if (OM->getReturnType() == Ctx.getObjCInstanceType())
|
|
|
|
return; // already has instancetype.
|
|
|
|
|
2013-08-03 02:00:53 +08:00
|
|
|
SourceRange R;
|
|
|
|
std::string ClassString;
|
2014-01-26 00:55:45 +08:00
|
|
|
if (TypeSourceInfo *TSInfo = OM->getReturnTypeSourceInfo()) {
|
2013-08-03 02:00:53 +08:00
|
|
|
TypeLoc TL = TSInfo->getTypeLoc();
|
|
|
|
R = SourceRange(TL.getBeginLoc(), TL.getEndLoc());
|
|
|
|
ClassString = "instancetype";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
R = SourceRange(OM->getLocStart(), OM->getLocStart());
|
|
|
|
ClassString = OM->isInstanceMethod() ? '-' : '+';
|
|
|
|
ClassString += " (instancetype)";
|
|
|
|
}
|
|
|
|
edit::Commit commit(*ASTC.Editor);
|
|
|
|
commit.replace(R, ClassString);
|
|
|
|
ASTC.Editor->commit(commit);
|
|
|
|
}
|
|
|
|
|
2013-10-11 02:23:13 +08:00
|
|
|
static void ReplaceWithClasstype(const ObjCMigrateASTConsumer &ASTC,
|
|
|
|
ObjCMethodDecl *OM) {
|
|
|
|
ObjCInterfaceDecl *IDecl = OM->getClassInterface();
|
|
|
|
SourceRange R;
|
|
|
|
std::string ClassString;
|
2014-01-26 00:55:45 +08:00
|
|
|
if (TypeSourceInfo *TSInfo = OM->getReturnTypeSourceInfo()) {
|
2013-10-11 02:23:13 +08:00
|
|
|
TypeLoc TL = TSInfo->getTypeLoc();
|
|
|
|
R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); {
|
|
|
|
ClassString = IDecl->getName();
|
|
|
|
ClassString += "*";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
R = SourceRange(OM->getLocStart(), OM->getLocStart());
|
|
|
|
ClassString = "+ (";
|
|
|
|
ClassString += IDecl->getName(); ClassString += "*)";
|
|
|
|
}
|
|
|
|
edit::Commit commit(*ASTC.Editor);
|
|
|
|
commit.replace(R, ClassString);
|
|
|
|
ASTC.Editor->commit(commit);
|
|
|
|
}
|
|
|
|
|
2013-07-24 07:34:42 +08:00
|
|
|
void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
|
|
|
|
ObjCContainerDecl *CDecl,
|
|
|
|
ObjCMethodDecl *OM) {
|
2013-07-24 06:42:28 +08:00
|
|
|
ObjCInstanceTypeFamily OIT_Family =
|
|
|
|
Selector::getInstTypeMethodFamily(OM->getSelector());
|
2013-08-03 04:54:18 +08:00
|
|
|
|
2013-07-25 03:18:37 +08:00
|
|
|
std::string ClassName;
|
2013-07-24 07:55:55 +08:00
|
|
|
switch (OIT_Family) {
|
2013-08-03 04:54:18 +08:00
|
|
|
case OIT_None:
|
|
|
|
migrateFactoryMethod(Ctx, CDecl, OM);
|
|
|
|
return;
|
2013-08-29 05:23:00 +08:00
|
|
|
case OIT_Array:
|
|
|
|
ClassName = "NSArray";
|
|
|
|
break;
|
|
|
|
case OIT_Dictionary:
|
|
|
|
ClassName = "NSDictionary";
|
|
|
|
break;
|
2013-08-03 04:54:18 +08:00
|
|
|
case OIT_Singleton:
|
|
|
|
migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton);
|
2013-07-24 07:55:55 +08:00
|
|
|
return;
|
2013-09-19 04:35:47 +08:00
|
|
|
case OIT_Init:
|
2014-01-26 00:55:45 +08:00
|
|
|
if (OM->getReturnType()->isObjCIdType())
|
2014-05-21 08:24:20 +08:00
|
|
|
ReplaceWithInstancetype(Ctx, *this, OM);
|
2013-09-19 04:35:47 +08:00
|
|
|
return;
|
2013-10-11 02:23:13 +08:00
|
|
|
case OIT_ReturnsSelf:
|
|
|
|
migrateFactoryMethod(Ctx, CDecl, OM, OIT_ReturnsSelf);
|
|
|
|
return;
|
2013-07-24 07:55:55 +08:00
|
|
|
}
|
2014-01-26 00:55:45 +08:00
|
|
|
if (!OM->getReturnType()->isObjCIdType())
|
2013-07-24 06:42:28 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
|
|
|
|
if (!IDecl) {
|
|
|
|
if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
|
|
|
|
IDecl = CatDecl->getClassInterface();
|
|
|
|
else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl))
|
|
|
|
IDecl = ImpDecl->getClassInterface();
|
|
|
|
}
|
2013-07-25 03:18:37 +08:00
|
|
|
if (!IDecl ||
|
2013-08-03 02:00:53 +08:00
|
|
|
!IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) {
|
|
|
|
migrateFactoryMethod(Ctx, CDecl, OM);
|
2013-07-24 06:42:28 +08:00
|
|
|
return;
|
2013-08-02 08:01:14 +08:00
|
|
|
}
|
2014-05-21 08:24:20 +08:00
|
|
|
ReplaceWithInstancetype(Ctx, *this, OM);
|
2013-07-24 06:42:28 +08:00
|
|
|
}
|
|
|
|
|
2013-08-31 07:52:08 +08:00
|
|
|
static bool TypeIsInnerPointer(QualType T) {
|
|
|
|
if (!T->isAnyPointerType())
|
|
|
|
return false;
|
|
|
|
if (T->isObjCObjectPointerType() || T->isObjCBuiltinType() ||
|
2013-09-27 05:43:47 +08:00
|
|
|
T->isBlockPointerType() || T->isFunctionPointerType() ||
|
|
|
|
ento::coreFoundation::isCFObjectRef(T))
|
2013-08-31 07:52:08 +08:00
|
|
|
return false;
|
2013-09-10 07:56:14 +08:00
|
|
|
// Also, typedef-of-pointer-to-incomplete-struct is something that we assume
|
|
|
|
// is not an innter pointer type.
|
|
|
|
QualType OrigT = T;
|
|
|
|
while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr()))
|
|
|
|
T = TD->getDecl()->getUnderlyingType();
|
|
|
|
if (OrigT == T || !T->isPointerType())
|
|
|
|
return true;
|
|
|
|
const PointerType* PT = T->getAs<PointerType>();
|
|
|
|
QualType UPointeeT = PT->getPointeeType().getUnqualifiedType();
|
|
|
|
if (UPointeeT->isRecordType()) {
|
|
|
|
const RecordType *RecordTy = UPointeeT->getAs<RecordType>();
|
|
|
|
if (!RecordTy->getDecl()->isCompleteDefinition())
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-31 07:52:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-01 08:26:48 +08:00
|
|
|
/// \brief Check whether the two versions match.
|
|
|
|
static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y) {
|
|
|
|
return (X == Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AvailabilityAttrsMatch - This routine checks that if comparing two
|
|
|
|
/// availability attributes, all their components match. It returns
|
|
|
|
/// true, if not dealing with availability or when all components of
|
|
|
|
/// availability attributes match. This routine is only called when
|
|
|
|
/// the attributes are of the same kind.
|
|
|
|
static bool AvailabilityAttrsMatch(Attr *At1, Attr *At2) {
|
|
|
|
const AvailabilityAttr *AA1 = dyn_cast<AvailabilityAttr>(At1);
|
|
|
|
if (!AA1)
|
2013-09-19 01:22:25 +08:00
|
|
|
return true;
|
2013-11-01 08:26:48 +08:00
|
|
|
const AvailabilityAttr *AA2 = dyn_cast<AvailabilityAttr>(At2);
|
2013-09-19 01:22:25 +08:00
|
|
|
|
2013-11-01 08:26:48 +08:00
|
|
|
VersionTuple Introduced1 = AA1->getIntroduced();
|
|
|
|
VersionTuple Deprecated1 = AA1->getDeprecated();
|
|
|
|
VersionTuple Obsoleted1 = AA1->getObsoleted();
|
|
|
|
bool IsUnavailable1 = AA1->getUnavailable();
|
|
|
|
VersionTuple Introduced2 = AA2->getIntroduced();
|
|
|
|
VersionTuple Deprecated2 = AA2->getDeprecated();
|
|
|
|
VersionTuple Obsoleted2 = AA2->getObsoleted();
|
|
|
|
bool IsUnavailable2 = AA2->getUnavailable();
|
|
|
|
return (versionsMatch(Introduced1, Introduced2) &&
|
|
|
|
versionsMatch(Deprecated1, Deprecated2) &&
|
|
|
|
versionsMatch(Obsoleted1, Obsoleted2) &&
|
|
|
|
IsUnavailable1 == IsUnavailable2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool MatchTwoAttributeLists(const AttrVec &Attrs1, const AttrVec &Attrs2,
|
|
|
|
bool &AvailabilityArgsMatch) {
|
2013-09-19 01:22:25 +08:00
|
|
|
// This list is very small, so this need not be optimized.
|
|
|
|
for (unsigned i = 0, e = Attrs1.size(); i != e; i++) {
|
|
|
|
bool match = false;
|
|
|
|
for (unsigned j = 0, f = Attrs2.size(); j != f; j++) {
|
2013-11-01 08:26:48 +08:00
|
|
|
// Matching attribute kind only. Except for Availabilty attributes,
|
|
|
|
// we are not getting into details of the attributes. For all practical purposes
|
2013-09-19 01:22:25 +08:00
|
|
|
// this is sufficient.
|
|
|
|
if (Attrs1[i]->getKind() == Attrs2[j]->getKind()) {
|
2013-11-01 08:26:48 +08:00
|
|
|
if (AvailabilityArgsMatch)
|
|
|
|
AvailabilityArgsMatch = AvailabilityAttrsMatch(Attrs1[i], Attrs2[j]);
|
2013-09-19 01:22:25 +08:00
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-01 08:26:48 +08:00
|
|
|
/// AttributesMatch - This routine checks list of attributes for two
|
|
|
|
/// decls. It returns false, if there is a mismatch in kind of
|
|
|
|
/// attributes seen in the decls. It returns true if the two decls
|
|
|
|
/// have list of same kind of attributes. Furthermore, when there
|
|
|
|
/// are availability attributes in the two decls, it sets the
|
|
|
|
/// AvailabilityArgsMatch to false if availability attributes have
|
|
|
|
/// different versions, etc.
|
|
|
|
static bool AttributesMatch(const Decl *Decl1, const Decl *Decl2,
|
|
|
|
bool &AvailabilityArgsMatch) {
|
|
|
|
if (!Decl1->hasAttrs() || !Decl2->hasAttrs()) {
|
|
|
|
AvailabilityArgsMatch = (Decl1->hasAttrs() == Decl2->hasAttrs());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
AvailabilityArgsMatch = true;
|
|
|
|
const AttrVec &Attrs1 = Decl1->getAttrs();
|
|
|
|
const AttrVec &Attrs2 = Decl2->getAttrs();
|
|
|
|
bool match = MatchTwoAttributeLists(Attrs1, Attrs2, AvailabilityArgsMatch);
|
|
|
|
if (match && (Attrs2.size() > Attrs1.size()))
|
|
|
|
return MatchTwoAttributeLists(Attrs2, Attrs1, AvailabilityArgsMatch);
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2013-09-28 06:55:54 +08:00
|
|
|
static bool IsValidIdentifier(ASTContext &Ctx,
|
|
|
|
const char *Name) {
|
|
|
|
if (!isIdentifierHead(Name[0]))
|
|
|
|
return false;
|
|
|
|
std::string NameString = Name;
|
|
|
|
NameString[0] = toLowercase(NameString[0]);
|
|
|
|
IdentifierInfo *II = &Ctx.Idents.get(NameString);
|
|
|
|
return II->getTokenID() == tok::identifier;
|
|
|
|
}
|
|
|
|
|
2013-09-07 07:45:20 +08:00
|
|
|
bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx,
|
2013-09-18 03:00:30 +08:00
|
|
|
ObjCContainerDecl *D,
|
2013-09-07 07:45:20 +08:00
|
|
|
ObjCMethodDecl *Method) {
|
|
|
|
if (Method->isPropertyAccessor() || !Method->isInstanceMethod() ||
|
|
|
|
Method->param_size() != 0)
|
|
|
|
return false;
|
|
|
|
// Is this method candidate to be a getter?
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType GRT = Method->getReturnType();
|
2013-09-07 07:45:20 +08:00
|
|
|
if (GRT->isVoidType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Selector GetterSelector = Method->getSelector();
|
2013-09-25 08:17:07 +08:00
|
|
|
ObjCInstanceTypeFamily OIT_Family =
|
|
|
|
Selector::getInstTypeMethodFamily(GetterSelector);
|
|
|
|
|
|
|
|
if (OIT_Family != OIT_None)
|
|
|
|
return false;
|
|
|
|
|
2013-09-07 07:45:20 +08:00
|
|
|
IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0);
|
|
|
|
Selector SetterSelector =
|
|
|
|
SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
|
|
|
|
PP.getSelectorTable(),
|
|
|
|
getterName);
|
2013-09-18 03:00:30 +08:00
|
|
|
ObjCMethodDecl *SetterMethod = D->getInstanceMethod(SetterSelector);
|
2013-09-12 01:05:15 +08:00
|
|
|
unsigned LengthOfPrefix = 0;
|
2013-09-07 07:45:20 +08:00
|
|
|
if (!SetterMethod) {
|
|
|
|
// try a different naming convention for getter: isXxxxx
|
|
|
|
StringRef getterNameString = getterName->getName();
|
2013-09-12 01:05:15 +08:00
|
|
|
bool IsPrefix = getterNameString.startswith("is");
|
2013-09-18 03:38:55 +08:00
|
|
|
// Note that we don't want to change an isXXX method of retainable object
|
|
|
|
// type to property (readonly or otherwise).
|
|
|
|
if (IsPrefix && GRT->isObjCRetainableType())
|
|
|
|
return false;
|
|
|
|
if (IsPrefix || getterNameString.startswith("get")) {
|
2013-09-12 01:05:15 +08:00
|
|
|
LengthOfPrefix = (IsPrefix ? 2 : 3);
|
|
|
|
const char *CGetterName = getterNameString.data() + LengthOfPrefix;
|
|
|
|
// Make sure that first character after "is" or "get" prefix can
|
|
|
|
// start an identifier.
|
2013-09-28 06:55:54 +08:00
|
|
|
if (!IsValidIdentifier(Ctx, CGetterName))
|
2013-09-12 01:05:15 +08:00
|
|
|
return false;
|
2013-09-07 07:45:20 +08:00
|
|
|
if (CGetterName[0] && isUppercase(CGetterName[0])) {
|
|
|
|
getterName = &Ctx.Idents.get(CGetterName);
|
|
|
|
SetterSelector =
|
|
|
|
SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
|
|
|
|
PP.getSelectorTable(),
|
|
|
|
getterName);
|
2013-09-18 03:00:30 +08:00
|
|
|
SetterMethod = D->getInstanceMethod(SetterSelector);
|
2013-09-07 07:45:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-18 03:38:55 +08:00
|
|
|
|
2013-09-07 07:45:20 +08:00
|
|
|
if (SetterMethod) {
|
2013-10-03 05:32:39 +08:00
|
|
|
if ((ASTMigrateActions & FrontendOptions::ObjCMT_ReadwriteProperty) == 0)
|
|
|
|
return false;
|
2013-11-01 08:26:48 +08:00
|
|
|
bool AvailabilityArgsMatch;
|
2013-09-19 01:22:25 +08:00
|
|
|
if (SetterMethod->isDeprecated() ||
|
2013-11-01 08:26:48 +08:00
|
|
|
!AttributesMatch(Method, SetterMethod, AvailabilityArgsMatch))
|
2013-09-18 06:41:25 +08:00
|
|
|
return false;
|
2013-09-19 01:22:25 +08:00
|
|
|
|
2013-09-07 07:45:20 +08:00
|
|
|
// Is this a valid setter, matching the target getter?
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType SRT = SetterMethod->getReturnType();
|
2013-09-07 07:45:20 +08:00
|
|
|
if (!SRT->isVoidType())
|
|
|
|
return false;
|
|
|
|
const ParmVarDecl *argDecl = *SetterMethod->param_begin();
|
|
|
|
QualType ArgType = argDecl->getType();
|
2013-09-19 01:22:25 +08:00
|
|
|
if (!Ctx.hasSameUnqualifiedType(ArgType, GRT))
|
2013-09-07 07:45:20 +08:00
|
|
|
return false;
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit,
|
2013-10-10 03:06:08 +08:00
|
|
|
LengthOfPrefix,
|
|
|
|
(ASTMigrateActions &
|
2013-11-01 08:26:48 +08:00
|
|
|
FrontendOptions::ObjCMT_AtomicProperty) != 0,
|
2013-11-13 08:08:36 +08:00
|
|
|
(ASTMigrateActions &
|
|
|
|
FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty) != 0,
|
2013-11-01 08:26:48 +08:00
|
|
|
AvailabilityArgsMatch);
|
2013-09-07 07:45:20 +08:00
|
|
|
Editor->commit(commit);
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-03 01:08:12 +08:00
|
|
|
else if (ASTMigrateActions & FrontendOptions::ObjCMT_ReadonlyProperty) {
|
2013-09-07 07:45:20 +08:00
|
|
|
// Try a non-void method with no argument (and no setter or property of same name
|
|
|
|
// as a 'readonly' property.
|
|
|
|
edit::Commit commit(*Editor);
|
2014-05-07 14:21:57 +08:00
|
|
|
rewriteToObjCProperty(Method, nullptr /*SetterMethod*/, *NSAPIObj, commit,
|
2013-10-10 03:06:08 +08:00
|
|
|
LengthOfPrefix,
|
|
|
|
(ASTMigrateActions &
|
2013-11-01 08:26:48 +08:00
|
|
|
FrontendOptions::ObjCMT_AtomicProperty) != 0,
|
2013-11-13 08:08:36 +08:00
|
|
|
(ASTMigrateActions &
|
|
|
|
FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty) != 0,
|
2013-11-01 08:26:48 +08:00
|
|
|
/*AvailabilityArgsMatch*/false);
|
2013-09-07 07:45:20 +08:00
|
|
|
Editor->commit(commit);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-31 07:52:08 +08:00
|
|
|
void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx,
|
|
|
|
ObjCMethodDecl *OM) {
|
2013-09-25 04:20:52 +08:00
|
|
|
if (OM->isImplicit() ||
|
2013-09-27 06:43:41 +08:00
|
|
|
!OM->isInstanceMethod() ||
|
2013-09-25 04:20:52 +08:00
|
|
|
OM->hasAttr<ObjCReturnsInnerPointerAttr>())
|
2013-08-31 07:52:08 +08:00
|
|
|
return;
|
2014-01-26 00:55:45 +08:00
|
|
|
|
|
|
|
QualType RT = OM->getReturnType();
|
2013-08-31 07:52:08 +08:00
|
|
|
if (!TypeIsInnerPointer(RT) ||
|
|
|
|
!Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition())
|
|
|
|
return;
|
|
|
|
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insertBefore(OM->getLocEnd(), " NS_RETURNS_INNER_POINTER");
|
|
|
|
Editor->commit(commit);
|
|
|
|
}
|
|
|
|
|
2013-09-25 04:20:52 +08:00
|
|
|
void ObjCMigrateASTConsumer::migratePropertyNsReturnsInnerPointer(ASTContext &Ctx,
|
|
|
|
ObjCPropertyDecl *P) {
|
|
|
|
QualType T = P->getType();
|
|
|
|
|
|
|
|
if (!TypeIsInnerPointer(T) ||
|
|
|
|
!Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition())
|
|
|
|
return;
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insertBefore(P->getLocEnd(), " NS_RETURNS_INNER_POINTER ");
|
|
|
|
Editor->commit(commit);
|
|
|
|
}
|
|
|
|
|
2013-10-03 06:49:59 +08:00
|
|
|
void ObjCMigrateASTConsumer::migrateAllMethodInstaceType(ASTContext &Ctx,
|
2013-07-24 06:42:28 +08:00
|
|
|
ObjCContainerDecl *CDecl) {
|
2013-11-20 02:17:31 +08:00
|
|
|
if (CDecl->isDeprecated() || IsCategoryNameWithDeprecatedSuffix(CDecl))
|
2013-09-18 23:43:52 +08:00
|
|
|
return;
|
|
|
|
|
2013-07-24 06:42:28 +08:00
|
|
|
// migrate methods which can have instancetype as their result type.
|
2014-03-14 03:03:34 +08:00
|
|
|
for (auto *Method : CDecl->methods()) {
|
2013-09-18 05:56:04 +08:00
|
|
|
if (Method->isDeprecated())
|
|
|
|
continue;
|
2013-07-24 06:42:28 +08:00
|
|
|
migrateMethodInstanceType(Ctx, CDecl, Method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 02:00:53 +08:00
|
|
|
void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
|
|
|
|
ObjCContainerDecl *CDecl,
|
2013-08-03 04:54:18 +08:00
|
|
|
ObjCMethodDecl *OM,
|
|
|
|
ObjCInstanceTypeFamily OIT_Family) {
|
2013-08-03 06:34:18 +08:00
|
|
|
if (OM->isInstanceMethod() ||
|
2014-01-26 00:55:45 +08:00
|
|
|
OM->getReturnType() == Ctx.getObjCInstanceType() ||
|
|
|
|
!OM->getReturnType()->isObjCIdType())
|
2013-08-03 02:00:53 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Candidate factory methods are + (id) NaMeXXX : ... which belong to a class
|
|
|
|
// NSYYYNamE with matching names be at least 3 characters long.
|
|
|
|
ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
|
|
|
|
if (!IDecl) {
|
|
|
|
if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
|
|
|
|
IDecl = CatDecl->getClassInterface();
|
|
|
|
else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl))
|
|
|
|
IDecl = ImpDecl->getClassInterface();
|
|
|
|
}
|
|
|
|
if (!IDecl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string StringClassName = IDecl->getName();
|
|
|
|
StringRef LoweredClassName(StringClassName);
|
|
|
|
std::string StringLoweredClassName = LoweredClassName.lower();
|
|
|
|
LoweredClassName = StringLoweredClassName;
|
|
|
|
|
|
|
|
IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0);
|
2013-08-14 02:01:42 +08:00
|
|
|
// Handle method with no name at its first selector slot; e.g. + (id):(int)x.
|
|
|
|
if (!MethodIdName)
|
|
|
|
return;
|
|
|
|
|
2013-08-03 02:00:53 +08:00
|
|
|
std::string MethodName = MethodIdName->getName();
|
2013-10-11 02:23:13 +08:00
|
|
|
if (OIT_Family == OIT_Singleton || OIT_Family == OIT_ReturnsSelf) {
|
2013-08-03 04:54:18 +08:00
|
|
|
StringRef STRefMethodName(MethodName);
|
|
|
|
size_t len = 0;
|
|
|
|
if (STRefMethodName.startswith("standard"))
|
|
|
|
len = strlen("standard");
|
|
|
|
else if (STRefMethodName.startswith("shared"))
|
|
|
|
len = strlen("shared");
|
|
|
|
else if (STRefMethodName.startswith("default"))
|
|
|
|
len = strlen("default");
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
MethodName = STRefMethodName.substr(len);
|
|
|
|
}
|
2013-08-03 02:00:53 +08:00
|
|
|
std::string MethodNameSubStr = MethodName.substr(0, 3);
|
|
|
|
StringRef MethodNamePrefix(MethodNameSubStr);
|
|
|
|
std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower();
|
|
|
|
MethodNamePrefix = StringLoweredMethodNamePrefix;
|
|
|
|
size_t Ix = LoweredClassName.rfind(MethodNamePrefix);
|
|
|
|
if (Ix == StringRef::npos)
|
|
|
|
return;
|
|
|
|
std::string ClassNamePostfix = LoweredClassName.substr(Ix);
|
|
|
|
StringRef LoweredMethodName(MethodName);
|
|
|
|
std::string StringLoweredMethodName = LoweredMethodName.lower();
|
|
|
|
LoweredMethodName = StringLoweredMethodName;
|
|
|
|
if (!LoweredMethodName.startswith(ClassNamePostfix))
|
|
|
|
return;
|
2013-10-11 02:23:13 +08:00
|
|
|
if (OIT_Family == OIT_ReturnsSelf)
|
|
|
|
ReplaceWithClasstype(*this, OM);
|
|
|
|
else
|
2014-05-21 08:24:20 +08:00
|
|
|
ReplaceWithInstancetype(Ctx, *this, OM);
|
2013-08-03 02:00:53 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 07:35:05 +08:00
|
|
|
static bool IsVoidStarType(QualType Ty) {
|
|
|
|
if (!Ty->isPointerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (const TypedefType *TD = dyn_cast<TypedefType>(Ty.getTypePtr()))
|
|
|
|
Ty = TD->getDecl()->getUnderlyingType();
|
|
|
|
|
|
|
|
// Is the type void*?
|
|
|
|
const PointerType* PT = Ty->getAs<PointerType>();
|
|
|
|
if (PT->getPointeeType().getUnqualifiedType()->isVoidType())
|
|
|
|
return true;
|
|
|
|
return IsVoidStarType(PT->getPointeeType());
|
|
|
|
}
|
|
|
|
|
2013-08-21 02:54:39 +08:00
|
|
|
/// AuditedType - This routine audits the type AT and returns false if it is one of known
|
|
|
|
/// CF object types or of the "void *" variety. It returns true if we don't care about the type
|
|
|
|
/// such as a non-pointer or pointers which have no ownership issues (such as "int *").
|
2013-08-28 06:42:30 +08:00
|
|
|
static bool AuditedType (QualType AT) {
|
|
|
|
if (!AT->isAnyPointerType() && !AT->isBlockPointerType())
|
2013-08-20 03:13:34 +08:00
|
|
|
return true;
|
|
|
|
// FIXME. There isn't much we can say about CF pointer type; or is there?
|
2013-08-21 02:54:39 +08:00
|
|
|
if (ento::coreFoundation::isCFObjectRef(AT) ||
|
|
|
|
IsVoidStarType(AT) ||
|
|
|
|
// If an ObjC object is type, assuming that it is not a CF function and
|
|
|
|
// that it is an un-audited function.
|
2013-08-23 06:27:36 +08:00
|
|
|
AT->isObjCObjectPointerType() || AT->isObjCBuiltinType())
|
2013-08-20 03:13:34 +08:00
|
|
|
return false;
|
2013-08-21 02:54:39 +08:00
|
|
|
// All other pointers are assumed audited as harmless.
|
2013-08-20 03:13:34 +08:00
|
|
|
return true;
|
2013-08-17 07:35:05 +08:00
|
|
|
}
|
|
|
|
|
2013-08-21 06:42:13 +08:00
|
|
|
void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) {
|
2013-08-23 02:35:27 +08:00
|
|
|
if (CFFunctionIBCandidates.empty())
|
|
|
|
return;
|
2013-08-21 06:42:13 +08:00
|
|
|
if (!Ctx.Idents.get("CF_IMPLICIT_BRIDGING_ENABLED").hasMacroDefinition()) {
|
|
|
|
CFFunctionIBCandidates.clear();
|
2013-11-14 02:20:31 +08:00
|
|
|
FileId = FileID();
|
2013-08-21 06:42:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Insert CF_IMPLICIT_BRIDGING_ENABLE/CF_IMPLICIT_BRIDGING_DISABLED
|
2013-08-23 02:35:27 +08:00
|
|
|
const Decl *FirstFD = CFFunctionIBCandidates[0];
|
|
|
|
const Decl *LastFD =
|
|
|
|
CFFunctionIBCandidates[CFFunctionIBCandidates.size()-1];
|
2013-08-21 06:42:13 +08:00
|
|
|
const char *PragmaString = "\nCF_IMPLICIT_BRIDGING_ENABLED\n\n";
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insertBefore(FirstFD->getLocStart(), PragmaString);
|
|
|
|
PragmaString = "\n\nCF_IMPLICIT_BRIDGING_DISABLED\n";
|
|
|
|
SourceLocation EndLoc = LastFD->getLocEnd();
|
|
|
|
// get location just past end of function location.
|
|
|
|
EndLoc = PP.getLocForEndOfToken(EndLoc);
|
2013-08-23 02:35:27 +08:00
|
|
|
if (isa<FunctionDecl>(LastFD)) {
|
|
|
|
// For Methods, EndLoc points to the ending semcolon. So,
|
|
|
|
// not of these extra work is needed.
|
|
|
|
Token Tok;
|
|
|
|
// get locaiton of token that comes after end of function.
|
|
|
|
bool Failed = PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true);
|
|
|
|
if (!Failed)
|
|
|
|
EndLoc = Tok.getLocation();
|
|
|
|
}
|
2013-08-21 06:42:13 +08:00
|
|
|
commit.insertAfterToken(EndLoc, PragmaString);
|
|
|
|
Editor->commit(commit);
|
2013-11-14 02:20:31 +08:00
|
|
|
FileId = FileID();
|
2013-08-21 06:42:13 +08:00
|
|
|
CFFunctionIBCandidates.clear();
|
|
|
|
}
|
|
|
|
|
2013-08-23 05:40:15 +08:00
|
|
|
void ObjCMigrateASTConsumer::migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl) {
|
2013-09-18 05:56:04 +08:00
|
|
|
if (Decl->isDeprecated())
|
|
|
|
return;
|
|
|
|
|
2013-08-23 05:40:15 +08:00
|
|
|
if (Decl->hasAttr<CFAuditedTransferAttr>()) {
|
2013-08-20 06:00:50 +08:00
|
|
|
assert(CFFunctionIBCandidates.empty() &&
|
2013-08-23 05:40:15 +08:00
|
|
|
"Cannot have audited functions/methods inside user "
|
2013-08-20 06:00:50 +08:00
|
|
|
"provided CF_IMPLICIT_BRIDGING_ENABLE");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-17 07:35:05 +08:00
|
|
|
// Finction must be annotated first.
|
2013-09-04 08:10:06 +08:00
|
|
|
if (const FunctionDecl *FuncDecl = dyn_cast<FunctionDecl>(Decl)) {
|
|
|
|
CF_BRIDGING_KIND AuditKind = migrateAddFunctionAnnotation(Ctx, FuncDecl);
|
|
|
|
if (AuditKind == CF_BRIDGING_ENABLE) {
|
2013-08-28 06:42:30 +08:00
|
|
|
CFFunctionIBCandidates.push_back(Decl);
|
2013-11-14 02:20:31 +08:00
|
|
|
if (FileId.isInvalid())
|
|
|
|
FileId = PP.getSourceManager().getFileID(Decl->getLocation());
|
2013-08-28 06:42:30 +08:00
|
|
|
}
|
2013-09-04 08:10:06 +08:00
|
|
|
else if (AuditKind == CF_BRIDGING_MAY_INCLUDE) {
|
|
|
|
if (!CFFunctionIBCandidates.empty()) {
|
|
|
|
CFFunctionIBCandidates.push_back(Decl);
|
2013-11-14 02:20:31 +08:00
|
|
|
if (FileId.isInvalid())
|
|
|
|
FileId = PP.getSourceManager().getFileID(Decl->getLocation());
|
2013-09-04 08:10:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
AnnotateImplicitBridging(Ctx);
|
2013-08-28 06:42:30 +08:00
|
|
|
}
|
2013-09-04 08:10:06 +08:00
|
|
|
else {
|
|
|
|
migrateAddMethodAnnotation(Ctx, cast<ObjCMethodDecl>(Decl));
|
2013-08-21 06:42:13 +08:00
|
|
|
AnnotateImplicitBridging(Ctx);
|
2013-09-04 08:10:06 +08:00
|
|
|
}
|
2013-08-17 07:35:05 +08:00
|
|
|
}
|
|
|
|
|
2013-08-28 06:42:30 +08:00
|
|
|
void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx,
|
|
|
|
const CallEffects &CE,
|
2013-08-28 07:56:54 +08:00
|
|
|
const FunctionDecl *FuncDecl,
|
|
|
|
bool ResultAnnotated) {
|
2013-08-28 06:42:30 +08:00
|
|
|
// Annotate function.
|
2013-08-28 07:56:54 +08:00
|
|
|
if (!ResultAnnotated) {
|
2013-08-20 03:13:34 +08:00
|
|
|
RetEffect Ret = CE.getReturnValue();
|
2014-05-07 14:21:57 +08:00
|
|
|
const char *AnnotationString = nullptr;
|
2013-09-05 06:49:19 +08:00
|
|
|
if (Ret.getObjKind() == RetEffect::CF) {
|
|
|
|
if (Ret.isOwned() &&
|
|
|
|
Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition())
|
2013-08-20 03:13:34 +08:00
|
|
|
AnnotationString = " CF_RETURNS_RETAINED";
|
2013-09-05 06:49:19 +08:00
|
|
|
else if (Ret.notOwned() &&
|
|
|
|
Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition())
|
2013-08-20 03:13:34 +08:00
|
|
|
AnnotationString = " CF_RETURNS_NOT_RETAINED";
|
|
|
|
}
|
2013-09-05 06:49:19 +08:00
|
|
|
else if (Ret.getObjKind() == RetEffect::ObjC) {
|
|
|
|
if (Ret.isOwned() &&
|
|
|
|
Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition())
|
|
|
|
AnnotationString = " NS_RETURNS_RETAINED";
|
|
|
|
}
|
|
|
|
|
2013-08-20 03:13:34 +08:00
|
|
|
if (AnnotationString) {
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insertAfterToken(FuncDecl->getLocEnd(), AnnotationString);
|
|
|
|
Editor->commit(commit);
|
|
|
|
}
|
2013-08-28 06:42:30 +08:00
|
|
|
}
|
2014-06-29 07:22:23 +08:00
|
|
|
ArrayRef<ArgEffect> AEArgs = CE.getArgs();
|
2013-08-28 06:42:30 +08:00
|
|
|
unsigned i = 0;
|
|
|
|
for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(),
|
|
|
|
pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) {
|
|
|
|
const ParmVarDecl *pd = *pi;
|
|
|
|
ArgEffect AE = AEArgs[i];
|
2013-12-19 10:39:40 +08:00
|
|
|
if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() &&
|
2013-08-28 06:42:30 +08:00
|
|
|
Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) {
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insertBefore(pd->getLocation(), "CF_CONSUMED ");
|
|
|
|
Editor->commit(commit);
|
|
|
|
}
|
2013-12-19 10:39:40 +08:00
|
|
|
else if (AE == DecRefMsg && !pd->hasAttr<NSConsumedAttr>() &&
|
2013-09-05 06:49:19 +08:00
|
|
|
Ctx.Idents.get("NS_CONSUMED").hasMacroDefinition()) {
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insertBefore(pd->getLocation(), "NS_CONSUMED ");
|
|
|
|
Editor->commit(commit);
|
|
|
|
}
|
2013-08-28 06:42:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObjCMigrateASTConsumer::CF_BRIDGING_KIND
|
|
|
|
ObjCMigrateASTConsumer::migrateAddFunctionAnnotation(
|
|
|
|
ASTContext &Ctx,
|
|
|
|
const FunctionDecl *FuncDecl) {
|
|
|
|
if (FuncDecl->hasBody())
|
|
|
|
return CF_BRIDGING_NONE;
|
|
|
|
|
|
|
|
CallEffects CE = CallEffects::getEffect(FuncDecl);
|
2013-12-19 10:39:40 +08:00
|
|
|
bool FuncIsReturnAnnotated = (FuncDecl->hasAttr<CFReturnsRetainedAttr>() ||
|
|
|
|
FuncDecl->hasAttr<CFReturnsNotRetainedAttr>() ||
|
|
|
|
FuncDecl->hasAttr<NSReturnsRetainedAttr>() ||
|
|
|
|
FuncDecl->hasAttr<NSReturnsNotRetainedAttr>() ||
|
|
|
|
FuncDecl->hasAttr<NSReturnsAutoreleasedAttr>());
|
2013-08-28 06:42:30 +08:00
|
|
|
|
|
|
|
// Trivial case of when funciton is annotated and has no argument.
|
|
|
|
if (FuncIsReturnAnnotated && FuncDecl->getNumParams() == 0)
|
|
|
|
return CF_BRIDGING_NONE;
|
|
|
|
|
|
|
|
bool ReturnCFAudited = false;
|
|
|
|
if (!FuncIsReturnAnnotated) {
|
|
|
|
RetEffect Ret = CE.getReturnValue();
|
|
|
|
if (Ret.getObjKind() == RetEffect::CF &&
|
2013-09-04 08:10:06 +08:00
|
|
|
(Ret.isOwned() || Ret.notOwned()))
|
2013-08-28 06:42:30 +08:00
|
|
|
ReturnCFAudited = true;
|
2014-01-26 00:55:45 +08:00
|
|
|
else if (!AuditedType(FuncDecl->getReturnType()))
|
2013-08-28 06:42:30 +08:00
|
|
|
return CF_BRIDGING_NONE;
|
2013-08-16 05:44:38 +08:00
|
|
|
}
|
2013-08-21 02:54:39 +08:00
|
|
|
|
2013-08-28 06:42:30 +08:00
|
|
|
// At this point result type is audited for potential inclusion.
|
2013-08-20 03:13:34 +08:00
|
|
|
// Now, how about argument types.
|
2014-06-29 07:22:23 +08:00
|
|
|
ArrayRef<ArgEffect> AEArgs = CE.getArgs();
|
2013-08-20 03:13:34 +08:00
|
|
|
unsigned i = 0;
|
2013-08-28 06:42:30 +08:00
|
|
|
bool ArgCFAudited = false;
|
2013-08-20 03:13:34 +08:00
|
|
|
for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(),
|
|
|
|
pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) {
|
2013-08-22 03:37:47 +08:00
|
|
|
const ParmVarDecl *pd = *pi;
|
2013-08-20 03:13:34 +08:00
|
|
|
ArgEffect AE = AEArgs[i];
|
2013-08-28 06:42:30 +08:00
|
|
|
if (AE == DecRef /*CFConsumed annotated*/ || AE == IncRef) {
|
2013-12-19 10:39:40 +08:00
|
|
|
if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>())
|
2013-08-28 06:42:30 +08:00
|
|
|
ArgCFAudited = true;
|
|
|
|
else if (AE == IncRef)
|
|
|
|
ArgCFAudited = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QualType AT = pd->getType();
|
|
|
|
if (!AuditedType(AT)) {
|
2013-08-28 07:56:54 +08:00
|
|
|
AddCFAnnotations(Ctx, CE, FuncDecl, FuncIsReturnAnnotated);
|
2013-08-28 06:42:30 +08:00
|
|
|
return CF_BRIDGING_NONE;
|
2013-08-22 03:37:47 +08:00
|
|
|
}
|
2013-08-21 02:54:39 +08:00
|
|
|
}
|
2013-08-16 05:44:38 +08:00
|
|
|
}
|
2013-08-28 06:42:30 +08:00
|
|
|
if (ReturnCFAudited || ArgCFAudited)
|
|
|
|
return CF_BRIDGING_ENABLE;
|
|
|
|
|
|
|
|
return CF_BRIDGING_MAY_INCLUDE;
|
2013-08-13 07:17:13 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 02:35:27 +08:00
|
|
|
void ObjCMigrateASTConsumer::migrateARCSafeAnnotation(ASTContext &Ctx,
|
|
|
|
ObjCContainerDecl *CDecl) {
|
2013-09-18 05:56:04 +08:00
|
|
|
if (!isa<ObjCInterfaceDecl>(CDecl) || CDecl->isDeprecated())
|
2013-08-23 02:35:27 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// migrate methods which can have instancetype as their result type.
|
2014-03-14 03:03:34 +08:00
|
|
|
for (const auto *Method : CDecl->methods())
|
2013-08-23 05:40:15 +08:00
|
|
|
migrateCFAnnotation(Ctx, Method);
|
2013-08-23 02:35:27 +08:00
|
|
|
}
|
|
|
|
|
2013-08-28 06:42:30 +08:00
|
|
|
void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx,
|
|
|
|
const CallEffects &CE,
|
2013-08-28 07:56:54 +08:00
|
|
|
const ObjCMethodDecl *MethodDecl,
|
|
|
|
bool ResultAnnotated) {
|
2013-08-28 06:42:30 +08:00
|
|
|
// Annotate function.
|
2013-08-28 07:56:54 +08:00
|
|
|
if (!ResultAnnotated) {
|
2013-08-23 02:35:27 +08:00
|
|
|
RetEffect Ret = CE.getReturnValue();
|
2014-05-07 14:21:57 +08:00
|
|
|
const char *AnnotationString = nullptr;
|
2013-09-05 06:49:19 +08:00
|
|
|
if (Ret.getObjKind() == RetEffect::CF) {
|
|
|
|
if (Ret.isOwned() &&
|
|
|
|
Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition())
|
2013-08-23 02:35:27 +08:00
|
|
|
AnnotationString = " CF_RETURNS_RETAINED";
|
2013-09-05 06:49:19 +08:00
|
|
|
else if (Ret.notOwned() &&
|
|
|
|
Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition())
|
2013-08-23 02:35:27 +08:00
|
|
|
AnnotationString = " CF_RETURNS_NOT_RETAINED";
|
|
|
|
}
|
2013-09-05 06:49:19 +08:00
|
|
|
else if (Ret.getObjKind() == RetEffect::ObjC) {
|
2013-09-06 07:04:33 +08:00
|
|
|
ObjCMethodFamily OMF = MethodDecl->getMethodFamily();
|
|
|
|
switch (OMF) {
|
|
|
|
case clang::OMF_alloc:
|
|
|
|
case clang::OMF_new:
|
|
|
|
case clang::OMF_copy:
|
|
|
|
case clang::OMF_init:
|
|
|
|
case clang::OMF_mutableCopy:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (Ret.isOwned() &&
|
|
|
|
Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition())
|
|
|
|
AnnotationString = " NS_RETURNS_RETAINED";
|
|
|
|
break;
|
|
|
|
}
|
2013-09-05 06:49:19 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 02:35:27 +08:00
|
|
|
if (AnnotationString) {
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insertBefore(MethodDecl->getLocEnd(), AnnotationString);
|
|
|
|
Editor->commit(commit);
|
|
|
|
}
|
2013-08-28 06:42:30 +08:00
|
|
|
}
|
2014-06-29 07:22:23 +08:00
|
|
|
ArrayRef<ArgEffect> AEArgs = CE.getArgs();
|
2013-08-28 06:42:30 +08:00
|
|
|
unsigned i = 0;
|
|
|
|
for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(),
|
|
|
|
pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) {
|
|
|
|
const ParmVarDecl *pd = *pi;
|
|
|
|
ArgEffect AE = AEArgs[i];
|
2013-12-19 10:39:40 +08:00
|
|
|
if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() &&
|
2013-08-28 06:42:30 +08:00
|
|
|
Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) {
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insertBefore(pd->getLocation(), "CF_CONSUMED ");
|
|
|
|
Editor->commit(commit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 08:10:06 +08:00
|
|
|
void ObjCMigrateASTConsumer::migrateAddMethodAnnotation(
|
2013-08-28 06:42:30 +08:00
|
|
|
ASTContext &Ctx,
|
|
|
|
const ObjCMethodDecl *MethodDecl) {
|
2013-09-05 06:49:19 +08:00
|
|
|
if (MethodDecl->hasBody() || MethodDecl->isImplicit())
|
2013-09-04 08:10:06 +08:00
|
|
|
return;
|
2013-08-28 06:42:30 +08:00
|
|
|
|
|
|
|
CallEffects CE = CallEffects::getEffect(MethodDecl);
|
2013-12-19 10:39:40 +08:00
|
|
|
bool MethodIsReturnAnnotated = (MethodDecl->hasAttr<CFReturnsRetainedAttr>() ||
|
|
|
|
MethodDecl->hasAttr<CFReturnsNotRetainedAttr>() ||
|
|
|
|
MethodDecl->hasAttr<NSReturnsRetainedAttr>() ||
|
|
|
|
MethodDecl->hasAttr<NSReturnsNotRetainedAttr>() ||
|
|
|
|
MethodDecl->hasAttr<NSReturnsAutoreleasedAttr>());
|
2013-09-06 07:04:33 +08:00
|
|
|
|
|
|
|
if (CE.getReceiver() == DecRefMsg &&
|
2013-12-19 10:39:40 +08:00
|
|
|
!MethodDecl->hasAttr<NSConsumesSelfAttr>() &&
|
2013-09-06 07:04:33 +08:00
|
|
|
MethodDecl->getMethodFamily() != OMF_init &&
|
|
|
|
MethodDecl->getMethodFamily() != OMF_release &&
|
|
|
|
Ctx.Idents.get("NS_CONSUMES_SELF").hasMacroDefinition()) {
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insertBefore(MethodDecl->getLocEnd(), " NS_CONSUMES_SELF");
|
|
|
|
Editor->commit(commit);
|
|
|
|
}
|
2013-08-28 06:42:30 +08:00
|
|
|
|
|
|
|
// Trivial case of when funciton is annotated and has no argument.
|
|
|
|
if (MethodIsReturnAnnotated &&
|
|
|
|
(MethodDecl->param_begin() == MethodDecl->param_end()))
|
2013-09-04 08:10:06 +08:00
|
|
|
return;
|
2013-08-28 06:42:30 +08:00
|
|
|
|
|
|
|
if (!MethodIsReturnAnnotated) {
|
|
|
|
RetEffect Ret = CE.getReturnValue();
|
2013-09-05 06:49:19 +08:00
|
|
|
if ((Ret.getObjKind() == RetEffect::CF ||
|
|
|
|
Ret.getObjKind() == RetEffect::ObjC) &&
|
|
|
|
(Ret.isOwned() || Ret.notOwned())) {
|
2013-09-05 00:43:57 +08:00
|
|
|
AddCFAnnotations(Ctx, CE, MethodDecl, false);
|
|
|
|
return;
|
2014-01-26 00:55:45 +08:00
|
|
|
} else if (!AuditedType(MethodDecl->getReturnType()))
|
2013-09-04 08:10:06 +08:00
|
|
|
return;
|
2013-08-23 02:35:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point result type is either annotated or audited.
|
|
|
|
// Now, how about argument types.
|
2014-06-29 07:22:23 +08:00
|
|
|
ArrayRef<ArgEffect> AEArgs = CE.getArgs();
|
2013-08-23 02:35:27 +08:00
|
|
|
unsigned i = 0;
|
|
|
|
for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(),
|
|
|
|
pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) {
|
|
|
|
const ParmVarDecl *pd = *pi;
|
|
|
|
ArgEffect AE = AEArgs[i];
|
2013-12-19 10:39:40 +08:00
|
|
|
if ((AE == DecRef && !pd->hasAttr<CFConsumedAttr>()) || AE == IncRef ||
|
2013-09-05 00:43:57 +08:00
|
|
|
!AuditedType(pd->getType())) {
|
|
|
|
AddCFAnnotations(Ctx, CE, MethodDecl, MethodIsReturnAnnotated);
|
|
|
|
return;
|
2013-08-23 02:35:27 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-04 08:10:06 +08:00
|
|
|
return;
|
2013-08-13 07:17:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 02:36:49 +08:00
|
|
|
namespace {
|
|
|
|
class SuperInitChecker : public RecursiveASTVisitor<SuperInitChecker> {
|
|
|
|
public:
|
|
|
|
bool shouldVisitTemplateInstantiations() const { return false; }
|
|
|
|
bool shouldWalkTypesOfTypeLocs() const { return false; }
|
|
|
|
|
|
|
|
bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
|
|
|
|
if (E->getReceiverKind() == ObjCMessageExpr::SuperInstance) {
|
|
|
|
if (E->getMethodFamily() == OMF_init)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
static bool hasSuperInitCall(const ObjCMethodDecl *MD) {
|
|
|
|
return !SuperInitChecker().TraverseStmt(MD->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCMigrateASTConsumer::inferDesignatedInitializers(
|
|
|
|
ASTContext &Ctx,
|
|
|
|
const ObjCImplementationDecl *ImplD) {
|
|
|
|
|
|
|
|
const ObjCInterfaceDecl *IFace = ImplD->getClassInterface();
|
|
|
|
if (!IFace || IFace->hasDesignatedInitializers())
|
|
|
|
return;
|
|
|
|
if (!Ctx.Idents.get("NS_DESIGNATED_INITIALIZER").hasMacroDefinition())
|
|
|
|
return;
|
|
|
|
|
2014-03-14 03:50:17 +08:00
|
|
|
for (const auto *MD : ImplD->instance_methods()) {
|
2013-12-11 02:36:49 +08:00
|
|
|
if (MD->isDeprecated() ||
|
|
|
|
MD->getMethodFamily() != OMF_init ||
|
|
|
|
MD->isDesignatedInitializerForTheInterface())
|
|
|
|
continue;
|
|
|
|
const ObjCMethodDecl *IFaceM = IFace->getMethod(MD->getSelector(),
|
|
|
|
/*isInstance=*/true);
|
|
|
|
if (!IFaceM)
|
|
|
|
continue;
|
|
|
|
if (hasSuperInitCall(MD)) {
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
commit.insert(IFaceM->getLocEnd(), " NS_DESIGNATED_INITIALIZER");
|
|
|
|
Editor->commit(commit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 03:01:46 +08:00
|
|
|
bool ObjCMigrateASTConsumer::InsertFoundation(ASTContext &Ctx,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
if (FoundationIncluded)
|
|
|
|
return true;
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return false;
|
|
|
|
edit::Commit commit(*Editor);
|
|
|
|
if (Ctx.getLangOpts().Modules)
|
2014-10-10 06:04:27 +08:00
|
|
|
commit.insert(Loc, "#ifndef NS_ENUM\n@import Foundation;\n#endif\n");
|
2014-10-08 03:01:46 +08:00
|
|
|
else
|
2014-10-10 06:04:27 +08:00
|
|
|
commit.insert(Loc, "#ifndef NS_ENUM\n#import <Foundation/Foundation.h>\n#endif\n");
|
2014-10-08 03:01:46 +08:00
|
|
|
Editor->commit(commit);
|
|
|
|
FoundationIncluded = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class RewritesReceiver : public edit::EditsReceiver {
|
|
|
|
Rewriter &Rewrite;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void insert(SourceLocation loc, StringRef text) override {
|
2012-03-07 04:06:33 +08:00
|
|
|
Rewrite.InsertText(loc, text);
|
|
|
|
}
|
2014-03-14 14:02:07 +08:00
|
|
|
void replace(CharSourceRange range, StringRef text) override {
|
2012-03-07 04:06:33 +08:00
|
|
|
Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-12 10:24:20 +08:00
|
|
|
class JSONEditWriter : public edit::EditsReceiver {
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
llvm::raw_ostream &OS;
|
|
|
|
|
|
|
|
public:
|
|
|
|
JSONEditWriter(SourceManager &SM, llvm::raw_ostream &OS)
|
|
|
|
: SourceMgr(SM), OS(OS) {
|
|
|
|
OS << "[\n";
|
|
|
|
}
|
|
|
|
~JSONEditWriter() {
|
|
|
|
OS << "]\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct EntryWriter {
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
llvm::raw_ostream &OS;
|
|
|
|
|
|
|
|
EntryWriter(SourceManager &SM, llvm::raw_ostream &OS)
|
|
|
|
: SourceMgr(SM), OS(OS) {
|
|
|
|
OS << " {\n";
|
|
|
|
}
|
|
|
|
~EntryWriter() {
|
|
|
|
OS << " },\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeLoc(SourceLocation Loc) {
|
|
|
|
FileID FID;
|
|
|
|
unsigned Offset;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(FID, Offset) = SourceMgr.getDecomposedLoc(Loc);
|
2013-12-12 10:24:20 +08:00
|
|
|
assert(!FID.isInvalid());
|
|
|
|
SmallString<200> Path =
|
|
|
|
StringRef(SourceMgr.getFileEntryForID(FID)->getName());
|
|
|
|
llvm::sys::fs::make_absolute(Path);
|
|
|
|
OS << " \"file\": \"";
|
|
|
|
OS.write_escaped(Path.str()) << "\",\n";
|
|
|
|
OS << " \"offset\": " << Offset << ",\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeRemove(CharSourceRange Range) {
|
|
|
|
assert(Range.isCharRange());
|
|
|
|
std::pair<FileID, unsigned> Begin =
|
|
|
|
SourceMgr.getDecomposedLoc(Range.getBegin());
|
|
|
|
std::pair<FileID, unsigned> End =
|
|
|
|
SourceMgr.getDecomposedLoc(Range.getEnd());
|
|
|
|
assert(Begin.first == End.first);
|
|
|
|
assert(Begin.second <= End.second);
|
|
|
|
unsigned Length = End.second - Begin.second;
|
|
|
|
|
|
|
|
OS << " \"remove\": " << Length << ",\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeText(StringRef Text) {
|
|
|
|
OS << " \"text\": \"";
|
|
|
|
OS.write_escaped(Text) << "\",\n";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void insert(SourceLocation Loc, StringRef Text) override {
|
2013-12-12 10:24:20 +08:00
|
|
|
EntryWriter Writer(SourceMgr, OS);
|
|
|
|
Writer.writeLoc(Loc);
|
|
|
|
Writer.writeText(Text);
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void replace(CharSourceRange Range, StringRef Text) override {
|
2013-12-12 10:24:20 +08:00
|
|
|
EntryWriter Writer(SourceMgr, OS);
|
|
|
|
Writer.writeLoc(Range.getBegin());
|
|
|
|
Writer.writeRemove(Range);
|
|
|
|
Writer.writeText(Text);
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void remove(CharSourceRange Range) override {
|
2013-12-12 10:24:20 +08:00
|
|
|
EntryWriter Writer(SourceMgr, OS);
|
|
|
|
Writer.writeLoc(Range.getBegin());
|
|
|
|
Writer.writeRemove(Range);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) {
|
2013-07-04 07:05:00 +08:00
|
|
|
|
|
|
|
TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
|
2013-10-03 05:32:39 +08:00
|
|
|
if (ASTMigrateActions & FrontendOptions::ObjCMT_MigrateDecls) {
|
2013-07-10 00:59:14 +08:00
|
|
|
for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end();
|
|
|
|
D != DEnd; ++D) {
|
2013-11-14 02:20:31 +08:00
|
|
|
FileID FID = PP.getSourceManager().getFileID((*D)->getLocation());
|
|
|
|
if (!FID.isInvalid())
|
|
|
|
if (!FileId.isInvalid() && FileId != FID) {
|
2013-10-03 06:49:59 +08:00
|
|
|
if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation)
|
|
|
|
AnnotateImplicitBridging(Ctx);
|
|
|
|
}
|
|
|
|
|
2013-07-10 00:59:14 +08:00
|
|
|
if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D))
|
2013-12-12 05:39:00 +08:00
|
|
|
if (canModify(CDecl))
|
|
|
|
migrateObjCInterfaceDecl(Ctx, CDecl);
|
2013-10-31 08:06:58 +08:00
|
|
|
if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(*D)) {
|
2013-12-12 05:39:00 +08:00
|
|
|
if (canModify(CatDecl))
|
|
|
|
migrateObjCInterfaceDecl(Ctx, CatDecl);
|
2013-10-31 08:06:58 +08:00
|
|
|
}
|
2013-07-13 06:32:19 +08:00
|
|
|
else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D))
|
2013-12-17 09:01:33 +08:00
|
|
|
ObjCProtocolDecls.insert(PDecl->getCanonicalDecl());
|
2013-07-13 06:32:19 +08:00
|
|
|
else if (const ObjCImplementationDecl *ImpDecl =
|
2013-10-03 06:49:59 +08:00
|
|
|
dyn_cast<ObjCImplementationDecl>(*D)) {
|
2013-12-12 05:39:00 +08:00
|
|
|
if ((ASTMigrateActions & FrontendOptions::ObjCMT_ProtocolConformance) &&
|
|
|
|
canModify(ImpDecl))
|
2013-10-03 06:49:59 +08:00
|
|
|
migrateProtocolConformance(Ctx, ImpDecl);
|
|
|
|
}
|
2013-07-19 04:11:45 +08:00
|
|
|
else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) {
|
2013-10-15 08:00:28 +08:00
|
|
|
if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros))
|
|
|
|
continue;
|
2013-12-12 05:39:00 +08:00
|
|
|
if (!canModify(ED))
|
|
|
|
continue;
|
2013-07-19 04:11:45 +08:00
|
|
|
DeclContext::decl_iterator N = D;
|
2013-10-15 08:00:28 +08:00
|
|
|
if (++N != DEnd) {
|
|
|
|
const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N);
|
|
|
|
if (migrateNSEnumDecl(Ctx, ED, TD) && TD)
|
|
|
|
D++;
|
|
|
|
}
|
|
|
|
else
|
2014-05-07 14:21:57 +08:00
|
|
|
migrateNSEnumDecl(Ctx, ED, /*TypedefDecl */nullptr);
|
2013-10-12 05:34:56 +08:00
|
|
|
}
|
|
|
|
else if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*D)) {
|
2013-10-18 07:13:13 +08:00
|
|
|
if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros))
|
|
|
|
continue;
|
2013-12-12 05:39:00 +08:00
|
|
|
if (!canModify(TD))
|
|
|
|
continue;
|
2013-10-18 07:13:13 +08:00
|
|
|
DeclContext::decl_iterator N = D;
|
|
|
|
if (++N == DEnd)
|
|
|
|
continue;
|
|
|
|
if (const EnumDecl *ED = dyn_cast<EnumDecl>(*N)) {
|
2013-10-15 08:00:28 +08:00
|
|
|
if (++N != DEnd)
|
2013-10-18 07:13:13 +08:00
|
|
|
if (const TypedefDecl *TDF = dyn_cast<TypedefDecl>(*N)) {
|
|
|
|
// prefer typedef-follows-enum to enum-follows-typedef pattern.
|
|
|
|
if (migrateNSEnumDecl(Ctx, ED, TDF)) {
|
|
|
|
++D; ++D;
|
|
|
|
CacheObjCNSIntegerTypedefed(TD);
|
2013-10-15 08:00:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-10-12 05:34:56 +08:00
|
|
|
}
|
2013-10-18 07:13:13 +08:00
|
|
|
if (migrateNSEnumDecl(Ctx, ED, TD)) {
|
|
|
|
++D;
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-15 08:00:28 +08:00
|
|
|
}
|
2013-10-18 07:13:13 +08:00
|
|
|
CacheObjCNSIntegerTypedefed(TD);
|
2013-10-03 06:49:59 +08:00
|
|
|
}
|
|
|
|
else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*D)) {
|
2013-12-12 05:39:00 +08:00
|
|
|
if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) &&
|
|
|
|
canModify(FD))
|
2013-10-03 06:49:59 +08:00
|
|
|
migrateCFAnnotation(Ctx, FD);
|
2013-07-19 04:11:45 +08:00
|
|
|
}
|
2013-08-14 02:01:42 +08:00
|
|
|
|
2013-08-23 02:35:27 +08:00
|
|
|
if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D)) {
|
2013-12-12 05:39:00 +08:00
|
|
|
bool CanModify = canModify(CDecl);
|
2013-08-23 02:35:27 +08:00
|
|
|
// migrate methods which can have instancetype as their result type.
|
2013-12-12 05:39:00 +08:00
|
|
|
if ((ASTMigrateActions & FrontendOptions::ObjCMT_Instancetype) &&
|
|
|
|
CanModify)
|
2013-10-03 06:49:59 +08:00
|
|
|
migrateAllMethodInstaceType(Ctx, CDecl);
|
2013-08-23 02:35:27 +08:00
|
|
|
// annotate methods with CF annotations.
|
2013-12-12 05:39:00 +08:00
|
|
|
if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) &&
|
|
|
|
CanModify)
|
2013-10-03 06:49:59 +08:00
|
|
|
migrateARCSafeAnnotation(Ctx, CDecl);
|
2013-08-23 02:35:27 +08:00
|
|
|
}
|
2013-12-11 02:36:49 +08:00
|
|
|
|
|
|
|
if (const ObjCImplementationDecl *
|
|
|
|
ImplD = dyn_cast<ObjCImplementationDecl>(*D)) {
|
2013-12-12 05:39:00 +08:00
|
|
|
if ((ASTMigrateActions & FrontendOptions::ObjCMT_DesignatedInitializer) &&
|
|
|
|
canModify(ImplD))
|
2013-12-11 02:36:49 +08:00
|
|
|
inferDesignatedInitializers(Ctx, ImplD);
|
|
|
|
}
|
2013-07-10 00:59:14 +08:00
|
|
|
}
|
2013-10-03 06:49:59 +08:00
|
|
|
if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation)
|
|
|
|
AnnotateImplicitBridging(Ctx);
|
2013-08-21 06:42:13 +08:00
|
|
|
}
|
2013-07-04 07:05:00 +08:00
|
|
|
|
2013-12-12 10:24:20 +08:00
|
|
|
if (IsOutputFile) {
|
2014-08-26 02:17:04 +08:00
|
|
|
std::error_code EC;
|
|
|
|
llvm::raw_fd_ostream OS(MigrateDir, EC, llvm::sys::fs::F_None);
|
|
|
|
if (EC) {
|
2014-01-26 14:17:37 +08:00
|
|
|
DiagnosticsEngine &Diags = Ctx.getDiagnostics();
|
|
|
|
Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
|
2014-08-26 02:17:04 +08:00
|
|
|
<< EC.message();
|
2013-12-12 10:24:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONEditWriter Writer(Ctx.getSourceManager(), OS);
|
|
|
|
Editor->applyRewrites(Writer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
|
2012-03-07 04:06:33 +08:00
|
|
|
RewritesReceiver Rec(rewriter);
|
|
|
|
Editor->applyRewrites(Rec);
|
|
|
|
|
|
|
|
for (Rewriter::buffer_iterator
|
|
|
|
I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
|
|
|
|
FileID FID = I->first;
|
|
|
|
RewriteBuffer &buf = I->second;
|
|
|
|
const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
|
|
|
|
assert(file);
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<512> newText;
|
2012-03-07 04:06:33 +08:00
|
|
|
llvm::raw_svector_ostream vecOS(newText);
|
|
|
|
buf.write(vecOS);
|
|
|
|
vecOS.flush();
|
2014-08-18 07:12:27 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> memBuf(
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(
|
|
|
|
StringRef(newText.data(), newText.size()), file->getName()));
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<64> filePath(file->getName());
|
2012-03-07 04:06:33 +08:00
|
|
|
FileMgr.FixupRelativePath(filePath);
|
2014-08-18 07:12:27 +08:00
|
|
|
Remapper.remap(filePath.str(), std::move(memBuf));
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsOutputFile) {
|
|
|
|
Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics());
|
|
|
|
} else {
|
|
|
|
Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) {
|
2012-05-25 00:48:23 +08:00
|
|
|
CI.getDiagnostics().setIgnoreAllWarnings(true);
|
2012-03-07 04:06:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-15 00:33:29 +08:00
|
|
|
static std::vector<std::string> getWhiteListFilenames(StringRef DirPath) {
|
|
|
|
using namespace llvm::sys::fs;
|
|
|
|
using namespace llvm::sys::path;
|
|
|
|
|
|
|
|
std::vector<std::string> Filenames;
|
|
|
|
if (DirPath.empty() || !is_directory(DirPath))
|
|
|
|
return Filenames;
|
2014-06-12 22:02:15 +08:00
|
|
|
|
|
|
|
std::error_code EC;
|
2013-11-15 00:33:29 +08:00
|
|
|
directory_iterator DI = directory_iterator(DirPath, EC);
|
|
|
|
directory_iterator DE;
|
|
|
|
for (; !EC && DI != DE; DI = DI.increment(EC)) {
|
|
|
|
if (is_regular_file(DI->path()))
|
|
|
|
Filenames.push_back(filename(DI->path()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Filenames;
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2012-12-04 15:27:05 +08:00
|
|
|
PPConditionalDirectiveRecord *
|
|
|
|
PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager());
|
2013-11-14 07:38:22 +08:00
|
|
|
unsigned ObjCMTAction = CI.getFrontendOpts().ObjCMTAction;
|
2013-11-15 00:33:20 +08:00
|
|
|
unsigned ObjCMTOpts = ObjCMTAction;
|
|
|
|
// These are companion flags, they do not enable transformations.
|
|
|
|
ObjCMTOpts &= ~(FrontendOptions::ObjCMT_AtomicProperty |
|
|
|
|
FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty);
|
|
|
|
if (ObjCMTOpts == FrontendOptions::ObjCMT_None) {
|
2013-11-14 07:38:22 +08:00
|
|
|
// If no specific option was given, enable literals+subscripting transforms
|
|
|
|
// by default.
|
2013-11-15 00:33:20 +08:00
|
|
|
ObjCMTAction |= FrontendOptions::ObjCMT_Literals |
|
|
|
|
FrontendOptions::ObjCMT_Subscripting;
|
2013-11-14 07:38:22 +08:00
|
|
|
}
|
2014-09-10 12:53:53 +08:00
|
|
|
CI.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
|
2013-11-15 00:33:29 +08:00
|
|
|
std::vector<std::string> WhiteList =
|
|
|
|
getWhiteListFilenames(CI.getFrontendOpts().ObjCMTWhiteListPath);
|
2014-08-11 03:56:51 +08:00
|
|
|
return llvm::make_unique<ObjCMigrateASTConsumer>(
|
|
|
|
CI.getFrontendOpts().OutputFile, ObjCMTAction, Remapper,
|
|
|
|
CI.getFileManager(), PPRec, CI.getPreprocessor(),
|
|
|
|
/*isOutputFile=*/true, WhiteList);
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
2013-12-12 10:24:20 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct EditEntry {
|
|
|
|
const FileEntry *File;
|
|
|
|
unsigned Offset;
|
|
|
|
unsigned RemoveLen;
|
|
|
|
std::string Text;
|
|
|
|
|
|
|
|
EditEntry() : File(), Offset(), RemoveLen() {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template<> struct DenseMapInfo<EditEntry> {
|
|
|
|
static inline EditEntry getEmptyKey() {
|
|
|
|
EditEntry Entry;
|
|
|
|
Entry.Offset = unsigned(-1);
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
static inline EditEntry getTombstoneKey() {
|
|
|
|
EditEntry Entry;
|
|
|
|
Entry.Offset = unsigned(-2);
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(const EditEntry& Val) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
ID.AddPointer(Val.File);
|
|
|
|
ID.AddInteger(Val.Offset);
|
|
|
|
ID.AddInteger(Val.RemoveLen);
|
|
|
|
ID.AddString(Val.Text);
|
|
|
|
return ID.ComputeHash();
|
|
|
|
}
|
|
|
|
static bool isEqual(const EditEntry &LHS, const EditEntry &RHS) {
|
|
|
|
return LHS.File == RHS.File &&
|
|
|
|
LHS.Offset == RHS.Offset &&
|
|
|
|
LHS.RemoveLen == RHS.RemoveLen &&
|
|
|
|
LHS.Text == RHS.Text;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RemapFileParser {
|
|
|
|
FileManager &FileMgr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RemapFileParser(FileManager &FileMgr) : FileMgr(FileMgr) { }
|
|
|
|
|
|
|
|
bool parse(StringRef File, SmallVectorImpl<EditEntry> &Entries) {
|
|
|
|
using namespace llvm::yaml;
|
|
|
|
|
2014-07-07 01:43:24 +08:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
|
|
|
|
llvm::MemoryBuffer::getFile(File);
|
|
|
|
if (!FileBufOrErr)
|
2013-12-12 10:24:20 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
llvm::SourceMgr SM;
|
2014-08-28 03:03:27 +08:00
|
|
|
Stream YAMLStream(FileBufOrErr.get()->getMemBufferRef(), SM);
|
2013-12-12 10:24:20 +08:00
|
|
|
document_iterator I = YAMLStream.begin();
|
|
|
|
if (I == YAMLStream.end())
|
|
|
|
return true;
|
|
|
|
Node *Root = I->getRoot();
|
|
|
|
if (!Root)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
SequenceNode *SeqNode = dyn_cast<SequenceNode>(Root);
|
|
|
|
if (!SeqNode)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (SequenceNode::iterator
|
|
|
|
AI = SeqNode->begin(), AE = SeqNode->end(); AI != AE; ++AI) {
|
|
|
|
MappingNode *MapNode = dyn_cast<MappingNode>(&*AI);
|
|
|
|
if (!MapNode)
|
|
|
|
continue;
|
|
|
|
parseEdit(MapNode, Entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void parseEdit(llvm::yaml::MappingNode *Node,
|
|
|
|
SmallVectorImpl<EditEntry> &Entries) {
|
|
|
|
using namespace llvm::yaml;
|
|
|
|
EditEntry Entry;
|
|
|
|
bool Ignore = false;
|
|
|
|
|
|
|
|
for (MappingNode::iterator
|
|
|
|
KVI = Node->begin(), KVE = Node->end(); KVI != KVE; ++KVI) {
|
|
|
|
ScalarNode *KeyString = dyn_cast<ScalarNode>((*KVI).getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
continue;
|
|
|
|
SmallString<10> KeyStorage;
|
|
|
|
StringRef Key = KeyString->getValue(KeyStorage);
|
|
|
|
|
|
|
|
ScalarNode *ValueString = dyn_cast<ScalarNode>((*KVI).getValue());
|
|
|
|
if (!ValueString)
|
|
|
|
continue;
|
|
|
|
SmallString<64> ValueStorage;
|
|
|
|
StringRef Val = ValueString->getValue(ValueStorage);
|
|
|
|
|
|
|
|
if (Key == "file") {
|
|
|
|
const FileEntry *FE = FileMgr.getFile(Val);
|
|
|
|
if (!FE)
|
|
|
|
Ignore = true;
|
|
|
|
Entry.File = FE;
|
|
|
|
} else if (Key == "offset") {
|
|
|
|
if (Val.getAsInteger(10, Entry.Offset))
|
|
|
|
Ignore = true;
|
|
|
|
} else if (Key == "remove") {
|
|
|
|
if (Val.getAsInteger(10, Entry.RemoveLen))
|
|
|
|
Ignore = true;
|
|
|
|
} else if (Key == "text") {
|
|
|
|
Entry.Text = Val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Ignore)
|
|
|
|
Entries.push_back(Entry);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool reportDiag(const Twine &Err, DiagnosticsEngine &Diag) {
|
2014-01-26 14:17:37 +08:00
|
|
|
Diag.Report(Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
|
|
|
|
<< Err.str();
|
2013-12-12 10:24:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string applyEditsToTemp(const FileEntry *FE,
|
|
|
|
ArrayRef<EditEntry> Edits,
|
|
|
|
FileManager &FileMgr,
|
|
|
|
DiagnosticsEngine &Diag) {
|
|
|
|
using namespace llvm::sys;
|
|
|
|
|
|
|
|
SourceManager SM(Diag, FileMgr);
|
|
|
|
FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
|
|
|
|
LangOptions LangOpts;
|
|
|
|
edit::EditedSource Editor(SM, LangOpts);
|
|
|
|
for (ArrayRef<EditEntry>::iterator
|
|
|
|
I = Edits.begin(), E = Edits.end(); I != E; ++I) {
|
|
|
|
const EditEntry &Entry = *I;
|
|
|
|
assert(Entry.File == FE);
|
|
|
|
SourceLocation Loc =
|
|
|
|
SM.getLocForStartOfFile(FID).getLocWithOffset(Entry.Offset);
|
|
|
|
CharSourceRange Range;
|
|
|
|
if (Entry.RemoveLen != 0) {
|
|
|
|
Range = CharSourceRange::getCharRange(Loc,
|
|
|
|
Loc.getLocWithOffset(Entry.RemoveLen));
|
|
|
|
}
|
|
|
|
|
|
|
|
edit::Commit commit(Editor);
|
|
|
|
if (Range.isInvalid()) {
|
|
|
|
commit.insert(Loc, Entry.Text);
|
|
|
|
} else if (Entry.Text.empty()) {
|
|
|
|
commit.remove(Range);
|
|
|
|
} else {
|
|
|
|
commit.replace(Range, Entry.Text);
|
|
|
|
}
|
|
|
|
Editor.commit(commit);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rewriter rewriter(SM, LangOpts);
|
|
|
|
RewritesReceiver Rec(rewriter);
|
|
|
|
Editor.applyRewrites(Rec);
|
|
|
|
|
|
|
|
const RewriteBuffer *Buf = rewriter.getRewriteBufferFor(FID);
|
|
|
|
SmallString<512> NewText;
|
|
|
|
llvm::raw_svector_ostream OS(NewText);
|
|
|
|
Buf->write(OS);
|
|
|
|
OS.flush();
|
|
|
|
|
|
|
|
SmallString<64> TempPath;
|
|
|
|
int FD;
|
|
|
|
if (fs::createTemporaryFile(path::filename(FE->getName()),
|
|
|
|
path::extension(FE->getName()), FD,
|
|
|
|
TempPath)) {
|
|
|
|
reportDiag("Could not create file: " + TempPath.str(), Diag);
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::raw_fd_ostream TmpOut(FD, /*shouldClose=*/true);
|
|
|
|
TmpOut.write(NewText.data(), NewText.size());
|
|
|
|
TmpOut.close();
|
|
|
|
|
|
|
|
return TempPath.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool arcmt::getFileRemappingsFromFileList(
|
|
|
|
std::vector<std::pair<std::string,std::string> > &remap,
|
|
|
|
ArrayRef<StringRef> remapFiles,
|
|
|
|
DiagnosticConsumer *DiagClient) {
|
|
|
|
bool hasErrorOccurred = false;
|
|
|
|
|
|
|
|
FileSystemOptions FSOpts;
|
|
|
|
FileManager FileMgr(FSOpts);
|
|
|
|
RemapFileParser Parser(FileMgr);
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
|
|
new DiagnosticsEngine(DiagID, new DiagnosticOptions,
|
|
|
|
DiagClient, /*ShouldOwnClient=*/false));
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<const FileEntry *, std::vector<EditEntry> >
|
|
|
|
FileEditEntriesTy;
|
|
|
|
FileEditEntriesTy FileEditEntries;
|
|
|
|
|
|
|
|
llvm::DenseSet<EditEntry> EntriesSet;
|
|
|
|
|
|
|
|
for (ArrayRef<StringRef>::iterator
|
|
|
|
I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) {
|
|
|
|
SmallVector<EditEntry, 16> Entries;
|
|
|
|
if (Parser.parse(*I, Entries))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (SmallVectorImpl<EditEntry>::iterator
|
|
|
|
EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
|
|
|
|
EditEntry &Entry = *EI;
|
|
|
|
if (!Entry.File)
|
|
|
|
continue;
|
|
|
|
std::pair<llvm::DenseSet<EditEntry>::iterator, bool>
|
|
|
|
Insert = EntriesSet.insert(Entry);
|
|
|
|
if (!Insert.second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
FileEditEntries[Entry.File].push_back(Entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (FileEditEntriesTy::iterator
|
|
|
|
I = FileEditEntries.begin(), E = FileEditEntries.end(); I != E; ++I) {
|
|
|
|
std::string TempFile = applyEditsToTemp(I->first, I->second,
|
|
|
|
FileMgr, *Diags);
|
|
|
|
if (TempFile.empty()) {
|
|
|
|
hasErrorOccurred = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
remap.push_back(std::make_pair(I->first->getName(), TempFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasErrorOccurred;
|
|
|
|
}
|