diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -1033,6 +1033,13 @@ return nullptr; } + /// Returns the FileEntryRef for the provided FileID. + Optional getFileEntryRefForID(FileID FID) const { + if (auto *Entry = getFileEntryForID(FID)) + return Entry->getLastRef(); + return None; + } + /// Returns the filename for the provided FileID, unless it's a built-in /// buffer that's not represented by a filename. /// diff --git a/clang/lib/ARCMigrate/ObjCMT.cpp b/clang/lib/ARCMigrate/ObjCMT.cpp --- a/clang/lib/ARCMigrate/ObjCMT.cpp +++ b/clang/lib/ARCMigrate/ObjCMT.cpp @@ -156,7 +156,7 @@ return WhiteListFilenames.find(llvm::sys::path::filename(Path)) != WhiteListFilenames.end(); } - bool canModifyFile(const FileEntry *FE) { + bool canModifyFile(Optional FE) { if (!FE) return false; return canModifyFile(FE->getName()); @@ -164,7 +164,7 @@ bool canModifyFile(FileID FID) { if (FID.isInvalid()) return false; - return canModifyFile(PP.getSourceManager().getFileEntryForID(FID)); + return canModifyFile(PP.getSourceManager().getFileEntryRefForID(FID)); } bool canModify(const Decl *D) { @@ -1964,7 +1964,7 @@ 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); + Optional file = Ctx.getSourceManager().getFileEntryRefForID(FID); assert(file); SmallString<512> newText; llvm::raw_svector_ostream vecOS(newText); @@ -2034,7 +2034,7 @@ namespace { struct EditEntry { - const FileEntry *File = nullptr; + Optional File; unsigned Offset = 0; unsigned RemoveLen = 0; std::string Text; @@ -2127,9 +2127,8 @@ StringRef Val = ValueString->getValue(ValueStorage); if (Key == "file") { - auto FE = FileMgr.getFile(Val); - if (FE) - Entry.File = *FE; + if (auto File = FileMgr.getOptionalFileRef(Val)) + Entry.File = File; else Ignore = true; } else if (Key == "offset") { @@ -2155,7 +2154,7 @@ return true; } -static std::string applyEditsToTemp(const FileEntry *FE, +static std::string applyEditsToTemp(FileEntryRef FE, ArrayRef Edits, FileManager &FileMgr, DiagnosticsEngine &Diag) { @@ -2199,8 +2198,8 @@ SmallString<64> TempPath; int FD; - if (fs::createTemporaryFile(path::filename(FE->getName()), - path::extension(FE->getName()).drop_front(), FD, + if (fs::createTemporaryFile(path::filename(FE.getName()), + path::extension(FE.getName()).drop_front(), FD, TempPath)) { reportDiag("Could not create file: " + TempPath.str(), Diag); return std::string(); @@ -2228,7 +2227,7 @@ new DiagnosticsEngine(DiagID, new DiagnosticOptions, DiagClient, /*ShouldOwnClient=*/false)); - typedef llvm::DenseMap > + typedef llvm::DenseMap > FileEditEntriesTy; FileEditEntriesTy FileEditEntries; @@ -2250,7 +2249,7 @@ if (!Insert.second) continue; - FileEditEntries[Entry.File].push_back(Entry); + FileEditEntries[*Entry.File].push_back(Entry); } } @@ -2263,7 +2262,7 @@ continue; } - remap.emplace_back(std::string(I->first->getName()), TempFile); + remap.emplace_back(std::string(I->first.getName()), TempFile); } return hasErrorOccurred;