|
| 1 | +//===- DependencyScanningFilesystemTest.cpp -------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h" |
| 10 | +#include "llvm/ADT/SmallString.h" |
| 11 | +#include "llvm/Support/VirtualFileSystem.h" |
| 12 | +#include "gtest/gtest.h" |
| 13 | + |
| 14 | +using namespace clang::tooling::dependencies; |
| 15 | + |
| 16 | +namespace { |
| 17 | +struct InstrumentingFilesystem |
| 18 | + : llvm::RTTIExtends<InstrumentingFilesystem, llvm::vfs::ProxyFileSystem> { |
| 19 | + unsigned NumGetRealPathCalls = 0; |
| 20 | + |
| 21 | + using llvm::RTTIExtends<InstrumentingFilesystem, |
| 22 | + llvm::vfs::ProxyFileSystem>::RTTIExtends; |
| 23 | + |
| 24 | + std::error_code getRealPath(const llvm::Twine &Path, |
| 25 | + llvm::SmallVectorImpl<char> &Output) override { |
| 26 | + ++NumGetRealPathCalls; |
| 27 | + return ProxyFileSystem::getRealPath(Path, Output); |
| 28 | + } |
| 29 | +}; |
| 30 | +} // namespace |
| 31 | + |
| 32 | +TEST(DependencyScanningFilesystem, CacheGetRealPath) { |
| 33 | + auto InMemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); |
| 34 | + InMemoryFS->setCurrentWorkingDirectory("/"); |
| 35 | + InMemoryFS->addFile("/foo", 0, llvm::MemoryBuffer::getMemBuffer("")); |
| 36 | + InMemoryFS->addFile("/bar", 0, llvm::MemoryBuffer::getMemBuffer("")); |
| 37 | + |
| 38 | + auto InstrumentingFS = |
| 39 | + llvm::makeIntrusiveRefCnt<InstrumentingFilesystem>(InMemoryFS); |
| 40 | + |
| 41 | + DependencyScanningFilesystemSharedCache SharedCache; |
| 42 | + DependencyScanningWorkerFilesystem DepFS(SharedCache, InstrumentingFS); |
| 43 | + |
| 44 | + { |
| 45 | + llvm::SmallString<128> Result; |
| 46 | + DepFS.getRealPath("/foo", Result); |
| 47 | + EXPECT_EQ(Result, "/foo"); |
| 48 | + EXPECT_EQ(InstrumentingFS->NumGetRealPathCalls, 1u); |
| 49 | + } |
| 50 | + |
| 51 | + { |
| 52 | + llvm::SmallString<128> Result; |
| 53 | + DepFS.getRealPath("/foo", Result); |
| 54 | + EXPECT_EQ(Result, "/foo"); |
| 55 | + EXPECT_EQ(InstrumentingFS->NumGetRealPathCalls, 1u); // Cached, no increase. |
| 56 | + } |
| 57 | + |
| 58 | + { |
| 59 | + llvm::SmallString<128> Result; |
| 60 | + DepFS.getRealPath("/bar", Result); |
| 61 | + EXPECT_EQ(Result, "/bar"); |
| 62 | + EXPECT_EQ(InstrumentingFS->NumGetRealPathCalls, 2u); |
| 63 | + } |
| 64 | +} |
0 commit comments