dexonsmith created this revision. Herald added subscribers: jdoerfert, kadircet, arphaman, jkorous.
Remove CompilerInstance::VirtualFileSystem and CompilerInstance::setVirtualFileSystem, instead relying on the VFS in the FileManager. CompilerInstance and its clients already went to some trouble to make these match. Now they are guaranteed to match. As part of this, I added a VFS parameter (defaults to nullptr) to CompilerInstance::createFileManager to avoid repeating construction logic in clients that just want to customize the VFS. https://reviews.llvm.org/D59377 Files: clang-tools-extra/clangd/Compiler.cpp clang/include/clang/Frontend/CompilerInstance.h clang/lib/Frontend/ASTUnit.cpp clang/lib/Frontend/CompilerInstance.cpp clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp
Index: lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp =================================================================== --- lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp +++ lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp @@ -671,7 +671,7 @@ } // Make sure clang uses the same VFS as LLDB. - instance->setVirtualFileSystem(FileSystem::Instance().GetVirtualFileSystem()); + instance->createFileManager(FileSystem::Instance().GetVirtualFileSystem()); instance->setDiagnostics(diagnostics_engine.get()); instance->setInvocation(invocation); Index: lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp =================================================================== --- lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp +++ lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp @@ -309,8 +309,7 @@ } // Make sure clang uses the same VFS as LLDB. - m_compiler->setVirtualFileSystem( - FileSystem::Instance().GetVirtualFileSystem()); + m_compiler->createFileManager(FileSystem::Instance().GetVirtualFileSystem()); lldb::LanguageType frame_lang = expr.Language(); // defaults to lldb::eLanguageTypeUnknown Index: clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp =================================================================== --- clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp +++ clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp @@ -82,8 +82,6 @@ Instance.getDiagnostics().setSourceManager(&SM); - Instance.setVirtualFileSystem(&CI.getVirtualFileSystem()); - // The instance wants to take ownership, however DisableFree frontend option // is set to true to avoid double free issues Instance.setFileManager(&CI.getFileManager()); Index: clang/lib/Frontend/CompilerInstance.cpp =================================================================== --- clang/lib/Frontend/CompilerInstance.cpp +++ clang/lib/Frontend/CompilerInstance.cpp @@ -89,10 +89,6 @@ void CompilerInstance::setFileManager(FileManager *Value) { FileMgr = Value; - if (Value) - VirtualFileSystem = Value->getVirtualFileSystem(); - else - VirtualFileSystem.reset(); } void CompilerInstance::setSourceManager(SourceManager *Value) { @@ -297,13 +293,13 @@ // File Manager -FileManager *CompilerInstance::createFileManager() { - if (!hasVirtualFileSystem()) { - IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = - createVFSFromCompilerInvocation(getInvocation(), getDiagnostics()); - setVirtualFileSystem(VFS); - } - FileMgr = new FileManager(getFileSystemOpts(), VirtualFileSystem); +FileManager *CompilerInstance::createFileManager( + IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { + if (!VFS && FileMgr) + VFS = FileMgr->getVirtualFileSystem(); // May be nullptr... + if (!VFS) + VFS = createVFSFromCompilerInvocation(getInvocation(), getDiagnostics()); + FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS)); return FileMgr.get(); } @@ -1101,8 +1097,6 @@ ImportingInstance.getDiagnosticClient()), /*ShouldOwnClient=*/true); - Instance.setVirtualFileSystem(&ImportingInstance.getVirtualFileSystem()); - // Note that this module is part of the module build stack, so that we // can detect cycles in the module graph. Instance.setFileManager(&ImportingInstance.getFileManager()); Index: clang/lib/Frontend/ASTUnit.cpp =================================================================== --- clang/lib/Frontend/ASTUnit.cpp +++ clang/lib/Frontend/ASTUnit.cpp @@ -1078,28 +1078,29 @@ if (!Invocation) return true; + if (VFS && FileMgr) + assert(VFS == FileMgr->getVirtualFileSystem() && + "VFS passed to Parse and VFS in FileMgr are different"); + auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation); if (OverrideMainBuffer) { assert(Preamble && "No preamble was built, but OverrideMainBuffer is not null"); - IntrusiveRefCntPtr<llvm::vfs::FileSystem> OldVFS = VFS; Preamble->AddImplicitPreamble(*CCInvocation, VFS, OverrideMainBuffer.get()); - if (OldVFS != VFS && FileMgr) { - assert(OldVFS == FileMgr->getVirtualFileSystem() && - "VFS passed to Parse and VFS in FileMgr are different"); - FileMgr = new FileManager(FileMgr->getFileSystemOpts(), VFS); - } + // VFS may have changed... } // Create the compiler instance to use for building the AST. std::unique_ptr<CompilerInstance> Clang( new CompilerInstance(std::move(PCHContainerOps))); - if (FileMgr && VFS) { - assert(VFS == FileMgr->getVirtualFileSystem() && - "VFS passed to Parse and VFS in FileMgr are different"); - } else if (VFS) { - Clang->setVirtualFileSystem(VFS); - } + + // Ensure that Clang has a FileManager with the right VFS, which may have + // changed above in AddImplicitPreamble. If VFS is nullptr, rely on + // createFileManager to create one. + if (VFS && FileMgr && FileMgr->getVirtualFileSystem() == VFS) + Clang->setFileManager(&*FileMgr); + else + FileMgr = Clang->createFileManager(std::move(VFS)); // Recover resources if we crash before exiting this method. llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> @@ -1136,10 +1137,6 @@ // Configure the various subsystems. LangOpts = Clang->getInvocation().LangOpts; FileSystemOpts = Clang->getFileSystemOpts(); - if (!FileMgr) { - Clang->createFileManager(); - FileMgr = &Clang->getFileManager(); - } ResetForParse(); Index: clang/include/clang/Frontend/CompilerInstance.h =================================================================== --- clang/include/clang/Frontend/CompilerInstance.h +++ clang/include/clang/Frontend/CompilerInstance.h @@ -82,9 +82,6 @@ /// Auxiliary Target info. IntrusiveRefCntPtr<TargetInfo> AuxTarget; - /// The virtual file system. - IntrusiveRefCntPtr<llvm::vfs::FileSystem> VirtualFileSystem; - /// The file manager. IntrusiveRefCntPtr<FileManager> FileMgr; @@ -382,20 +379,8 @@ /// @name Virtual File System /// { - bool hasVirtualFileSystem() const { return VirtualFileSystem != nullptr; } - llvm::vfs::FileSystem &getVirtualFileSystem() const { - assert(hasVirtualFileSystem() && - "Compiler instance has no virtual file system"); - return *VirtualFileSystem; - } - - /// Replace the current virtual file system. - /// - /// \note Most clients should use setFileManager, which will implicitly reset - /// the virtual file system to the one contained in the file manager. - void setVirtualFileSystem(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) { - VirtualFileSystem = std::move(FS); + return *getFileManager().getVirtualFileSystem(); } /// } @@ -645,7 +630,8 @@ /// Create the file manager and replace any existing one with it. /// /// \return The new file manager on success, or null on failure. - FileManager *createFileManager(); + FileManager * + createFileManager(IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr); /// Create the source manager and replace any existing one with it. void createSourceManager(FileManager &FileMgr); Index: clang-tools-extra/clangd/Compiler.cpp =================================================================== --- clang-tools-extra/clangd/Compiler.cpp +++ clang-tools-extra/clangd/Compiler.cpp @@ -95,7 +95,7 @@ if (auto VFSWithRemapping = createVFSFromCompilerInvocation( Clang->getInvocation(), Clang->getDiagnostics(), VFS)) VFS = VFSWithRemapping; - Clang->setVirtualFileSystem(VFS); + Clang->createFileManager(VFS); Clang->setTarget(TargetInfo::CreateTargetInfo( Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits