Skip to content

Commit 1ee923c

Browse files
committed
[clang][deps] Overload 'Filesystem::exists' in 'DependencyScanningFilesystem' to have it use cached status
As-is, calls to `exists()` fallback on the implementation in 'ProxyFileSystem::exists' which explicitly calls out to the underlying `FS`, which for the 'DependencyScanningFilesystem' (overlay) is the real underlying filesystem. Instead, directly overloading 'exists' allows us to have it rely on the cached `status` behavior used elsewhere by the 'DependencyScanningFilesystem'.
1 parent c2b2cd0 commit 1ee923c

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ class DependencyScanningWorkerFilesystem
365365
return LocalCache.insertEntryForFilename(Filename, Entry);
366366
}
367367

368+
/// Check whether \p Path exists. By default checks cached result of \c status(),
369+
/// and falls back on FS if unable to do so.
370+
bool exists(const Twine &Path) override;
371+
368372
/// Returns entry associated with the filename in the shared cache if there is
369373
/// some. Otherwise, constructs new one with the given error code, associates
370374
/// it with the filename and returns the result.

clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ DependencyScanningWorkerFilesystem::status(const Twine &Path) {
270270
return Result->getStatus();
271271
}
272272

273+
bool
274+
DependencyScanningWorkerFilesystem::exists(const Twine &Path) {
275+
llvm::ErrorOr<llvm::vfs::Status> Status = status(Path);
276+
return Status && Status->exists();
277+
}
278+
273279
namespace {
274280

275281
/// The VFS that is used by clang consumes the \c CachedFileSystemEntry using

llvm/unittests/Support/VirtualFileSystemTest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,3 +3560,27 @@ TEST(RedirectingFileSystemTest, ExistsRedirectOnly) {
35603560
EXPECT_FALSE(Redirecting->exists("/b"));
35613561
EXPECT_TRUE(Redirecting->exists("/vfile"));
35623562
}
3563+
3564+
TEST(DependencyScanningWorkerFilesystemTest, ExistsUsesStatCache) {
3565+
struct StatCountingProxyFS : llvm::vfs::ProxyFileSystem {
3566+
int StatCount;
3567+
3568+
StatCountingProxyFS(IntrusiveRefCntPtr<FileSystem> UnderlyingFS)
3569+
: ProxyFileSystem(UnderlyingFS), StatCount(0) {}
3570+
3571+
llvm::ErrorOr<llvm::vfs::Status> status(const Twine &Path) override {
3572+
StatCount++;
3573+
return ProxyFileSystem::status(Path);
3574+
}
3575+
};
3576+
auto BaseFS = IntrusiveRefCntPtr<DummyFileSystem>(new DummyFileSystem());
3577+
auto ScanFS = makeIntrusiveRefCnt<StatCountingProxyFS>(BaseFS);
3578+
BaseFS->addRegularFile("/a");
3579+
BaseFS->addRegularFile("/b");
3580+
ScanFS->status("/a");
3581+
ScanFS->status("/b");
3582+
EXPECT_TRUE(ScanFS->StatCount == 2);
3583+
ScanFS->exists("/a");
3584+
ScanFS->exists("/b");
3585+
EXPECT_TRUE(ScanFS->StatCount == 2);
3586+
}

0 commit comments

Comments
 (0)