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<FileData, llvm::BumpPtrAllocator> StatCalls; 30 31 void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) { 32 #ifndef _WIN32 33 SmallString<128> NormalizedPath(Path); 34 llvm::sys::path::native(NormalizedPath); 35 Path = NormalizedPath.c_str(); 36 #endif 37 38 FileData Data; 39 Data.Name = Path; 40 Data.Size = 0; 41 Data.ModTime = 0; 42 Data.UniqueID = llvm::sys::fs::UniqueID(1, INode); 43 Data.IsDirectory = !IsFile; 44 Data.IsNamedPipe = false; 45 Data.InPCH = false; 46 StatCalls[Path] = Data; 47 } 48 49 public: 50 // Inject a file with the given inode value to the fake file system. 51 void InjectFile(const char *Path, ino_t INode) { 52 InjectFileOrDirectory(Path, INode, /*IsFile=*/true); 53 } 54 55 // Inject a directory with the given inode value to the fake file system. 56 void InjectDirectory(const char *Path, ino_t INode) { 57 InjectFileOrDirectory(Path, INode, /*IsFile=*/false); 58 } 59 60 // Implement FileSystemStatCache::getStat(). 61 LookupResult getStat(StringRef Path, FileData &Data, bool isFile, 62 std::unique_ptr<llvm::vfs::File> *F, 63 llvm::vfs::FileSystem &FS) override { 64 #ifndef _WIN32 65 SmallString<128> NormalizedPath(Path); 66 llvm::sys::path::native(NormalizedPath); 67 Path = NormalizedPath.c_str(); 68 #endif 69 70 if (StatCalls.count(Path) != 0) { 71 Data = StatCalls[Path]; 72 return CacheExists; 73 } 74 75 return CacheMissing; // This means the file/directory doesn't exist. 76 } 77 }; 78 79 // The test fixture. 80 class FileManagerTest : public ::testing::Test { 81 protected: 82 FileManagerTest() : manager(options) { 83 } 84 85 FileSystemOptions options; 86 FileManager manager; 87 }; 88 89 // When a virtual file is added, its getDir() field is set correctly 90 // (not NULL, correct name). 91 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) { 92 const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0); 93 ASSERT_TRUE(file != nullptr); 94 95 const DirectoryEntry *dir = file->getDir(); 96 ASSERT_TRUE(dir != nullptr); 97 EXPECT_EQ(".", dir->getName()); 98 99 file = manager.getVirtualFile("x/y/z.cpp", 42, 0); 100 ASSERT_TRUE(file != nullptr); 101 102 dir = file->getDir(); 103 ASSERT_TRUE(dir != nullptr); 104 EXPECT_EQ("x/y", dir->getName()); 105 } 106 107 // Before any virtual file is added, no virtual directory exists. 108 TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) { 109 // An empty FakeStatCache causes all stat calls made by the 110 // FileManager to report "file/directory doesn't exist". This 111 // avoids the possibility of the result of this test being affected 112 // by what's in the real file system. 113 manager.setStatCache(llvm::make_unique<FakeStatCache>()); 114 115 EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); 116 EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir")); 117 EXPECT_EQ(nullptr, manager.getDirectory("virtual")); 118 } 119 120 // When a virtual file is added, all of its ancestors should be created. 121 TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) { 122 // Fake an empty real file system. 123 manager.setStatCache(llvm::make_unique<FakeStatCache>()); 124 125 manager.getVirtualFile("virtual/dir/bar.h", 100, 0); 126 EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); 127 128 const DirectoryEntry *dir = manager.getDirectory("virtual/dir"); 129 ASSERT_TRUE(dir != nullptr); 130 EXPECT_EQ("virtual/dir", dir->getName()); 131 132 dir = manager.getDirectory("virtual"); 133 ASSERT_TRUE(dir != nullptr); 134 EXPECT_EQ("virtual", dir->getName()); 135 } 136 137 // getFile() returns non-NULL if a real file exists at the given path. 138 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) { 139 // Inject fake files into the file system. 140 auto statCache = llvm::make_unique<FakeStatCache>(); 141 statCache->InjectDirectory("/tmp", 42); 142 statCache->InjectFile("/tmp/test", 43); 143 144 #ifdef _WIN32 145 const char *DirName = "C:."; 146 const char *FileName = "C:test"; 147 statCache->InjectDirectory(DirName, 44); 148 statCache->InjectFile(FileName, 45); 149 #endif 150 151 manager.setStatCache(std::move(statCache)); 152 153 const FileEntry *file = manager.getFile("/tmp/test"); 154 ASSERT_TRUE(file != nullptr); 155 ASSERT_TRUE(file->isValid()); 156 EXPECT_EQ("/tmp/test", file->getName()); 157 158 const DirectoryEntry *dir = file->getDir(); 159 ASSERT_TRUE(dir != nullptr); 160 EXPECT_EQ("/tmp", dir->getName()); 161 162 #ifdef _WIN32 163 file = manager.getFile(FileName); 164 ASSERT_TRUE(file != NULL); 165 166 dir = file->getDir(); 167 ASSERT_TRUE(dir != NULL); 168 EXPECT_EQ(DirName, dir->getName()); 169 #endif 170 } 171 172 // getFile() returns non-NULL if a virtual file exists at the given path. 173 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) { 174 // Fake an empty real file system. 175 manager.setStatCache(llvm::make_unique<FakeStatCache>()); 176 177 manager.getVirtualFile("virtual/dir/bar.h", 100, 0); 178 const FileEntry *file = manager.getFile("virtual/dir/bar.h"); 179 ASSERT_TRUE(file != nullptr); 180 ASSERT_TRUE(file->isValid()); 181 EXPECT_EQ("virtual/dir/bar.h", file->getName()); 182 183 const DirectoryEntry *dir = file->getDir(); 184 ASSERT_TRUE(dir != nullptr); 185 EXPECT_EQ("virtual/dir", dir->getName()); 186 } 187 188 // getFile() returns different FileEntries for different paths when 189 // there's no aliasing. 190 TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) { 191 // Inject two fake files into the file system. Different inodes 192 // mean the files are not symlinked together. 193 auto statCache = llvm::make_unique<FakeStatCache>(); 194 statCache->InjectDirectory(".", 41); 195 statCache->InjectFile("foo.cpp", 42); 196 statCache->InjectFile("bar.cpp", 43); 197 manager.setStatCache(std::move(statCache)); 198 199 const FileEntry *fileFoo = manager.getFile("foo.cpp"); 200 const FileEntry *fileBar = manager.getFile("bar.cpp"); 201 ASSERT_TRUE(fileFoo != nullptr); 202 ASSERT_TRUE(fileFoo->isValid()); 203 ASSERT_TRUE(fileBar != nullptr); 204 ASSERT_TRUE(fileBar->isValid()); 205 EXPECT_NE(fileFoo, fileBar); 206 } 207 208 // getFile() returns NULL if neither a real file nor a virtual file 209 // exists at the given path. 210 TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) { 211 // Inject a fake foo.cpp into the file system. 212 auto statCache = llvm::make_unique<FakeStatCache>(); 213 statCache->InjectDirectory(".", 41); 214 statCache->InjectFile("foo.cpp", 42); 215 manager.setStatCache(std::move(statCache)); 216 217 // Create a virtual bar.cpp file. 218 manager.getVirtualFile("bar.cpp", 200, 0); 219 220 const FileEntry *file = manager.getFile("xyz.txt"); 221 EXPECT_EQ(nullptr, file); 222 } 223 224 // The following tests apply to Unix-like system only. 225 226 #ifndef _WIN32 227 228 // getFile() returns the same FileEntry for real files that are aliases. 229 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) { 230 // Inject two real files with the same inode. 231 auto statCache = llvm::make_unique<FakeStatCache>(); 232 statCache->InjectDirectory("abc", 41); 233 statCache->InjectFile("abc/foo.cpp", 42); 234 statCache->InjectFile("abc/bar.cpp", 42); 235 manager.setStatCache(std::move(statCache)); 236 237 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); 238 } 239 240 // getFile() returns the same FileEntry for virtual files that have 241 // corresponding real files that are aliases. 242 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) { 243 // Inject two real files with the same inode. 244 auto statCache = llvm::make_unique<FakeStatCache>(); 245 statCache->InjectDirectory("abc", 41); 246 statCache->InjectFile("abc/foo.cpp", 42); 247 statCache->InjectFile("abc/bar.cpp", 42); 248 manager.setStatCache(std::move(statCache)); 249 250 ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid()); 251 ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid()); 252 253 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); 254 } 255 256 // getFile() Should return the same entry as getVirtualFile if the file actually 257 // is a virtual file, even if the name is not exactly the same (but is after 258 // normalisation done by the file system, like on Windows). This can be checked 259 // here by checking the size. 260 TEST_F(FileManagerTest, getVirtualFileWithDifferentName) { 261 // Inject fake files into the file system. 262 auto statCache = llvm::make_unique<FakeStatCache>(); 263 statCache->InjectDirectory("c:\\tmp", 42); 264 statCache->InjectFile("c:\\tmp\\test", 43); 265 266 manager.setStatCache(std::move(statCache)); 267 268 // Inject the virtual file: 269 const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1); 270 ASSERT_TRUE(file1 != nullptr); 271 ASSERT_TRUE(file1->isValid()); 272 EXPECT_EQ(43U, file1->getUniqueID().getFile()); 273 EXPECT_EQ(123, file1->getSize()); 274 275 // Lookup the virtual file with a different name: 276 const FileEntry *file2 = manager.getFile("c:/tmp/test", 100, 1); 277 ASSERT_TRUE(file2 != nullptr); 278 ASSERT_TRUE(file2->isValid()); 279 // Check that it's the same UFE: 280 EXPECT_EQ(file1, file2); 281 EXPECT_EQ(43U, file2->getUniqueID().getFile()); 282 // Check that the contents of the UFE are not overwritten by the entry in the 283 // filesystem: 284 EXPECT_EQ(123, file2->getSize()); 285 } 286 287 #endif // !_WIN32 288 289 TEST_F(FileManagerTest, makeAbsoluteUsesVFS) { 290 SmallString<64> CustomWorkingDir; 291 #ifdef _WIN32 292 CustomWorkingDir = "C:"; 293 #else 294 CustomWorkingDir = "/"; 295 #endif 296 llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path"); 297 298 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 299 new llvm::vfs::InMemoryFileSystem); 300 // setCurrentworkingdirectory must finish without error. 301 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 302 303 FileSystemOptions Opts; 304 FileManager Manager(Opts, FS); 305 306 SmallString<64> Path("a/foo.cpp"); 307 308 SmallString<64> ExpectedResult(CustomWorkingDir); 309 llvm::sys::path::append(ExpectedResult, Path); 310 311 ASSERT_TRUE(Manager.makeAbsolutePath(Path)); 312 EXPECT_EQ(Path, ExpectedResult); 313 } 314 315 // getVirtualFile should always fill the real path. 316 TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) { 317 SmallString<64> CustomWorkingDir; 318 #ifdef _WIN32 319 CustomWorkingDir = "C:/"; 320 #else 321 CustomWorkingDir = "/"; 322 #endif 323 324 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 325 new llvm::vfs::InMemoryFileSystem); 326 // setCurrentworkingdirectory must finish without error. 327 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 328 329 FileSystemOptions Opts; 330 FileManager Manager(Opts, FS); 331 332 // Inject fake files into the file system. 333 auto statCache = llvm::make_unique<FakeStatCache>(); 334 statCache->InjectDirectory("/tmp", 42); 335 statCache->InjectFile("/tmp/test", 43); 336 337 Manager.setStatCache(std::move(statCache)); 338 339 // Check for real path. 340 const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1); 341 ASSERT_TRUE(file != nullptr); 342 ASSERT_TRUE(file->isValid()); 343 SmallString<64> ExpectedResult = CustomWorkingDir; 344 345 llvm::sys::path::append(ExpectedResult, "tmp", "test"); 346 EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult); 347 } 348 349 } // anonymous namespace 350