Skip to content

Commit fce732e

Browse files
committed
Add and test extra assertion
1 parent ab3850f commit fce732e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,13 @@ DependencyScanningFilesystemSharedCache::CacheShard::
132132
std::lock_guard<std::mutex> LockGuard(CacheLock);
133133
auto [It, Inserted] = CacheByFilename.insert({Filename, {nullptr, nullptr}});
134134
auto &[CachedEntry, CachedRealPath] = It->getValue();
135-
if (!CachedEntry)
135+
if (!CachedEntry) {
136+
// The entry is not present in the shared cache. Either the cache doesn't
137+
// know about the file at all, or it only knows about its real path.
138+
assert((Inserted || CachedRealPath) && "existing file with empty pair");
136139
CachedEntry =
137140
new (EntryStorage.Allocate()) CachedFileSystemEntry(std::move(Stat));
141+
}
138142
return *CachedEntry;
139143
}
140144

clang/unittests/Tooling/DependencyScanning/DependencyScanningFilesystemTest.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,58 @@ TEST(DependencyScanningFilesystem, CacheGetRealPath) {
9595
EXPECT_EQ(InstrumentingFS->NumGetRealPathCalls, 2u); // Shared cache.
9696
}
9797
}
98+
99+
TEST(DependencyScanningFilesystem, RealPathAndStatusInvariants) {
100+
auto InMemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
101+
InMemoryFS->setCurrentWorkingDirectory("/");
102+
InMemoryFS->addFile("/foo.c", 0, llvm::MemoryBuffer::getMemBuffer(""));
103+
InMemoryFS->addFile("/bar.c", 0, llvm::MemoryBuffer::getMemBuffer(""));
104+
105+
auto InstrumentingFS =
106+
llvm::makeIntrusiveRefCnt<InstrumentingFilesystem>(InMemoryFS);
107+
108+
DependencyScanningFilesystemSharedCache SharedCache;
109+
DependencyScanningWorkerFilesystem DepFS(SharedCache, InstrumentingFS);
110+
111+
// Success.
112+
{
113+
DepFS.status("/foo.c");
114+
115+
llvm::SmallString<128> Result;
116+
DepFS.getRealPath("/foo.c", Result);
117+
}
118+
{
119+
llvm::SmallString<128> Result;
120+
DepFS.getRealPath("/bar.c", Result);
121+
122+
DepFS.status("/bar.c");
123+
}
124+
125+
// Failure.
126+
{
127+
DepFS.status("/foo.m");
128+
129+
llvm::SmallString<128> Result;
130+
DepFS.getRealPath("/foo.m", Result);
131+
}
132+
{
133+
llvm::SmallString<128> Result;
134+
DepFS.getRealPath("/bar.m", Result);
135+
136+
DepFS.status("/bar.m");
137+
}
138+
139+
// Failure without caching.
140+
{
141+
DepFS.status("/foo");
142+
143+
llvm::SmallString<128> Result;
144+
DepFS.getRealPath("/foo", Result);
145+
}
146+
{
147+
llvm::SmallString<128> Result;
148+
DepFS.getRealPath("/bar", Result);
149+
150+
DepFS.status("/bar");
151+
}
152+
}

0 commit comments

Comments
 (0)