FileManagerTest.cpp 11 KB

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