blob: c1f715ed05924e66a7fd2251156085ce57090011 [file] [log] [blame]
Chris Lattner10e286a2010-11-23 19:19:34 +00001//===--- FileManager.cpp - File System Probing and Caching ----------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the FileManager interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// TODO: This should index all interesting directories with dirent calls.
15// getdirentries ?
16// opendir/readdir_r/closedir ?
17//
18//===----------------------------------------------------------------------===//
19
20#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000021#include "clang/Basic/FileSystemStatCache.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallString.h"
Chris Lattnerc070da42010-08-23 23:50:42 +000023#include "llvm/ADT/StringExtras.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000024#include "llvm/Support/FileSystem.h"
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000027#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000028#include "llvm/Support/system_error.h"
Ted Kremenek6bb816a2008-02-24 03:15:25 +000029#include "llvm/Config/config.h"
Benjamin Kramer458fb102009-09-05 09:49:39 +000030#include <map>
31#include <set>
32#include <string>
Chris Lattner291fcf02010-11-23 21:53:15 +000033
34// FIXME: This is terrible, we need this for ::close.
35#if !defined(_MSC_VER) && !defined(__MINGW32__)
36#include <unistd.h>
37#include <sys/uio.h>
38#else
39#include <io.h>
40#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000041using namespace clang;
42
43// FIXME: Enhance libsystem to support inode and other fields.
44#include <sys/stat.h>
45
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000046/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000047/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000048#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000049
Chris Lattnerf9f77662010-11-23 20:50:22 +000050/// NON_EXISTENT_FILE - A special value distinct from null that is used to
51/// represent a filename that doesn't exist on the disk.
52#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
53
54
55FileEntry::~FileEntry() {
56 // If this FileEntry owns an open file descriptor that never got used, close
57 // it.
58 if (FD != -1) ::close(FD);
59}
60
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000061//===----------------------------------------------------------------------===//
62// Windows.
63//===----------------------------------------------------------------------===//
64
Ted Kremenek6bb816a2008-02-24 03:15:25 +000065#ifdef LLVM_ON_WIN32
66
Ted Kremenek6bb816a2008-02-24 03:15:25 +000067namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000068 static std::string GetFullPath(const char *relPath) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000069 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
70 assert(absPathStrPtr && "_fullpath() returned NULL!");
71
72 std::string absPath(absPathStrPtr);
73
74 free(absPathStrPtr);
75 return absPath;
76 }
77}
78
79class FileManager::UniqueDirContainer {
80 /// UniqueDirs - Cache from full path to existing directories/files.
81 ///
Mike Stump1eb44332009-09-09 15:08:12 +000082 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000083
84public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000085 /// getDirectory - Return an existing DirectoryEntry with the given
86 /// name if there is already one; otherwise create and return a
87 /// default-constructed DirectoryEntry.
88 DirectoryEntry &getDirectory(const char *Name,
89 const struct stat & /*StatBuf*/) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000090 std::string FullPath(GetFullPath(Name));
Chris Lattnerf3e8a992010-11-23 20:30:42 +000091 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattnerf3e8a992010-11-23 20:30:42 +000094 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +000095};
96
97class FileManager::UniqueFileContainer {
98 /// UniqueFiles - Cache from full path to existing directories/files.
99 ///
Ted Kremenek75368892009-01-28 01:01:07 +0000100 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000101
102public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000103 /// getFile - Return an existing FileEntry with the given name if
104 /// there is already one; otherwise create and return a
105 /// default-constructed FileEntry.
106 FileEntry &getFile(const char *Name, const struct stat & /*StatBuf*/) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000107 std::string FullPath(GetFullPath(Name));
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000108
Chris Lattnerc070da42010-08-23 23:50:42 +0000109 // LowercaseString because Windows filesystem is case insensitive.
110 FullPath = llvm::LowercaseString(FullPath);
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000111 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000112 }
113
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000114 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000115};
116
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000117//===----------------------------------------------------------------------===//
118// Unix-like Systems.
119//===----------------------------------------------------------------------===//
120
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000121#else
122
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000123class FileManager::UniqueDirContainer {
124 /// UniqueDirs - Cache from ID's to existing directories/files.
Mike Stump1eb44332009-09-09 15:08:12 +0000125 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000126
127public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000128 /// getDirectory - Return an existing DirectoryEntry with the given
129 /// ID's if there is already one; otherwise create and return a
130 /// default-constructed DirectoryEntry.
131 DirectoryEntry &getDirectory(const char * /*Name*/,
132 const struct stat &StatBuf) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000133 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
134 }
135
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000136 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000137};
138
139class FileManager::UniqueFileContainer {
140 /// UniqueFiles - Cache from ID's to existing directories/files.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000141 std::set<FileEntry> UniqueFiles;
142
143public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000144 /// getFile - Return an existing FileEntry with the given ID's if
145 /// there is already one; otherwise create and return a
146 /// default-constructed FileEntry.
147 FileEntry &getFile(const char * /*Name*/, const struct stat &StatBuf) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000148 return
149 const_cast<FileEntry&>(
150 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000151 StatBuf.st_ino,
152 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000153 }
154
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000155 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000156};
157
158#endif
159
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000160//===----------------------------------------------------------------------===//
161// Common logic.
162//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000163
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000164FileManager::FileManager(const FileSystemOptions &FSO)
165 : FileSystemOpts(FSO),
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000166 UniqueRealDirs(*new UniqueDirContainer()),
167 UniqueRealFiles(*new UniqueFileContainer()),
168 SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000169 NumDirLookups = NumFileLookups = 0;
170 NumDirCacheMisses = NumFileCacheMisses = 0;
171}
172
173FileManager::~FileManager() {
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000174 delete &UniqueRealDirs;
175 delete &UniqueRealFiles;
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000176 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
177 delete VirtualFileEntries[i];
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000178 for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
179 delete VirtualDirectoryEntries[i];
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000180}
181
Chris Lattner10e286a2010-11-23 19:19:34 +0000182void FileManager::addStatCache(FileSystemStatCache *statCache,
183 bool AtBeginning) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000184 assert(statCache && "No stat cache provided?");
185 if (AtBeginning || StatCache.get() == 0) {
186 statCache->setNextStatCache(StatCache.take());
187 StatCache.reset(statCache);
188 return;
189 }
190
Chris Lattner10e286a2010-11-23 19:19:34 +0000191 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000192 while (LastCache->getNextStatCache())
193 LastCache = LastCache->getNextStatCache();
194
195 LastCache->setNextStatCache(statCache);
196}
197
Chris Lattner10e286a2010-11-23 19:19:34 +0000198void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000199 if (!statCache)
200 return;
201
202 if (StatCache.get() == statCache) {
203 // This is the first stat cache.
204 StatCache.reset(StatCache->takeNextStatCache());
205 return;
206 }
207
208 // Find the stat cache in the list.
Chris Lattner10e286a2010-11-23 19:19:34 +0000209 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000210 while (PrevCache && PrevCache->getNextStatCache() != statCache)
211 PrevCache = PrevCache->getNextStatCache();
Chris Lattnerf9f77662010-11-23 20:50:22 +0000212
213 assert(PrevCache && "Stat cache not found for removal");
214 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregor52e71082009-10-16 18:18:30 +0000215}
216
Douglas Gregor057e5672009-12-02 18:12:28 +0000217/// \brief Retrieve the directory that the given file name resides in.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000218/// Filename can point to either a real file or a virtual file.
Douglas Gregor057e5672009-12-02 18:12:28 +0000219static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Douglas Gregor6e975c42011-09-13 23:15:45 +0000220 StringRef Filename,
221 bool CacheFailure) {
Zhanyong Wan21af8872011-02-11 21:25:35 +0000222 if (Filename.empty())
223 return NULL;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000224
Zhanyong Wan21af8872011-02-11 21:25:35 +0000225 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
226 return NULL; // If Filename is a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000227
Chris Lattner5f9e2722011-07-23 10:55:15 +0000228 StringRef DirName = llvm::sys::path::parent_path(Filename);
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000229 // Use the current directory if file has no path component.
Zhanyong Wan21af8872011-02-11 21:25:35 +0000230 if (DirName.empty())
231 DirName = ".";
Douglas Gregor057e5672009-12-02 18:12:28 +0000232
Douglas Gregor6e975c42011-09-13 23:15:45 +0000233 return FileMgr.getDirectory(DirName, CacheFailure);
Douglas Gregor057e5672009-12-02 18:12:28 +0000234}
235
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000236/// Add all ancestors of the given path (pointing to either a file or
237/// a directory) as virtual directories.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000238void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
239 StringRef DirName = llvm::sys::path::parent_path(Path);
Zhanyong Wan21af8872011-02-11 21:25:35 +0000240 if (DirName.empty())
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000241 return;
242
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000243 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
244 SeenDirEntries.GetOrCreateValue(DirName);
245
246 // When caching a virtual directory, we always cache its ancestors
247 // at the same time. Therefore, if DirName is already in the cache,
248 // we don't need to recurse as its ancestors must also already be in
249 // the cache.
250 if (NamedDirEnt.getValue())
251 return;
252
253 // Add the virtual directory to the cache.
254 DirectoryEntry *UDE = new DirectoryEntry;
255 UDE->Name = NamedDirEnt.getKeyData();
256 NamedDirEnt.setValue(UDE);
257 VirtualDirectoryEntries.push_back(UDE);
258
259 // Recursively add the other ancestors.
260 addAncestorsAsVirtualDirs(DirName);
261}
262
263/// getDirectory - Lookup, cache, and verify the specified directory
264/// (real or virtual). This returns NULL if the directory doesn't
265/// exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000266///
Douglas Gregor6e975c42011-09-13 23:15:45 +0000267const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
268 bool CacheFailure) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 ++NumDirLookups;
270 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000271 SeenDirEntries.GetOrCreateValue(DirName);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000273 // See if there was already an entry in the map. Note that the map
274 // contains both virtual and real directories.
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000276 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000282 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 // Get the null-terminated directory name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000285 // SeenDirEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // Check to see if the directory exists.
289 struct stat StatBuf;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000290 if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/)) {
291 // There's no real directory at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000292 if (!CacheFailure)
293 SeenDirEntries.erase(DirName);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 return 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000295 }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000296
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000297 // It exists. See if we have already opened a directory with the
298 // same inode (this occurs on Unix-like systems when one dir is
299 // symlinked to another, for example) or the same path (on
300 // Windows).
301 DirectoryEntry &UDE = UniqueRealDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 NamedDirEnt.setValue(&UDE);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000304 if (!UDE.getName()) {
305 // We don't have this directory yet, add it. We use the string
306 // key from the SeenDirEntries map as the string.
307 UDE.Name = InterndDirName;
308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 return &UDE;
311}
312
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000313/// getFile - Lookup, cache, and verify the specified file (real or
314/// virtual). This returns NULL if the file doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000315///
Douglas Gregor6e975c42011-09-13 23:15:45 +0000316const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
317 bool CacheFailure) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 // See if there is already an entry in the map.
321 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000322 SeenFileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000323
324 // See if there is already an entry in the map.
325 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000326 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 ++NumFileCacheMisses;
330
331 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000332 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000333
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 // Get the null-terminated file name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000335 // SeenFileEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000338 // Look up the directory for the file. When looking up something like
339 // sys/foo.h we'll discover all of the search directories that have a 'sys'
340 // subdirectory. This will let us avoid having to waste time on known-to-fail
341 // searches when we go to find sys/bar.h, because all the search directories
342 // without a 'sys' subdir will get a cached failure result.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000343 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
344 CacheFailure);
345 if (DirInfo == 0) { // Directory doesn't exist, file can't exist.
346 if (!CacheFailure)
347 SeenFileEntries.erase(Filename);
348
Douglas Gregor057e5672009-12-02 18:12:28 +0000349 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000350 }
351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // FIXME: Use the directory info to prune this, before doing the stat syscall.
353 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // Nope, there isn't. Check to see if the file exists.
Chris Lattner898a0612010-11-23 21:17:56 +0000356 int FileDescriptor = -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 struct stat StatBuf;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000358 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) {
359 // There's no real file at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000360 if (!CacheFailure)
361 SeenFileEntries.erase(Filename);
362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 return 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000366 if (FileDescriptor != -1 && !openFile) {
367 close(FileDescriptor);
368 FileDescriptor = -1;
369 }
370
Ted Kremenekbca6d122007-12-18 22:29:39 +0000371 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 // This occurs when one dir is symlinked to another, for example.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000373 FileEntry &UFE = UniqueRealFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 NamedFileEnt.setValue(&UFE);
Chris Lattner898a0612010-11-23 21:17:56 +0000376 if (UFE.getName()) { // Already have an entry with this inode, return it.
377 // If the stat process opened the file, close it to avoid a FD leak.
378 if (FileDescriptor != -1)
379 close(FileDescriptor);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000380
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 return &UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000382 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000383
384 // Otherwise, we don't have this directory yet, add it.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000385 // FIXME: Change the name to be a char* that points back to the
386 // 'SeenFileEntries' key.
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 UFE.Name = InterndFileName;
388 UFE.Size = StatBuf.st_size;
389 UFE.ModTime = StatBuf.st_mtime;
390 UFE.Dir = DirInfo;
391 UFE.UID = NextFileUID++;
Chris Lattner898a0612010-11-23 21:17:56 +0000392 UFE.FD = FileDescriptor;
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 return &UFE;
394}
395
Douglas Gregor057e5672009-12-02 18:12:28 +0000396const FileEntry *
Chris Lattner5f9e2722011-07-23 10:55:15 +0000397FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000398 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000399 ++NumFileLookups;
400
401 // See if there is already an entry in the map.
402 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000403 SeenFileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000404
405 // See if there is already an entry in the map.
Axel Naumann04331162011-01-27 10:55:51 +0000406 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
407 return NamedFileEnt.getValue();
Douglas Gregor057e5672009-12-02 18:12:28 +0000408
409 ++NumFileCacheMisses;
410
411 // By default, initialize it to invalid.
412 NamedFileEnt.setValue(NON_EXISTENT_FILE);
413
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000414 addAncestorsAsVirtualDirs(Filename);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000415 FileEntry *UFE = 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000416
417 // Now that all ancestors of Filename are in the cache, the
418 // following call is guaranteed to find the DirectoryEntry from the
419 // cache.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000420 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
421 /*CacheFailure=*/true);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000422 assert(DirInfo &&
423 "The directory of a virtual file should already be in the cache.");
Douglas Gregor057e5672009-12-02 18:12:28 +0000424
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000425 // Check to see if the file exists. If so, drop the virtual file
426 int FileDescriptor = -1;
427 struct stat StatBuf;
428 const char *InterndFileName = NamedFileEnt.getKeyData();
429 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor) == 0) {
430 // If the stat process opened the file, close it to avoid a FD leak.
431 if (FileDescriptor != -1)
432 close(FileDescriptor);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000433
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000434 StatBuf.st_size = Size;
435 StatBuf.st_mtime = ModificationTime;
436 UFE = &UniqueRealFiles.getFile(InterndFileName, StatBuf);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000437
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000438 NamedFileEnt.setValue(UFE);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000439
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000440 // If we had already opened this file, close it now so we don't
441 // leak the descriptor. We're not going to use the file
442 // descriptor anyway, since this is a virtual file.
443 if (UFE->FD != -1) {
444 close(UFE->FD);
445 UFE->FD = -1;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000446 }
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000447
448 // If we already have an entry with this inode, return it.
449 if (UFE->getName())
450 return UFE;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000451 }
452
453 if (!UFE) {
454 UFE = new FileEntry();
455 VirtualFileEntries.push_back(UFE);
456 NamedFileEnt.setValue(UFE);
457 }
Douglas Gregor057e5672009-12-02 18:12:28 +0000458
Chris Lattnerf9f77662010-11-23 20:50:22 +0000459 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000460 UFE->Size = Size;
461 UFE->ModTime = ModificationTime;
462 UFE->Dir = DirInfo;
463 UFE->UID = NextFileUID++;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000464 UFE->FD = -1;
Douglas Gregor057e5672009-12-02 18:12:28 +0000465 return UFE;
466}
467
Chris Lattner5f9e2722011-07-23 10:55:15 +0000468void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
469 StringRef pathRef(path.data(), path.size());
Anders Carlssonaf036a62011-03-06 22:25:35 +0000470
Anders Carlsson2e2468e2011-03-14 01:13:54 +0000471 if (FileSystemOpts.WorkingDir.empty()
472 || llvm::sys::path::is_absolute(pathRef))
Michael J. Spencer256053b2010-12-17 21:22:22 +0000473 return;
474
Anders Carlsson2e2468e2011-03-14 01:13:54 +0000475 llvm::SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonaf036a62011-03-06 22:25:35 +0000476 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner67452f52010-11-23 04:45:28 +0000477 path = NewPath;
478}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000479
Chris Lattner67452f52010-11-23 04:45:28 +0000480llvm::MemoryBuffer *FileManager::
Chris Lattner75dfb652010-11-23 09:19:42 +0000481getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000482 llvm::OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000483 llvm::error_code ec;
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000484
485 const char *Filename = Entry->getName();
486 // If the file is already open, use the open file descriptor.
487 if (Entry->FD != -1) {
488 ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result,
489 Entry->getSize());
490 if (ErrorStr)
491 *ErrorStr = ec.message();
492
493 close(Entry->FD);
494 Entry->FD = -1;
495 return Result.take();
496 }
497
498 // Otherwise, open the file.
499
Chris Lattner5cc1c732010-11-23 22:32:37 +0000500 if (FileSystemOpts.WorkingDir.empty()) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000501 ec = llvm::MemoryBuffer::getFile(Filename, Result, Entry->getSize());
502 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000503 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000504 return Result.take();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000505 }
Anders Carlssonaf036a62011-03-06 22:25:35 +0000506
507 llvm::SmallString<128> FilePath(Entry->getName());
Anders Carlsson03fd3622011-03-07 01:28:33 +0000508 FixupRelativePath(FilePath);
Anders Carlssonaf036a62011-03-06 22:25:35 +0000509 ec = llvm::MemoryBuffer::getFile(FilePath.str(), Result, Entry->getSize());
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000510 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000511 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000512 return Result.take();
Chris Lattner75dfb652010-11-23 09:19:42 +0000513}
514
515llvm::MemoryBuffer *FileManager::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000516getBufferForFile(StringRef Filename, std::string *ErrorStr) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000517 llvm::OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000518 llvm::error_code ec;
519 if (FileSystemOpts.WorkingDir.empty()) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000520 ec = llvm::MemoryBuffer::getFile(Filename, Result);
521 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000522 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000523 return Result.take();
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000524 }
525
Anders Carlssonaf036a62011-03-06 22:25:35 +0000526 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000527 FixupRelativePath(FilePath);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000528 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
529 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000530 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000531 return Result.take();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000532}
533
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000534/// getStatValue - Get the 'stat' information for the specified path,
535/// using the cache to accelerate it if possible. This returns true
536/// if the path points to a virtual file or does not exist, or returns
537/// false if it's an existent real file. If FileDescriptor is NULL,
538/// do directory look-up instead of file look-up.
Chris Lattnerf9f77662010-11-23 20:50:22 +0000539bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
Chris Lattner898a0612010-11-23 21:17:56 +0000540 int *FileDescriptor) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000541 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
542 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000543 if (FileSystemOpts.WorkingDir.empty())
Chris Lattner898a0612010-11-23 21:17:56 +0000544 return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
545 StatCache.get());
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000546
Anders Carlssonaf036a62011-03-06 22:25:35 +0000547 llvm::SmallString<128> FilePath(Path);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000548 FixupRelativePath(FilePath);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000549
Chris Lattner898a0612010-11-23 21:17:56 +0000550 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
551 StatCache.get());
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000552}
553
Chris Lattner5f9e2722011-07-23 10:55:15 +0000554bool FileManager::getNoncachedStatValue(StringRef Path,
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000555 struct stat &StatBuf) {
556 llvm::SmallString<128> FilePath(Path);
557 FixupRelativePath(FilePath);
558
559 return ::stat(FilePath.c_str(), &StatBuf) != 0;
560}
561
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000562void FileManager::GetUniqueIDMapping(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000563 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000564 UIDToFiles.clear();
565 UIDToFiles.resize(NextFileUID);
566
567 // Map file entries
568 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000569 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000570 FE != FEEnd; ++FE)
571 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
572 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
573
574 // Map virtual file entries
Chris Lattner5f9e2722011-07-23 10:55:15 +0000575 for (SmallVector<FileEntry*, 4>::const_iterator
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000576 VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
577 VFE != VFEEnd; ++VFE)
578 if (*VFE && *VFE != NON_EXISTENT_FILE)
579 UIDToFiles[(*VFE)->getUID()] = *VFE;
580}
Chris Lattner10e286a2010-11-23 19:19:34 +0000581
582
Reid Spencer5f016e22007-07-11 17:01:13 +0000583void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000584 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000585 llvm::errs() << UniqueRealFiles.size() << " real files found, "
586 << UniqueRealDirs.size() << " real dirs found.\n";
587 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
588 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000589 llvm::errs() << NumDirLookups << " dir lookups, "
590 << NumDirCacheMisses << " dir cache misses.\n";
591 llvm::errs() << NumFileLookups << " file lookups, "
592 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000594 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000595}