From: Fariborz Jahanian Date: Tue, 1 Oct 2013 21:16:29 +0000 (+0000) Subject: ObjectiveC migrator: When doing migration, migrator must suggest X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6badc76787dc9480fd7c21d3eb75aab79d2df3f5;p=clang ObjectiveC migrator: When doing migration, migrator must suggest migration of headers which have become system headers by user having put the .system_framework in the sdk directory. // rdar://15066802 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@191796 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Edit/Commit.h b/include/clang/Edit/Commit.h index 0ff7034ba0..626b1dd630 100644 --- a/include/clang/Edit/Commit.h +++ b/include/clang/Edit/Commit.h @@ -49,7 +49,8 @@ private: const LangOptions &LangOpts; const PPConditionalDirectiveRecord *PPRec; EditedSource *Editor; - + + const bool ForceCommitInSystemHeader; bool IsCommitable; SmallVector CachedEdits; @@ -60,7 +61,7 @@ public: Commit(const SourceManager &SM, const LangOptions &LangOpts, const PPConditionalDirectiveRecord *PPRec = 0) : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), Editor(0), - IsCommitable(true) { } + ForceCommitInSystemHeader(true), IsCommitable(true) { } bool isCommitable() const { return IsCommitable; } diff --git a/include/clang/Edit/EditedSource.h b/include/clang/Edit/EditedSource.h index 733ad400c9..3ad5a6be14 100644 --- a/include/clang/Edit/EditedSource.h +++ b/include/clang/Edit/EditedSource.h @@ -28,6 +28,7 @@ class EditedSource { const SourceManager &SourceMgr; const LangOptions &LangOpts; const PPConditionalDirectiveRecord *PPRec; + const bool ForceCommitInSystemHeader; struct FileEdit { StringRef Text; @@ -45,8 +46,10 @@ class EditedSource { public: EditedSource(const SourceManager &SM, const LangOptions &LangOpts, - const PPConditionalDirectiveRecord *PPRec = 0) + const PPConditionalDirectiveRecord *PPRec = 0, + const bool FCommitInSystemHeader = true) : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), + ForceCommitInSystemHeader(FCommitInSystemHeader), StrAlloc(/*size=*/512) { } const SourceManager &getSourceManager() const { return SourceMgr; } @@ -54,6 +57,10 @@ public: const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const { return PPRec; } + + bool getForceCommitInSystemHeader() const { + return ForceCommitInSystemHeader; + } bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs); diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index 71ad48f58b..d84cbbcffc 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -113,7 +113,7 @@ protected: NSAPIObj.reset(new NSAPI(Context)); Editor.reset(new edit::EditedSource(Context.getSourceManager(), Context.getLangOpts(), - PPRec)); + PPRec, false)); } virtual bool HandleTopLevelDecl(DeclGroupRef DG) { @@ -1310,6 +1310,34 @@ public: } +static bool +IsReallyASystemHeader(ASTContext &Ctx, const FileEntry *file, FileID FID) { + bool Invalid = false; + const SrcMgr::SLocEntry &SEntry = + Ctx.getSourceManager().getSLocEntry(FID, &Invalid); + if (!Invalid && SEntry.isFile()) { + const SrcMgr::FileInfo &FI = SEntry.getFile(); + if (!FI.hasLineDirectives()) { + if (FI.getFileCharacteristic() == SrcMgr::C_ExternCSystem) + return true; + if (FI.getFileCharacteristic() == SrcMgr::C_System) { + // This file is in a system header directory. Continue with commiting change + // only if it is a user specified system directory because user put a + // .system_framework file in the framework directory. + StringRef Directory(file->getDir()->getName()); + size_t Ix = Directory.rfind(".framework"); + if (Ix == StringRef::npos) + return true; + std::string PatchToSystemFramework = Directory.slice(0, Ix+sizeof(".framework")); + PatchToSystemFramework += ".system_framework"; + if (!llvm::sys::fs::exists(PatchToSystemFramework.data())) + return true; + } + } + } + return false; +} + void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl(); @@ -1360,6 +1388,8 @@ void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { RewriteBuffer &buf = I->second; const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); assert(file); + if (IsReallyASystemHeader(Ctx, file, FID)) + continue; SmallString<512> newText; llvm::raw_svector_ostream vecOS(newText); buf.write(vecOS); diff --git a/lib/Edit/Commit.cpp b/lib/Edit/Commit.cpp index 9c08cc28ac..706c732dba 100644 --- a/lib/Edit/Commit.cpp +++ b/lib/Edit/Commit.cpp @@ -38,7 +38,9 @@ CharSourceRange Commit::Edit::getInsertFromRange(SourceManager &SM) const { Commit::Commit(EditedSource &Editor) : SourceMgr(Editor.getSourceManager()), LangOpts(Editor.getLangOpts()), PPRec(Editor.getPPCondDirectiveRecord()), - Editor(&Editor), IsCommitable(true) { } + Editor(&Editor), + ForceCommitInSystemHeader(Editor.getForceCommitInSystemHeader()), + IsCommitable(true) { } bool Commit::insert(SourceLocation loc, StringRef text, bool afterToken, bool beforePreviousInsertions) { @@ -232,7 +234,7 @@ bool Commit::canInsert(SourceLocation loc, FileOffset &offs) { if (!isAtStartOfMacroExpansion(loc, &loc)) return false; - if (SM.isInSystemHeader(loc)) + if (SM.isInSystemHeader(loc) && ForceCommitInSystemHeader) return false; std::pair locInfo = SM.getDecomposedLoc(loc); @@ -263,7 +265,7 @@ bool Commit::canInsertAfterToken(SourceLocation loc, FileOffset &offs, if (!isAtEndOfMacroExpansion(loc, &loc)) return false; - if (SM.isInSystemHeader(loc)) + if (SM.isInSystemHeader(loc) && ForceCommitInSystemHeader) return false; loc = Lexer::getLocForEndOfToken(loc, 0, SourceMgr, LangOpts); @@ -301,8 +303,8 @@ bool Commit::canRemoveRange(CharSourceRange range, if (range.getBegin().isMacroID() || range.getEnd().isMacroID()) return false; - if (SM.isInSystemHeader(range.getBegin()) || - SM.isInSystemHeader(range.getEnd())) + if ((SM.isInSystemHeader(range.getBegin()) || + SM.isInSystemHeader(range.getEnd())) && ForceCommitInSystemHeader) return false; if (PPRec && PPRec->rangeIntersectsConditionalDirective(range.getAsRange()))