From: Eric Liu Date: Wed, 5 Sep 2018 09:45:27 +0000 (+0000) Subject: [VFS] Cache the current working directory for the real FS. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1143a41d6e32d05fc5fc2f1581708ab82282fa4f;p=clang [VFS] Cache the current working directory for the real FS. Reviewers: sammccall Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D51641 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@341455 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Basic/VirtualFileSystem.cpp b/lib/Basic/VirtualFileSystem.cpp index 35716fd7b8..9972ac4cba 100644 --- a/lib/Basic/VirtualFileSystem.cpp +++ b/lib/Basic/VirtualFileSystem.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -244,6 +245,9 @@ public: std::error_code setCurrentWorkingDirectory(const Twine &Path) override; std::error_code getRealPath(const Twine &Path, SmallVectorImpl &Output) const override; +private: + mutable std::mutex CWDMutex; + mutable std::string CWDCache; }; } // namespace @@ -266,10 +270,14 @@ RealFileSystem::openFileForRead(const Twine &Name) { } llvm::ErrorOr RealFileSystem::getCurrentWorkingDirectory() const { + std::lock_guard Lock(CWDMutex); + if (!CWDCache.empty()) + return CWDCache; SmallString<256> Dir; if (std::error_code EC = llvm::sys::fs::current_path(Dir)) return EC; - return Dir.str().str(); + CWDCache = Dir.str(); + return CWDCache; } std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) { @@ -280,7 +288,13 @@ std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) { // difference for example on network filesystems, where symlinks might be // switched during runtime of the tool. Fixing this depends on having a // file system abstraction that allows openat() style interactions. - return llvm::sys::fs::set_current_path(Path); + if (auto EC = llvm::sys::fs::set_current_path(Path)) + return EC; + + // Invalidate cache. + std::lock_guard Lock(CWDMutex); + CWDCache.clear(); + return std::error_code(); } std::error_code