blob: e53213b4bcc9dbbb9472d04ad705ba0f8ce4a980 [file] [log] [blame]
Zhanyong Wanbc402ab2011-02-15 21:30:27 +00001//===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===//
Zhanyong Wane1dd3e22011-02-11 18:44:49 +00002//
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
Chandler Carruth320d9662012-12-04 09:45:34 +000010#include "clang/Basic/FileManager.h"
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000011#include "clang/Basic/FileSystemOptions.h"
12#include "clang/Basic/FileSystemStatCache.h"
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000013#include "gtest/gtest.h"
Alp Toker1d257e12014-06-04 03:28:55 +000014#include "llvm/Config/llvm-config.h"
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000015
16using namespace llvm;
17using namespace clang;
18
19namespace {
20
21// Used to create a fake file system for running the tests with such
22// that the tests are not affected by the structure/contents of the
23// file system on the machine running the tests.
24class FakeStatCache : public FileSystemStatCache {
25private:
26 // Maps a file/directory path to its desired stat result. Anything
27 // not in this map is considered to not exist in the file system.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000028 llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000029
30 void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000031 FileData Data;
Ben Langmuird066d4c2014-02-28 21:16:07 +000032 Data.Name = Path;
33 Data.Size = 0;
34 Data.ModTime = 0;
35 Data.UniqueID = llvm::sys::fs::UniqueID(1, INode);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000036 Data.IsDirectory = !IsFile;
Ben Langmuird066d4c2014-02-28 21:16:07 +000037 Data.IsNamedPipe = false;
38 Data.InPCH = false;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000039 StatCalls[Path] = Data;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000040 }
41
42public:
43 // Inject a file with the given inode value to the fake file system.
44 void InjectFile(const char *Path, ino_t INode) {
45 InjectFileOrDirectory(Path, INode, /*IsFile=*/true);
46 }
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
53 // Implement FileSystemStatCache::getStat().
Benjamin Kramer46741752014-07-08 16:07:39 +000054 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
55 std::unique_ptr<vfs::File> *F,
56 vfs::FileSystem &FS) override {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000057 if (StatCalls.count(Path) != 0) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000058 Data = StatCalls[Path];
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000059 return CacheExists;
60 }
61
62 return CacheMissing; // This means the file/directory doesn't exist.
63 }
64};
65
66// The test fixture.
67class FileManagerTest : public ::testing::Test {
68 protected:
69 FileManagerTest() : manager(options) {
70 }
71
72 FileSystemOptions options;
73 FileManager manager;
74};
75
76// When a virtual file is added, its getDir() field is set correctly
77// (not NULL, correct name).
78TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
79 const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
Craig Topper416fa342014-06-08 08:38:12 +000080 ASSERT_TRUE(file != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000081
82 const DirectoryEntry *dir = file->getDir();
Craig Topper416fa342014-06-08 08:38:12 +000083 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000084 EXPECT_STREQ(".", dir->getName());
85
86 file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
Craig Topper416fa342014-06-08 08:38:12 +000087 ASSERT_TRUE(file != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000088
89 dir = file->getDir();
Craig Topper416fa342014-06-08 08:38:12 +000090 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000091 EXPECT_STREQ("x/y", dir->getName());
92}
93
94// Before any virtual file is added, no virtual directory exists.
95TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
96 // An empty FakeStatCache causes all stat calls made by the
97 // FileManager to report "file/directory doesn't exist". This
98 // avoids the possibility of the result of this test being affected
99 // by what's in the real file system.
David Blaikie23430cc2014-08-11 21:29:24 +0000100 manager.addStatCache(llvm::make_unique<FakeStatCache>());
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000101
Craig Topper416fa342014-06-08 08:38:12 +0000102 EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo"));
103 EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir"));
104 EXPECT_EQ(nullptr, manager.getDirectory("virtual"));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000105}
106
107// When a virtual file is added, all of its ancestors should be created.
108TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
109 // Fake an empty real file system.
David Blaikie23430cc2014-08-11 21:29:24 +0000110 manager.addStatCache(llvm::make_unique<FakeStatCache>());
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000111
112 manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
Craig Topper416fa342014-06-08 08:38:12 +0000113 EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo"));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000114
115 const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
Craig Topper416fa342014-06-08 08:38:12 +0000116 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000117 EXPECT_STREQ("virtual/dir", dir->getName());
118
119 dir = manager.getDirectory("virtual");
Craig Topper416fa342014-06-08 08:38:12 +0000120 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000121 EXPECT_STREQ("virtual", dir->getName());
122}
123
124// getFile() returns non-NULL if a real file exists at the given path.
125TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
126 // Inject fake files into the file system.
David Blaikie23430cc2014-08-11 21:29:24 +0000127 auto statCache = llvm::make_unique<FakeStatCache>();
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000128 statCache->InjectDirectory("/tmp", 42);
129 statCache->InjectFile("/tmp/test", 43);
Rafael Espindolaee305462013-07-29 15:47:24 +0000130
Hans Wennborg501eadb2014-03-12 16:07:46 +0000131#ifdef LLVM_ON_WIN32
Rafael Espindolaee305462013-07-29 15:47:24 +0000132 const char *DirName = "C:.";
133 const char *FileName = "C:test";
134 statCache->InjectDirectory(DirName, 44);
135 statCache->InjectFile(FileName, 45);
136#endif
137
David Blaikie23430cc2014-08-11 21:29:24 +0000138 manager.addStatCache(std::move(statCache));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000139
140 const FileEntry *file = manager.getFile("/tmp/test");
Craig Topper416fa342014-06-08 08:38:12 +0000141 ASSERT_TRUE(file != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000142 EXPECT_STREQ("/tmp/test", file->getName());
143
144 const DirectoryEntry *dir = file->getDir();
Craig Topper416fa342014-06-08 08:38:12 +0000145 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000146 EXPECT_STREQ("/tmp", dir->getName());
Rafael Espindolaee305462013-07-29 15:47:24 +0000147
Hans Wennborg501eadb2014-03-12 16:07:46 +0000148#ifdef LLVM_ON_WIN32
Rafael Espindolaee305462013-07-29 15:47:24 +0000149 file = manager.getFile(FileName);
150 ASSERT_TRUE(file != NULL);
151
152 dir = file->getDir();
153 ASSERT_TRUE(dir != NULL);
154 EXPECT_STREQ(DirName, dir->getName());
155#endif
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000156}
157
158// getFile() returns non-NULL if a virtual file exists at the given path.
159TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
160 // Fake an empty real file system.
David Blaikie23430cc2014-08-11 21:29:24 +0000161 manager.addStatCache(llvm::make_unique<FakeStatCache>());
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000162
163 manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
164 const FileEntry *file = manager.getFile("virtual/dir/bar.h");
Craig Topper416fa342014-06-08 08:38:12 +0000165 ASSERT_TRUE(file != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000166 EXPECT_STREQ("virtual/dir/bar.h", file->getName());
167
168 const DirectoryEntry *dir = file->getDir();
Craig Topper416fa342014-06-08 08:38:12 +0000169 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000170 EXPECT_STREQ("virtual/dir", dir->getName());
171}
172
173// getFile() returns different FileEntries for different paths when
174// there's no aliasing.
175TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
176 // Inject two fake files into the file system. Different inodes
177 // mean the files are not symlinked together.
David Blaikie23430cc2014-08-11 21:29:24 +0000178 auto statCache = llvm::make_unique<FakeStatCache>();
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000179 statCache->InjectDirectory(".", 41);
180 statCache->InjectFile("foo.cpp", 42);
181 statCache->InjectFile("bar.cpp", 43);
David Blaikie23430cc2014-08-11 21:29:24 +0000182 manager.addStatCache(std::move(statCache));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000183
184 const FileEntry *fileFoo = manager.getFile("foo.cpp");
185 const FileEntry *fileBar = manager.getFile("bar.cpp");
Craig Topper416fa342014-06-08 08:38:12 +0000186 ASSERT_TRUE(fileFoo != nullptr);
187 ASSERT_TRUE(fileBar != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000188 EXPECT_NE(fileFoo, fileBar);
189}
190
191// getFile() returns NULL if neither a real file nor a virtual file
192// exists at the given path.
193TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) {
194 // Inject a fake foo.cpp into the file system.
David Blaikie23430cc2014-08-11 21:29:24 +0000195 auto statCache = llvm::make_unique<FakeStatCache>();
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000196 statCache->InjectDirectory(".", 41);
197 statCache->InjectFile("foo.cpp", 42);
David Blaikie23430cc2014-08-11 21:29:24 +0000198 manager.addStatCache(std::move(statCache));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000199
200 // Create a virtual bar.cpp file.
201 manager.getVirtualFile("bar.cpp", 200, 0);
202
203 const FileEntry *file = manager.getFile("xyz.txt");
Craig Topper416fa342014-06-08 08:38:12 +0000204 EXPECT_EQ(nullptr, file);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000205}
206
207// The following tests apply to Unix-like system only.
208
Hans Wennborg501eadb2014-03-12 16:07:46 +0000209#ifndef LLVM_ON_WIN32
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000210
211// getFile() returns the same FileEntry for real files that are aliases.
212TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
213 // Inject two real files with the same inode.
David Blaikie23430cc2014-08-11 21:29:24 +0000214 auto statCache = llvm::make_unique<FakeStatCache>();
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000215 statCache->InjectDirectory("abc", 41);
216 statCache->InjectFile("abc/foo.cpp", 42);
217 statCache->InjectFile("abc/bar.cpp", 42);
David Blaikie23430cc2014-08-11 21:29:24 +0000218 manager.addStatCache(std::move(statCache));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000219
220 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
221}
222
223// getFile() returns the same FileEntry for virtual files that have
224// corresponding real files that are aliases.
225TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
226 // Inject two real files with the same inode.
David Blaikie23430cc2014-08-11 21:29:24 +0000227 auto statCache = llvm::make_unique<FakeStatCache>();
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000228 statCache->InjectDirectory("abc", 41);
229 statCache->InjectFile("abc/foo.cpp", 42);
230 statCache->InjectFile("abc/bar.cpp", 42);
David Blaikie23430cc2014-08-11 21:29:24 +0000231 manager.addStatCache(std::move(statCache));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000232
233 manager.getVirtualFile("abc/foo.cpp", 100, 0);
234 manager.getVirtualFile("abc/bar.cpp", 200, 0);
235
236 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
237}
238
David Blaikie23430cc2014-08-11 21:29:24 +0000239TEST_F(FileManagerTest, addRemoveStatCache) {
240 manager.addStatCache(llvm::make_unique<FakeStatCache>());
241 auto statCacheOwner = llvm::make_unique<FakeStatCache>();
242 auto *statCache = statCacheOwner.get();
243 manager.addStatCache(std::move(statCacheOwner));
244 manager.addStatCache(llvm::make_unique<FakeStatCache>());
245 manager.removeStatCache(statCache);
246}
247
Hans Wennborg501eadb2014-03-12 16:07:46 +0000248#endif // !LLVM_ON_WIN32
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000249
250} // anonymous namespace