FileManagerTest.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 "llvm/ADT/STLExtras.h"
  13. #include "llvm/Support/Path.h"
  14. #include "llvm/Support/VirtualFileSystem.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<llvm::vfs::File> *F,
  55. llvm::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.setStatCache(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.setStatCache(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.setStatCache(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.setStatCache(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.setStatCache(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.setStatCache(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. // When calling getFile(OpenFile=false); getFile(OpenFile=true) the file is
  187. // opened for the second call.
  188. TEST_F(FileManagerTest, getFileDefersOpen) {
  189. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS(
  190. new llvm::vfs::InMemoryFileSystem());
  191. FS->addFile("/tmp/test", 0, llvm::MemoryBuffer::getMemBufferCopy("test"));
  192. FS->addFile("/tmp/testv", 0, llvm::MemoryBuffer::getMemBufferCopy("testv"));
  193. FileManager manager(options, FS);
  194. const FileEntry *file = manager.getFile("/tmp/test", /*OpenFile=*/false);
  195. ASSERT_TRUE(file != nullptr);
  196. ASSERT_TRUE(file->isValid());
  197. // "real path name" reveals whether the file was actually opened.
  198. EXPECT_FALSE(file->isOpenForTests());
  199. file = manager.getFile("/tmp/test", /*OpenFile=*/true);
  200. ASSERT_TRUE(file != nullptr);
  201. ASSERT_TRUE(file->isValid());
  202. EXPECT_TRUE(file->isOpenForTests());
  203. // However we should never try to open a file previously opened as virtual.
  204. ASSERT_TRUE(manager.getVirtualFile("/tmp/testv", 5, 0));
  205. ASSERT_TRUE(manager.getFile("/tmp/testv", /*OpenFile=*/false));
  206. file = manager.getFile("/tmp/testv", /*OpenFile=*/true);
  207. EXPECT_FALSE(file->isOpenForTests());
  208. }
  209. // The following tests apply to Unix-like system only.
  210. #ifndef _WIN32
  211. // getFile() returns the same FileEntry for real files that are aliases.
  212. TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
  213. // Inject two real files with the same inode.
  214. auto statCache = llvm::make_unique<FakeStatCache>();
  215. statCache->InjectDirectory("abc", 41);
  216. statCache->InjectFile("abc/foo.cpp", 42);
  217. statCache->InjectFile("abc/bar.cpp", 42);
  218. manager.setStatCache(std::move(statCache));
  219. EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
  220. }
  221. // getFile() returns the same FileEntry for virtual files that have
  222. // corresponding real files that are aliases.
  223. TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
  224. // Inject two real files with the same inode.
  225. auto statCache = llvm::make_unique<FakeStatCache>();
  226. statCache->InjectDirectory("abc", 41);
  227. statCache->InjectFile("abc/foo.cpp", 42);
  228. statCache->InjectFile("abc/bar.cpp", 42);
  229. manager.setStatCache(std::move(statCache));
  230. ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid());
  231. ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid());
  232. EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
  233. }
  234. // getFile() Should return the same entry as getVirtualFile if the file actually
  235. // is a virtual file, even if the name is not exactly the same (but is after
  236. // normalisation done by the file system, like on Windows). This can be checked
  237. // here by checking the size.
  238. TEST_F(FileManagerTest, getVirtualFileWithDifferentName) {
  239. // Inject fake files into the file system.
  240. auto statCache = llvm::make_unique<FakeStatCache>();
  241. statCache->InjectDirectory("c:\\tmp", 42);
  242. statCache->InjectFile("c:\\tmp\\test", 43);
  243. manager.setStatCache(std::move(statCache));
  244. // Inject the virtual file:
  245. const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1);
  246. ASSERT_TRUE(file1 != nullptr);
  247. ASSERT_TRUE(file1->isValid());
  248. EXPECT_EQ(43U, file1->getUniqueID().getFile());
  249. EXPECT_EQ(123, file1->getSize());
  250. // Lookup the virtual file with a different name:
  251. const FileEntry *file2 = manager.getFile("c:/tmp/test", 100, 1);
  252. ASSERT_TRUE(file2 != nullptr);
  253. ASSERT_TRUE(file2->isValid());
  254. // Check that it's the same UFE:
  255. EXPECT_EQ(file1, file2);
  256. EXPECT_EQ(43U, file2->getUniqueID().getFile());
  257. // Check that the contents of the UFE are not overwritten by the entry in the
  258. // filesystem:
  259. EXPECT_EQ(123, file2->getSize());
  260. }
  261. #endif // !_WIN32
  262. TEST_F(FileManagerTest, makeAbsoluteUsesVFS) {
  263. SmallString<64> CustomWorkingDir;
  264. #ifdef _WIN32
  265. CustomWorkingDir = "C:";
  266. #else
  267. CustomWorkingDir = "/";
  268. #endif
  269. llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path");
  270. auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
  271. new llvm::vfs::InMemoryFileSystem);
  272. // setCurrentworkingdirectory must finish without error.
  273. ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
  274. FileSystemOptions Opts;
  275. FileManager Manager(Opts, FS);
  276. SmallString<64> Path("a/foo.cpp");
  277. SmallString<64> ExpectedResult(CustomWorkingDir);
  278. llvm::sys::path::append(ExpectedResult, Path);
  279. ASSERT_TRUE(Manager.makeAbsolutePath(Path));
  280. EXPECT_EQ(Path, ExpectedResult);
  281. }
  282. // getVirtualFile should always fill the real path.
  283. TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) {
  284. SmallString<64> CustomWorkingDir;
  285. #ifdef _WIN32
  286. CustomWorkingDir = "C:/";
  287. #else
  288. CustomWorkingDir = "/";
  289. #endif
  290. auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
  291. new llvm::vfs::InMemoryFileSystem);
  292. // setCurrentworkingdirectory must finish without error.
  293. ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
  294. FileSystemOptions Opts;
  295. FileManager Manager(Opts, FS);
  296. // Inject fake files into the file system.
  297. auto statCache = llvm::make_unique<FakeStatCache>();
  298. statCache->InjectDirectory("/tmp", 42);
  299. statCache->InjectFile("/tmp/test", 43);
  300. Manager.setStatCache(std::move(statCache));
  301. // Check for real path.
  302. const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1);
  303. ASSERT_TRUE(file != nullptr);
  304. ASSERT_TRUE(file->isValid());
  305. SmallString<64> ExpectedResult = CustomWorkingDir;
  306. llvm::sys::path::append(ExpectedResult, "tmp", "test");
  307. EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult);
  308. }
  309. } // anonymous namespace