FileManagerTest.cpp 13 KB

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