diff options
author | Eric Liu <ioeric@google.com> | 2018-09-05 09:45:27 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2018-09-05 09:45:27 +0000 |
commit | cea78e3a1974e5ab16d1127d001c0d3f36b1c0a3 (patch) | |
tree | d926258a16564a86b25ad0ef41796a34e9d81da9 /clang/lib/Basic/VirtualFileSystem.cpp | |
parent | c91b27d9ee6e3bc49525532680dd636f1d5a1eed (diff) | |
download | llvm-cea78e3a1974e5ab16d1127d001c0d3f36b1c0a3.zip llvm-cea78e3a1974e5ab16d1127d001c0d3f36b1c0a3.tar.gz llvm-cea78e3a1974e5ab16d1127d001c0d3f36b1c0a3.tar.bz2 |
[VFS] Cache the current working directory for the real FS.
Reviewers: sammccall
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D51641
llvm-svn: 341455
Diffstat (limited to 'clang/lib/Basic/VirtualFileSystem.cpp')
-rw-r--r-- | clang/lib/Basic/VirtualFileSystem.cpp | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/clang/lib/Basic/VirtualFileSystem.cpp b/clang/lib/Basic/VirtualFileSystem.cpp index 35716fd7..9972ac4 100644 --- a/clang/lib/Basic/VirtualFileSystem.cpp +++ b/clang/lib/Basic/VirtualFileSystem.cpp @@ -49,6 +49,7 @@ #include <limits> #include <map> #include <memory> +#include <mutex> #include <string> #include <system_error> #include <utility> @@ -244,6 +245,9 @@ public: std::error_code setCurrentWorkingDirectory(const Twine &Path) override; std::error_code getRealPath(const Twine &Path, SmallVectorImpl<char> &Output) const override; +private: + mutable std::mutex CWDMutex; + mutable std::string CWDCache; }; } // namespace @@ -266,10 +270,14 @@ RealFileSystem::openFileForRead(const Twine &Name) { } llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const { + std::lock_guard<std::mutex> 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<std::mutex> Lock(CWDMutex); + CWDCache.clear(); + return std::error_code(); } std::error_code |