Index: clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp =================================================================== --- clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp +++ clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp @@ -136,11 +136,11 @@ /// /// \returns A map mapping FileEntry to a set of Replacement targeting that /// file. -static llvm::DenseMap> +static llvm::DenseMap> groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs, const clang::SourceManager &SM) { std::set Warned; - llvm::DenseMap> + llvm::DenseMap> GroupedReplacements; // Deduplicate identical replacements in diagnostics unless they are from the @@ -155,7 +155,7 @@ const tooling::TranslationUnitDiagnostics *SourceTU) { // Use the file manager to deduplicate paths. FileEntries are // automatically canonicalized. - if (auto Entry = SM.getFileManager().getFile(R.getFilePath())) { + if (auto Entry = SM.getFileManager().getOptionalFileRef(R.getFilePath())) { if (SourceTU) { auto &Replaces = DiagReplacements[*Entry]; auto It = Replaces.find(R); @@ -203,10 +203,10 @@ // To report conflicting replacements on corresponding file, all replacements // are stored into 1 big AtomicChange. for (const auto &FileAndReplacements : GroupedReplacements) { - const FileEntry *Entry = FileAndReplacements.first; + FileEntryRef Entry = FileAndReplacements.first; const SourceLocation BeginLoc = SM.getLocForStartOfFile(SM.getOrCreateFileID(Entry, SrcMgr::C_User)); - tooling::AtomicChange FileChange(Entry->getName(), Entry->getName()); + tooling::AtomicChange FileChange(Entry.getName(), Entry.getName()); for (const auto &R : FileAndReplacements.second) { llvm::Error Err = FileChange.replace(SM, BeginLoc.getLocWithOffset(R.getOffset()), Index: clang-tools-extra/clang-change-namespace/tool/ClangChangeNamespace.cpp =================================================================== --- clang-tools-extra/clang-change-namespace/tool/ClangChangeNamespace.cpp +++ clang-tools-extra/clang-change-namespace/tool/ClangChangeNamespace.cpp @@ -147,7 +147,7 @@ for (auto I = ChangedFiles.begin(), E = ChangedFiles.end(); I != E; ++I) { OS << " {\n"; OS << " \"FilePath\": \"" << *I << "\",\n"; - const auto Entry = FileMgr.getFile(*I); + const auto Entry = FileMgr.getOptionalFileRef(*I); auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User); std::string Content; llvm::raw_string_ostream ContentStream(Content); @@ -165,7 +165,7 @@ } for (const auto &File : ChangedFiles) { - const auto Entry = FileMgr.getFile(File); + const auto Entry = FileMgr.getOptionalFileRef(File); auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User); outs() << "============== " << File << " ==============\n"; Index: clang-tools-extra/clang-move/Move.cpp =================================================================== --- clang-tools-extra/clang-move/Move.cpp +++ clang-tools-extra/clang-move/Move.cpp @@ -843,8 +843,9 @@ // Move all contents from OldFile to NewFile. void ClangMoveTool::moveAll(SourceManager &SM, StringRef OldFile, StringRef NewFile) { - auto FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile)); + auto FE = SM.getFileManager().getFileRef(makeAbsolutePath(OldFile)); if (!FE) { + llvm::consumeError(FE.takeError()); llvm::errs() << "Failed to get file: " << OldFile << "\n"; return; } Index: clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp =================================================================== --- clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp +++ clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp @@ -78,7 +78,7 @@ Tool.applyAllReplacements(Rewrite); for (const auto &File : Files) { - auto Entry = FileMgr.getFile(File); + auto Entry = FileMgr.getOptionalFileRef(File); const auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User); Rewrite.getEditBuffer(ID).write(outs()); } Index: clang-tools-extra/clang-tidy/ClangTidy.cpp =================================================================== --- clang-tools-extra/clang-tidy/ClangTidy.cpp +++ clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -245,7 +245,7 @@ if (FilePath.empty()) return SourceLocation(); - auto File = SourceMgr.getFileManager().getFile(FilePath); + auto File = SourceMgr.getFileManager().getOptionalFileRef(FilePath); if (!File) return SourceLocation(); Index: clang/include/clang/Basic/SourceManager.h =================================================================== --- clang/include/clang/Basic/SourceManager.h +++ clang/include/clang/Basic/SourceManager.h @@ -848,6 +848,9 @@ /// /// The main file should be set prior to calling this function. bool isMainFile(const FileEntry &SourceFile); + bool isMainFile(FileEntryRef SourceFile) { + return isMainFile(SourceFile.getFileEntry()); + } /// Set the file ID for the precompiled preamble. void setPreambleFileID(FileID Preamble) { @@ -866,10 +869,6 @@ /// being \#included from the specified IncludePosition. /// /// This translates NULL into standard input. - FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, - SrcMgr::CharacteristicKind FileCharacter, - int LoadedID = 0, unsigned LoadedOffset = 0); - FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID = 0, unsigned LoadedOffset = 0); @@ -894,7 +893,7 @@ /// Get the FileID for \p SourceFile if it exists. Otherwise, create a /// new FileID for the \p SourceFile. - FileID getOrCreateFileID(const FileEntry *SourceFile, + FileID getOrCreateFileID(FileEntryRef SourceFile, SrcMgr::CharacteristicKind FileCharacter); /// Return a new SourceLocation that encodes the Index: clang/lib/Basic/SourceManager.cpp =================================================================== --- clang/lib/Basic/SourceManager.cpp +++ clang/lib/Basic/SourceManager.cpp @@ -522,17 +522,6 @@ /// being \#included from the specified IncludePosition. /// /// This translates NULL into standard input. -FileID SourceManager::createFileID(const FileEntry *SourceFile, - SourceLocation IncludePos, - SrcMgr::CharacteristicKind FileCharacter, - int LoadedID, unsigned LoadedOffset) { - assert(SourceFile && "Null source file!"); - SrcMgr::ContentCache &IR = - getOrCreateContentCache(SourceFile, isSystem(FileCharacter)); - return createFileIDImpl(IR, SourceFile->getName(), IncludePos, FileCharacter, - LoadedID, LoadedOffset); -} - FileID SourceManager::createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, @@ -571,7 +560,7 @@ /// Get the FileID for \p SourceFile if it exists. Otherwise, create a /// new FileID for the \p SourceFile. FileID -SourceManager::getOrCreateFileID(const FileEntry *SourceFile, +SourceManager::getOrCreateFileID(FileEntryRef SourceFile, SrcMgr::CharacteristicKind FileCharacter) { FileID ID = translateFile(SourceFile); return ID.isValid() ? ID : createFileID(SourceFile, SourceLocation(), @@ -2205,7 +2194,7 @@ IntrusiveRefCntPtr(new DiagnosticIDs), new DiagnosticOptions); SourceMgr = std::make_unique(*Diagnostics, *FileMgr); - FileID ID = SourceMgr->createFileID(*FileMgr->getFile(FileName), + FileID ID = SourceMgr->createFileID(*FileMgr->getOptionalFileRef(FileName), SourceLocation(), clang::SrcMgr::C_User); assert(ID.isValid()); SourceMgr->setMainFileID(ID); Index: clang/lib/Tooling/Core/Replacement.cpp =================================================================== --- clang/lib/Tooling/Core/Replacement.cpp +++ clang/lib/Tooling/Core/Replacement.cpp @@ -67,7 +67,7 @@ bool Replacement::apply(Rewriter &Rewrite) const { SourceManager &SM = Rewrite.getSourceMgr(); - auto Entry = SM.getFileManager().getFile(FilePath); + auto Entry = SM.getFileManager().getOptionalFileRef(FilePath); if (!Entry) return false; Index: clang/lib/Tooling/Refactoring.cpp =================================================================== --- clang/lib/Tooling/Refactoring.cpp +++ clang/lib/Tooling/Refactoring.cpp @@ -78,11 +78,8 @@ const std::string &FilePath = FileAndReplaces.first; auto &CurReplaces = FileAndReplaces.second; - const FileEntry *Entry = nullptr; - if (auto File = Files.getFile(FilePath)) - Entry = *File; - - FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User); + Optional Entry = Files.getOptionalFileRef(FilePath); + FileID ID = SM.getOrCreateFileID(*Entry, SrcMgr::C_User); StringRef Code = SM.getBufferData(ID); auto CurStyle = format::getStyle(Style, FilePath, "LLVM"); Index: clang/tools/clang-rename/ClangRename.cpp =================================================================== --- clang/tools/clang-rename/ClangRename.cpp +++ clang/tools/clang-rename/ClangRename.cpp @@ -222,7 +222,7 @@ Tool.applyAllReplacements(Rewrite); for (const auto &File : Files) { - auto Entry = FileMgr.getFile(File); + auto Entry = FileMgr.getOptionalFileRef(File); const auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User); Rewrite.getEditBuffer(ID).write(outs()); } Index: clang/unittests/Basic/SourceManagerTest.cpp =================================================================== --- clang/unittests/Basic/SourceManagerTest.cpp +++ clang/unittests/Basic/SourceManagerTest.cpp @@ -167,12 +167,12 @@ std::unique_ptr Buf = llvm::MemoryBuffer::getMemBuffer(Source); - const FileEntry *SourceFile = - FileMgr.getVirtualFile("/mainFile.cpp", Buf->getBufferSize(), 0); + FileEntryRef SourceFile = + FileMgr.getVirtualFileRef("/mainFile.cpp", Buf->getBufferSize(), 0); SourceMgr.overrideFileContents(SourceFile, std::move(Buf)); - const FileEntry *HeaderFile = - FileMgr.getVirtualFile("/test-header.h", HeaderBuf->getBufferSize(), 0); + FileEntryRef HeaderFile = + FileMgr.getVirtualFileRef("/test-header.h", HeaderBuf->getBufferSize(), 0); SourceMgr.overrideFileContents(HeaderFile, std::move(HeaderBuf)); FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User); @@ -496,8 +496,8 @@ std::unique_ptr Buf = llvm::MemoryBuffer::getMemBuffer(Source); - const FileEntry *SourceFile = - FileMgr.getVirtualFile("mainFile.cpp", Buf->getBufferSize(), 0); + FileEntryRef SourceFile = + FileMgr.getVirtualFileRef("mainFile.cpp", Buf->getBufferSize(), 0); SourceMgr.overrideFileContents(SourceFile, std::move(Buf)); std::unique_ptr Buf2 = @@ -509,8 +509,8 @@ FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User); SourceMgr.setMainFileID(MainFileID); - EXPECT_TRUE(SourceMgr.isMainFile(*SourceFile)); - EXPECT_TRUE(SourceMgr.isMainFile(*SourceFile)); + EXPECT_TRUE(SourceMgr.isMainFile(SourceFile)); + EXPECT_TRUE(SourceMgr.isMainFile(SourceFile)); EXPECT_FALSE(SourceMgr.isMainFile(*SecondFile)); }