Skip to content

Commit 6ad4695

Browse files
committed
Add test
1 parent fd1f967 commit 6ad4695

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

clang/unittests/Tooling/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ add_clang_unittest(ToolingTests
1313
CastExprTest.cpp
1414
CommentHandlerTest.cpp
1515
CompilationDatabaseTest.cpp
16-
DependencyScannerTest.cpp
1716
DiagnosticsYamlTest.cpp
1817
ExecutionTest.cpp
1918
FixItTest.cpp
@@ -24,6 +23,8 @@ add_clang_unittest(ToolingTests
2423
LookupTest.cpp
2524
QualTypeNamesTest.cpp
2625
RangeSelectorTest.cpp
26+
DependencyScanning/DependencyScannerTest.cpp
27+
DependencyScanning/DependencyScanningFilesystemTest.cpp
2728
RecursiveASTVisitorTests/Attr.cpp
2829
RecursiveASTVisitorTests/BitfieldInitializer.cpp
2930
RecursiveASTVisitorTests/CallbacksLeaf.cpp

clang/unittests/Tooling/DependencyScannerTest.cpp renamed to clang/unittests/Tooling/DependencyScanning/DependencyScannerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- unittest/Tooling/ToolingTest.cpp - Tooling unit tests --------------===//
1+
//===- DependencyScannerTest.cpp ------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)