blob: 6a0fbf80b905c57b1bc69a8248641d6a0e069c04 [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
Daniel Dunbarce36ecd2012-11-05 22:53:33 +000060bool FileEntry::isNamedPipe() const {
61 return FileMode & S_IFIFO;
62}
63
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000064//===----------------------------------------------------------------------===//
65// Windows.
66//===----------------------------------------------------------------------===//
67
Ted Kremenek6bb816a2008-02-24 03:15:25 +000068#ifdef LLVM_ON_WIN32
69
Ted Kremenek6bb816a2008-02-24 03:15:25 +000070namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000071 static std::string GetFullPath(const char *relPath) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000072 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
73 assert(absPathStrPtr && "_fullpath() returned NULL!");
74
75 std::string absPath(absPathStrPtr);
76
77 free(absPathStrPtr);
78 return absPath;
79 }
80}
81
82class FileManager::UniqueDirContainer {
83 /// UniqueDirs - Cache from full path to existing directories/files.
84 ///
Mike Stump1eb44332009-09-09 15:08:12 +000085 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000086
87public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000088 /// getDirectory - Return an existing DirectoryEntry with the given
89 /// name if there is already one; otherwise create and return a
90 /// default-constructed DirectoryEntry.
91 DirectoryEntry &getDirectory(const char *Name,
92 const struct stat & /*StatBuf*/) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000093 std::string FullPath(GetFullPath(Name));
Chris Lattnerf3e8a992010-11-23 20:30:42 +000094 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000095 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattnerf3e8a992010-11-23 20:30:42 +000097 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +000098};
99
100class FileManager::UniqueFileContainer {
101 /// UniqueFiles - Cache from full path to existing directories/files.
102 ///
Ted Kremenek75368892009-01-28 01:01:07 +0000103 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000104
105public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000106 /// getFile - Return an existing FileEntry with the given name if
107 /// there is already one; otherwise create and return a
108 /// default-constructed FileEntry.
109 FileEntry &getFile(const char *Name, const struct stat & /*StatBuf*/) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000110 std::string FullPath(GetFullPath(Name));
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000111
Benjamin Kramer90c78922011-11-06 20:36:48 +0000112 // Lowercase string because Windows filesystem is case insensitive.
113 FullPath = StringRef(FullPath).lower();
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000114 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000115 }
116
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000117 size_t size() const { return UniqueFiles.size(); }
Axel Naumann3ce42c32012-06-27 09:17:42 +0000118
Axel Naumannbe779592012-07-11 09:41:34 +0000119 void erase(const FileEntry *Entry) {
120 std::string FullPath(GetFullPath(Entry->getName()));
121
122 // Lowercase string because Windows filesystem is case insensitive.
123 FullPath = StringRef(FullPath).lower();
124 UniqueFiles.erase(FullPath);
125 }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000126};
127
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000128//===----------------------------------------------------------------------===//
129// Unix-like Systems.
130//===----------------------------------------------------------------------===//
131
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000132#else
133
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000134class FileManager::UniqueDirContainer {
135 /// UniqueDirs - Cache from ID's to existing directories/files.
Mike Stump1eb44332009-09-09 15:08:12 +0000136 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000137
138public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000139 /// getDirectory - Return an existing DirectoryEntry with the given
140 /// ID's if there is already one; otherwise create and return a
141 /// default-constructed DirectoryEntry.
142 DirectoryEntry &getDirectory(const char * /*Name*/,
143 const struct stat &StatBuf) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000144 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
145 }
146
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000147 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000148};
149
150class FileManager::UniqueFileContainer {
151 /// UniqueFiles - Cache from ID's to existing directories/files.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000152 std::set<FileEntry> UniqueFiles;
153
154public:
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000155 /// getFile - Return an existing FileEntry with the given ID's if
156 /// there is already one; otherwise create and return a
157 /// default-constructed FileEntry.
158 FileEntry &getFile(const char * /*Name*/, const struct stat &StatBuf) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000159 return
160 const_cast<FileEntry&>(
161 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000162 StatBuf.st_ino,
163 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000164 }
165
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000166 size_t size() const { return UniqueFiles.size(); }
Axel Naumann3ce42c32012-06-27 09:17:42 +0000167
Axel Naumann5ba05592012-07-10 16:50:27 +0000168 void erase(const FileEntry *Entry) { UniqueFiles.erase(*Entry); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000169};
170
171#endif
172
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000173//===----------------------------------------------------------------------===//
174// Common logic.
175//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000176
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000177FileManager::FileManager(const FileSystemOptions &FSO)
178 : FileSystemOpts(FSO),
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000179 UniqueRealDirs(*new UniqueDirContainer()),
180 UniqueRealFiles(*new UniqueFileContainer()),
181 SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000182 NumDirLookups = NumFileLookups = 0;
183 NumDirCacheMisses = NumFileCacheMisses = 0;
184}
185
186FileManager::~FileManager() {
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000187 delete &UniqueRealDirs;
188 delete &UniqueRealFiles;
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000189 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
190 delete VirtualFileEntries[i];
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000191 for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
192 delete VirtualDirectoryEntries[i];
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000193}
194
Chris Lattner10e286a2010-11-23 19:19:34 +0000195void FileManager::addStatCache(FileSystemStatCache *statCache,
196 bool AtBeginning) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000197 assert(statCache && "No stat cache provided?");
198 if (AtBeginning || StatCache.get() == 0) {
199 statCache->setNextStatCache(StatCache.take());
200 StatCache.reset(statCache);
201 return;
202 }
203
Chris Lattner10e286a2010-11-23 19:19:34 +0000204 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000205 while (LastCache->getNextStatCache())
206 LastCache = LastCache->getNextStatCache();
207
208 LastCache->setNextStatCache(statCache);
209}
210
Chris Lattner10e286a2010-11-23 19:19:34 +0000211void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000212 if (!statCache)
213 return;
214
215 if (StatCache.get() == statCache) {
216 // This is the first stat cache.
217 StatCache.reset(StatCache->takeNextStatCache());
218 return;
219 }
220
221 // Find the stat cache in the list.
Chris Lattner10e286a2010-11-23 19:19:34 +0000222 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000223 while (PrevCache && PrevCache->getNextStatCache() != statCache)
224 PrevCache = PrevCache->getNextStatCache();
Chris Lattnerf9f77662010-11-23 20:50:22 +0000225
226 assert(PrevCache && "Stat cache not found for removal");
227 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregor52e71082009-10-16 18:18:30 +0000228}
229
Manuel Klimek98be8602012-07-31 13:56:54 +0000230void FileManager::clearStatCaches() {
231 StatCache.reset(0);
232}
233
Douglas Gregor057e5672009-12-02 18:12:28 +0000234/// \brief Retrieve the directory that the given file name resides in.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000235/// Filename can point to either a real file or a virtual file.
Douglas Gregor057e5672009-12-02 18:12:28 +0000236static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Douglas Gregor6e975c42011-09-13 23:15:45 +0000237 StringRef Filename,
238 bool CacheFailure) {
Zhanyong Wan21af8872011-02-11 21:25:35 +0000239 if (Filename.empty())
240 return NULL;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000241
Zhanyong Wan21af8872011-02-11 21:25:35 +0000242 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
243 return NULL; // If Filename is a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000244
Chris Lattner5f9e2722011-07-23 10:55:15 +0000245 StringRef DirName = llvm::sys::path::parent_path(Filename);
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000246 // Use the current directory if file has no path component.
Zhanyong Wan21af8872011-02-11 21:25:35 +0000247 if (DirName.empty())
248 DirName = ".";
Douglas Gregor057e5672009-12-02 18:12:28 +0000249
Douglas Gregor6e975c42011-09-13 23:15:45 +0000250 return FileMgr.getDirectory(DirName, CacheFailure);
Douglas Gregor057e5672009-12-02 18:12:28 +0000251}
252
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000253/// Add all ancestors of the given path (pointing to either a file or
254/// a directory) as virtual directories.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
256 StringRef DirName = llvm::sys::path::parent_path(Path);
Zhanyong Wan21af8872011-02-11 21:25:35 +0000257 if (DirName.empty())
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000258 return;
259
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000260 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
261 SeenDirEntries.GetOrCreateValue(DirName);
262
263 // When caching a virtual directory, we always cache its ancestors
264 // at the same time. Therefore, if DirName is already in the cache,
265 // we don't need to recurse as its ancestors must also already be in
266 // the cache.
267 if (NamedDirEnt.getValue())
268 return;
269
270 // Add the virtual directory to the cache.
271 DirectoryEntry *UDE = new DirectoryEntry;
272 UDE->Name = NamedDirEnt.getKeyData();
273 NamedDirEnt.setValue(UDE);
274 VirtualDirectoryEntries.push_back(UDE);
275
276 // Recursively add the other ancestors.
277 addAncestorsAsVirtualDirs(DirName);
278}
279
Douglas Gregor6e975c42011-09-13 23:15:45 +0000280const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
281 bool CacheFailure) {
NAKAMURA Takumi759a4b42012-06-16 06:04:10 +0000282 // stat doesn't like trailing separators except for root directory.
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000283 // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
284 // (though it can strip '\\')
NAKAMURA Takumi759a4b42012-06-16 06:04:10 +0000285 if (DirName.size() > 1 &&
286 DirName != llvm::sys::path::root_path(DirName) &&
287 llvm::sys::path::is_separator(DirName.back()))
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000288 DirName = DirName.substr(0, DirName.size()-1);
289
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 ++NumDirLookups;
291 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000292 SeenDirEntries.GetOrCreateValue(DirName);
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000294 // See if there was already an entry in the map. Note that the map
295 // contains both virtual and real directories.
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000297 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000303 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // Get the null-terminated directory name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000306 // SeenDirEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 // Check to see if the directory exists.
310 struct stat StatBuf;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000311 if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/)) {
312 // There's no real directory at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000313 if (!CacheFailure)
314 SeenDirEntries.erase(DirName);
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 return 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000316 }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000317
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000318 // It exists. See if we have already opened a directory with the
319 // same inode (this occurs on Unix-like systems when one dir is
320 // symlinked to another, for example) or the same path (on
321 // Windows).
322 DirectoryEntry &UDE = UniqueRealDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 NamedDirEnt.setValue(&UDE);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000325 if (!UDE.getName()) {
326 // We don't have this directory yet, add it. We use the string
327 // key from the SeenDirEntries map as the string.
328 UDE.Name = InterndDirName;
329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 return &UDE;
332}
333
Douglas Gregor6e975c42011-09-13 23:15:45 +0000334const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
335 bool CacheFailure) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 // See if there is already an entry in the map.
339 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000340 SeenFileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000341
342 // See if there is already an entry in the map.
343 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000344 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 ++NumFileCacheMisses;
348
349 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000350 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // Get the null-terminated file name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000353 // SeenFileEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000356 // Look up the directory for the file. When looking up something like
357 // sys/foo.h we'll discover all of the search directories that have a 'sys'
358 // subdirectory. This will let us avoid having to waste time on known-to-fail
359 // searches when we go to find sys/bar.h, because all the search directories
360 // without a 'sys' subdir will get a cached failure result.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000361 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
362 CacheFailure);
363 if (DirInfo == 0) { // Directory doesn't exist, file can't exist.
364 if (!CacheFailure)
365 SeenFileEntries.erase(Filename);
366
Douglas Gregor057e5672009-12-02 18:12:28 +0000367 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000368 }
369
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 // FIXME: Use the directory info to prune this, before doing the stat syscall.
371 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 // Nope, there isn't. Check to see if the file exists.
Chris Lattner898a0612010-11-23 21:17:56 +0000374 int FileDescriptor = -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 struct stat StatBuf;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000376 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) {
377 // There's no real file at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000378 if (!CacheFailure)
379 SeenFileEntries.erase(Filename);
380
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 return 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000384 if (FileDescriptor != -1 && !openFile) {
385 close(FileDescriptor);
386 FileDescriptor = -1;
387 }
388
Ted Kremenekbca6d122007-12-18 22:29:39 +0000389 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 // This occurs when one dir is symlinked to another, for example.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000391 FileEntry &UFE = UniqueRealFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 NamedFileEnt.setValue(&UFE);
Chris Lattner898a0612010-11-23 21:17:56 +0000394 if (UFE.getName()) { // Already have an entry with this inode, return it.
395 // If the stat process opened the file, close it to avoid a FD leak.
396 if (FileDescriptor != -1)
397 close(FileDescriptor);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000398
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 return &UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000400 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000401
402 // Otherwise, we don't have this directory yet, add it.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000403 // FIXME: Change the name to be a char* that points back to the
404 // 'SeenFileEntries' key.
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 UFE.Name = InterndFileName;
406 UFE.Size = StatBuf.st_size;
407 UFE.ModTime = StatBuf.st_mtime;
408 UFE.Dir = DirInfo;
409 UFE.UID = NextFileUID++;
Chris Lattner898a0612010-11-23 21:17:56 +0000410 UFE.FD = FileDescriptor;
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 return &UFE;
412}
413
Douglas Gregor057e5672009-12-02 18:12:28 +0000414const FileEntry *
Chris Lattner5f9e2722011-07-23 10:55:15 +0000415FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000416 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000417 ++NumFileLookups;
418
419 // See if there is already an entry in the map.
420 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000421 SeenFileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000422
423 // See if there is already an entry in the map.
Axel Naumann04331162011-01-27 10:55:51 +0000424 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
425 return NamedFileEnt.getValue();
Douglas Gregor057e5672009-12-02 18:12:28 +0000426
427 ++NumFileCacheMisses;
428
429 // By default, initialize it to invalid.
430 NamedFileEnt.setValue(NON_EXISTENT_FILE);
431
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000432 addAncestorsAsVirtualDirs(Filename);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000433 FileEntry *UFE = 0;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000434
435 // Now that all ancestors of Filename are in the cache, the
436 // following call is guaranteed to find the DirectoryEntry from the
437 // cache.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000438 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
439 /*CacheFailure=*/true);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000440 assert(DirInfo &&
441 "The directory of a virtual file should already be in the cache.");
Douglas Gregor057e5672009-12-02 18:12:28 +0000442
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000443 // Check to see if the file exists. If so, drop the virtual file
444 int FileDescriptor = -1;
445 struct stat StatBuf;
446 const char *InterndFileName = NamedFileEnt.getKeyData();
447 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor) == 0) {
448 // If the stat process opened the file, close it to avoid a FD leak.
449 if (FileDescriptor != -1)
450 close(FileDescriptor);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000451
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000452 StatBuf.st_size = Size;
453 StatBuf.st_mtime = ModificationTime;
454 UFE = &UniqueRealFiles.getFile(InterndFileName, StatBuf);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000455
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000456 NamedFileEnt.setValue(UFE);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000457
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000458 // If we had already opened this file, close it now so we don't
459 // leak the descriptor. We're not going to use the file
460 // descriptor anyway, since this is a virtual file.
461 if (UFE->FD != -1) {
462 close(UFE->FD);
463 UFE->FD = -1;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000464 }
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000465
466 // If we already have an entry with this inode, return it.
467 if (UFE->getName())
468 return UFE;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000469 }
470
471 if (!UFE) {
472 UFE = new FileEntry();
473 VirtualFileEntries.push_back(UFE);
474 NamedFileEnt.setValue(UFE);
475 }
Douglas Gregor057e5672009-12-02 18:12:28 +0000476
Chris Lattnerf9f77662010-11-23 20:50:22 +0000477 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000478 UFE->Size = Size;
479 UFE->ModTime = ModificationTime;
480 UFE->Dir = DirInfo;
481 UFE->UID = NextFileUID++;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000482 UFE->FD = -1;
Douglas Gregor057e5672009-12-02 18:12:28 +0000483 return UFE;
484}
485
Chris Lattner5f9e2722011-07-23 10:55:15 +0000486void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
487 StringRef pathRef(path.data(), path.size());
Anders Carlssonaf036a62011-03-06 22:25:35 +0000488
Anders Carlsson2e2468e2011-03-14 01:13:54 +0000489 if (FileSystemOpts.WorkingDir.empty()
490 || llvm::sys::path::is_absolute(pathRef))
Michael J. Spencer256053b2010-12-17 21:22:22 +0000491 return;
492
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000493 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonaf036a62011-03-06 22:25:35 +0000494 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner67452f52010-11-23 04:45:28 +0000495 path = NewPath;
496}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000497
Chris Lattner67452f52010-11-23 04:45:28 +0000498llvm::MemoryBuffer *FileManager::
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000499getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
500 bool isVolatile) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000501 OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000502 llvm::error_code ec;
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000503
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000504 uint64_t FileSize = Entry->getSize();
505 // If there's a high enough chance that the file have changed since we
506 // got its size, force a stat before opening it.
507 if (isVolatile)
508 FileSize = -1;
509
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000510 const char *Filename = Entry->getName();
511 // If the file is already open, use the open file descriptor.
512 if (Entry->FD != -1) {
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000513 ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result, FileSize);
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000514 if (ErrorStr)
515 *ErrorStr = ec.message();
516
517 close(Entry->FD);
518 Entry->FD = -1;
519 return Result.take();
520 }
521
522 // Otherwise, open the file.
523
Chris Lattner5cc1c732010-11-23 22:32:37 +0000524 if (FileSystemOpts.WorkingDir.empty()) {
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000525 ec = llvm::MemoryBuffer::getFile(Filename, Result, FileSize);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000526 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000527 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000528 return Result.take();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000529 }
Anders Carlssonaf036a62011-03-06 22:25:35 +0000530
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000531 SmallString<128> FilePath(Entry->getName());
Anders Carlsson03fd3622011-03-07 01:28:33 +0000532 FixupRelativePath(FilePath);
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000533 ec = llvm::MemoryBuffer::getFile(FilePath.str(), Result, FileSize);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000534 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000535 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000536 return Result.take();
Chris Lattner75dfb652010-11-23 09:19:42 +0000537}
538
539llvm::MemoryBuffer *FileManager::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000540getBufferForFile(StringRef Filename, std::string *ErrorStr) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000541 OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000542 llvm::error_code ec;
543 if (FileSystemOpts.WorkingDir.empty()) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000544 ec = llvm::MemoryBuffer::getFile(Filename, Result);
545 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000546 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000547 return Result.take();
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000548 }
549
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000550 SmallString<128> FilePath(Filename);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000551 FixupRelativePath(FilePath);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000552 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
553 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000554 *ErrorStr = ec.message();
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000555 return Result.take();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000556}
557
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000558/// getStatValue - Get the 'stat' information for the specified path,
559/// using the cache to accelerate it if possible. This returns true
560/// if the path points to a virtual file or does not exist, or returns
561/// false if it's an existent real file. If FileDescriptor is NULL,
562/// do directory look-up instead of file look-up.
Chris Lattnerf9f77662010-11-23 20:50:22 +0000563bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
Chris Lattner898a0612010-11-23 21:17:56 +0000564 int *FileDescriptor) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000565 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
566 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000567 if (FileSystemOpts.WorkingDir.empty())
Chris Lattner898a0612010-11-23 21:17:56 +0000568 return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
569 StatCache.get());
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000570
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000571 SmallString<128> FilePath(Path);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000572 FixupRelativePath(FilePath);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000573
Chris Lattner898a0612010-11-23 21:17:56 +0000574 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
575 StatCache.get());
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000576}
577
Chris Lattner5f9e2722011-07-23 10:55:15 +0000578bool FileManager::getNoncachedStatValue(StringRef Path,
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000579 struct stat &StatBuf) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000580 SmallString<128> FilePath(Path);
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000581 FixupRelativePath(FilePath);
582
583 return ::stat(FilePath.c_str(), &StatBuf) != 0;
584}
585
Axel Naumann5ba05592012-07-10 16:50:27 +0000586void FileManager::invalidateCache(const FileEntry *Entry) {
587 assert(Entry && "Cannot invalidate a NULL FileEntry");
Axel Naumann3ce42c32012-06-27 09:17:42 +0000588
589 SeenFileEntries.erase(Entry->getName());
Axel Naumann5ba05592012-07-10 16:50:27 +0000590
591 // FileEntry invalidation should not block future optimizations in the file
592 // caches. Possible alternatives are cache truncation (invalidate last N) or
593 // invalidation of the whole cache.
594 UniqueRealFiles.erase(Entry);
Axel Naumann3ce42c32012-06-27 09:17:42 +0000595}
596
597
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000598void FileManager::GetUniqueIDMapping(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000599 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000600 UIDToFiles.clear();
601 UIDToFiles.resize(NextFileUID);
602
603 // Map file entries
604 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000605 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000606 FE != FEEnd; ++FE)
607 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
608 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
609
610 // Map virtual file entries
Chris Lattner5f9e2722011-07-23 10:55:15 +0000611 for (SmallVector<FileEntry*, 4>::const_iterator
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000612 VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
613 VFE != VFEEnd; ++VFE)
614 if (*VFE && *VFE != NON_EXISTENT_FILE)
615 UIDToFiles[(*VFE)->getUID()] = *VFE;
616}
Chris Lattner10e286a2010-11-23 19:19:34 +0000617
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +0000618void FileManager::modifyFileEntry(FileEntry *File,
619 off_t Size, time_t ModificationTime) {
620 File->Size = Size;
621 File->ModTime = ModificationTime;
622}
623
Chris Lattner10e286a2010-11-23 19:19:34 +0000624
Reid Spencer5f016e22007-07-11 17:01:13 +0000625void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000626 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000627 llvm::errs() << UniqueRealFiles.size() << " real files found, "
628 << UniqueRealDirs.size() << " real dirs found.\n";
629 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
630 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000631 llvm::errs() << NumDirLookups << " dir lookups, "
632 << NumDirCacheMisses << " dir cache misses.\n";
633 llvm::errs() << NumFileLookups << " file lookups, "
634 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000636 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000637}