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

llvm-svn: 191796
This commit is contained in:
Fariborz Jahanian 2013-10-01 21:16:29 +00:00
parent 79b887f3e3
commit 8f5225b3a7
4 changed files with 49 additions and 9 deletions

View File

@ -49,7 +49,8 @@ private:
const LangOptions &LangOpts;
const PPConditionalDirectiveRecord *PPRec;
EditedSource *Editor;
const bool ForceCommitInSystemHeader;
bool IsCommitable;
SmallVector<Edit, 8> 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; }

View File

@ -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);

View File

@ -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);

View File

@ -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<FileID, unsigned> 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()))