Skip to content

Commit cea78e3

Browse files
author
Eric Liu
committedSep 5, 2018
[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
1 parent c91b27d commit cea78e3

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed
 

‎clang/lib/Basic/VirtualFileSystem.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <limits>
5050
#include <map>
5151
#include <memory>
52+
#include <mutex>
5253
#include <string>
5354
#include <system_error>
5455
#include <utility>
@@ -244,6 +245,9 @@ class RealFileSystem : public FileSystem {
244245
std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
245246
std::error_code getRealPath(const Twine &Path,
246247
SmallVectorImpl<char> &Output) const override;
248+
private:
249+
mutable std::mutex CWDMutex;
250+
mutable std::string CWDCache;
247251
};
248252

249253
} // namespace
@@ -266,10 +270,14 @@ RealFileSystem::openFileForRead(const Twine &Name) {
266270
}
267271

268272
llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
273+
std::lock_guard<std::mutex> Lock(CWDMutex);
274+
if (!CWDCache.empty())
275+
return CWDCache;
269276
SmallString<256> Dir;
270277
if (std::error_code EC = llvm::sys::fs::current_path(Dir))
271278
return EC;
272-
return Dir.str().str();
279+
CWDCache = Dir.str();
280+
return CWDCache;
273281
}
274282

275283
std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
@@ -280,7 +288,13 @@ std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
280288
// difference for example on network filesystems, where symlinks might be
281289
// switched during runtime of the tool. Fixing this depends on having a
282290
// file system abstraction that allows openat() style interactions.
283-
return llvm::sys::fs::set_current_path(Path);
291+
if (auto EC = llvm::sys::fs::set_current_path(Path))
292+
return EC;
293+
294+
// Invalidate cache.
295+
std::lock_guard<std::mutex> Lock(CWDMutex);
296+
CWDCache.clear();
297+
return std::error_code();
284298
}
285299

286300
std::error_code

0 commit comments

Comments
 (0)
Please sign in to comment.