FileManagerTest.cpp 11 KB

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