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 // When calling getFile(OpenFile=false); getFile(OpenFile=true) the file is
225 // opened for the second call.
226 TEST_F(FileManagerTest, getFileDefersOpen) {
227   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS(
228       new llvm::vfs::InMemoryFileSystem());
229   FS->addFile("/tmp/test", 0, llvm::MemoryBuffer::getMemBufferCopy("test"));
230   FS->addFile("/tmp/testv", 0, llvm::MemoryBuffer::getMemBufferCopy("testv"));
231   FileManager manager(options, FS);
232 
233   const FileEntry *file = manager.getFile("/tmp/test", /*OpenFile=*/false);
234   ASSERT_TRUE(file != nullptr);
235   ASSERT_TRUE(file->isValid());
236   // "real path name" reveals whether the file was actually opened.
237   EXPECT_FALSE(file->isOpenForTests());
238 
239   file = manager.getFile("/tmp/test", /*OpenFile=*/true);
240   ASSERT_TRUE(file != nullptr);
241   ASSERT_TRUE(file->isValid());
242   EXPECT_TRUE(file->isOpenForTests());
243 
244   // However we should never try to open a file previously opened as virtual.
245   ASSERT_TRUE(manager.getVirtualFile("/tmp/testv", 5, 0));
246   ASSERT_TRUE(manager.getFile("/tmp/testv", /*OpenFile=*/false));
247   file = manager.getFile("/tmp/testv", /*OpenFile=*/true);
248   EXPECT_FALSE(file->isOpenForTests());
249 }
250 
251 // The following tests apply to Unix-like system only.
252 
253 #ifndef _WIN32
254 
255 // getFile() returns the same FileEntry for real files that are aliases.
256 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
257   // Inject two real files with the same inode.
258   auto statCache = llvm::make_unique<FakeStatCache>();
259   statCache->InjectDirectory("abc", 41);
260   statCache->InjectFile("abc/foo.cpp", 42);
261   statCache->InjectFile("abc/bar.cpp", 42);
262   manager.setStatCache(std::move(statCache));
263 
264   EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
265 }
266 
267 // getFile() returns the same FileEntry for virtual files that have
268 // corresponding real files that are aliases.
269 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
270   // Inject two real files with the same inode.
271   auto statCache = llvm::make_unique<FakeStatCache>();
272   statCache->InjectDirectory("abc", 41);
273   statCache->InjectFile("abc/foo.cpp", 42);
274   statCache->InjectFile("abc/bar.cpp", 42);
275   manager.setStatCache(std::move(statCache));
276 
277   ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid());
278   ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid());
279 
280   EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
281 }
282 
283 // getFile() Should return the same entry as getVirtualFile if the file actually
284 // is a virtual file, even if the name is not exactly the same (but is after
285 // normalisation done by the file system, like on Windows). This can be checked
286 // here by checking the size.
287 TEST_F(FileManagerTest, getVirtualFileWithDifferentName) {
288   // Inject fake files into the file system.
289   auto statCache = llvm::make_unique<FakeStatCache>();
290   statCache->InjectDirectory("c:\\tmp", 42);
291   statCache->InjectFile("c:\\tmp\\test", 43);
292 
293   manager.setStatCache(std::move(statCache));
294 
295   // Inject the virtual file:
296   const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1);
297   ASSERT_TRUE(file1 != nullptr);
298   ASSERT_TRUE(file1->isValid());
299   EXPECT_EQ(43U, file1->getUniqueID().getFile());
300   EXPECT_EQ(123, file1->getSize());
301 
302   // Lookup the virtual file with a different name:
303   const FileEntry *file2 = manager.getFile("c:/tmp/test", 100, 1);
304   ASSERT_TRUE(file2 != nullptr);
305   ASSERT_TRUE(file2->isValid());
306   // Check that it's the same UFE:
307   EXPECT_EQ(file1, file2);
308   EXPECT_EQ(43U, file2->getUniqueID().getFile());
309   // Check that the contents of the UFE are not overwritten by the entry in the
310   // filesystem:
311   EXPECT_EQ(123, file2->getSize());
312 }
313 
314 #endif  // !_WIN32
315 
316 TEST_F(FileManagerTest, makeAbsoluteUsesVFS) {
317   SmallString<64> CustomWorkingDir;
318 #ifdef _WIN32
319   CustomWorkingDir = "C:";
320 #else
321   CustomWorkingDir = "/";
322 #endif
323   llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path");
324 
325   auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
326       new llvm::vfs::InMemoryFileSystem);
327   // setCurrentworkingdirectory must finish without error.
328   ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
329 
330   FileSystemOptions Opts;
331   FileManager Manager(Opts, FS);
332 
333   SmallString<64> Path("a/foo.cpp");
334 
335   SmallString<64> ExpectedResult(CustomWorkingDir);
336   llvm::sys::path::append(ExpectedResult, Path);
337 
338   ASSERT_TRUE(Manager.makeAbsolutePath(Path));
339   EXPECT_EQ(Path, ExpectedResult);
340 }
341 
342 // getVirtualFile should always fill the real path.
343 TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) {
344   SmallString<64> CustomWorkingDir;
345 #ifdef _WIN32
346   CustomWorkingDir = "C:/";
347 #else
348   CustomWorkingDir = "/";
349 #endif
350 
351   auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
352       new llvm::vfs::InMemoryFileSystem);
353   // setCurrentworkingdirectory must finish without error.
354   ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
355 
356   FileSystemOptions Opts;
357   FileManager Manager(Opts, FS);
358 
359   // Inject fake files into the file system.
360   auto statCache = llvm::make_unique<FakeStatCache>();
361   statCache->InjectDirectory("/tmp", 42);
362   statCache->InjectFile("/tmp/test", 43);
363 
364   Manager.setStatCache(std::move(statCache));
365 
366   // Check for real path.
367   const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1);
368   ASSERT_TRUE(file != nullptr);
369   ASSERT_TRUE(file->isValid());
370   SmallString<64> ExpectedResult = CustomWorkingDir;
371 
372   llvm::sys::path::append(ExpectedResult, "tmp", "test");
373   EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult);
374 }
375 
376 } // anonymous namespace
377