blob: ba016db011ee63cf2c56fa525090d0d893ce8fc2 [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Config/llvm-config.h"
David Blaikied2725a32015-12-09 17:23:13 +000024#include "llvm/ADT/STLExtras.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000025#include "llvm/Support/FileSystem.h"
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000026#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000027#include "llvm/Support/Path.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "llvm/Support/raw_ostream.h"
Benjamin Kramer26db6482009-09-05 09:49:39 +000029#include <map>
30#include <set>
31#include <string>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000032#include <system_error>
Chris Lattner278038b2010-11-23 21:53:15 +000033
Chris Lattner22eb9722006-06-18 05:43:12 +000034using namespace clang;
35
Ted Kremenek8d71e252008-01-11 20:42:05 +000036/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Chris Lattneraf653752006-10-30 03:06:54 +000037/// represent a dir name that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +000038#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Chris Lattneraf653752006-10-30 03:06:54 +000039
Chris Lattner9624b692010-11-23 20:50:22 +000040/// NON_EXISTENT_FILE - A special value distinct from null that is used to
41/// represent a filename that doesn't exist on the disk.
42#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
43
Ted Kremenek5c04bd82009-01-28 00:27:31 +000044//===----------------------------------------------------------------------===//
45// Common logic.
46//===----------------------------------------------------------------------===//
Ted Kremenekd87eef82008-02-24 03:15:25 +000047
Ben Langmuirc8130a72014-02-20 21:59:23 +000048FileManager::FileManager(const FileSystemOptions &FSO,
49 IntrusiveRefCntPtr<vfs::FileSystem> FS)
50 : FS(FS), FileSystemOpts(FSO),
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000051 SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
Ted Kremenekd87eef82008-02-24 03:15:25 +000052 NumDirLookups = NumFileLookups = 0;
53 NumDirCacheMisses = NumFileCacheMisses = 0;
Ben Langmuirc8130a72014-02-20 21:59:23 +000054
55 // If the caller doesn't provide a virtual file system, just grab the real
56 // file system.
57 if (!FS)
58 this->FS = vfs::getRealFileSystem();
Ted Kremenekd87eef82008-02-24 03:15:25 +000059}
60
David Blaikied2725a32015-12-09 17:23:13 +000061FileManager::~FileManager() = default;
Ted Kremenekd87eef82008-02-24 03:15:25 +000062
David Blaikie23430cc2014-08-11 21:29:24 +000063void FileManager::addStatCache(std::unique_ptr<FileSystemStatCache> statCache,
Chris Lattner226efd32010-11-23 19:19:34 +000064 bool AtBeginning) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +000065 assert(statCache && "No stat cache provided?");
Craig Topperf1186c52014-05-08 06:41:40 +000066 if (AtBeginning || !StatCache.get()) {
David Blaikie23430cc2014-08-11 21:29:24 +000067 statCache->setNextStatCache(std::move(StatCache));
68 StatCache = std::move(statCache);
Douglas Gregord2eb58a2009-10-16 18:18:30 +000069 return;
70 }
71
Chris Lattner226efd32010-11-23 19:19:34 +000072 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +000073 while (LastCache->getNextStatCache())
74 LastCache = LastCache->getNextStatCache();
David Blaikie23430cc2014-08-11 21:29:24 +000075
76 LastCache->setNextStatCache(std::move(statCache));
Douglas Gregord2eb58a2009-10-16 18:18:30 +000077}
78
Chris Lattner226efd32010-11-23 19:19:34 +000079void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +000080 if (!statCache)
81 return;
82
83 if (StatCache.get() == statCache) {
84 // This is the first stat cache.
David Blaikiedd0e1e82014-08-10 16:57:11 +000085 StatCache = StatCache->takeNextStatCache();
Douglas Gregord2eb58a2009-10-16 18:18:30 +000086 return;
87 }
88
89 // Find the stat cache in the list.
Chris Lattner226efd32010-11-23 19:19:34 +000090 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +000091 while (PrevCache && PrevCache->getNextStatCache() != statCache)
92 PrevCache = PrevCache->getNextStatCache();
Chris Lattner9624b692010-11-23 20:50:22 +000093
94 assert(PrevCache && "Stat cache not found for removal");
David Blaikie23430cc2014-08-11 21:29:24 +000095 PrevCache->setNextStatCache(statCache->takeNextStatCache());
Douglas Gregord2eb58a2009-10-16 18:18:30 +000096}
97
Manuel Klimek3aad8552012-07-31 13:56:54 +000098void FileManager::clearStatCaches() {
David Blaikie3875a822014-07-19 01:06:45 +000099 StatCache.reset();
Manuel Klimek3aad8552012-07-31 13:56:54 +0000100}
101
Douglas Gregor407e2122009-12-02 18:12:28 +0000102/// \brief Retrieve the directory that the given file name resides in.
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000103/// Filename can point to either a real file or a virtual file.
Douglas Gregor407e2122009-12-02 18:12:28 +0000104static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000105 StringRef Filename,
106 bool CacheFailure) {
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000107 if (Filename.empty())
Craig Topperf1186c52014-05-08 06:41:40 +0000108 return nullptr;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000109
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000110 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
Craig Topperf1186c52014-05-08 06:41:40 +0000111 return nullptr; // If Filename is a directory.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000112
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000113 StringRef DirName = llvm::sys::path::parent_path(Filename);
Chris Lattner0c0e8042010-11-21 09:50:16 +0000114 // Use the current directory if file has no path component.
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000115 if (DirName.empty())
116 DirName = ".";
Douglas Gregor407e2122009-12-02 18:12:28 +0000117
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000118 return FileMgr.getDirectory(DirName, CacheFailure);
Douglas Gregor407e2122009-12-02 18:12:28 +0000119}
120
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000121/// Add all ancestors of the given path (pointing to either a file or
122/// a directory) as virtual directories.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000123void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
124 StringRef DirName = llvm::sys::path::parent_path(Path);
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000125 if (DirName.empty())
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000126 return;
127
David Blaikie13156b62014-11-19 03:06:06 +0000128 auto &NamedDirEnt =
129 *SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000130
131 // When caching a virtual directory, we always cache its ancestors
132 // at the same time. Therefore, if DirName is already in the cache,
133 // we don't need to recurse as its ancestors must also already be in
134 // the cache.
Richard Smitha8cfffa2015-11-26 02:04:16 +0000135 if (NamedDirEnt.second && NamedDirEnt.second != NON_EXISTENT_DIR)
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000136 return;
137
138 // Add the virtual directory to the cache.
David Blaikied2725a32015-12-09 17:23:13 +0000139 auto UDE = llvm::make_unique<DirectoryEntry>();
David Blaikie13156b62014-11-19 03:06:06 +0000140 UDE->Name = NamedDirEnt.first().data();
David Blaikied2725a32015-12-09 17:23:13 +0000141 NamedDirEnt.second = UDE.get();
142 VirtualDirectoryEntries.push_back(std::move(UDE));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000143
144 // Recursively add the other ancestors.
145 addAncestorsAsVirtualDirs(DirName);
146}
147
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000148const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
149 bool CacheFailure) {
NAKAMURA Takumi8bd8ee72012-06-16 06:04:10 +0000150 // stat doesn't like trailing separators except for root directory.
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000151 // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
152 // (though it can strip '\\')
NAKAMURA Takumi8bd8ee72012-06-16 06:04:10 +0000153 if (DirName.size() > 1 &&
154 DirName != llvm::sys::path::root_path(DirName) &&
155 llvm::sys::path::is_separator(DirName.back()))
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000156 DirName = DirName.substr(0, DirName.size()-1);
Rafael Espindolaee305462013-07-29 15:47:24 +0000157#ifdef LLVM_ON_WIN32
158 // Fixing a problem with "clang C:test.c" on Windows.
159 // Stat("C:") does not recognize "C:" as a valid directory
160 std::string DirNameStr;
161 if (DirName.size() > 1 && DirName.back() == ':' &&
162 DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
163 DirNameStr = DirName.str() + '.';
164 DirName = DirNameStr;
165 }
166#endif
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000167
Chris Lattner22eb9722006-06-18 05:43:12 +0000168 ++NumDirLookups;
David Blaikie13156b62014-11-19 03:06:06 +0000169 auto &NamedDirEnt =
170 *SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first;
Mike Stump11289f42009-09-09 15:08:12 +0000171
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000172 // See if there was already an entry in the map. Note that the map
173 // contains both virtual and real directories.
David Blaikie13156b62014-11-19 03:06:06 +0000174 if (NamedDirEnt.second)
175 return NamedDirEnt.second == NON_EXISTENT_DIR ? nullptr
176 : NamedDirEnt.second;
Mike Stump11289f42009-09-09 15:08:12 +0000177
Chris Lattner22eb9722006-06-18 05:43:12 +0000178 ++NumDirCacheMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000179
Chris Lattneraf653752006-10-30 03:06:54 +0000180 // By default, initialize it to invalid.
David Blaikie13156b62014-11-19 03:06:06 +0000181 NamedDirEnt.second = NON_EXISTENT_DIR;
Mike Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattner43fd42e2006-10-30 03:40:58 +0000183 // Get the null-terminated directory name as stored as the key of the
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000184 // SeenDirEntries map.
David Blaikie13156b62014-11-19 03:06:06 +0000185 const char *InterndDirName = NamedDirEnt.first().data();
Mike Stump11289f42009-09-09 15:08:12 +0000186
Chris Lattneraf653752006-10-30 03:06:54 +0000187 // Check to see if the directory exists.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000188 FileData Data;
Craig Topperf1186c52014-05-08 06:41:40 +0000189 if (getStatValue(InterndDirName, Data, false, nullptr /*directory lookup*/)) {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000190 // There's no real directory at the given path.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000191 if (!CacheFailure)
192 SeenDirEntries.erase(DirName);
Craig Topperf1186c52014-05-08 06:41:40 +0000193 return nullptr;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000194 }
Ted Kremenekd87eef82008-02-24 03:15:25 +0000195
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000196 // It exists. See if we have already opened a directory with the
197 // same inode (this occurs on Unix-like systems when one dir is
198 // symlinked to another, for example) or the same path (on
199 // Windows).
Ben Langmuirc9b72342014-02-27 22:21:32 +0000200 DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID];
Mike Stump11289f42009-09-09 15:08:12 +0000201
David Blaikie13156b62014-11-19 03:06:06 +0000202 NamedDirEnt.second = &UDE;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000203 if (!UDE.getName()) {
204 // We don't have this directory yet, add it. We use the string
205 // key from the SeenDirEntries map as the string.
206 UDE.Name = InterndDirName;
207 }
Mike Stump11289f42009-09-09 15:08:12 +0000208
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000209 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000210}
211
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000212const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
213 bool CacheFailure) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000214 ++NumFileLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000215
Chris Lattner22eb9722006-06-18 05:43:12 +0000216 // See if there is already an entry in the map.
David Blaikie13156b62014-11-19 03:06:06 +0000217 auto &NamedFileEnt =
218 *SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first;
Chris Lattner22eb9722006-06-18 05:43:12 +0000219
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000220 // See if there is already an entry in the map.
David Blaikie13156b62014-11-19 03:06:06 +0000221 if (NamedFileEnt.second)
222 return NamedFileEnt.second == NON_EXISTENT_FILE ? nullptr
223 : NamedFileEnt.second;
Mike Stump11289f42009-09-09 15:08:12 +0000224
Chris Lattner22eb9722006-06-18 05:43:12 +0000225 ++NumFileCacheMisses;
226
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000227 // By default, initialize it to invalid.
David Blaikie13156b62014-11-19 03:06:06 +0000228 NamedFileEnt.second = NON_EXISTENT_FILE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000229
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000230 // Get the null-terminated file name as stored as the key of the
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000231 // SeenFileEntries map.
David Blaikie13156b62014-11-19 03:06:06 +0000232 const char *InterndFileName = NamedFileEnt.first().data();
Mike Stump11289f42009-09-09 15:08:12 +0000233
Chris Lattner966b25b2010-11-23 20:30:42 +0000234 // Look up the directory for the file. When looking up something like
235 // sys/foo.h we'll discover all of the search directories that have a 'sys'
236 // subdirectory. This will let us avoid having to waste time on known-to-fail
237 // searches when we go to find sys/bar.h, because all the search directories
238 // without a 'sys' subdir will get a cached failure result.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000239 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
240 CacheFailure);
Craig Topperf1186c52014-05-08 06:41:40 +0000241 if (DirInfo == nullptr) { // Directory doesn't exist, file can't exist.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000242 if (!CacheFailure)
243 SeenFileEntries.erase(Filename);
Craig Topperf1186c52014-05-08 06:41:40 +0000244
245 return nullptr;
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000246 }
247
Chris Lattner22eb9722006-06-18 05:43:12 +0000248 // FIXME: Use the directory info to prune this, before doing the stat syscall.
249 // FIXME: This will reduce the # syscalls.
Mike Stump11289f42009-09-09 15:08:12 +0000250
Chris Lattner22eb9722006-06-18 05:43:12 +0000251 // Nope, there isn't. Check to see if the file exists.
David Blaikie326ffb32014-07-08 15:46:02 +0000252 std::unique_ptr<vfs::File> F;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000253 FileData Data;
Craig Topperf1186c52014-05-08 06:41:40 +0000254 if (getStatValue(InterndFileName, Data, true, openFile ? &F : nullptr)) {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000255 // There's no real file at the given path.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000256 if (!CacheFailure)
257 SeenFileEntries.erase(Filename);
Craig Topperf1186c52014-05-08 06:41:40 +0000258
259 return nullptr;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000260 }
Mike Stump11289f42009-09-09 15:08:12 +0000261
Patrik Hagglundab01d4b2014-02-21 07:23:53 +0000262 assert((openFile || !F) && "undesired open file");
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000263
Ted Kremenekf4c38c92007-12-18 22:29:39 +0000264 // It exists. See if we have already opened a file with the same inode.
Chris Lattner22eb9722006-06-18 05:43:12 +0000265 // This occurs when one dir is symlinked to another, for example.
Ben Langmuirc9b72342014-02-27 22:21:32 +0000266 FileEntry &UFE = UniqueRealFiles[Data.UniqueID];
Mike Stump11289f42009-09-09 15:08:12 +0000267
David Blaikie13156b62014-11-19 03:06:06 +0000268 NamedFileEnt.second = &UFE;
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000269
270 // If the name returned by getStatValue is different than Filename, re-intern
271 // the name.
272 if (Data.Name != Filename) {
David Blaikie13156b62014-11-19 03:06:06 +0000273 auto &NamedFileEnt =
274 *SeenFileEntries.insert(std::make_pair(Data.Name, nullptr)).first;
275 if (!NamedFileEnt.second)
276 NamedFileEnt.second = &UFE;
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000277 else
David Blaikie13156b62014-11-19 03:06:06 +0000278 assert(NamedFileEnt.second == &UFE &&
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000279 "filename from getStatValue() refers to wrong file");
David Blaikie13156b62014-11-19 03:06:06 +0000280 InterndFileName = NamedFileEnt.first().data();
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000281 }
282
Ben Langmuirc8a71462014-02-27 17:23:33 +0000283 if (UFE.isValid()) { // Already have an entry with this inode, return it.
Ben Langmuir5de00f32014-05-23 18:15:47 +0000284
285 // FIXME: this hack ensures that if we look up a file by a virtual path in
286 // the VFS that the getDir() will have the virtual path, even if we found
287 // the file by a 'real' path first. This is required in order to find a
288 // module's structure when its headers/module map are mapped in the VFS.
289 // We should remove this as soon as we can properly support a file having
290 // multiple names.
291 if (DirInfo != UFE.Dir && Data.IsVFSMapped)
292 UFE.Dir = DirInfo;
293
Manuel Klimekc0ff9902014-08-13 12:34:41 +0000294 // Always update the name to use the last name by which a file was accessed.
295 // FIXME: Neither this nor always using the first name is correct; we want
296 // to switch towards a design where we return a FileName object that
297 // encapsulates both the name by which the file was accessed and the
298 // corresponding FileEntry.
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000299 UFE.Name = InterndFileName;
Manuel Klimekc0ff9902014-08-13 12:34:41 +0000300
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000301 return &UFE;
Chris Lattnerdd278432010-11-23 21:17:56 +0000302 }
Chris Lattner269c2322006-06-25 06:23:00 +0000303
Ben Langmuirc9b72342014-02-27 22:21:32 +0000304 // Otherwise, we don't have this file yet, add it.
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000305 UFE.Name = InterndFileName;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000306 UFE.Size = Data.Size;
307 UFE.ModTime = Data.ModTime;
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000308 UFE.Dir = DirInfo;
309 UFE.UID = NextFileUID++;
Ben Langmuirc9b72342014-02-27 22:21:32 +0000310 UFE.UniqueID = Data.UniqueID;
311 UFE.IsNamedPipe = Data.IsNamedPipe;
312 UFE.InPCH = Data.InPCH;
David Blaikie326ffb32014-07-08 15:46:02 +0000313 UFE.File = std::move(F);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000314 UFE.IsValid = true;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000315 return &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000316}
317
Douglas Gregor407e2122009-12-02 18:12:28 +0000318const FileEntry *
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000319FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner5159f612010-11-23 08:35:12 +0000320 time_t ModificationTime) {
Douglas Gregor407e2122009-12-02 18:12:28 +0000321 ++NumFileLookups;
322
323 // See if there is already an entry in the map.
David Blaikie13156b62014-11-19 03:06:06 +0000324 auto &NamedFileEnt =
325 *SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first;
Douglas Gregor407e2122009-12-02 18:12:28 +0000326
327 // See if there is already an entry in the map.
David Blaikie13156b62014-11-19 03:06:06 +0000328 if (NamedFileEnt.second && NamedFileEnt.second != NON_EXISTENT_FILE)
329 return NamedFileEnt.second;
Douglas Gregor407e2122009-12-02 18:12:28 +0000330
331 ++NumFileCacheMisses;
332
333 // By default, initialize it to invalid.
David Blaikie13156b62014-11-19 03:06:06 +0000334 NamedFileEnt.second = NON_EXISTENT_FILE;
Douglas Gregor407e2122009-12-02 18:12:28 +0000335
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000336 addAncestorsAsVirtualDirs(Filename);
Craig Topperf1186c52014-05-08 06:41:40 +0000337 FileEntry *UFE = nullptr;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000338
339 // Now that all ancestors of Filename are in the cache, the
340 // following call is guaranteed to find the DirectoryEntry from the
341 // cache.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000342 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
343 /*CacheFailure=*/true);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000344 assert(DirInfo &&
345 "The directory of a virtual file should already be in the cache.");
Douglas Gregor407e2122009-12-02 18:12:28 +0000346
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000347 // Check to see if the file exists. If so, drop the virtual file
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000348 FileData Data;
David Blaikie13156b62014-11-19 03:06:06 +0000349 const char *InterndFileName = NamedFileEnt.first().data();
Craig Topperf1186c52014-05-08 06:41:40 +0000350 if (getStatValue(InterndFileName, Data, true, nullptr) == 0) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000351 Data.Size = Size;
352 Data.ModTime = ModificationTime;
Ben Langmuirc9b72342014-02-27 22:21:32 +0000353 UFE = &UniqueRealFiles[Data.UniqueID];
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000354
David Blaikie13156b62014-11-19 03:06:06 +0000355 NamedFileEnt.second = UFE;
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000356
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000357 // If we had already opened this file, close it now so we don't
358 // leak the descriptor. We're not going to use the file
359 // descriptor anyway, since this is a virtual file.
Ben Langmuirc8130a72014-02-20 21:59:23 +0000360 if (UFE->File)
361 UFE->closeFile();
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000362
363 // If we already have an entry with this inode, return it.
Ben Langmuirc8a71462014-02-27 17:23:33 +0000364 if (UFE->isValid())
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000365 return UFE;
Ben Langmuirc9b72342014-02-27 22:21:32 +0000366
367 UFE->UniqueID = Data.UniqueID;
368 UFE->IsNamedPipe = Data.IsNamedPipe;
369 UFE->InPCH = Data.InPCH;
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000370 }
371
372 if (!UFE) {
David Blaikied2725a32015-12-09 17:23:13 +0000373 VirtualFileEntries.push_back(llvm::make_unique<FileEntry>());
374 UFE = VirtualFileEntries.back().get();
David Blaikie13156b62014-11-19 03:06:06 +0000375 NamedFileEnt.second = UFE;
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000376 }
Douglas Gregor407e2122009-12-02 18:12:28 +0000377
Chris Lattner9624b692010-11-23 20:50:22 +0000378 UFE->Name = InterndFileName;
Douglas Gregor407e2122009-12-02 18:12:28 +0000379 UFE->Size = Size;
380 UFE->ModTime = ModificationTime;
381 UFE->Dir = DirInfo;
382 UFE->UID = NextFileUID++;
Ben Langmuirc8130a72014-02-20 21:59:23 +0000383 UFE->File.reset();
Douglas Gregor407e2122009-12-02 18:12:28 +0000384 return UFE;
385}
386
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +0000387bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000388 StringRef pathRef(path.data(), path.size());
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000389
Anders Carlsson9ba8fb12011-03-14 01:13:54 +0000390 if (FileSystemOpts.WorkingDir.empty()
391 || llvm::sys::path::is_absolute(pathRef))
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +0000392 return false;
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000393
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000394 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000395 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner6e640992010-11-23 04:45:28 +0000396 path = NewPath;
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +0000397 return true;
398}
399
400bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
401 bool Changed = FixupRelativePath(Path);
402
403 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
404 llvm::sys::fs::make_absolute(Path);
405 Changed = true;
406 }
407
408 return Changed;
Chris Lattner6e640992010-11-23 04:45:28 +0000409}
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000410
Benjamin Kramera8857962014-10-26 22:44:13 +0000411llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
412FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
413 bool ShouldCloseOpenFile) {
Argyrios Kyrtzidis6d7833f2012-07-11 20:59:04 +0000414 uint64_t FileSize = Entry->getSize();
415 // If there's a high enough chance that the file have changed since we
416 // got its size, force a stat before opening it.
417 if (isVolatile)
418 FileSize = -1;
419
Argyrios Kyrtzidis669b0b12011-03-15 00:47:44 +0000420 const char *Filename = Entry->getName();
421 // If the file is already open, use the open file descriptor.
Ben Langmuirc8130a72014-02-20 21:59:23 +0000422 if (Entry->File) {
Benjamin Kramera8857962014-10-26 22:44:13 +0000423 auto Result =
424 Entry->File->getBuffer(Filename, FileSize,
425 /*RequiresNullTerminator=*/true, isVolatile);
Ben Langmuir9801b252014-06-20 00:24:56 +0000426 // FIXME: we need a set of APIs that can make guarantees about whether a
427 // FileEntry is open or not.
428 if (ShouldCloseOpenFile)
429 Entry->closeFile();
Rafael Espindola6406f7b2014-08-26 19:54:40 +0000430 return Result;
Argyrios Kyrtzidis669b0b12011-03-15 00:47:44 +0000431 }
432
433 // Otherwise, open the file.
434
Benjamin Kramera8857962014-10-26 22:44:13 +0000435 if (FileSystemOpts.WorkingDir.empty())
436 return FS->getBufferForFile(Filename, FileSize,
437 /*RequiresNullTerminator=*/true, isVolatile);
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000438
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000439 SmallString<128> FilePath(Entry->getName());
Anders Carlsson878b3e22011-03-07 01:28:33 +0000440 FixupRelativePath(FilePath);
Yaron Keren92e1b622015-03-18 10:17:07 +0000441 return FS->getBufferForFile(FilePath, FileSize,
Benjamin Kramera8857962014-10-26 22:44:13 +0000442 /*RequiresNullTerminator=*/true, isVolatile);
Chris Lattner26b5c192010-11-23 09:19:42 +0000443}
444
Benjamin Kramera8857962014-10-26 22:44:13 +0000445llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
446FileManager::getBufferForFile(StringRef Filename) {
447 if (FileSystemOpts.WorkingDir.empty())
448 return FS->getBufferForFile(Filename);
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000449
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000450 SmallString<128> FilePath(Filename);
Anders Carlsson878b3e22011-03-07 01:28:33 +0000451 FixupRelativePath(FilePath);
Benjamin Kramera8857962014-10-26 22:44:13 +0000452 return FS->getBufferForFile(FilePath.c_str());
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000453}
454
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000455/// getStatValue - Get the 'stat' information for the specified path,
456/// using the cache to accelerate it if possible. This returns true
457/// if the path points to a virtual file or does not exist, or returns
458/// false if it's an existent real file. If FileDescriptor is NULL,
459/// do directory look-up instead of file look-up.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000460bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
David Blaikie326ffb32014-07-08 15:46:02 +0000461 std::unique_ptr<vfs::File> *F) {
Chris Lattner226efd32010-11-23 19:19:34 +0000462 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
463 // absolute!
Chris Lattner5769c3d2010-11-23 19:56:39 +0000464 if (FileSystemOpts.WorkingDir.empty())
Ben Langmuirc8130a72014-02-20 21:59:23 +0000465 return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000466
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000467 SmallString<128> FilePath(Path);
Anders Carlsson878b3e22011-03-07 01:28:33 +0000468 FixupRelativePath(FilePath);
Chris Lattner5769c3d2010-11-23 19:56:39 +0000469
Ben Langmuirc8130a72014-02-20 21:59:23 +0000470 return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F,
471 StatCache.get(), *FS);
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000472}
473
Rafael Espindolae4777f42013-07-29 18:22:23 +0000474bool FileManager::getNoncachedStatValue(StringRef Path,
Ben Langmuirc8130a72014-02-20 21:59:23 +0000475 vfs::Status &Result) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000476 SmallString<128> FilePath(Path);
Anders Carlsson5e368402011-03-18 19:23:19 +0000477 FixupRelativePath(FilePath);
478
Ben Langmuirc8130a72014-02-20 21:59:23 +0000479 llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str());
480 if (!S)
481 return true;
482 Result = *S;
483 return false;
Anders Carlsson5e368402011-03-18 19:23:19 +0000484}
485
Axel Naumannb3074002012-07-10 16:50:27 +0000486void FileManager::invalidateCache(const FileEntry *Entry) {
487 assert(Entry && "Cannot invalidate a NULL FileEntry");
Axel Naumann38179d92012-06-27 09:17:42 +0000488
489 SeenFileEntries.erase(Entry->getName());
Axel Naumannb3074002012-07-10 16:50:27 +0000490
491 // FileEntry invalidation should not block future optimizations in the file
492 // caches. Possible alternatives are cache truncation (invalidate last N) or
493 // invalidation of the whole cache.
Ben Langmuirc9b72342014-02-27 22:21:32 +0000494 UniqueRealFiles.erase(Entry->getUniqueID());
Axel Naumann38179d92012-06-27 09:17:42 +0000495}
496
497
Douglas Gregor09b69892011-02-10 17:09:37 +0000498void FileManager::GetUniqueIDMapping(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000499 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregor09b69892011-02-10 17:09:37 +0000500 UIDToFiles.clear();
501 UIDToFiles.resize(NextFileUID);
502
503 // Map file entries
504 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000505 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregor09b69892011-02-10 17:09:37 +0000506 FE != FEEnd; ++FE)
507 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
508 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
509
510 // Map virtual file entries
David Blaikied2725a32015-12-09 17:23:13 +0000511 for (const auto &VFE : VirtualFileEntries)
512 if (VFE && VFE.get() != NON_EXISTENT_FILE)
513 UIDToFiles[VFE->getUID()] = VFE.get();
Douglas Gregor09b69892011-02-10 17:09:37 +0000514}
Chris Lattner226efd32010-11-23 19:19:34 +0000515
Argyrios Kyrtzidis6eec06d2012-05-03 21:50:39 +0000516void FileManager::modifyFileEntry(FileEntry *File,
517 off_t Size, time_t ModificationTime) {
518 File->Size = Size;
519 File->ModTime = ModificationTime;
520}
521
Douglas Gregore00c8b22013-01-26 00:55:12 +0000522StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
523 // FIXME: use llvm::sys::fs::canonical() when it gets implemented
Douglas Gregore00c8b22013-01-26 00:55:12 +0000524 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
525 = CanonicalDirNames.find(Dir);
526 if (Known != CanonicalDirNames.end())
527 return Known->second;
528
529 StringRef CanonicalName(Dir->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +0000530
531#ifdef LLVM_ON_UNIX
Douglas Gregore00c8b22013-01-26 00:55:12 +0000532 char CanonicalNameBuf[PATH_MAX];
Benjamin Kramerda4690a2015-08-04 11:27:08 +0000533 if (realpath(Dir->getName(), CanonicalNameBuf))
534 CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
Richard Smith54cc3c22014-12-11 20:50:24 +0000535#else
536 SmallString<256> CanonicalNameBuf(CanonicalName);
537 llvm::sys::fs::make_absolute(CanonicalNameBuf);
538 llvm::sys::path::native(CanonicalNameBuf);
Sean Silvade381662015-07-30 07:30:24 +0000539 // We've run into needing to remove '..' here in the wild though, so
540 // remove it.
541 // On Windows, symlinks are significantly less prevalent, so removing
542 // '..' is pretty safe.
543 // Ideally we'd have an equivalent of `realpath` and could implement
544 // sys::fs::canonical across all the platforms.
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +0000545 llvm::sys::path::remove_dots(CanonicalNameBuf, /* remove_dot_dot */ true);
Benjamin Kramerda4690a2015-08-04 11:27:08 +0000546 CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
Richard Smith54cc3c22014-12-11 20:50:24 +0000547#endif
Douglas Gregore00c8b22013-01-26 00:55:12 +0000548
549 CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
550 return CanonicalName;
Douglas Gregore00c8b22013-01-26 00:55:12 +0000551}
Chris Lattner226efd32010-11-23 19:19:34 +0000552
Chris Lattner22eb9722006-06-18 05:43:12 +0000553void FileManager::PrintStats() const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000554 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000555 llvm::errs() << UniqueRealFiles.size() << " real files found, "
556 << UniqueRealDirs.size() << " real dirs found.\n";
557 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
558 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000559 llvm::errs() << NumDirLookups << " dir lookups, "
560 << NumDirCacheMisses << " dir cache misses.\n";
561 llvm::errs() << NumFileLookups << " file lookups, "
562 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000563
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000564 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Chris Lattner22eb9722006-06-18 05:43:12 +0000565}