blob: 6a0fbf80b905c57b1bc69a8248641d6a0e069c04 [file] [log] [blame]
Chris Lattner226efd32010-11-23 19:19:34 +00001//===--- FileManager.cpp - File System Probing and Caching ----------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +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 Lattner226efd32010-11-23 19:19:34 +000021#include "clang/Basic/FileSystemStatCache.h"
Chris Lattner2f4a89a2006-10-30 03:55:17 +000022#include "llvm/ADT/SmallString.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000023#include "llvm/Support/FileSystem.h"
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattner3441b4f2009-08-23 22:45:33 +000025#include "llvm/Support/raw_ostream.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000026#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000027#include "llvm/Support/system_error.h"
Dylan Noblesmith1ced7372011-12-22 22:49:47 +000028#include "llvm/Config/llvm-config.h"
Benjamin Kramer26db6482009-09-05 09:49:39 +000029#include <map>
30#include <set>
31#include <string>
Chris Lattner278038b2010-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
Chris Lattner22eb9722006-06-18 05:43:12 +000040using namespace clang;
41
42// FIXME: Enhance libsystem to support inode and other fields.
43#include <sys/stat.h>
44
Ted Kremenek8d71e252008-01-11 20:42:05 +000045/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Chris Lattneraf653752006-10-30 03:06:54 +000046/// represent a dir name that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +000047#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Chris Lattneraf653752006-10-30 03:06:54 +000048
Chris Lattner9624b692010-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 Dunbare2951f42012-11-05 22:53:33 +000060bool FileEntry::isNamedPipe() const {
61 return FileMode & S_IFIFO;
62}
63
Ted Kremenek5c04bd82009-01-28 00:27:31 +000064//===----------------------------------------------------------------------===//
65// Windows.
66//===----------------------------------------------------------------------===//
67
Ted Kremenekd87eef82008-02-24 03:15:25 +000068#ifdef LLVM_ON_WIN32
69
Ted Kremenekd87eef82008-02-24 03:15:25 +000070namespace {
Mike Stump11289f42009-09-09 15:08:12 +000071 static std::string GetFullPath(const char *relPath) {
Ted Kremenekd87eef82008-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 Stump11289f42009-09-09 15:08:12 +000085 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenekd87eef82008-02-24 03:15:25 +000086
87public:
Zhanyong Wane1dd3e22011-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 Kremenekd87eef82008-02-24 03:15:25 +000093 std::string FullPath(GetFullPath(Name));
Chris Lattner966b25b2010-11-23 20:30:42 +000094 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
Ted Kremenekd87eef82008-02-24 03:15:25 +000095 }
Mike Stump11289f42009-09-09 15:08:12 +000096
Chris Lattner966b25b2010-11-23 20:30:42 +000097 size_t size() const { return UniqueDirs.size(); }
Ted Kremenekd87eef82008-02-24 03:15:25 +000098};
99
100class FileManager::UniqueFileContainer {
101 /// UniqueFiles - Cache from full path to existing directories/files.
102 ///
Ted Kremenek1502b7e2009-01-28 01:01:07 +0000103 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000104
105public:
Zhanyong Wane1dd3e22011-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 Kremenekd87eef82008-02-24 03:15:25 +0000110 std::string FullPath(GetFullPath(Name));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000111
Benjamin Kramer44f91da2011-11-06 20:36:48 +0000112 // Lowercase string because Windows filesystem is case insensitive.
113 FullPath = StringRef(FullPath).lower();
Chris Lattner966b25b2010-11-23 20:30:42 +0000114 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
Ted Kremenekd87eef82008-02-24 03:15:25 +0000115 }
116
Chris Lattner966b25b2010-11-23 20:30:42 +0000117 size_t size() const { return UniqueFiles.size(); }
Axel Naumann38179d92012-06-27 09:17:42 +0000118
Axel Naumann23c1676d2012-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 Kremenekd87eef82008-02-24 03:15:25 +0000126};
127
Ted Kremenek5c04bd82009-01-28 00:27:31 +0000128//===----------------------------------------------------------------------===//
129// Unix-like Systems.
130//===----------------------------------------------------------------------===//
131
Ted Kremenekd87eef82008-02-24 03:15:25 +0000132#else
133
Ted Kremenekd87eef82008-02-24 03:15:25 +0000134class FileManager::UniqueDirContainer {
135 /// UniqueDirs - Cache from ID's to existing directories/files.
Mike Stump11289f42009-09-09 15:08:12 +0000136 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000137
138public:
Zhanyong Wane1dd3e22011-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 Kremenekd87eef82008-02-24 03:15:25 +0000144 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
145 }
146
Chris Lattner966b25b2010-11-23 20:30:42 +0000147 size_t size() const { return UniqueDirs.size(); }
Ted Kremenekd87eef82008-02-24 03:15:25 +0000148};
149
150class FileManager::UniqueFileContainer {
151 /// UniqueFiles - Cache from ID's to existing directories/files.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000152 std::set<FileEntry> UniqueFiles;
153
154public:
Zhanyong Wane1dd3e22011-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 Kremenekd87eef82008-02-24 03:15:25 +0000159 return
160 const_cast<FileEntry&>(
161 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000162 StatBuf.st_ino,
163 StatBuf.st_mode)).first);
Ted Kremenekd87eef82008-02-24 03:15:25 +0000164 }
165
Chris Lattner966b25b2010-11-23 20:30:42 +0000166 size_t size() const { return UniqueFiles.size(); }
Axel Naumann38179d92012-06-27 09:17:42 +0000167
Axel Naumannb3074002012-07-10 16:50:27 +0000168 void erase(const FileEntry *Entry) { UniqueFiles.erase(*Entry); }
Ted Kremenekd87eef82008-02-24 03:15:25 +0000169};
170
171#endif
172
Ted Kremenek5c04bd82009-01-28 00:27:31 +0000173//===----------------------------------------------------------------------===//
174// Common logic.
175//===----------------------------------------------------------------------===//
Ted Kremenekd87eef82008-02-24 03:15:25 +0000176
Chris Lattner3f5a9ef2010-11-23 07:51:02 +0000177FileManager::FileManager(const FileSystemOptions &FSO)
178 : FileSystemOpts(FSO),
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000179 UniqueRealDirs(*new UniqueDirContainer()),
180 UniqueRealFiles(*new UniqueFileContainer()),
181 SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
Ted Kremenekd87eef82008-02-24 03:15:25 +0000182 NumDirLookups = NumFileLookups = 0;
183 NumDirCacheMisses = NumFileCacheMisses = 0;
184}
185
186FileManager::~FileManager() {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000187 delete &UniqueRealDirs;
188 delete &UniqueRealFiles;
Chris Lattner966b25b2010-11-23 20:30:42 +0000189 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
190 delete VirtualFileEntries[i];
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000191 for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
192 delete VirtualDirectoryEntries[i];
Ted Kremenekd87eef82008-02-24 03:15:25 +0000193}
194
Chris Lattner226efd32010-11-23 19:19:34 +0000195void FileManager::addStatCache(FileSystemStatCache *statCache,
196 bool AtBeginning) {
Douglas Gregord2eb58a2009-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 Lattner226efd32010-11-23 19:19:34 +0000204 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000205 while (LastCache->getNextStatCache())
206 LastCache = LastCache->getNextStatCache();
207
208 LastCache->setNextStatCache(statCache);
209}
210
Chris Lattner226efd32010-11-23 19:19:34 +0000211void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregord2eb58a2009-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 Lattner226efd32010-11-23 19:19:34 +0000222 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000223 while (PrevCache && PrevCache->getNextStatCache() != statCache)
224 PrevCache = PrevCache->getNextStatCache();
Chris Lattner9624b692010-11-23 20:50:22 +0000225
226 assert(PrevCache && "Stat cache not found for removal");
227 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000228}
229
Manuel Klimek3aad8552012-07-31 13:56:54 +0000230void FileManager::clearStatCaches() {
231 StatCache.reset(0);
232}
233
Douglas Gregor407e2122009-12-02 18:12:28 +0000234/// \brief Retrieve the directory that the given file name resides in.
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000235/// Filename can point to either a real file or a virtual file.
Douglas Gregor407e2122009-12-02 18:12:28 +0000236static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000237 StringRef Filename,
238 bool CacheFailure) {
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000239 if (Filename.empty())
240 return NULL;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000241
Zhanyong Wanf3c0ff72011-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 Kramer3cf715d2010-11-21 11:32:22 +0000244
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000245 StringRef DirName = llvm::sys::path::parent_path(Filename);
Chris Lattner0c0e8042010-11-21 09:50:16 +0000246 // Use the current directory if file has no path component.
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000247 if (DirName.empty())
248 DirName = ".";
Douglas Gregor407e2122009-12-02 18:12:28 +0000249
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000250 return FileMgr.getDirectory(DirName, CacheFailure);
Douglas Gregor407e2122009-12-02 18:12:28 +0000251}
252
Zhanyong Wane1dd3e22011-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 Lattner0e62c1c2011-07-23 10:55:15 +0000255void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
256 StringRef DirName = llvm::sys::path::parent_path(Path);
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000257 if (DirName.empty())
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000258 return;
259
Zhanyong Wane1dd3e22011-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 Gregor1735f4e2011-09-13 23:15:45 +0000280const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
281 bool CacheFailure) {
NAKAMURA Takumi8bd8ee72012-06-16 06:04:10 +0000282 // stat doesn't like trailing separators except for root directory.
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000283 // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
284 // (though it can strip '\\')
NAKAMURA Takumi8bd8ee72012-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 Takumi32f1acf2011-11-17 06:16:05 +0000288 DirName = DirName.substr(0, DirName.size()-1);
289
Chris Lattner22eb9722006-06-18 05:43:12 +0000290 ++NumDirLookups;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000291 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000292 SeenDirEntries.GetOrCreateValue(DirName);
Mike Stump11289f42009-09-09 15:08:12 +0000293
Zhanyong Wane1dd3e22011-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.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000296 if (NamedDirEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000297 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000298 ? 0 : NamedDirEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000299
Chris Lattner22eb9722006-06-18 05:43:12 +0000300 ++NumDirCacheMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattneraf653752006-10-30 03:06:54 +0000302 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000303 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Chris Lattner43fd42e2006-10-30 03:40:58 +0000305 // Get the null-terminated directory name as stored as the key of the
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000306 // SeenDirEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000307 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattneraf653752006-10-30 03:06:54 +0000309 // Check to see if the directory exists.
Chris Lattner22eb9722006-06-18 05:43:12 +0000310 struct stat StatBuf;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000311 if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/)) {
312 // There's no real directory at the given path.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000313 if (!CacheFailure)
314 SeenDirEntries.erase(DirName);
Chris Lattner22eb9722006-06-18 05:43:12 +0000315 return 0;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000316 }
Ted Kremenekd87eef82008-02-24 03:15:25 +0000317
Zhanyong Wane1dd3e22011-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 Stump11289f42009-09-09 15:08:12 +0000323
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000324 NamedDirEnt.setValue(&UDE);
Zhanyong Wane1dd3e22011-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 Stump11289f42009-09-09 15:08:12 +0000330
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000331 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000332}
333
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000334const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
335 bool CacheFailure) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000336 ++NumFileLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000337
Chris Lattner22eb9722006-06-18 05:43:12 +0000338 // See if there is already an entry in the map.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000339 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000340 SeenFileEntries.GetOrCreateValue(Filename);
Chris Lattner22eb9722006-06-18 05:43:12 +0000341
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000342 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000343 if (NamedFileEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000344 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000345 ? 0 : NamedFileEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattner22eb9722006-06-18 05:43:12 +0000347 ++NumFileCacheMisses;
348
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000349 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000350 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Chris Lattner22eb9722006-06-18 05:43:12 +0000351
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000352 // Get the null-terminated file name as stored as the key of the
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000353 // SeenFileEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000354 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000355
Chris Lattner966b25b2010-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 Gregor1735f4e2011-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 Gregor407e2122009-12-02 18:12:28 +0000367 return 0;
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000368 }
369
Chris Lattner22eb9722006-06-18 05:43:12 +0000370 // FIXME: Use the directory info to prune this, before doing the stat syscall.
371 // FIXME: This will reduce the # syscalls.
Mike Stump11289f42009-09-09 15:08:12 +0000372
Chris Lattner22eb9722006-06-18 05:43:12 +0000373 // Nope, there isn't. Check to see if the file exists.
Chris Lattnerdd278432010-11-23 21:17:56 +0000374 int FileDescriptor = -1;
Chris Lattner22eb9722006-06-18 05:43:12 +0000375 struct stat StatBuf;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000376 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) {
377 // There's no real file at the given path.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000378 if (!CacheFailure)
379 SeenFileEntries.erase(Filename);
380
Chris Lattner22eb9722006-06-18 05:43:12 +0000381 return 0;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000382 }
Mike Stump11289f42009-09-09 15:08:12 +0000383
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000384 if (FileDescriptor != -1 && !openFile) {
385 close(FileDescriptor);
386 FileDescriptor = -1;
387 }
388
Ted Kremenekf4c38c92007-12-18 22:29:39 +0000389 // It exists. See if we have already opened a file with the same inode.
Chris Lattner22eb9722006-06-18 05:43:12 +0000390 // This occurs when one dir is symlinked to another, for example.
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000391 FileEntry &UFE = UniqueRealFiles.getFile(InterndFileName, StatBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000392
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000393 NamedFileEnt.setValue(&UFE);
Chris Lattnerdd278432010-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 Wane1dd3e22011-02-11 18:44:49 +0000398
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000399 return &UFE;
Chris Lattnerdd278432010-11-23 21:17:56 +0000400 }
Chris Lattner269c2322006-06-25 06:23:00 +0000401
Chris Lattner22eb9722006-06-18 05:43:12 +0000402 // Otherwise, we don't have this directory yet, add it.
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000403 // FIXME: Change the name to be a char* that points back to the
404 // 'SeenFileEntries' key.
Chris Lattner2f4a89a2006-10-30 03:55:17 +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 Lattnerdd278432010-11-23 21:17:56 +0000410 UFE.FD = FileDescriptor;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000411 return &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000412}
413
Douglas Gregor407e2122009-12-02 18:12:28 +0000414const FileEntry *
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000415FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner5159f612010-11-23 08:35:12 +0000416 time_t ModificationTime) {
Douglas Gregor407e2122009-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 Wane1dd3e22011-02-11 18:44:49 +0000421 SeenFileEntries.GetOrCreateValue(Filename);
Douglas Gregor407e2122009-12-02 18:12:28 +0000422
423 // See if there is already an entry in the map.
Axel Naumann63fbaed2011-01-27 10:55:51 +0000424 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
425 return NamedFileEnt.getValue();
Douglas Gregor407e2122009-12-02 18:12:28 +0000426
427 ++NumFileCacheMisses;
428
429 // By default, initialize it to invalid.
430 NamedFileEnt.setValue(NON_EXISTENT_FILE);
431
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000432 addAncestorsAsVirtualDirs(Filename);
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000433 FileEntry *UFE = 0;
Zhanyong Wane1dd3e22011-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 Gregor1735f4e2011-09-13 23:15:45 +0000438 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
439 /*CacheFailure=*/true);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000440 assert(DirInfo &&
441 "The directory of a virtual file should already be in the cache.");
Douglas Gregor407e2122009-12-02 18:12:28 +0000442
Zhanyong Wane1dd3e22011-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 Gregor606c4ac2011-02-05 19:42:43 +0000451
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000452 StatBuf.st_size = Size;
453 StatBuf.st_mtime = ModificationTime;
454 UFE = &UniqueRealFiles.getFile(InterndFileName, StatBuf);
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000455
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000456 NamedFileEnt.setValue(UFE);
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000457
Zhanyong Wane1dd3e22011-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 Gregor606c4ac2011-02-05 19:42:43 +0000464 }
Zhanyong Wane1dd3e22011-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 Gregor606c4ac2011-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 Gregor407e2122009-12-02 18:12:28 +0000476
Chris Lattner9624b692010-11-23 20:50:22 +0000477 UFE->Name = InterndFileName;
Douglas Gregor407e2122009-12-02 18:12:28 +0000478 UFE->Size = Size;
479 UFE->ModTime = ModificationTime;
480 UFE->Dir = DirInfo;
481 UFE->UID = NextFileUID++;
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000482 UFE->FD = -1;
Douglas Gregor407e2122009-12-02 18:12:28 +0000483 return UFE;
484}
485
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000486void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
487 StringRef pathRef(path.data(), path.size());
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000488
Anders Carlsson9ba8fb12011-03-14 01:13:54 +0000489 if (FileSystemOpts.WorkingDir.empty()
490 || llvm::sys::path::is_absolute(pathRef))
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000491 return;
492
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000493 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000494 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner6e640992010-11-23 04:45:28 +0000495 path = NewPath;
496}
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000497
Chris Lattner6e640992010-11-23 04:45:28 +0000498llvm::MemoryBuffer *FileManager::
Argyrios Kyrtzidis6d7833f2012-07-11 20:59:04 +0000499getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
500 bool isVolatile) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000501 OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000502 llvm::error_code ec;
Argyrios Kyrtzidis669b0b12011-03-15 00:47:44 +0000503
Argyrios Kyrtzidis6d7833f2012-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 Kyrtzidis669b0b12011-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 Kyrtzidis6d7833f2012-07-11 20:59:04 +0000513 ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result, FileSize);
Argyrios Kyrtzidis669b0b12011-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 Lattner5ea7d072010-11-23 22:32:37 +0000524 if (FileSystemOpts.WorkingDir.empty()) {
Argyrios Kyrtzidis6d7833f2012-07-11 20:59:04 +0000525 ec = llvm::MemoryBuffer::getFile(Filename, Result, FileSize);
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000526 if (ec && ErrorStr)
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000527 *ErrorStr = ec.message();
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000528 return Result.take();
Chris Lattner5ea7d072010-11-23 22:32:37 +0000529 }
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000530
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000531 SmallString<128> FilePath(Entry->getName());
Anders Carlsson878b3e22011-03-07 01:28:33 +0000532 FixupRelativePath(FilePath);
Argyrios Kyrtzidis6d7833f2012-07-11 20:59:04 +0000533 ec = llvm::MemoryBuffer::getFile(FilePath.str(), Result, FileSize);
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000534 if (ec && ErrorStr)
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000535 *ErrorStr = ec.message();
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000536 return Result.take();
Chris Lattner26b5c192010-11-23 09:19:42 +0000537}
538
539llvm::MemoryBuffer *FileManager::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000540getBufferForFile(StringRef Filename, std::string *ErrorStr) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000541 OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000542 llvm::error_code ec;
543 if (FileSystemOpts.WorkingDir.empty()) {
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000544 ec = llvm::MemoryBuffer::getFile(Filename, Result);
545 if (ec && ErrorStr)
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000546 *ErrorStr = ec.message();
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000547 return Result.take();
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000548 }
549
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000550 SmallString<128> FilePath(Filename);
Anders Carlsson878b3e22011-03-07 01:28:33 +0000551 FixupRelativePath(FilePath);
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000552 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
553 if (ec && ErrorStr)
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000554 *ErrorStr = ec.message();
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000555 return Result.take();
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000556}
557
Zhanyong Wane1dd3e22011-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 Lattner9624b692010-11-23 20:50:22 +0000563bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
Chris Lattnerdd278432010-11-23 21:17:56 +0000564 int *FileDescriptor) {
Chris Lattner226efd32010-11-23 19:19:34 +0000565 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
566 // absolute!
Chris Lattner5769c3d2010-11-23 19:56:39 +0000567 if (FileSystemOpts.WorkingDir.empty())
Chris Lattnerdd278432010-11-23 21:17:56 +0000568 return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
569 StatCache.get());
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000570
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000571 SmallString<128> FilePath(Path);
Anders Carlsson878b3e22011-03-07 01:28:33 +0000572 FixupRelativePath(FilePath);
Chris Lattner5769c3d2010-11-23 19:56:39 +0000573
Chris Lattnerdd278432010-11-23 21:17:56 +0000574 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
575 StatCache.get());
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000576}
577
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000578bool FileManager::getNoncachedStatValue(StringRef Path,
Anders Carlsson5e368402011-03-18 19:23:19 +0000579 struct stat &StatBuf) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000580 SmallString<128> FilePath(Path);
Anders Carlsson5e368402011-03-18 19:23:19 +0000581 FixupRelativePath(FilePath);
582
583 return ::stat(FilePath.c_str(), &StatBuf) != 0;
584}
585
Axel Naumannb3074002012-07-10 16:50:27 +0000586void FileManager::invalidateCache(const FileEntry *Entry) {
587 assert(Entry && "Cannot invalidate a NULL FileEntry");
Axel Naumann38179d92012-06-27 09:17:42 +0000588
589 SeenFileEntries.erase(Entry->getName());
Axel Naumannb3074002012-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 Naumann38179d92012-06-27 09:17:42 +0000595}
596
597
Douglas Gregor09b69892011-02-10 17:09:37 +0000598void FileManager::GetUniqueIDMapping(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000599 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregor09b69892011-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 Wane1dd3e22011-02-11 18:44:49 +0000605 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregor09b69892011-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 Lattner0e62c1c2011-07-23 10:55:15 +0000611 for (SmallVector<FileEntry*, 4>::const_iterator
Douglas Gregor09b69892011-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 Lattner226efd32010-11-23 19:19:34 +0000617
Argyrios Kyrtzidis6eec06d2012-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 Lattner226efd32010-11-23 19:19:34 +0000624
Chris Lattner22eb9722006-06-18 05:43:12 +0000625void FileManager::PrintStats() const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000626 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wane1dd3e22011-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 Kramer89b422c2009-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 Stump11289f42009-09-09 15:08:12 +0000635
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000636 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Chris Lattner22eb9722006-06-18 05:43:12 +0000637}