blob: 5cbcdf4477e1bc777eaf86085213261fbbda03b4 [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"
Eugene Zelenko35b79c22016-08-13 01:05:35 +000029#include <algorithm>
30#include <cassert>
31#include <climits>
32#include <cstdint>
33#include <cstdlib>
Benjamin Kramer26db6482009-09-05 09:49:39 +000034#include <string>
Eugene Zelenko35b79c22016-08-13 01:05:35 +000035#include <utility>
Chris Lattner278038b2010-11-23 21:53:15 +000036
Chris Lattner22eb9722006-06-18 05:43:12 +000037using namespace clang;
38
Ted Kremenek8d71e252008-01-11 20:42:05 +000039/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Chris Lattneraf653752006-10-30 03:06:54 +000040/// represent a dir name that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +000041#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Chris Lattneraf653752006-10-30 03:06:54 +000042
Chris Lattner9624b692010-11-23 20:50:22 +000043/// NON_EXISTENT_FILE - A special value distinct from null that is used to
44/// represent a filename that doesn't exist on the disk.
45#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
46
Ted Kremenek5c04bd82009-01-28 00:27:31 +000047//===----------------------------------------------------------------------===//
48// Common logic.
49//===----------------------------------------------------------------------===//
Ted Kremenekd87eef82008-02-24 03:15:25 +000050
Ben Langmuirc8130a72014-02-20 21:59:23 +000051FileManager::FileManager(const FileSystemOptions &FSO,
52 IntrusiveRefCntPtr<vfs::FileSystem> FS)
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000053 : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
54 SeenFileEntries(64), NextFileUID(0) {
Ted Kremenekd87eef82008-02-24 03:15:25 +000055 NumDirLookups = NumFileLookups = 0;
56 NumDirCacheMisses = NumFileCacheMisses = 0;
Ben Langmuirc8130a72014-02-20 21:59:23 +000057
58 // If the caller doesn't provide a virtual file system, just grab the real
59 // file system.
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000060 if (!this->FS)
Ben Langmuirc8130a72014-02-20 21:59:23 +000061 this->FS = vfs::getRealFileSystem();
Ted Kremenekd87eef82008-02-24 03:15:25 +000062}
63
David Blaikied2725a32015-12-09 17:23:13 +000064FileManager::~FileManager() = default;
Ted Kremenekd87eef82008-02-24 03:15:25 +000065
David Blaikie23430cc2014-08-11 21:29:24 +000066void FileManager::addStatCache(std::unique_ptr<FileSystemStatCache> statCache,
Chris Lattner226efd32010-11-23 19:19:34 +000067 bool AtBeginning) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +000068 assert(statCache && "No stat cache provided?");
Craig Topperf1186c52014-05-08 06:41:40 +000069 if (AtBeginning || !StatCache.get()) {
David Blaikie23430cc2014-08-11 21:29:24 +000070 statCache->setNextStatCache(std::move(StatCache));
71 StatCache = std::move(statCache);
Douglas Gregord2eb58a2009-10-16 18:18:30 +000072 return;
73 }
74
Chris Lattner226efd32010-11-23 19:19:34 +000075 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +000076 while (LastCache->getNextStatCache())
77 LastCache = LastCache->getNextStatCache();
David Blaikie23430cc2014-08-11 21:29:24 +000078
79 LastCache->setNextStatCache(std::move(statCache));
Douglas Gregord2eb58a2009-10-16 18:18:30 +000080}
81
Chris Lattner226efd32010-11-23 19:19:34 +000082void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +000083 if (!statCache)
84 return;
85
86 if (StatCache.get() == statCache) {
87 // This is the first stat cache.
David Blaikiedd0e1e82014-08-10 16:57:11 +000088 StatCache = StatCache->takeNextStatCache();
Douglas Gregord2eb58a2009-10-16 18:18:30 +000089 return;
90 }
91
92 // Find the stat cache in the list.
Chris Lattner226efd32010-11-23 19:19:34 +000093 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +000094 while (PrevCache && PrevCache->getNextStatCache() != statCache)
95 PrevCache = PrevCache->getNextStatCache();
Chris Lattner9624b692010-11-23 20:50:22 +000096
97 assert(PrevCache && "Stat cache not found for removal");
David Blaikie23430cc2014-08-11 21:29:24 +000098 PrevCache->setNextStatCache(statCache->takeNextStatCache());
Douglas Gregord2eb58a2009-10-16 18:18:30 +000099}
100
Manuel Klimek3aad8552012-07-31 13:56:54 +0000101void FileManager::clearStatCaches() {
David Blaikie3875a822014-07-19 01:06:45 +0000102 StatCache.reset();
Manuel Klimek3aad8552012-07-31 13:56:54 +0000103}
104
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000105/// Retrieve the directory that the given file name resides in.
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000106/// Filename can point to either a real file or a virtual file.
Douglas Gregor407e2122009-12-02 18:12:28 +0000107static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000108 StringRef Filename,
109 bool CacheFailure) {
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000110 if (Filename.empty())
Craig Topperf1186c52014-05-08 06:41:40 +0000111 return nullptr;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000112
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000113 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
Craig Topperf1186c52014-05-08 06:41:40 +0000114 return nullptr; // If Filename is a directory.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000115
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000116 StringRef DirName = llvm::sys::path::parent_path(Filename);
Chris Lattner0c0e8042010-11-21 09:50:16 +0000117 // Use the current directory if file has no path component.
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000118 if (DirName.empty())
119 DirName = ".";
Douglas Gregor407e2122009-12-02 18:12:28 +0000120
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000121 return FileMgr.getDirectory(DirName, CacheFailure);
Douglas Gregor407e2122009-12-02 18:12:28 +0000122}
123
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000124/// Add all ancestors of the given path (pointing to either a file or
125/// a directory) as virtual directories.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000126void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
127 StringRef DirName = llvm::sys::path::parent_path(Path);
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000128 if (DirName.empty())
David Majnemer1834dc72016-04-12 16:33:53 +0000129 DirName = ".";
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000130
David Blaikie13156b62014-11-19 03:06:06 +0000131 auto &NamedDirEnt =
132 *SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000133
134 // When caching a virtual directory, we always cache its ancestors
135 // at the same time. Therefore, if DirName is already in the cache,
136 // we don't need to recurse as its ancestors must also already be in
137 // the cache.
Richard Smitha8cfffa2015-11-26 02:04:16 +0000138 if (NamedDirEnt.second && NamedDirEnt.second != NON_EXISTENT_DIR)
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000139 return;
140
141 // Add the virtual directory to the cache.
David Blaikied2725a32015-12-09 17:23:13 +0000142 auto UDE = llvm::make_unique<DirectoryEntry>();
Mehdi Amini0df59d82016-10-11 07:31:29 +0000143 UDE->Name = NamedDirEnt.first();
David Blaikied2725a32015-12-09 17:23:13 +0000144 NamedDirEnt.second = UDE.get();
145 VirtualDirectoryEntries.push_back(std::move(UDE));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000146
147 // Recursively add the other ancestors.
148 addAncestorsAsVirtualDirs(DirName);
149}
150
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000151const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
152 bool CacheFailure) {
NAKAMURA Takumi8bd8ee72012-06-16 06:04:10 +0000153 // stat doesn't like trailing separators except for root directory.
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000154 // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
155 // (though it can strip '\\')
NAKAMURA Takumi8bd8ee72012-06-16 06:04:10 +0000156 if (DirName.size() > 1 &&
157 DirName != llvm::sys::path::root_path(DirName) &&
158 llvm::sys::path::is_separator(DirName.back()))
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000159 DirName = DirName.substr(0, DirName.size()-1);
Nico Weber1865df42018-04-27 19:11:14 +0000160#ifdef _WIN32
Rafael Espindolaee305462013-07-29 15:47:24 +0000161 // Fixing a problem with "clang C:test.c" on Windows.
162 // Stat("C:") does not recognize "C:" as a valid directory
163 std::string DirNameStr;
164 if (DirName.size() > 1 && DirName.back() == ':' &&
165 DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
166 DirNameStr = DirName.str() + '.';
167 DirName = DirNameStr;
168 }
169#endif
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000170
Chris Lattner22eb9722006-06-18 05:43:12 +0000171 ++NumDirLookups;
David Blaikie13156b62014-11-19 03:06:06 +0000172 auto &NamedDirEnt =
173 *SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first;
Mike Stump11289f42009-09-09 15:08:12 +0000174
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000175 // See if there was already an entry in the map. Note that the map
176 // contains both virtual and real directories.
David Blaikie13156b62014-11-19 03:06:06 +0000177 if (NamedDirEnt.second)
178 return NamedDirEnt.second == NON_EXISTENT_DIR ? nullptr
179 : NamedDirEnt.second;
Mike Stump11289f42009-09-09 15:08:12 +0000180
Chris Lattner22eb9722006-06-18 05:43:12 +0000181 ++NumDirCacheMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattneraf653752006-10-30 03:06:54 +0000183 // By default, initialize it to invalid.
David Blaikie13156b62014-11-19 03:06:06 +0000184 NamedDirEnt.second = NON_EXISTENT_DIR;
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner43fd42e2006-10-30 03:40:58 +0000186 // Get the null-terminated directory name as stored as the key of the
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000187 // SeenDirEntries map.
Mehdi Amini0df59d82016-10-11 07:31:29 +0000188 StringRef InterndDirName = NamedDirEnt.first();
Mike Stump11289f42009-09-09 15:08:12 +0000189
Chris Lattneraf653752006-10-30 03:06:54 +0000190 // Check to see if the directory exists.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000191 FileData Data;
Craig Topperf1186c52014-05-08 06:41:40 +0000192 if (getStatValue(InterndDirName, Data, false, nullptr /*directory lookup*/)) {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000193 // There's no real directory at the given path.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000194 if (!CacheFailure)
195 SeenDirEntries.erase(DirName);
Craig Topperf1186c52014-05-08 06:41:40 +0000196 return nullptr;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000197 }
Ted Kremenekd87eef82008-02-24 03:15:25 +0000198
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000199 // It exists. See if we have already opened a directory with the
200 // same inode (this occurs on Unix-like systems when one dir is
201 // symlinked to another, for example) or the same path (on
202 // Windows).
Ben Langmuirc9b72342014-02-27 22:21:32 +0000203 DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID];
Mike Stump11289f42009-09-09 15:08:12 +0000204
David Blaikie13156b62014-11-19 03:06:06 +0000205 NamedDirEnt.second = &UDE;
Mehdi Amini0df59d82016-10-11 07:31:29 +0000206 if (UDE.getName().empty()) {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000207 // We don't have this directory yet, add it. We use the string
208 // key from the SeenDirEntries map as the string.
209 UDE.Name = InterndDirName;
210 }
Mike Stump11289f42009-09-09 15:08:12 +0000211
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000212 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000213}
214
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000215const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
216 bool CacheFailure) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000217 ++NumFileLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000218
Chris Lattner22eb9722006-06-18 05:43:12 +0000219 // See if there is already an entry in the map.
David Blaikie13156b62014-11-19 03:06:06 +0000220 auto &NamedFileEnt =
221 *SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first;
Chris Lattner22eb9722006-06-18 05:43:12 +0000222
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000223 // See if there is already an entry in the map.
David Blaikie13156b62014-11-19 03:06:06 +0000224 if (NamedFileEnt.second)
225 return NamedFileEnt.second == NON_EXISTENT_FILE ? nullptr
226 : NamedFileEnt.second;
Mike Stump11289f42009-09-09 15:08:12 +0000227
Chris Lattner22eb9722006-06-18 05:43:12 +0000228 ++NumFileCacheMisses;
229
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000230 // By default, initialize it to invalid.
David Blaikie13156b62014-11-19 03:06:06 +0000231 NamedFileEnt.second = NON_EXISTENT_FILE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000232
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000233 // Get the null-terminated file name as stored as the key of the
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000234 // SeenFileEntries map.
Mehdi Amini0df59d82016-10-11 07:31:29 +0000235 StringRef InterndFileName = NamedFileEnt.first();
Mike Stump11289f42009-09-09 15:08:12 +0000236
Chris Lattner966b25b2010-11-23 20:30:42 +0000237 // Look up the directory for the file. When looking up something like
238 // sys/foo.h we'll discover all of the search directories that have a 'sys'
239 // subdirectory. This will let us avoid having to waste time on known-to-fail
240 // searches when we go to find sys/bar.h, because all the search directories
241 // without a 'sys' subdir will get a cached failure result.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000242 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
243 CacheFailure);
Craig Topperf1186c52014-05-08 06:41:40 +0000244 if (DirInfo == nullptr) { // Directory doesn't exist, file can't exist.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000245 if (!CacheFailure)
246 SeenFileEntries.erase(Filename);
Craig Topperf1186c52014-05-08 06:41:40 +0000247
248 return nullptr;
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000249 }
250
Chris Lattner22eb9722006-06-18 05:43:12 +0000251 // FIXME: Use the directory info to prune this, before doing the stat syscall.
252 // FIXME: This will reduce the # syscalls.
Mike Stump11289f42009-09-09 15:08:12 +0000253
Chris Lattner22eb9722006-06-18 05:43:12 +0000254 // Nope, there isn't. Check to see if the file exists.
David Blaikie326ffb32014-07-08 15:46:02 +0000255 std::unique_ptr<vfs::File> F;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000256 FileData Data;
Craig Topperf1186c52014-05-08 06:41:40 +0000257 if (getStatValue(InterndFileName, Data, true, openFile ? &F : nullptr)) {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000258 // There's no real file at the given path.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000259 if (!CacheFailure)
260 SeenFileEntries.erase(Filename);
Craig Topperf1186c52014-05-08 06:41:40 +0000261
262 return nullptr;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000263 }
Mike Stump11289f42009-09-09 15:08:12 +0000264
Patrik Hagglundab01d4b2014-02-21 07:23:53 +0000265 assert((openFile || !F) && "undesired open file");
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000266
Ted Kremenekf4c38c92007-12-18 22:29:39 +0000267 // It exists. See if we have already opened a file with the same inode.
Chris Lattner22eb9722006-06-18 05:43:12 +0000268 // This occurs when one dir is symlinked to another, for example.
Ben Langmuirc9b72342014-02-27 22:21:32 +0000269 FileEntry &UFE = UniqueRealFiles[Data.UniqueID];
Mike Stump11289f42009-09-09 15:08:12 +0000270
David Blaikie13156b62014-11-19 03:06:06 +0000271 NamedFileEnt.second = &UFE;
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000272
273 // If the name returned by getStatValue is different than Filename, re-intern
274 // the name.
275 if (Data.Name != Filename) {
David Blaikie13156b62014-11-19 03:06:06 +0000276 auto &NamedFileEnt =
277 *SeenFileEntries.insert(std::make_pair(Data.Name, nullptr)).first;
278 if (!NamedFileEnt.second)
279 NamedFileEnt.second = &UFE;
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000280 else
David Blaikie13156b62014-11-19 03:06:06 +0000281 assert(NamedFileEnt.second == &UFE &&
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000282 "filename from getStatValue() refers to wrong file");
David Blaikie13156b62014-11-19 03:06:06 +0000283 InterndFileName = NamedFileEnt.first().data();
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000284 }
285
Ben Langmuirc8a71462014-02-27 17:23:33 +0000286 if (UFE.isValid()) { // Already have an entry with this inode, return it.
Ben Langmuir5de00f32014-05-23 18:15:47 +0000287
288 // FIXME: this hack ensures that if we look up a file by a virtual path in
289 // the VFS that the getDir() will have the virtual path, even if we found
290 // the file by a 'real' path first. This is required in order to find a
291 // module's structure when its headers/module map are mapped in the VFS.
292 // We should remove this as soon as we can properly support a file having
293 // multiple names.
294 if (DirInfo != UFE.Dir && Data.IsVFSMapped)
295 UFE.Dir = DirInfo;
296
Manuel Klimekc0ff9902014-08-13 12:34:41 +0000297 // Always update the name to use the last name by which a file was accessed.
298 // FIXME: Neither this nor always using the first name is correct; we want
299 // to switch towards a design where we return a FileName object that
300 // encapsulates both the name by which the file was accessed and the
301 // corresponding FileEntry.
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000302 UFE.Name = InterndFileName;
Manuel Klimekc0ff9902014-08-13 12:34:41 +0000303
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000304 return &UFE;
Chris Lattnerdd278432010-11-23 21:17:56 +0000305 }
Chris Lattner269c2322006-06-25 06:23:00 +0000306
Ben Langmuirc9b72342014-02-27 22:21:32 +0000307 // Otherwise, we don't have this file yet, add it.
Ben Langmuirab86fbe2014-09-08 16:15:54 +0000308 UFE.Name = InterndFileName;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000309 UFE.Size = Data.Size;
310 UFE.ModTime = Data.ModTime;
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000311 UFE.Dir = DirInfo;
312 UFE.UID = NextFileUID++;
Ben Langmuirc9b72342014-02-27 22:21:32 +0000313 UFE.UniqueID = Data.UniqueID;
314 UFE.IsNamedPipe = Data.IsNamedPipe;
315 UFE.InPCH = Data.InPCH;
David Blaikie326ffb32014-07-08 15:46:02 +0000316 UFE.File = std::move(F);
Ben Langmuirc8a71462014-02-27 17:23:33 +0000317 UFE.IsValid = true;
Taewook Ohf42103c2016-06-13 20:40:21 +0000318 if (UFE.File)
319 if (auto RealPathName = UFE.File->getName())
320 UFE.RealPathName = *RealPathName;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000321 return &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000322}
323
Douglas Gregor407e2122009-12-02 18:12:28 +0000324const FileEntry *
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000325FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner5159f612010-11-23 08:35:12 +0000326 time_t ModificationTime) {
Douglas Gregor407e2122009-12-02 18:12:28 +0000327 ++NumFileLookups;
328
329 // See if there is already an entry in the map.
David Blaikie13156b62014-11-19 03:06:06 +0000330 auto &NamedFileEnt =
331 *SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first;
Douglas Gregor407e2122009-12-02 18:12:28 +0000332
333 // See if there is already an entry in the map.
David Blaikie13156b62014-11-19 03:06:06 +0000334 if (NamedFileEnt.second && NamedFileEnt.second != NON_EXISTENT_FILE)
335 return NamedFileEnt.second;
Douglas Gregor407e2122009-12-02 18:12:28 +0000336
337 ++NumFileCacheMisses;
338
339 // By default, initialize it to invalid.
David Blaikie13156b62014-11-19 03:06:06 +0000340 NamedFileEnt.second = NON_EXISTENT_FILE;
Douglas Gregor407e2122009-12-02 18:12:28 +0000341
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000342 addAncestorsAsVirtualDirs(Filename);
Craig Topperf1186c52014-05-08 06:41:40 +0000343 FileEntry *UFE = nullptr;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000344
345 // Now that all ancestors of Filename are in the cache, the
346 // following call is guaranteed to find the DirectoryEntry from the
347 // cache.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000348 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
349 /*CacheFailure=*/true);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000350 assert(DirInfo &&
351 "The directory of a virtual file should already be in the cache.");
Douglas Gregor407e2122009-12-02 18:12:28 +0000352
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000353 // Check to see if the file exists. If so, drop the virtual file
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000354 FileData Data;
David Blaikie13156b62014-11-19 03:06:06 +0000355 const char *InterndFileName = NamedFileEnt.first().data();
Craig Topperf1186c52014-05-08 06:41:40 +0000356 if (getStatValue(InterndFileName, Data, true, nullptr) == 0) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000357 Data.Size = Size;
358 Data.ModTime = ModificationTime;
Ben Langmuirc9b72342014-02-27 22:21:32 +0000359 UFE = &UniqueRealFiles[Data.UniqueID];
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000360
David Blaikie13156b62014-11-19 03:06:06 +0000361 NamedFileEnt.second = UFE;
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000362
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000363 // If we had already opened this file, close it now so we don't
364 // leak the descriptor. We're not going to use the file
365 // descriptor anyway, since this is a virtual file.
Ben Langmuirc8130a72014-02-20 21:59:23 +0000366 if (UFE->File)
367 UFE->closeFile();
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000368
369 // If we already have an entry with this inode, return it.
Ben Langmuirc8a71462014-02-27 17:23:33 +0000370 if (UFE->isValid())
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000371 return UFE;
Ben Langmuirc9b72342014-02-27 22:21:32 +0000372
373 UFE->UniqueID = Data.UniqueID;
374 UFE->IsNamedPipe = Data.IsNamedPipe;
375 UFE->InPCH = Data.InPCH;
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000376 }
377
378 if (!UFE) {
David Blaikied2725a32015-12-09 17:23:13 +0000379 VirtualFileEntries.push_back(llvm::make_unique<FileEntry>());
380 UFE = VirtualFileEntries.back().get();
David Blaikie13156b62014-11-19 03:06:06 +0000381 NamedFileEnt.second = UFE;
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000382 }
Douglas Gregor407e2122009-12-02 18:12:28 +0000383
Chris Lattner9624b692010-11-23 20:50:22 +0000384 UFE->Name = InterndFileName;
Douglas Gregor407e2122009-12-02 18:12:28 +0000385 UFE->Size = Size;
386 UFE->ModTime = ModificationTime;
387 UFE->Dir = DirInfo;
388 UFE->UID = NextFileUID++;
Erik Verbruggendfffaf52017-03-28 09:18:05 +0000389 UFE->IsValid = true;
Ben Langmuirc8130a72014-02-20 21:59:23 +0000390 UFE->File.reset();
Douglas Gregor407e2122009-12-02 18:12:28 +0000391 return UFE;
392}
393
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +0000394bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000395 StringRef pathRef(path.data(), path.size());
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000396
Anders Carlsson9ba8fb12011-03-14 01:13:54 +0000397 if (FileSystemOpts.WorkingDir.empty()
398 || llvm::sys::path::is_absolute(pathRef))
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +0000399 return false;
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000400
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000401 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000402 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner6e640992010-11-23 04:45:28 +0000403 path = NewPath;
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +0000404 return true;
405}
406
407bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
408 bool Changed = FixupRelativePath(Path);
409
410 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
Ilya Biryukov47035c02017-08-02 07:25:24 +0000411 FS->makeAbsolute(Path);
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +0000412 Changed = true;
413 }
414
415 return Changed;
Chris Lattner6e640992010-11-23 04:45:28 +0000416}
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000417
Benjamin Kramera8857962014-10-26 22:44:13 +0000418llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
419FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
420 bool ShouldCloseOpenFile) {
Argyrios Kyrtzidis6d7833f2012-07-11 20:59:04 +0000421 uint64_t FileSize = Entry->getSize();
422 // If there's a high enough chance that the file have changed since we
423 // got its size, force a stat before opening it.
424 if (isVolatile)
425 FileSize = -1;
426
Mehdi Amini004b9c72016-10-10 22:52:47 +0000427 StringRef Filename = Entry->getName();
Argyrios Kyrtzidis669b0b12011-03-15 00:47:44 +0000428 // If the file is already open, use the open file descriptor.
Ben Langmuirc8130a72014-02-20 21:59:23 +0000429 if (Entry->File) {
Benjamin Kramera8857962014-10-26 22:44:13 +0000430 auto Result =
431 Entry->File->getBuffer(Filename, FileSize,
432 /*RequiresNullTerminator=*/true, isVolatile);
Ben Langmuir9801b252014-06-20 00:24:56 +0000433 // FIXME: we need a set of APIs that can make guarantees about whether a
434 // FileEntry is open or not.
435 if (ShouldCloseOpenFile)
436 Entry->closeFile();
Rafael Espindola6406f7b2014-08-26 19:54:40 +0000437 return Result;
Argyrios Kyrtzidis669b0b12011-03-15 00:47:44 +0000438 }
439
440 // Otherwise, open the file.
441
Benjamin Kramera8857962014-10-26 22:44:13 +0000442 if (FileSystemOpts.WorkingDir.empty())
443 return FS->getBufferForFile(Filename, FileSize,
444 /*RequiresNullTerminator=*/true, isVolatile);
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000445
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000446 SmallString<128> FilePath(Entry->getName());
Anders Carlsson878b3e22011-03-07 01:28:33 +0000447 FixupRelativePath(FilePath);
Yaron Keren92e1b622015-03-18 10:17:07 +0000448 return FS->getBufferForFile(FilePath, FileSize,
Benjamin Kramera8857962014-10-26 22:44:13 +0000449 /*RequiresNullTerminator=*/true, isVolatile);
Chris Lattner26b5c192010-11-23 09:19:42 +0000450}
451
Benjamin Kramera8857962014-10-26 22:44:13 +0000452llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
453FileManager::getBufferForFile(StringRef Filename) {
454 if (FileSystemOpts.WorkingDir.empty())
455 return FS->getBufferForFile(Filename);
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000456
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000457 SmallString<128> FilePath(Filename);
Anders Carlsson878b3e22011-03-07 01:28:33 +0000458 FixupRelativePath(FilePath);
Benjamin Kramera8857962014-10-26 22:44:13 +0000459 return FS->getBufferForFile(FilePath.c_str());
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000460}
461
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000462/// getStatValue - Get the 'stat' information for the specified path,
463/// using the cache to accelerate it if possible. This returns true
464/// if the path points to a virtual file or does not exist, or returns
465/// false if it's an existent real file. If FileDescriptor is NULL,
466/// do directory look-up instead of file look-up.
Mehdi Amini0df59d82016-10-11 07:31:29 +0000467bool FileManager::getStatValue(StringRef Path, FileData &Data, bool isFile,
David Blaikie326ffb32014-07-08 15:46:02 +0000468 std::unique_ptr<vfs::File> *F) {
Chris Lattner226efd32010-11-23 19:19:34 +0000469 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
470 // absolute!
Chris Lattner5769c3d2010-11-23 19:56:39 +0000471 if (FileSystemOpts.WorkingDir.empty())
Ben Langmuirc8130a72014-02-20 21:59:23 +0000472 return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000473
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000474 SmallString<128> FilePath(Path);
Anders Carlsson878b3e22011-03-07 01:28:33 +0000475 FixupRelativePath(FilePath);
Chris Lattner5769c3d2010-11-23 19:56:39 +0000476
Ben Langmuirc8130a72014-02-20 21:59:23 +0000477 return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F,
478 StatCache.get(), *FS);
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000479}
480
Rafael Espindolae4777f42013-07-29 18:22:23 +0000481bool FileManager::getNoncachedStatValue(StringRef Path,
Ben Langmuirc8130a72014-02-20 21:59:23 +0000482 vfs::Status &Result) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000483 SmallString<128> FilePath(Path);
Anders Carlsson5e368402011-03-18 19:23:19 +0000484 FixupRelativePath(FilePath);
485
Ben Langmuirc8130a72014-02-20 21:59:23 +0000486 llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str());
487 if (!S)
488 return true;
489 Result = *S;
490 return false;
Anders Carlsson5e368402011-03-18 19:23:19 +0000491}
492
Axel Naumannb3074002012-07-10 16:50:27 +0000493void FileManager::invalidateCache(const FileEntry *Entry) {
494 assert(Entry && "Cannot invalidate a NULL FileEntry");
Axel Naumann38179d92012-06-27 09:17:42 +0000495
496 SeenFileEntries.erase(Entry->getName());
Axel Naumannb3074002012-07-10 16:50:27 +0000497
498 // FileEntry invalidation should not block future optimizations in the file
499 // caches. Possible alternatives are cache truncation (invalidate last N) or
500 // invalidation of the whole cache.
Ben Langmuirc9b72342014-02-27 22:21:32 +0000501 UniqueRealFiles.erase(Entry->getUniqueID());
Axel Naumann38179d92012-06-27 09:17:42 +0000502}
503
Douglas Gregor09b69892011-02-10 17:09:37 +0000504void FileManager::GetUniqueIDMapping(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000505 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregor09b69892011-02-10 17:09:37 +0000506 UIDToFiles.clear();
507 UIDToFiles.resize(NextFileUID);
508
509 // Map file entries
510 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000511 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregor09b69892011-02-10 17:09:37 +0000512 FE != FEEnd; ++FE)
513 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
514 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
515
516 // Map virtual file entries
David Blaikied2725a32015-12-09 17:23:13 +0000517 for (const auto &VFE : VirtualFileEntries)
518 if (VFE && VFE.get() != NON_EXISTENT_FILE)
519 UIDToFiles[VFE->getUID()] = VFE.get();
Douglas Gregor09b69892011-02-10 17:09:37 +0000520}
Chris Lattner226efd32010-11-23 19:19:34 +0000521
Argyrios Kyrtzidis6eec06d2012-05-03 21:50:39 +0000522void FileManager::modifyFileEntry(FileEntry *File,
523 off_t Size, time_t ModificationTime) {
524 File->Size = Size;
525 File->ModTime = ModificationTime;
526}
527
Douglas Gregore00c8b22013-01-26 00:55:12 +0000528StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
529 // FIXME: use llvm::sys::fs::canonical() when it gets implemented
Douglas Gregore00c8b22013-01-26 00:55:12 +0000530 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
531 = CanonicalDirNames.find(Dir);
532 if (Known != CanonicalDirNames.end())
533 return Known->second;
534
535 StringRef CanonicalName(Dir->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +0000536
Eric Liu5fb18fe2018-05-17 10:26:23 +0000537 SmallString<4096> CanonicalNameBuf;
538 if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf))
Benjamin Kramerda4690a2015-08-04 11:27:08 +0000539 CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
Douglas Gregore00c8b22013-01-26 00:55:12 +0000540
541 CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
542 return CanonicalName;
Douglas Gregore00c8b22013-01-26 00:55:12 +0000543}
Chris Lattner226efd32010-11-23 19:19:34 +0000544
Chris Lattner22eb9722006-06-18 05:43:12 +0000545void FileManager::PrintStats() const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000546 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000547 llvm::errs() << UniqueRealFiles.size() << " real files found, "
548 << UniqueRealDirs.size() << " real dirs found.\n";
549 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
550 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000551 llvm::errs() << NumDirLookups << " dir lookups, "
552 << NumDirCacheMisses << " dir cache misses.\n";
553 llvm::errs() << NumFileLookups << " file lookups, "
554 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000555
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000556 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Chris Lattner22eb9722006-06-18 05:43:12 +0000557}