blob: 9804105e67166f8b666b748c0e43ca086b089d4e [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"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000023#include "llvm/Support/FileSystem.h"
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000025#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000026#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000027#include "llvm/Support/system_error.h"
Dylan Noblesmith1770e0d2011-12-22 22:49:47 +000028#include "llvm/Config/llvm-config.h"
Benjamin Kramer458fb102009-09-05 09:49:39 +000029#include <map>
30#include <set>
31#include <string>
Chris Lattner291fcf02010-11-23 21:53:15 +000032
33// FIXME: This is terrible, we need this for ::close.
34#if !defined(_MSC_VER) && !defined(__MINGW32__)
35#include <unistd.h>
36#include <sys/uio.h>
37#else
38#include <io.h>
39#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000040using namespace clang;
41
42// FIXME: Enhance libsystem to support inode and other fields.
43#include <sys/stat.h>
44
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000045/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000046/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000047#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000048
Chris Lattnerf9f77662010-11-23 20:50:22 +000049/// NON_EXISTENT_FILE - A special value distinct from null that is used to
50/// represent a filename that doesn't exist on the disk.
51#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
52
53
54FileEntry::~FileEntry() {
55 // If this FileEntry owns an open file descriptor that never got used, close
56 // it.
57 if (FD != -1) ::close(FD);
58}
59
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000060//===----------------------------------------------------------------------===//
61// Windows.
62//===----------------------------------------------------------------------===//
63
Ted Kremenek6bb816a2008-02-24 03:15:25 +000064#ifdef LLVM_ON_WIN32
65
Ted Kremenek6bb816a2008-02-24 03:15:25 +000066namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000067 static std::string GetFullPath(const char *relPath) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000068 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
69 assert(absPathStrPtr && "_fullpath() returned NULL!");
70
71 std::string absPath(absPathStrPtr);
72
73 free(absPathStrPtr);
74 return absPath;
75 }
76}
77
78class FileManager::UniqueDirContainer {
79 /// UniqueDirs - Cache from full path to existing directories/files.
80 ///
Mike Stump1eb44332009-09-09 15:08:12 +000081 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000082
83public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000084 /// getDirectory - Return an existing DirectoryEntry with the given
85 /// name if there is already one; otherwise create and return a
86 /// default-constructed DirectoryEntry.
87 DirectoryEntry &getDirectory(const char *Name,
88 const struct stat & /*StatBuf*/) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000089 std::string FullPath(GetFullPath(Name));
Chris Lattnerf3e8a992010-11-23 20:30:42 +000090 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000091 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattnerf3e8a992010-11-23 20:30:42 +000093 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +000094};
95
96class FileManager::UniqueFileContainer {
97 /// UniqueFiles - Cache from full path to existing directories/files.
98 ///
Ted Kremenek75368892009-01-28 01:01:07 +000099 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000100
101public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000102 /// getFile - Return an existing FileEntry with the given name if
103 /// there is already one; otherwise create and return a
104 /// default-constructed FileEntry.
105 FileEntry &getFile(const char *Name, const struct stat & /*StatBuf*/) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000106 std::string FullPath(GetFullPath(Name));
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000107
Benjamin Kramer90c78922011-11-06 20:36:48 +0000108 // Lowercase string because Windows filesystem is case insensitive.
109 FullPath = StringRef(FullPath).lower();
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000110 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000111 }
112
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000113 size_t size() const { return UniqueFiles.size(); }
Axel Naumann3ce42c32012-06-27 09:17:42 +0000114
Axel Naumann5ba05592012-07-10 16:50:27 +0000115 void erase(const FileEntry *Entry) { UniqueFiles.erase(Entry->getName()); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000116};
117
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000118//===----------------------------------------------------------------------===//
119// Unix-like Systems.
120//===----------------------------------------------------------------------===//
121
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000122#else
123
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000124class FileManager::UniqueDirContainer {
125 /// UniqueDirs - Cache from ID's to existing directories/files.
Mike Stump1eb44332009-09-09 15:08:12 +0000126 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000127
128public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000129 /// getDirectory - Return an existing DirectoryEntry with the given
130 /// ID's if there is already one; otherwise create and return a
131 /// default-constructed DirectoryEntry.
132 DirectoryEntry &getDirectory(const char * /*Name*/,
133 const struct stat &StatBuf) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000134 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
135 }
136
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000137 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000138};
139
140class FileManager::UniqueFileContainer {
141 /// UniqueFiles - Cache from ID's to existing directories/files.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000142 std::set<FileEntry> UniqueFiles;
143
144public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000145 /// getFile - Return an existing FileEntry with the given ID's if
146 /// there is already one; otherwise create and return a
147 /// default-constructed FileEntry.
148 FileEntry &getFile(const char * /*Name*/, const struct stat &StatBuf) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000149 return
150 const_cast<FileEntry&>(
151 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000152 StatBuf.st_ino,
153 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000154 }
155
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000156 size_t size() const { return UniqueFiles.size(); }
Axel Naumann3ce42c32012-06-27 09:17:42 +0000157
Axel Naumann5ba05592012-07-10 16:50:27 +0000158 void erase(const FileEntry *Entry) { UniqueFiles.erase(*Entry); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000159};
160
161#endif
162
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000163//===----------------------------------------------------------------------===//
164// Common logic.
165//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000166
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000167FileManager::FileManager(const FileSystemOptions &FSO)
168 : FileSystemOpts(FSO),
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000169 UniqueRealDirs(*new UniqueDirContainer()),
170 UniqueRealFiles(*new UniqueFileContainer()),
171 SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000172 NumDirLookups = NumFileLookups = 0;
173 NumDirCacheMisses = NumFileCacheMisses = 0;
174}
175
176FileManager::~FileManager() {
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000177 delete &UniqueRealDirs;
178 delete &UniqueRealFiles;
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000179 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
180 delete VirtualFileEntries[i];
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000181 for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
182 delete VirtualDirectoryEntries[i];
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000183}
184
Chris Lattner10e286a2010-11-23 19:19:34 +0000185void FileManager::addStatCache(FileSystemStatCache *statCache,
186 bool AtBeginning) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000187 assert(statCache && "No stat cache provided?");
188 if (AtBeginning || StatCache.get() == 0) {
189 statCache->setNextStatCache(StatCache.take());
190 StatCache.reset(statCache);
191 return;
192 }
193
Chris Lattner10e286a2010-11-23 19:19:34 +0000194 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000195 while (LastCache->getNextStatCache())
196 LastCache = LastCache->getNextStatCache();
197
198 LastCache->setNextStatCache(statCache);
199}
200
Chris Lattner10e286a2010-11-23 19:19:34 +0000201void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000202 if (!statCache)
203 return;
204
205 if (StatCache.get() == statCache) {
206 // This is the first stat cache.
207 StatCache.reset(StatCache->takeNextStatCache());
208 return;
209 }
210
211 // Find the stat cache in the list.
Chris Lattner10e286a2010-11-23 19:19:34 +0000212 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000213 while (PrevCache && PrevCache->getNextStatCache() != statCache)
214 PrevCache = PrevCache->getNextStatCache();
Chris Lattnerf9f77662010-11-23 20:50:22 +0000215
216 assert(PrevCache && "Stat cache not found for removal");
217 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregor52e71082009-10-16 18:18:30 +0000218}
219
Douglas Gregor057e5672009-12-02 18:12:28 +0000220/// \brief Retrieve the directory that the given file name resides in.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000221/// Filename can point to either a real file or a virtual file.
Douglas Gregor057e5672009-12-02 18:12:28 +0000222static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Douglas Gregor6e975c42011-09-13 23:15:45 +0000223 StringRef Filename,
224 bool CacheFailure) {
Zhanyong Wan21af8872011-02-11 21:25:35 +0000225 if (Filename.empty())
226 return NULL;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000227
Zhanyong Wan21af8872011-02-11 21:25:35 +0000228 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
229 return NULL; // If Filename is a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000230
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231 StringRef DirName = llvm::sys::path::parent_path(Filename);
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000232 // Use the current directory if file has no path component.
Zhanyong Wan21af8872011-02-11 21:25:35 +0000233 if (DirName.empty())
234 DirName = ".";
Douglas Gregor057e5672009-12-02 18:12:28 +0000235
Douglas Gregor6e975c42011-09-13 23:15:45 +0000236 return FileMgr.getDirectory(DirName, CacheFailure);
Douglas Gregor057e5672009-12-02 18:12:28 +0000237}
238
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000239/// Add all ancestors of the given path (pointing to either a file or
240/// a directory) as virtual directories.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
242 StringRef DirName = llvm::sys::path::parent_path(Path);
Zhanyong Wan21af8872011-02-11 21:25:35 +0000243 if (DirName.empty())
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000244 return;
245
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000246 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
247 SeenDirEntries.GetOrCreateValue(DirName);
248
249 // When caching a virtual directory, we always cache its ancestors
250 // at the same time. Therefore, if DirName is already in the cache,
251 // we don't need to recurse as its ancestors must also already be in
252 // the cache.
253 if (NamedDirEnt.getValue())
254 return;
255
256 // Add the virtual directory to the cache.
257 DirectoryEntry *UDE = new DirectoryEntry;
258 UDE->Name = NamedDirEnt.getKeyData();
259 NamedDirEnt.setValue(UDE);
260 VirtualDirectoryEntries.push_back(UDE);
261
262 // Recursively add the other ancestors.
263 addAncestorsAsVirtualDirs(DirName);
264}
265
Douglas Gregor6e975c42011-09-13 23:15:45 +0000266const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
267 bool CacheFailure) {
NAKAMURA Takumi759a4b42012-06-16 06:04:10 +0000268 // stat doesn't like trailing separators except for root directory.
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000269 // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
270 // (though it can strip '\\')
NAKAMURA Takumi759a4b42012-06-16 06:04:10 +0000271 if (DirName.size() > 1 &&
272 DirName != llvm::sys::path::root_path(DirName) &&
273 llvm::sys::path::is_separator(DirName.back()))
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000274 DirName = DirName.substr(0, DirName.size()-1);
275
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 ++NumDirLookups;
277 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000278 SeenDirEntries.GetOrCreateValue(DirName);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000280 // See if there was already an entry in the map. Note that the map
281 // contains both virtual and real directories.
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000283 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000289 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 // Get the null-terminated directory name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000292 // SeenDirEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Check to see if the directory exists.
296 struct stat StatBuf;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000297 if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/)) {
298 // There's no real directory at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000299 if (!CacheFailure)
300 SeenDirEntries.erase(DirName);
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 return 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000302 }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000303
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000304 // It exists. See if we have already opened a directory with the
305 // same inode (this occurs on Unix-like systems when one dir is
306 // symlinked to another, for example) or the same path (on
307 // Windows).
308 DirectoryEntry &UDE = UniqueRealDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 NamedDirEnt.setValue(&UDE);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000311 if (!UDE.getName()) {
312 // We don't have this directory yet, add it. We use the string
313 // key from the SeenDirEntries map as the string.
314 UDE.Name = InterndDirName;
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 return &UDE;
318}
319
Douglas Gregor6e975c42011-09-13 23:15:45 +0000320const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
321 bool CacheFailure) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // See if there is already an entry in the map.
325 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000326 SeenFileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000327
328 // See if there is already an entry in the map.
329 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000330 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 ++NumFileCacheMisses;
334
335 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000336 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000337
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 // Get the null-terminated file name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000339 // SeenFileEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000342 // Look up the directory for the file. When looking up something like
343 // sys/foo.h we'll discover all of the search directories that have a 'sys'
344 // subdirectory. This will let us avoid having to waste time on known-to-fail
345 // searches when we go to find sys/bar.h, because all the search directories
346 // without a 'sys' subdir will get a cached failure result.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000347 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
348 CacheFailure);
349 if (DirInfo == 0) { // Directory doesn't exist, file can't exist.
350 if (!CacheFailure)
351 SeenFileEntries.erase(Filename);
352
Douglas Gregor057e5672009-12-02 18:12:28 +0000353 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000354 }
355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // FIXME: Use the directory info to prune this, before doing the stat syscall.
357 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 // Nope, there isn't. Check to see if the file exists.
Chris Lattner898a0612010-11-23 21:17:56 +0000360 int FileDescriptor = -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 struct stat StatBuf;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000362 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) {
363 // There's no real file at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000364 if (!CacheFailure)
365 SeenFileEntries.erase(Filename);
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 return 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000370 if (FileDescriptor != -1 && !openFile) {
371 close(FileDescriptor);
372 FileDescriptor = -1;
373 }
374
Ted Kremenekbca6d122007-12-18 22:29:39 +0000375 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 // This occurs when one dir is symlinked to another, for example.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000377 FileEntry &UFE = UniqueRealFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 NamedFileEnt.setValue(&UFE);
Chris Lattner898a0612010-11-23 21:17:56 +0000380 if (UFE.getName()) { // Already have an entry with this inode, return it.
381 // If the stat process opened the file, close it to avoid a FD leak.
382 if (FileDescriptor != -1)
383 close(FileDescriptor);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000384
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 return &UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000386 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000387
388 // Otherwise, we don't have this directory yet, add it.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000389 // FIXME: Change the name to be a char* that points back to the
390 // 'SeenFileEntries' key.
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 UFE.Name = InterndFileName;
392 UFE.Size = StatBuf.st_size;
393 UFE.ModTime = StatBuf.st_mtime;
394 UFE.Dir = DirInfo;
395 UFE.UID = NextFileUID++;
Chris Lattner898a0612010-11-23 21:17:56 +0000396 UFE.FD = FileDescriptor;
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 return &UFE;
398}
399
Douglas Gregor057e5672009-12-02 18:12:28 +0000400const FileEntry *
Chris Lattner5f9e2722011-07-23 10:55:15 +0000401FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000402 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000403 ++NumFileLookups;
404
405 // See if there is already an entry in the map.
406 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000407 SeenFileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000408
409 // See if there is already an entry in the map.
Axel Naumann04331162011-01-27 10:55:51 +0000410 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
411 return NamedFileEnt.getValue();
Douglas Gregor057e5672009-12-02 18:12:28 +0000412
413 ++NumFileCacheMisses;
414
415 // By default, initialize it to invalid.
416 NamedFileEnt.setValue(NON_EXISTENT_FILE);
417
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000418 addAncestorsAsVirtualDirs(Filename);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000419 FileEntry *UFE = 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000420
421 // Now that all ancestors of Filename are in the cache, the
422 // following call is guaranteed to find the DirectoryEntry from the
423 // cache.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000424 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
425 /*CacheFailure=*/true);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000426 assert(DirInfo &&
427 "The directory of a virtual file should already be in the cache.");
Douglas Gregor057e5672009-12-02 18:12:28 +0000428
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000429 // Check to see if the file exists. If so, drop the virtual file
430 int FileDescriptor = -1;
431 struct stat StatBuf;
432 const char *InterndFileName = NamedFileEnt.getKeyData();
433 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor) == 0) {
434 // If the stat process opened the file, close it to avoid a FD leak.
435 if (FileDescriptor != -1)
436 close(FileDescriptor);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000437
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000438 StatBuf.st_size = Size;
439 StatBuf.st_mtime = ModificationTime;
440 UFE = &UniqueRealFiles.getFile(InterndFileName, StatBuf);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000441
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000442 NamedFileEnt.setValue(UFE);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000443
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000444 // If we had already opened this file, close it now so we don't
445 // leak the descriptor. We're not going to use the file
446 // descriptor anyway, since this is a virtual file.
447 if (UFE->FD != -1) {
448 close(UFE->FD);
449 UFE->FD = -1;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000450 }
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000451
452 // If we already have an entry with this inode, return it.
453 if (UFE->getName())
454 return UFE;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000455 }
456
457 if (!UFE) {
458 UFE = new FileEntry();
459 VirtualFileEntries.push_back(UFE);
460 NamedFileEnt.setValue(UFE);
461 }
Douglas Gregor057e5672009-12-02 18:12:28 +0000462
Chris Lattnerf9f77662010-11-23 20:50:22 +0000463 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000464 UFE->Size = Size;
465 UFE->ModTime = ModificationTime;
466 UFE->Dir = DirInfo;
467 UFE->UID = NextFileUID++;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000468 UFE->FD = -1;
Douglas Gregor057e5672009-12-02 18:12:28 +0000469 return UFE;
470}
471
Chris Lattner5f9e2722011-07-23 10:55:15 +0000472void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
473 StringRef pathRef(path.data(), path.size());
Anders Carlssonaf036a62011-03-06 22:25:35 +0000474
Anders Carlsson2e2468e2011-03-14 01:13:54 +0000475 if (FileSystemOpts.WorkingDir.empty()
476 || llvm::sys::path::is_absolute(pathRef))
Michael J. Spencer256053b2010-12-17 21:22:22 +0000477 return;
478
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000479 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonaf036a62011-03-06 22:25:35 +0000480 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner67452f52010-11-23 04:45:28 +0000481 path = NewPath;
482}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000483
Chris Lattner67452f52010-11-23 04:45:28 +0000484llvm::MemoryBuffer *FileManager::
Chris Lattner75dfb652010-11-23 09:19:42 +0000485getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000486 OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000487 llvm::error_code ec;
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000488
489 const char *Filename = Entry->getName();
490 // If the file is already open, use the open file descriptor.
491 if (Entry->FD != -1) {
492 ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result,
493 Entry->getSize());
494 if (ErrorStr)
495 *ErrorStr = ec.message();
496
497 close(Entry->FD);
498 Entry->FD = -1;
499 return Result.take();
500 }
501
502 // Otherwise, open the file.
503
Chris Lattner5cc1c732010-11-23 22:32:37 +0000504 if (FileSystemOpts.WorkingDir.empty()) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000505 ec = llvm::MemoryBuffer::getFile(Filename, Result, Entry->getSize());
506 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000507 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000508 return Result.take();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000509 }
Anders Carlssonaf036a62011-03-06 22:25:35 +0000510
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000511 SmallString<128> FilePath(Entry->getName());
Anders Carlsson03fd3622011-03-07 01:28:33 +0000512 FixupRelativePath(FilePath);
Anders Carlssonaf036a62011-03-06 22:25:35 +0000513 ec = llvm::MemoryBuffer::getFile(FilePath.str(), Result, Entry->getSize());
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000514 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000515 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000516 return Result.take();
Chris Lattner75dfb652010-11-23 09:19:42 +0000517}
518
519llvm::MemoryBuffer *FileManager::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000520getBufferForFile(StringRef Filename, std::string *ErrorStr) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000521 OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000522 llvm::error_code ec;
523 if (FileSystemOpts.WorkingDir.empty()) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000524 ec = llvm::MemoryBuffer::getFile(Filename, Result);
525 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000526 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000527 return Result.take();
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000528 }
529
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000530 SmallString<128> FilePath(Filename);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000531 FixupRelativePath(FilePath);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000532 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
533 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000534 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000535 return Result.take();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000536}
537
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000538/// getStatValue - Get the 'stat' information for the specified path,
539/// using the cache to accelerate it if possible. This returns true
540/// if the path points to a virtual file or does not exist, or returns
541/// false if it's an existent real file. If FileDescriptor is NULL,
542/// do directory look-up instead of file look-up.
Chris Lattnerf9f77662010-11-23 20:50:22 +0000543bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
Chris Lattner898a0612010-11-23 21:17:56 +0000544 int *FileDescriptor) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000545 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
546 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000547 if (FileSystemOpts.WorkingDir.empty())
Chris Lattner898a0612010-11-23 21:17:56 +0000548 return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
549 StatCache.get());
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000550
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000551 SmallString<128> FilePath(Path);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000552 FixupRelativePath(FilePath);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000553
Chris Lattner898a0612010-11-23 21:17:56 +0000554 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
555 StatCache.get());
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000556}
557
Chris Lattner5f9e2722011-07-23 10:55:15 +0000558bool FileManager::getNoncachedStatValue(StringRef Path,
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000559 struct stat &StatBuf) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000560 SmallString<128> FilePath(Path);
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000561 FixupRelativePath(FilePath);
562
563 return ::stat(FilePath.c_str(), &StatBuf) != 0;
564}
565
Axel Naumann5ba05592012-07-10 16:50:27 +0000566void FileManager::invalidateCache(const FileEntry *Entry) {
567 assert(Entry && "Cannot invalidate a NULL FileEntry");
Axel Naumann3ce42c32012-06-27 09:17:42 +0000568
569 SeenFileEntries.erase(Entry->getName());
Axel Naumann5ba05592012-07-10 16:50:27 +0000570
571 // FileEntry invalidation should not block future optimizations in the file
572 // caches. Possible alternatives are cache truncation (invalidate last N) or
573 // invalidation of the whole cache.
574 UniqueRealFiles.erase(Entry);
Axel Naumann3ce42c32012-06-27 09:17:42 +0000575}
576
577
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000578void FileManager::GetUniqueIDMapping(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000579 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000580 UIDToFiles.clear();
581 UIDToFiles.resize(NextFileUID);
582
583 // Map file entries
584 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000585 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000586 FE != FEEnd; ++FE)
587 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
588 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
589
590 // Map virtual file entries
Chris Lattner5f9e2722011-07-23 10:55:15 +0000591 for (SmallVector<FileEntry*, 4>::const_iterator
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000592 VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
593 VFE != VFEEnd; ++VFE)
594 if (*VFE && *VFE != NON_EXISTENT_FILE)
595 UIDToFiles[(*VFE)->getUID()] = *VFE;
596}
Chris Lattner10e286a2010-11-23 19:19:34 +0000597
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +0000598void FileManager::modifyFileEntry(FileEntry *File,
599 off_t Size, time_t ModificationTime) {
600 File->Size = Size;
601 File->ModTime = ModificationTime;
602}
603
Chris Lattner10e286a2010-11-23 19:19:34 +0000604
Reid Spencer5f016e22007-07-11 17:01:13 +0000605void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000606 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000607 llvm::errs() << UniqueRealFiles.size() << " real files found, "
608 << UniqueRealDirs.size() << " real dirs found.\n";
609 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
610 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000611 llvm::errs() << NumDirLookups << " dir lookups, "
612 << NumDirCacheMisses << " dir cache misses.\n";
613 llvm::errs() << NumFileLookups << " file lookups, "
614 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000616 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000617}