1 //===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===// 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/Basic/FileManager.h" 10 #include "clang/Basic/FileSystemOptions.h" 11 #include "clang/Basic/FileSystemStatCache.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/Support/Path.h" 14 #include "llvm/Support/VirtualFileSystem.h" 15 #include "gtest/gtest.h" 16 17 using namespace llvm; 18 using namespace clang; 19 20 namespace { 21 22 // Used to create a fake file system for running the tests with such 23 // that the tests are not affected by the structure/contents of the 24 // file system on the machine running the tests. 25 class FakeStatCache : public FileSystemStatCache { 26 private: 27 // Maps a file/directory path to its desired stat result. Anything 28 // not in this map is considered to not exist in the file system. 29 llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls; 30 31 void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile, 32 const char *StatPath) { 33 #ifndef _WIN32 34 SmallString<128> NormalizedPath(Path); 35 llvm::sys::path::native(NormalizedPath); 36 Path = NormalizedPath.c_str(); 37 38 SmallString<128> NormalizedStatPath; 39 if (StatPath) { 40 NormalizedStatPath = StatPath; 41 llvm::sys::path::native(NormalizedStatPath); 42 StatPath = NormalizedStatPath.c_str(); 43 } 44 #endif 45 46 if (!StatPath) 47 StatPath = Path; 48 49 auto fileType = IsFile ? 50 llvm::sys::fs::file_type::regular_file : 51 llvm::sys::fs::file_type::directory_file; 52 llvm::vfs::Status Status(StatPath, llvm::sys::fs::UniqueID(1, INode), 53 /*MTime*/{}, /*User*/0, /*Group*/0, 54 /*Size*/0, fileType, 55 llvm::sys::fs::perms::all_all); 56 StatCalls[Path] = Status; 57 } 58 59 public: 60 // Inject a file with the given inode value to the fake file system. 61 void InjectFile(const char *Path, ino_t INode, 62 const char *StatPath = nullptr) { 63 InjectFileOrDirectory(Path, INode, /*IsFile=*/true, StatPath); 64 } 65 66 // Inject a directory with the given inode value to the fake file system. 67 void InjectDirectory(const char *Path, ino_t INode) { 68 InjectFileOrDirectory(Path, INode, /*IsFile=*/false, nullptr); 69 } 70 71 // Implement FileSystemStatCache::getStat(). 72 std::error_code getStat(StringRef Path, llvm::vfs::Status &Status, 73 bool isFile, 74 std::unique_ptr<llvm::vfs::File> *F, 75 llvm::vfs::FileSystem &FS) override { 76 #ifndef _WIN32 77 SmallString<128> NormalizedPath(Path); 78 llvm::sys::path::native(NormalizedPath); 79 Path = NormalizedPath.c_str(); 80 #endif 81 82 if (StatCalls.count(Path) != 0) { 83 Status = StatCalls[Path]; 84 return std::error_code(); 85 } 86 87 return std::make_error_code(std::errc::no_such_file_or_directory); 88 } 89 }; 90 91 // The test fixture. 92 class FileManagerTest : public ::testing::Test { 93 protected: 94 FileManagerTest() : manager(options) { 95 } 96 97 FileSystemOptions options; 98 FileManager manager; 99 }; 100 101 // When a virtual file is added, its getDir() field is set correctly 102 // (not NULL, correct name). 103 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) { 104 const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0); 105 ASSERT_TRUE(file != nullptr); 106 107 const DirectoryEntry *dir = file->getDir(); 108 ASSERT_TRUE(dir != nullptr); 109 EXPECT_EQ(".", dir->getName()); 110 111 file = manager.getVirtualFile("x/y/z.cpp", 42, 0); 112 ASSERT_TRUE(file != nullptr); 113 114 dir = file->getDir(); 115 ASSERT_TRUE(dir != nullptr); 116 EXPECT_EQ("x/y", dir->getName()); 117 } 118 119 // Before any virtual file is added, no virtual directory exists. 120 TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) { 121 // An empty FakeStatCache causes all stat calls made by the 122 // FileManager to report "file/directory doesn't exist". This 123 // avoids the possibility of the result of this test being affected 124 // by what's in the real file system. 125 manager.setStatCache(std::make_unique<FakeStatCache>()); 126 127 ASSERT_FALSE(manager.getDirectory("virtual/dir/foo")); 128 ASSERT_FALSE(manager.getDirectory("virtual/dir")); 129 ASSERT_FALSE(manager.getDirectory("virtual")); 130 } 131 132 // When a virtual file is added, all of its ancestors should be created. 133 TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) { 134 // Fake an empty real file system. 135 manager.setStatCache(std::make_unique<FakeStatCache>()); 136 137 manager.getVirtualFile("virtual/dir/bar.h", 100, 0); 138 ASSERT_FALSE(manager.getDirectory("virtual/dir/foo")); 139 140 auto dir = manager.getDirectory("virtual/dir"); 141 ASSERT_TRUE(dir); 142 EXPECT_EQ("virtual/dir", (*dir)->getName()); 143 144 dir = manager.getDirectory("virtual"); 145 ASSERT_TRUE(dir); 146 EXPECT_EQ("virtual", (*dir)->getName()); 147 } 148 149 // getFile() returns non-NULL if a real file exists at the given path. 150 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) { 151 // Inject fake files into the file system. 152 auto statCache = std::make_unique<FakeStatCache>(); 153 statCache->InjectDirectory("/tmp", 42); 154 statCache->InjectFile("/tmp/test", 43); 155 156 #ifdef _WIN32 157 const char *DirName = "C:."; 158 const char *FileName = "C:test"; 159 statCache->InjectDirectory(DirName, 44); 160 statCache->InjectFile(FileName, 45); 161 #endif 162 163 manager.setStatCache(std::move(statCache)); 164 165 auto file = manager.getFile("/tmp/test"); 166 ASSERT_TRUE(file); 167 ASSERT_TRUE((*file)->isValid()); 168 EXPECT_EQ("/tmp/test", (*file)->getName()); 169 170 const DirectoryEntry *dir = (*file)->getDir(); 171 ASSERT_TRUE(dir != nullptr); 172 EXPECT_EQ("/tmp", dir->getName()); 173 174 #ifdef _WIN32 175 file = manager.getFile(FileName); 176 ASSERT_TRUE(file); 177 178 dir = (*file)->getDir(); 179 ASSERT_TRUE(dir != NULL); 180 EXPECT_EQ(DirName, dir->getName()); 181 #endif 182 } 183 184 // getFile() returns non-NULL if a virtual file exists at the given path. 185 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) { 186 // Fake an empty real file system. 187 manager.setStatCache(std::make_unique<FakeStatCache>()); 188 189 manager.getVirtualFile("virtual/dir/bar.h", 100, 0); 190 auto file = manager.getFile("virtual/dir/bar.h"); 191 ASSERT_TRUE(file); 192 ASSERT_TRUE((*file)->isValid()); 193 EXPECT_EQ("virtual/dir/bar.h", (*file)->getName()); 194 195 const DirectoryEntry *dir = (*file)->getDir(); 196 ASSERT_TRUE(dir != nullptr); 197 EXPECT_EQ("virtual/dir", dir->getName()); 198 } 199 200 // getFile() returns different FileEntries for different paths when 201 // there's no aliasing. 202 TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) { 203 // Inject two fake files into the file system. Different inodes 204 // mean the files are not symlinked together. 205 auto statCache = std::make_unique<FakeStatCache>(); 206 statCache->InjectDirectory(".", 41); 207 statCache->InjectFile("foo.cpp", 42); 208 statCache->InjectFile("bar.cpp", 43); 209 manager.setStatCache(std::move(statCache)); 210 211 auto fileFoo = manager.getFile("foo.cpp"); 212 auto fileBar = manager.getFile("bar.cpp"); 213 ASSERT_TRUE(fileFoo); 214 ASSERT_TRUE((*fileFoo)->isValid()); 215 ASSERT_TRUE(fileBar); 216 ASSERT_TRUE((*fileBar)->isValid()); 217 EXPECT_NE(*fileFoo, *fileBar); 218 } 219 220 // getFile() returns an error if neither a real file nor a virtual file 221 // exists at the given path. 222 TEST_F(FileManagerTest, getFileReturnsErrorForNonexistentFile) { 223 // Inject a fake foo.cpp into the file system. 224 auto statCache = std::make_unique<FakeStatCache>(); 225 statCache->InjectDirectory(".", 41); 226 statCache->InjectFile("foo.cpp", 42); 227 statCache->InjectDirectory("MyDirectory", 49); 228 manager.setStatCache(std::move(statCache)); 229 230 // Create a virtual bar.cpp file. 231 manager.getVirtualFile("bar.cpp", 200, 0); 232 233 auto file = manager.getFile("xyz.txt"); 234 ASSERT_FALSE(file); 235 ASSERT_EQ(file.getError(), std::errc::no_such_file_or_directory); 236 237 auto readingDirAsFile = manager.getFile("MyDirectory"); 238 ASSERT_FALSE(readingDirAsFile); 239 ASSERT_EQ(readingDirAsFile.getError(), std::errc::is_a_directory); 240 241 auto readingFileAsDir = manager.getDirectory("foo.cpp"); 242 ASSERT_FALSE(readingFileAsDir); 243 ASSERT_EQ(readingFileAsDir.getError(), std::errc::not_a_directory); 244 } 245 246 // The following tests apply to Unix-like system only. 247 248 #ifndef _WIN32 249 250 // getFile() returns the same FileEntry for real files that are aliases. 251 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) { 252 // Inject two real files with the same inode. 253 auto statCache = std::make_unique<FakeStatCache>(); 254 statCache->InjectDirectory("abc", 41); 255 statCache->InjectFile("abc/foo.cpp", 42); 256 statCache->InjectFile("abc/bar.cpp", 42); 257 manager.setStatCache(std::move(statCache)); 258 259 auto f1 = manager.getFile("abc/foo.cpp"); 260 auto f2 = manager.getFile("abc/bar.cpp"); 261 262 EXPECT_EQ(f1 ? *f1 : nullptr, 263 f2 ? *f2 : nullptr); 264 265 // Check that getFileRef also does the right thing. 266 auto r1 = manager.getFileRef("abc/foo.cpp"); 267 auto r2 = manager.getFileRef("abc/bar.cpp"); 268 ASSERT_FALSE(!r1); 269 ASSERT_FALSE(!r2); 270 271 EXPECT_EQ("abc/foo.cpp", r1->getName()); 272 EXPECT_EQ("abc/bar.cpp", r2->getName()); 273 EXPECT_EQ((f1 ? *f1 : nullptr), &r1->getFileEntry()); 274 EXPECT_EQ((f2 ? *f2 : nullptr), &r2->getFileEntry()); 275 } 276 277 TEST_F(FileManagerTest, getFileRefReturnsCorrectNameForDifferentStatPath) { 278 // Inject files with the same inode, but where some files have a stat that 279 // gives a different name. This is adding coverage for weird stat behaviour 280 // triggered by the RedirectingFileSystem that FileManager::getFileRef has 281 // special logic for. 282 auto StatCache = std::make_unique<FakeStatCache>(); 283 StatCache->InjectDirectory("dir", 40); 284 StatCache->InjectFile("dir/f1.cpp", 41); 285 StatCache->InjectFile("dir/f1-alias.cpp", 41, "dir/f1.cpp"); 286 StatCache->InjectFile("dir/f2.cpp", 42); 287 StatCache->InjectFile("dir/f2-alias.cpp", 42, "dir/f2.cpp"); 288 manager.setStatCache(std::move(StatCache)); 289 290 // With F2, test accessing the non-redirected name first. 291 auto F1 = manager.getFileRef("dir/f1.cpp"); 292 auto F1Alias = manager.getFileRef("dir/f1-alias.cpp"); 293 auto F1Alias2 = manager.getFileRef("dir/f1-alias.cpp"); 294 ASSERT_FALSE(!F1); 295 ASSERT_FALSE(!F1Alias); 296 ASSERT_FALSE(!F1Alias2); 297 EXPECT_EQ("dir/f1.cpp", F1->getName()); 298 EXPECT_EQ("dir/f1.cpp", F1->getFileEntry().getName()); 299 EXPECT_EQ("dir/f1.cpp", F1Alias->getName()); 300 EXPECT_EQ("dir/f1.cpp", F1Alias2->getName()); 301 EXPECT_EQ(&F1->getFileEntry(), &F1Alias->getFileEntry()); 302 EXPECT_EQ(&F1->getFileEntry(), &F1Alias2->getFileEntry()); 303 304 // With F2, test accessing the redirected name first. 305 auto F2Alias = manager.getFileRef("dir/f2-alias.cpp"); 306 auto F2 = manager.getFileRef("dir/f2.cpp"); 307 auto F2Alias2 = manager.getFileRef("dir/f2-alias.cpp"); 308 ASSERT_FALSE(!F2); 309 ASSERT_FALSE(!F2Alias); 310 ASSERT_FALSE(!F2Alias2); 311 EXPECT_EQ("dir/f2.cpp", F2->getName()); 312 EXPECT_EQ("dir/f2.cpp", F2->getFileEntry().getName()); 313 EXPECT_EQ("dir/f2.cpp", F2Alias->getName()); 314 EXPECT_EQ("dir/f2.cpp", F2Alias2->getName()); 315 EXPECT_EQ(&F2->getFileEntry(), &F2Alias->getFileEntry()); 316 EXPECT_EQ(&F2->getFileEntry(), &F2Alias2->getFileEntry()); 317 } 318 319 // getFile() returns the same FileEntry for virtual files that have 320 // corresponding real files that are aliases. 321 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) { 322 // Inject two real files with the same inode. 323 auto statCache = std::make_unique<FakeStatCache>(); 324 statCache->InjectDirectory("abc", 41); 325 statCache->InjectFile("abc/foo.cpp", 42); 326 statCache->InjectFile("abc/bar.cpp", 42); 327 manager.setStatCache(std::move(statCache)); 328 329 ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid()); 330 ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid()); 331 332 auto f1 = manager.getFile("abc/foo.cpp"); 333 auto f2 = manager.getFile("abc/bar.cpp"); 334 335 EXPECT_EQ(f1 ? *f1 : nullptr, 336 f2 ? *f2 : nullptr); 337 } 338 339 // getFile() Should return the same entry as getVirtualFile if the file actually 340 // is a virtual file, even if the name is not exactly the same (but is after 341 // normalisation done by the file system, like on Windows). This can be checked 342 // here by checking the size. 343 TEST_F(FileManagerTest, getVirtualFileWithDifferentName) { 344 // Inject fake files into the file system. 345 auto statCache = std::make_unique<FakeStatCache>(); 346 statCache->InjectDirectory("c:\\tmp", 42); 347 statCache->InjectFile("c:\\tmp\\test", 43); 348 349 manager.setStatCache(std::move(statCache)); 350 351 // Inject the virtual file: 352 const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1); 353 ASSERT_TRUE(file1 != nullptr); 354 ASSERT_TRUE(file1->isValid()); 355 EXPECT_EQ(43U, file1->getUniqueID().getFile()); 356 EXPECT_EQ(123, file1->getSize()); 357 358 // Lookup the virtual file with a different name: 359 auto file2 = manager.getFile("c:/tmp/test", 100, 1); 360 ASSERT_TRUE(file2); 361 ASSERT_TRUE((*file2)->isValid()); 362 // Check that it's the same UFE: 363 EXPECT_EQ(file1, *file2); 364 EXPECT_EQ(43U, (*file2)->getUniqueID().getFile()); 365 // Check that the contents of the UFE are not overwritten by the entry in the 366 // filesystem: 367 EXPECT_EQ(123, (*file2)->getSize()); 368 } 369 370 #endif // !_WIN32 371 372 TEST_F(FileManagerTest, makeAbsoluteUsesVFS) { 373 SmallString<64> CustomWorkingDir; 374 #ifdef _WIN32 375 CustomWorkingDir = "C:"; 376 #else 377 CustomWorkingDir = "/"; 378 #endif 379 llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path"); 380 381 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 382 new llvm::vfs::InMemoryFileSystem); 383 // setCurrentworkingdirectory must finish without error. 384 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 385 386 FileSystemOptions Opts; 387 FileManager Manager(Opts, FS); 388 389 SmallString<64> Path("a/foo.cpp"); 390 391 SmallString<64> ExpectedResult(CustomWorkingDir); 392 llvm::sys::path::append(ExpectedResult, Path); 393 394 ASSERT_TRUE(Manager.makeAbsolutePath(Path)); 395 EXPECT_EQ(Path, ExpectedResult); 396 } 397 398 // getVirtualFile should always fill the real path. 399 TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) { 400 SmallString<64> CustomWorkingDir; 401 #ifdef _WIN32 402 CustomWorkingDir = "C:/"; 403 #else 404 CustomWorkingDir = "/"; 405 #endif 406 407 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 408 new llvm::vfs::InMemoryFileSystem); 409 // setCurrentworkingdirectory must finish without error. 410 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 411 412 FileSystemOptions Opts; 413 FileManager Manager(Opts, FS); 414 415 // Inject fake files into the file system. 416 auto statCache = std::make_unique<FakeStatCache>(); 417 statCache->InjectDirectory("/tmp", 42); 418 statCache->InjectFile("/tmp/test", 43); 419 420 Manager.setStatCache(std::move(statCache)); 421 422 // Check for real path. 423 const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1); 424 ASSERT_TRUE(file != nullptr); 425 ASSERT_TRUE(file->isValid()); 426 SmallString<64> ExpectedResult = CustomWorkingDir; 427 428 llvm::sys::path::append(ExpectedResult, "tmp", "test"); 429 EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult); 430 } 431 432 TEST_F(FileManagerTest, getFileDontOpenRealPath) { 433 SmallString<64> CustomWorkingDir; 434 #ifdef _WIN32 435 CustomWorkingDir = "C:/"; 436 #else 437 CustomWorkingDir = "/"; 438 #endif 439 440 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 441 new llvm::vfs::InMemoryFileSystem); 442 // setCurrentworkingdirectory must finish without error. 443 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 444 445 FileSystemOptions Opts; 446 FileManager Manager(Opts, FS); 447 448 // Inject fake files into the file system. 449 auto statCache = std::make_unique<FakeStatCache>(); 450 statCache->InjectDirectory("/tmp", 42); 451 statCache->InjectFile("/tmp/test", 43); 452 453 Manager.setStatCache(std::move(statCache)); 454 455 // Check for real path. 456 auto file = Manager.getFile("/tmp/test", /*OpenFile=*/false); 457 ASSERT_TRUE(file); 458 ASSERT_TRUE((*file)->isValid()); 459 SmallString<64> ExpectedResult = CustomWorkingDir; 460 461 llvm::sys::path::append(ExpectedResult, "tmp", "test"); 462 EXPECT_EQ((*file)->tryGetRealPathName(), ExpectedResult); 463 } 464 465 TEST_F(FileManagerTest, getBypassFile) { 466 SmallString<64> CustomWorkingDir; 467 #ifdef _WIN32 468 CustomWorkingDir = "C:/"; 469 #else 470 CustomWorkingDir = "/"; 471 #endif 472 473 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 474 new llvm::vfs::InMemoryFileSystem); 475 // setCurrentworkingdirectory must finish without error. 476 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 477 478 FileSystemOptions Opts; 479 FileManager Manager(Opts, FS); 480 481 // Inject fake files into the file system. 482 auto Cache = std::make_unique<FakeStatCache>(); 483 Cache->InjectDirectory("/tmp", 42); 484 Cache->InjectFile("/tmp/test", 43); 485 Manager.setStatCache(std::move(Cache)); 486 487 // Set up a virtual file with a different size than FakeStatCache uses. 488 const FileEntry *File = Manager.getVirtualFile("/tmp/test", /*Size=*/10, 0); 489 ASSERT_TRUE(File); 490 FileEntryRef Ref("/tmp/test", *File); 491 EXPECT_TRUE(Ref.isValid()); 492 EXPECT_EQ(Ref.getSize(), 10); 493 494 // Calling a second time should not affect the UID or size. 495 unsigned VirtualUID = Ref.getUID(); 496 EXPECT_EQ(*expectedToOptional(Manager.getFileRef("/tmp/test")), Ref); 497 EXPECT_EQ(Ref.getUID(), VirtualUID); 498 EXPECT_EQ(Ref.getSize(), 10); 499 500 // Bypass the file. 501 llvm::Optional<FileEntryRef> BypassRef = Manager.getBypassFile(Ref); 502 ASSERT_TRUE(BypassRef); 503 EXPECT_TRUE(BypassRef->isValid()); 504 EXPECT_EQ(BypassRef->getName(), Ref.getName()); 505 506 // Check that it's different in the right ways. 507 EXPECT_NE(&BypassRef->getFileEntry(), File); 508 EXPECT_NE(BypassRef->getUID(), VirtualUID); 509 EXPECT_NE(BypassRef->getSize(), Ref.getSize()); 510 511 // The virtual file should still be returned when searching. 512 EXPECT_EQ(*expectedToOptional(Manager.getFileRef("/tmp/test")), Ref); 513 } 514 515 } // anonymous namespace 516