blob: 22beed7dccef0c8b86c5c82b6641b6bc38bf44ae [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "llvm/Config/llvm-config.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000024#include "llvm/Support/FileSystem.h"
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000025#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000026#include "llvm/Support/Path.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000027#include "llvm/Support/raw_ostream.h"
Benjamin Kramer458fb102009-09-05 09:49:39 +000028#include <map>
29#include <set>
30#include <string>
Stephen Hinesc568f1e2014-07-21 00:47:37 -070031#include <system_error>
Chris Lattner291fcf02010-11-23 21:53:15 +000032
Reid Spencer5f016e22007-07-11 17:01:13 +000033using namespace clang;
34
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000035/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000036/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000037#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000038
Chris Lattnerf9f77662010-11-23 20:50:22 +000039/// NON_EXISTENT_FILE - A special value distinct from null that is used to
40/// represent a filename that doesn't exist on the disk.
41#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
42
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000043//===----------------------------------------------------------------------===//
44// Common logic.
45//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +000046
Stephen Hines651f13c2014-04-23 16:59:28 -070047FileManager::FileManager(const FileSystemOptions &FSO,
48 IntrusiveRefCntPtr<vfs::FileSystem> FS)
49 : FS(FS), FileSystemOpts(FSO),
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000050 SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000051 NumDirLookups = NumFileLookups = 0;
52 NumDirCacheMisses = NumFileCacheMisses = 0;
Stephen Hines651f13c2014-04-23 16:59:28 -070053
54 // If the caller doesn't provide a virtual file system, just grab the real
55 // file system.
56 if (!FS)
57 this->FS = vfs::getRealFileSystem();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000058}
59
60FileManager::~FileManager() {
Chris Lattnerf3e8a992010-11-23 20:30:42 +000061 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
62 delete VirtualFileEntries[i];
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000063 for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
64 delete VirtualDirectoryEntries[i];
Ted Kremenek6bb816a2008-02-24 03:15:25 +000065}
66
Chris Lattner10e286a2010-11-23 19:19:34 +000067void FileManager::addStatCache(FileSystemStatCache *statCache,
68 bool AtBeginning) {
Douglas Gregor52e71082009-10-16 18:18:30 +000069 assert(statCache && "No stat cache provided?");
Stephen Hines6bcf27b2014-05-29 04:14:42 -070070 if (AtBeginning || !StatCache.get()) {
Stephen Hines651f13c2014-04-23 16:59:28 -070071 statCache->setNextStatCache(StatCache.release());
Douglas Gregor52e71082009-10-16 18:18:30 +000072 StatCache.reset(statCache);
73 return;
74 }
75
Chris Lattner10e286a2010-11-23 19:19:34 +000076 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +000077 while (LastCache->getNextStatCache())
78 LastCache = LastCache->getNextStatCache();
79
80 LastCache->setNextStatCache(statCache);
81}
82
Chris Lattner10e286a2010-11-23 19:19:34 +000083void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregor52e71082009-10-16 18:18:30 +000084 if (!statCache)
85 return;
86
87 if (StatCache.get() == statCache) {
88 // This is the first stat cache.
89 StatCache.reset(StatCache->takeNextStatCache());
90 return;
91 }
92
93 // Find the stat cache in the list.
Chris Lattner10e286a2010-11-23 19:19:34 +000094 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +000095 while (PrevCache && PrevCache->getNextStatCache() != statCache)
96 PrevCache = PrevCache->getNextStatCache();
Chris Lattnerf9f77662010-11-23 20:50:22 +000097
98 assert(PrevCache && "Stat cache not found for removal");
99 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregor52e71082009-10-16 18:18:30 +0000100}
101
Manuel Klimek98be8602012-07-31 13:56:54 +0000102void FileManager::clearStatCaches() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700103 StatCache.reset(nullptr);
Manuel Klimek98be8602012-07-31 13:56:54 +0000104}
105
Douglas Gregor057e5672009-12-02 18:12:28 +0000106/// \brief Retrieve the directory that the given file name resides in.
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000107/// Filename can point to either a real file or a virtual file.
Douglas Gregor057e5672009-12-02 18:12:28 +0000108static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Douglas Gregor6e975c42011-09-13 23:15:45 +0000109 StringRef Filename,
110 bool CacheFailure) {
Zhanyong Wan21af8872011-02-11 21:25:35 +0000111 if (Filename.empty())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700112 return nullptr;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000113
Zhanyong Wan21af8872011-02-11 21:25:35 +0000114 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700115 return nullptr; // If Filename is a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000116
Chris Lattner5f9e2722011-07-23 10:55:15 +0000117 StringRef DirName = llvm::sys::path::parent_path(Filename);
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000118 // Use the current directory if file has no path component.
Zhanyong Wan21af8872011-02-11 21:25:35 +0000119 if (DirName.empty())
120 DirName = ".";
Douglas Gregor057e5672009-12-02 18:12:28 +0000121
Douglas Gregor6e975c42011-09-13 23:15:45 +0000122 return FileMgr.getDirectory(DirName, CacheFailure);
Douglas Gregor057e5672009-12-02 18:12:28 +0000123}
124
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000125/// Add all ancestors of the given path (pointing to either a file or
126/// a directory) as virtual directories.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
128 StringRef DirName = llvm::sys::path::parent_path(Path);
Zhanyong Wan21af8872011-02-11 21:25:35 +0000129 if (DirName.empty())
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000130 return;
131
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000132 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
133 SeenDirEntries.GetOrCreateValue(DirName);
134
135 // When caching a virtual directory, we always cache its ancestors
136 // at the same time. Therefore, if DirName is already in the cache,
137 // we don't need to recurse as its ancestors must also already be in
138 // the cache.
139 if (NamedDirEnt.getValue())
140 return;
141
142 // Add the virtual directory to the cache.
143 DirectoryEntry *UDE = new DirectoryEntry;
144 UDE->Name = NamedDirEnt.getKeyData();
145 NamedDirEnt.setValue(UDE);
146 VirtualDirectoryEntries.push_back(UDE);
147
148 // Recursively add the other ancestors.
149 addAncestorsAsVirtualDirs(DirName);
150}
151
Douglas Gregor6e975c42011-09-13 23:15:45 +0000152const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
153 bool CacheFailure) {
NAKAMURA Takumi759a4b42012-06-16 06:04:10 +0000154 // stat doesn't like trailing separators except for root directory.
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000155 // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
156 // (though it can strip '\\')
NAKAMURA Takumi759a4b42012-06-16 06:04:10 +0000157 if (DirName.size() > 1 &&
158 DirName != llvm::sys::path::root_path(DirName) &&
159 llvm::sys::path::is_separator(DirName.back()))
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000160 DirName = DirName.substr(0, DirName.size()-1);
Rafael Espindola146d57f2013-07-29 15:47:24 +0000161#ifdef LLVM_ON_WIN32
162 // Fixing a problem with "clang C:test.c" on Windows.
163 // Stat("C:") does not recognize "C:" as a valid directory
164 std::string DirNameStr;
165 if (DirName.size() > 1 && DirName.back() == ':' &&
166 DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
167 DirNameStr = DirName.str() + '.';
168 DirName = DirNameStr;
169 }
170#endif
NAKAMURA Takumi678a3ea2011-11-17 06:16:05 +0000171
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 ++NumDirLookups;
173 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000174 SeenDirEntries.GetOrCreateValue(DirName);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000176 // See if there was already an entry in the map. Note that the map
177 // contains both virtual and real directories.
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 if (NamedDirEnt.getValue())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700179 return NamedDirEnt.getValue() == NON_EXISTENT_DIR ? nullptr
180 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000185 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // Get the null-terminated directory name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000188 // SeenDirEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 // Check to see if the directory exists.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000192 FileData Data;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700193 if (getStatValue(InterndDirName, Data, false, nullptr /*directory lookup*/)) {
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000194 // There's no real directory at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000195 if (!CacheFailure)
196 SeenDirEntries.erase(DirName);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700197 return nullptr;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000198 }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000199
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000200 // It exists. See if we have already opened a directory with the
201 // same inode (this occurs on Unix-like systems when one dir is
202 // symlinked to another, for example) or the same path (on
203 // Windows).
Stephen Hines651f13c2014-04-23 16:59:28 -0700204 DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID];
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 NamedDirEnt.setValue(&UDE);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000207 if (!UDE.getName()) {
208 // We don't have this directory yet, add it. We use the string
209 // key from the SeenDirEntries map as the string.
210 UDE.Name = InterndDirName;
211 }
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 return &UDE;
214}
215
Douglas Gregor6e975c42011-09-13 23:15:45 +0000216const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
217 bool CacheFailure) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 // See if there is already an entry in the map.
221 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000222 SeenFileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000223
224 // See if there is already an entry in the map.
225 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000226 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700227 ? nullptr : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 ++NumFileCacheMisses;
230
231 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000232 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 // Get the null-terminated file name as stored as the key of the
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000235 // SeenFileEntries map.
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000238 // Look up the directory for the file. When looking up something like
239 // sys/foo.h we'll discover all of the search directories that have a 'sys'
240 // subdirectory. This will let us avoid having to waste time on known-to-fail
241 // searches when we go to find sys/bar.h, because all the search directories
242 // without a 'sys' subdir will get a cached failure result.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000243 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
244 CacheFailure);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700245 if (DirInfo == nullptr) { // Directory doesn't exist, file can't exist.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000246 if (!CacheFailure)
247 SeenFileEntries.erase(Filename);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700248
249 return nullptr;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000250 }
251
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 // FIXME: Use the directory info to prune this, before doing the stat syscall.
253 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // Nope, there isn't. Check to see if the file exists.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700256 std::unique_ptr<vfs::File> F;
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000257 FileData Data;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700258 if (getStatValue(InterndFileName, Data, true, openFile ? &F : nullptr)) {
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000259 // There's no real file at the given path.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000260 if (!CacheFailure)
261 SeenFileEntries.erase(Filename);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700262
263 return nullptr;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Stephen Hines651f13c2014-04-23 16:59:28 -0700266 assert((openFile || !F) && "undesired open file");
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000267
Ted Kremenekbca6d122007-12-18 22:29:39 +0000268 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // This occurs when one dir is symlinked to another, for example.
Stephen Hines651f13c2014-04-23 16:59:28 -0700270 FileEntry &UFE = UniqueRealFiles[Data.UniqueID];
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 NamedFileEnt.setValue(&UFE);
Stephen Hines651f13c2014-04-23 16:59:28 -0700273 if (UFE.isValid()) { // Already have an entry with this inode, return it.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700274
275 // FIXME: this hack ensures that if we look up a file by a virtual path in
276 // the VFS that the getDir() will have the virtual path, even if we found
277 // the file by a 'real' path first. This is required in order to find a
278 // module's structure when its headers/module map are mapped in the VFS.
279 // We should remove this as soon as we can properly support a file having
280 // multiple names.
281 if (DirInfo != UFE.Dir && Data.IsVFSMapped)
282 UFE.Dir = DirInfo;
283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 return &UFE;
Chris Lattner898a0612010-11-23 21:17:56 +0000285 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000286
Stephen Hines651f13c2014-04-23 16:59:28 -0700287 // Otherwise, we don't have this file yet, add it.
288 UFE.Name = Data.Name;
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000289 UFE.Size = Data.Size;
290 UFE.ModTime = Data.ModTime;
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 UFE.Dir = DirInfo;
292 UFE.UID = NextFileUID++;
Stephen Hines651f13c2014-04-23 16:59:28 -0700293 UFE.UniqueID = Data.UniqueID;
294 UFE.IsNamedPipe = Data.IsNamedPipe;
295 UFE.InPCH = Data.InPCH;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700296 UFE.File = std::move(F);
Stephen Hines651f13c2014-04-23 16:59:28 -0700297 UFE.IsValid = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 return &UFE;
299}
300
Douglas Gregor057e5672009-12-02 18:12:28 +0000301const FileEntry *
Chris Lattner5f9e2722011-07-23 10:55:15 +0000302FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000303 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000304 ++NumFileLookups;
305
306 // See if there is already an entry in the map.
307 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000308 SeenFileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000309
310 // See if there is already an entry in the map.
Axel Naumann04331162011-01-27 10:55:51 +0000311 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
312 return NamedFileEnt.getValue();
Douglas Gregor057e5672009-12-02 18:12:28 +0000313
314 ++NumFileCacheMisses;
315
316 // By default, initialize it to invalid.
317 NamedFileEnt.setValue(NON_EXISTENT_FILE);
318
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000319 addAncestorsAsVirtualDirs(Filename);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700320 FileEntry *UFE = nullptr;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000321
322 // Now that all ancestors of Filename are in the cache, the
323 // following call is guaranteed to find the DirectoryEntry from the
324 // cache.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000325 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
326 /*CacheFailure=*/true);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000327 assert(DirInfo &&
328 "The directory of a virtual file should already be in the cache.");
Douglas Gregor057e5672009-12-02 18:12:28 +0000329
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000330 // Check to see if the file exists. If so, drop the virtual file
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000331 FileData Data;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000332 const char *InterndFileName = NamedFileEnt.getKeyData();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700333 if (getStatValue(InterndFileName, Data, true, nullptr) == 0) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000334 Data.Size = Size;
335 Data.ModTime = ModificationTime;
Stephen Hines651f13c2014-04-23 16:59:28 -0700336 UFE = &UniqueRealFiles[Data.UniqueID];
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000337
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000338 NamedFileEnt.setValue(UFE);
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000339
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000340 // If we had already opened this file, close it now so we don't
341 // leak the descriptor. We're not going to use the file
342 // descriptor anyway, since this is a virtual file.
Stephen Hines651f13c2014-04-23 16:59:28 -0700343 if (UFE->File)
344 UFE->closeFile();
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000345
346 // If we already have an entry with this inode, return it.
Stephen Hines651f13c2014-04-23 16:59:28 -0700347 if (UFE->isValid())
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000348 return UFE;
Stephen Hines651f13c2014-04-23 16:59:28 -0700349
350 UFE->UniqueID = Data.UniqueID;
351 UFE->IsNamedPipe = Data.IsNamedPipe;
352 UFE->InPCH = Data.InPCH;
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000353 }
354
355 if (!UFE) {
356 UFE = new FileEntry();
357 VirtualFileEntries.push_back(UFE);
358 NamedFileEnt.setValue(UFE);
359 }
Douglas Gregor057e5672009-12-02 18:12:28 +0000360
Chris Lattnerf9f77662010-11-23 20:50:22 +0000361 UFE->Name = InterndFileName;
Douglas Gregor057e5672009-12-02 18:12:28 +0000362 UFE->Size = Size;
363 UFE->ModTime = ModificationTime;
364 UFE->Dir = DirInfo;
365 UFE->UID = NextFileUID++;
Stephen Hines651f13c2014-04-23 16:59:28 -0700366 UFE->File.reset();
Douglas Gregor057e5672009-12-02 18:12:28 +0000367 return UFE;
368}
369
Chris Lattner5f9e2722011-07-23 10:55:15 +0000370void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
371 StringRef pathRef(path.data(), path.size());
Anders Carlssonaf036a62011-03-06 22:25:35 +0000372
Anders Carlsson2e2468e2011-03-14 01:13:54 +0000373 if (FileSystemOpts.WorkingDir.empty()
374 || llvm::sys::path::is_absolute(pathRef))
Michael J. Spencer256053b2010-12-17 21:22:22 +0000375 return;
376
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000377 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonaf036a62011-03-06 22:25:35 +0000378 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner67452f52010-11-23 04:45:28 +0000379 path = NewPath;
380}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000381
Chris Lattner67452f52010-11-23 04:45:28 +0000382llvm::MemoryBuffer *FileManager::
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000383getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700384 bool isVolatile, bool ShouldCloseOpenFile) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700385 std::unique_ptr<llvm::MemoryBuffer> Result;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700386 std::error_code ec;
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000387
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000388 uint64_t FileSize = Entry->getSize();
389 // If there's a high enough chance that the file have changed since we
390 // got its size, force a stat before opening it.
391 if (isVolatile)
392 FileSize = -1;
393
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000394 const char *Filename = Entry->getName();
395 // If the file is already open, use the open file descriptor.
Stephen Hines651f13c2014-04-23 16:59:28 -0700396 if (Entry->File) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700397 ec = Entry->File->getBuffer(Filename, Result, FileSize,
398 /*RequiresNullTerminator=*/true, isVolatile);
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000399 if (ErrorStr)
400 *ErrorStr = ec.message();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700401 // FIXME: we need a set of APIs that can make guarantees about whether a
402 // FileEntry is open or not.
403 if (ShouldCloseOpenFile)
404 Entry->closeFile();
Stephen Hines651f13c2014-04-23 16:59:28 -0700405 return Result.release();
Argyrios Kyrtzidisa8d530e2011-03-15 00:47:44 +0000406 }
407
408 // Otherwise, open the file.
409
Chris Lattner5cc1c732010-11-23 22:32:37 +0000410 if (FileSystemOpts.WorkingDir.empty()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700411 ec = FS->getBufferForFile(Filename, Result, FileSize,
412 /*RequiresNullTerminator=*/true, isVolatile);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000413 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000414 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700415 return Result.release();
Chris Lattner5cc1c732010-11-23 22:32:37 +0000416 }
Anders Carlssonaf036a62011-03-06 22:25:35 +0000417
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000418 SmallString<128> FilePath(Entry->getName());
Anders Carlsson03fd3622011-03-07 01:28:33 +0000419 FixupRelativePath(FilePath);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700420 ec = FS->getBufferForFile(FilePath.str(), Result, FileSize,
421 /*RequiresNullTerminator=*/true, isVolatile);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000422 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000423 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700424 return Result.release();
Chris Lattner75dfb652010-11-23 09:19:42 +0000425}
426
427llvm::MemoryBuffer *FileManager::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000428getBufferForFile(StringRef Filename, std::string *ErrorStr) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700429 std::unique_ptr<llvm::MemoryBuffer> Result;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700430 std::error_code ec;
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000431 if (FileSystemOpts.WorkingDir.empty()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700432 ec = FS->getBufferForFile(Filename, Result);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000433 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000434 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700435 return Result.release();
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000436 }
437
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000438 SmallString<128> FilePath(Filename);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000439 FixupRelativePath(FilePath);
Stephen Hines651f13c2014-04-23 16:59:28 -0700440 ec = FS->getBufferForFile(FilePath.c_str(), Result);
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000441 if (ec && ErrorStr)
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000442 *ErrorStr = ec.message();
Stephen Hines651f13c2014-04-23 16:59:28 -0700443 return Result.release();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000444}
445
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000446/// getStatValue - Get the 'stat' information for the specified path,
447/// using the cache to accelerate it if possible. This returns true
448/// if the path points to a virtual file or does not exist, or returns
449/// false if it's an existent real file. If FileDescriptor is NULL,
450/// do directory look-up instead of file look-up.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000451bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700452 std::unique_ptr<vfs::File> *F) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000453 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
454 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000455 if (FileSystemOpts.WorkingDir.empty())
Stephen Hines651f13c2014-04-23 16:59:28 -0700456 return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS);
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000457
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000458 SmallString<128> FilePath(Path);
Anders Carlsson03fd3622011-03-07 01:28:33 +0000459 FixupRelativePath(FilePath);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000460
Stephen Hines651f13c2014-04-23 16:59:28 -0700461 return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F,
462 StatCache.get(), *FS);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000463}
464
Rafael Espindolaaefb1d32013-07-29 18:22:23 +0000465bool FileManager::getNoncachedStatValue(StringRef Path,
Stephen Hines651f13c2014-04-23 16:59:28 -0700466 vfs::Status &Result) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000467 SmallString<128> FilePath(Path);
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000468 FixupRelativePath(FilePath);
469
Stephen Hines651f13c2014-04-23 16:59:28 -0700470 llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str());
471 if (!S)
472 return true;
473 Result = *S;
474 return false;
Anders Carlsson7dbafb32011-03-18 19:23:19 +0000475}
476
Axel Naumann5ba05592012-07-10 16:50:27 +0000477void FileManager::invalidateCache(const FileEntry *Entry) {
478 assert(Entry && "Cannot invalidate a NULL FileEntry");
Axel Naumann3ce42c32012-06-27 09:17:42 +0000479
480 SeenFileEntries.erase(Entry->getName());
Axel Naumann5ba05592012-07-10 16:50:27 +0000481
482 // FileEntry invalidation should not block future optimizations in the file
483 // caches. Possible alternatives are cache truncation (invalidate last N) or
484 // invalidation of the whole cache.
Stephen Hines651f13c2014-04-23 16:59:28 -0700485 UniqueRealFiles.erase(Entry->getUniqueID());
Axel Naumann3ce42c32012-06-27 09:17:42 +0000486}
487
488
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000489void FileManager::GetUniqueIDMapping(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000490 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000491 UIDToFiles.clear();
492 UIDToFiles.resize(NextFileUID);
493
494 // Map file entries
495 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000496 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000497 FE != FEEnd; ++FE)
498 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
499 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
500
501 // Map virtual file entries
Craig Topper09d19ef2013-07-04 03:08:24 +0000502 for (SmallVectorImpl<FileEntry *>::const_iterator
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000503 VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
504 VFE != VFEEnd; ++VFE)
505 if (*VFE && *VFE != NON_EXISTENT_FILE)
506 UIDToFiles[(*VFE)->getUID()] = *VFE;
507}
Chris Lattner10e286a2010-11-23 19:19:34 +0000508
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +0000509void FileManager::modifyFileEntry(FileEntry *File,
510 off_t Size, time_t ModificationTime) {
511 File->Size = Size;
512 File->ModTime = ModificationTime;
513}
514
Douglas Gregor713b7c02013-01-26 00:55:12 +0000515StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
516 // FIXME: use llvm::sys::fs::canonical() when it gets implemented
517#ifdef LLVM_ON_UNIX
518 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
519 = CanonicalDirNames.find(Dir);
520 if (Known != CanonicalDirNames.end())
521 return Known->second;
522
523 StringRef CanonicalName(Dir->getName());
524 char CanonicalNameBuf[PATH_MAX];
525 if (realpath(Dir->getName(), CanonicalNameBuf)) {
526 unsigned Len = strlen(CanonicalNameBuf);
527 char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1));
528 memcpy(Mem, CanonicalNameBuf, Len);
529 CanonicalName = StringRef(Mem, Len);
530 }
531
532 CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
533 return CanonicalName;
534#else
535 return StringRef(Dir->getName());
536#endif
537}
Chris Lattner10e286a2010-11-23 19:19:34 +0000538
Reid Spencer5f016e22007-07-11 17:01:13 +0000539void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000540 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000541 llvm::errs() << UniqueRealFiles.size() << " real files found, "
542 << UniqueRealDirs.size() << " real dirs found.\n";
543 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
544 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000545 llvm::errs() << NumDirLookups << " dir lookups, "
546 << NumDirCacheMisses << " dir cache misses.\n";
547 llvm::errs() << NumFileLookups << " file lookups, "
548 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000550 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000551}