From db8a742206749b2677c78d0125a21968a7df37b3 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Tue, 26 Mar 2019 22:32:06 +0000 Subject: [PATCH] Basic: Return a reference from FileManager::getVirtualFileSystem, NFC FileManager constructs a VFS in its constructor if it isn't passed one, and there's no way to reset it. Make that contract clear by returning a reference from its accessor. https://reviews.llvm.org/D59388 llvm-svn: 357038 --- clang-tools-extra/clang-move/Move.cpp | 3 +-- clang-tools-extra/clang-tidy/ClangTidy.cpp | 4 ++-- clang-tools-extra/clangd/SourceCode.cpp | 2 +- clang/include/clang/Basic/FileManager.h | 4 +--- .../include/clang/Frontend/CompilerInstance.h | 2 +- clang/lib/Frontend/ASTUnit.cpp | 19 ++++++++++--------- clang/lib/Frontend/CompilerInstance.cpp | 4 ++-- clang/lib/Frontend/FrontendAction.cpp | 4 ++-- clang/lib/Lex/HeaderSearch.cpp | 4 ++-- clang/lib/Lex/ModuleMap.cpp | 4 ++-- clang/lib/Lex/PPLexerChange.cpp | 2 +- clang/lib/Sema/SemaCodeComplete.cpp | 5 +++-- clang/lib/Tooling/Tooling.cpp | 2 +- clang/unittests/AST/ASTImporterTest.cpp | 2 +- 14 files changed, 30 insertions(+), 31 deletions(-) diff --git a/clang-tools-extra/clang-move/Move.cpp b/clang-tools-extra/clang-move/Move.cpp index 8c56861009e0..efb67c68adba 100644 --- a/clang-tools-extra/clang-move/Move.cpp +++ b/clang-tools-extra/clang-move/Move.cpp @@ -87,8 +87,7 @@ std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) { std::string MakeAbsolutePath(const SourceManager &SM, StringRef Path) { llvm::SmallString<128> AbsolutePath(Path); if (std::error_code EC = - SM.getFileManager().getVirtualFileSystem()->makeAbsolute( - AbsolutePath)) + SM.getFileManager().getVirtualFileSystem().makeAbsolute(AbsolutePath)) llvm::errs() << "Warning: could not make absolute file: '" << EC.message() << '\n'; // Handle symbolic link path cases. diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp index 42ab826663a9..c8a07e659821 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -362,7 +362,7 @@ ClangTidyASTConsumerFactory::CreateASTConsumer( auto WorkingDir = Compiler.getSourceManager() .getFileManager() .getVirtualFileSystem() - ->getCurrentWorkingDirectory(); + .getCurrentWorkingDirectory(); if (WorkingDir) Context.setCurrentBuildDirectory(WorkingDir.get()); @@ -555,7 +555,7 @@ void handleErrors(llvm::ArrayRef Errors, llvm::IntrusiveRefCntPtr BaseFS) { ErrorReporter Reporter(Context, Fix, BaseFS); llvm::vfs::FileSystem &FileSystem = - *Reporter.getSourceManager().getFileManager().getVirtualFileSystem(); + Reporter.getSourceManager().getFileManager().getVirtualFileSystem(); auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory(); if (!InitialWorkingDir) llvm::report_fatal_error("Cannot get current working path."); diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp index 86146758a541..4366f36072ea 100644 --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -265,7 +265,7 @@ llvm::Optional getCanonicalPath(const FileEntry *F, llvm::SmallString<128> FilePath = F->getName(); if (!llvm::sys::path::is_absolute(FilePath)) { if (auto EC = - SourceMgr.getFileManager().getVirtualFileSystem()->makeAbsolute( + SourceMgr.getFileManager().getVirtualFileSystem().makeAbsolute( FilePath)) { elog("Could not turn relative path '{0}' to absolute: {1}", FilePath, EC.message()); diff --git a/clang/include/clang/Basic/FileManager.h b/clang/include/clang/Basic/FileManager.h index b8348269124d..96983475f454 100644 --- a/clang/include/clang/Basic/FileManager.h +++ b/clang/include/clang/Basic/FileManager.h @@ -221,9 +221,7 @@ public: FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; } const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; } - IntrusiveRefCntPtr getVirtualFileSystem() const { - return FS; - } + llvm::vfs::FileSystem &getVirtualFileSystem() const { return *FS; } /// Retrieve a file entry for a "virtual" file that acts as /// if there were a file with the given name on disk. diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h index f63ff48f69ac..2eb1349f49c7 100644 --- a/clang/include/clang/Frontend/CompilerInstance.h +++ b/clang/include/clang/Frontend/CompilerInstance.h @@ -380,7 +380,7 @@ public: /// { llvm::vfs::FileSystem &getVirtualFileSystem() const { - return *getFileManager().getVirtualFileSystem(); + return getFileManager().getVirtualFileSystem(); } /// } diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 7bc3010fcad3..6a78b93b2050 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -1079,7 +1079,7 @@ bool ASTUnit::Parse(std::shared_ptr PCHContainerOps, return true; if (VFS && FileMgr) - assert(VFS == FileMgr->getVirtualFileSystem() && + assert(VFS == &FileMgr->getVirtualFileSystem() && "VFS passed to Parse and VFS in FileMgr are different"); auto CCInvocation = std::make_shared(*Invocation); @@ -1097,7 +1097,7 @@ bool ASTUnit::Parse(std::shared_ptr PCHContainerOps, // 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) + if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS) Clang->setFileManager(&*FileMgr); else FileMgr = Clang->createFileManager(std::move(VFS)); @@ -1690,7 +1690,7 @@ std::unique_ptr ASTUnit::LoadFromCompilerInvocation( if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps), PrecompilePreambleAfterNParses, - AST->FileMgr->getVirtualFileSystem())) + &AST->FileMgr->getVirtualFileSystem())) return nullptr; return AST; } @@ -1797,7 +1797,7 @@ bool ASTUnit::Reparse(std::shared_ptr PCHContainerOps, if (!VFS) { assert(FileMgr && "FileMgr is null on Reparse call"); - VFS = FileMgr->getVirtualFileSystem(); + VFS = &FileMgr->getVirtualFileSystem(); } clearFileLevelDecls(); @@ -2211,18 +2211,18 @@ void ASTUnit::CodeComplete( if (Preamble) { std::string CompleteFilePath(File); - auto VFS = FileMgr.getVirtualFileSystem(); - auto CompleteFileStatus = VFS->status(CompleteFilePath); + auto &VFS = FileMgr.getVirtualFileSystem(); + auto CompleteFileStatus = VFS.status(CompleteFilePath); if (CompleteFileStatus) { llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID(); std::string MainPath(OriginalSourceFile); - auto MainStatus = VFS->status(MainPath); + auto MainStatus = VFS.status(MainPath); if (MainStatus) { llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID(); if (CompleteFileID == MainID && Line > 1) OverrideMainBuffer = getMainBufferWithPrecompiledPreamble( - PCHContainerOps, Inv, VFS, false, Line - 1); + PCHContainerOps, Inv, &VFS, false, Line - 1); } } } @@ -2233,7 +2233,8 @@ void ASTUnit::CodeComplete( assert(Preamble && "No preamble was built, but OverrideMainBuffer is not null"); - auto VFS = FileMgr.getVirtualFileSystem(); + IntrusiveRefCntPtr VFS = + &FileMgr.getVirtualFileSystem(); Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS, OverrideMainBuffer.get()); // FIXME: there is no way to update VFS if it was changed by diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 6c82a6e95321..559bdb3c0e5c 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -169,7 +169,7 @@ static void collectIncludePCH(CompilerInstance &CI, std::error_code EC; SmallString<128> DirNative; llvm::sys::path::native(PCHDir->getName(), DirNative); - llvm::vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); + llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); SimpleASTReaderListener Validator(CI.getPreprocessor()); for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd; Dir != DirEnd && !EC; Dir.increment(EC)) { @@ -296,7 +296,7 @@ CompilerInstance::createDiagnostics(DiagnosticOptions *Opts, FileManager *CompilerInstance::createFileManager( IntrusiveRefCntPtr VFS) { if (!VFS) - VFS = FileMgr ? FileMgr->getVirtualFileSystem() + VFS = FileMgr ? &FileMgr->getVirtualFileSystem() : createVFSFromCompilerInvocation(getInvocation(), getDiagnostics()); assert(VFS && "FileManager has no VFS?"); diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp index 3392923380f3..2f4f5ef64c26 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -360,7 +360,7 @@ static std::error_code collectModuleHeaderIncludes( SmallString<128> DirNative; llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative); - llvm::vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); + llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End; Dir != End && !EC; Dir.increment(EC)) { // Check whether this entry has an extension typically associated with @@ -714,7 +714,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, SmallString<128> DirNative; llvm::sys::path::native(PCHDir->getName(), DirNative); bool Found = false; - llvm::vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); + llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd; Dir != DirEnd && !EC; Dir.increment(EC)) { diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp index 9c92f772c2b5..e0ffb8758cfb 100644 --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -1579,7 +1579,7 @@ void HeaderSearch::collectAllModules(SmallVectorImpl &Modules) { DirNative); // Search each of the ".framework" directories to load them as modules. - llvm::vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); + llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd; Dir != DirEnd && !EC; Dir.increment(EC)) { @@ -1650,7 +1650,7 @@ void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) { FileMgr.makeAbsolutePath(Dir); SmallString<128> DirNative; llvm::sys::path::native(Dir, DirNative); - llvm::vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); + llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd; Dir != DirEnd && !EC; Dir.increment(EC)) { bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework"; diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp index 31eff14553e4..b597c6dafcbc 100644 --- a/clang/lib/Lex/ModuleMap.cpp +++ b/clang/lib/Lex/ModuleMap.cpp @@ -1021,7 +1021,7 @@ Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir, = StringRef(FrameworkDir->getName()); llvm::sys::path::append(SubframeworksDirName, "Frameworks"); llvm::sys::path::native(SubframeworksDirName); - llvm::vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); + llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); for (llvm::vfs::directory_iterator Dir = FS.dir_begin(SubframeworksDirName, EC), DirEnd; @@ -2397,7 +2397,7 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { std::error_code EC; SmallVector Headers; llvm::vfs::FileSystem &FS = - *SourceMgr.getFileManager().getVirtualFileSystem(); + SourceMgr.getFileManager().getVirtualFileSystem(); for (llvm::vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E; I != E && !EC; I.increment(EC)) { if (const FileEntry *FE = SourceMgr.getFileManager().getFile(I->path())) { diff --git a/clang/lib/Lex/PPLexerChange.cpp b/clang/lib/Lex/PPLexerChange.cpp index 2f1e67c67c49..5f547d9f1cc9 100644 --- a/clang/lib/Lex/PPLexerChange.cpp +++ b/clang/lib/Lex/PPLexerChange.cpp @@ -270,7 +270,7 @@ void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) { ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); const DirectoryEntry *Dir = Mod.getUmbrellaDir().Entry; - llvm::vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); + llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); std::error_code EC; for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC), End; diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp index 0946a24676dc..2412d919ec96 100644 --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -8379,7 +8379,8 @@ void Sema::CodeCompleteIncludedFile(llvm::StringRef Dir, bool Angled) { // We need the native slashes for the actual file system interactions. SmallString<128> NativeRelDir = StringRef(RelDir); llvm::sys::path::native(NativeRelDir); - auto FS = getSourceManager().getFileManager().getVirtualFileSystem(); + llvm::vfs::FileSystem &FS = + getSourceManager().getFileManager().getVirtualFileSystem(); ResultBuilder Results(*this, CodeCompleter->getAllocator(), CodeCompleter->getCodeCompletionTUInfo(), @@ -8425,7 +8426,7 @@ void Sema::CodeCompleteIncludedFile(llvm::StringRef Dir, bool Angled) { std::error_code EC; unsigned Count = 0; - for (auto It = FS->dir_begin(Dir, EC); + for (auto It = FS.dir_begin(Dir, EC); !EC && It != llvm::vfs::directory_iterator(); It.increment(EC)) { if (++Count == 2500) // If we happen to hit a huge directory, break; // bail out early so we're not too slow. diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index c82be38ca15c..68760ef75694 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -301,7 +301,7 @@ bool ToolInvocation::run() { DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false); const std::unique_ptr Driver( - newDriver(&Diagnostics, BinaryName, Files->getVirtualFileSystem())); + newDriver(&Diagnostics, BinaryName, &Files->getVirtualFileSystem())); // The "input file not found" diagnostics from the driver are useful. // The driver is only aware of the VFS working directory, but some clients // change this at the FileManager level instead. diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index d6bd13498567..a282e2a02442 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -44,7 +44,7 @@ createVirtualFileIfNeeded(ASTUnit *ToAST, StringRef FileName, assert(ToAST); ASTContext &ToCtx = ToAST->getASTContext(); auto *OFS = static_cast( - ToCtx.getSourceManager().getFileManager().getVirtualFileSystem().get()); + &ToCtx.getSourceManager().getFileManager().getVirtualFileSystem()); auto *MFS = static_cast( OFS->overlays_begin()->get()); MFS->addFile(FileName, 0, std::move(Buffer));