blob: efc08aac96de5f2ffaaa30196f0719730f94c055 [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"
Michael J. Spencer740857f2010-12-21 16:45:57 +000024#include "llvm/Support/FileSystem.h"
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000025#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000026#include "llvm/Support/Path.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "llvm/Support/raw_ostream.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000028#include "llvm/Support/system_error.h"
Benjamin Kramer26db6482009-09-05 09:49:39 +000029#include <map>
30#include <set>
31#include <string>
Chris Lattner278038b2010-11-23 21:53:15 +000032
Chris Lattner22eb9722006-06-18 05:43:12 +000033using namespace clang;
34
35// FIXME: Enhance libsystem to support inode and other fields.
36#include <sys/stat.h>
37
Ted Kremenek8d71e252008-01-11 20:42:05 +000038/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Chris Lattneraf653752006-10-30 03:06:54 +000039/// represent a dir name that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +000040#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Chris Lattneraf653752006-10-30 03:06:54 +000041
Chris Lattner9624b692010-11-23 20:50:22 +000042/// NON_EXISTENT_FILE - A special value distinct from null that is used to
43/// represent a filename that doesn't exist on the disk.
44#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
45
46
Ted Kremenekd87eef82008-02-24 03:15:25 +000047class FileManager::UniqueDirContainer {
48 /// UniqueDirs - Cache from ID's to existing directories/files.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000049 std::map<llvm::sys::fs::UniqueID, DirectoryEntry> UniqueDirs;
Ted Kremenekd87eef82008-02-24 03:15:25 +000050
51public:
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000052 /// getDirectory - Return an existing DirectoryEntry with the given
53 /// ID's if there is already one; otherwise create and return a
54 /// default-constructed DirectoryEntry.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000055 DirectoryEntry &getDirectory(const llvm::sys::fs::UniqueID &UniqueID) {
56 return UniqueDirs[UniqueID];
Ted Kremenekd87eef82008-02-24 03:15:25 +000057 }
58
Chris Lattner966b25b2010-11-23 20:30:42 +000059 size_t size() const { return UniqueDirs.size(); }
Ted Kremenekd87eef82008-02-24 03:15:25 +000060};
61
62class FileManager::UniqueFileContainer {
63 /// UniqueFiles - Cache from ID's to existing directories/files.
Ted Kremenekd87eef82008-02-24 03:15:25 +000064 std::set<FileEntry> UniqueFiles;
65
66public:
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000067 /// getFile - Return an existing FileEntry with the given ID's if
68 /// there is already one; otherwise create and return a
69 /// default-constructed FileEntry.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000070 FileEntry &getFile(llvm::sys::fs::UniqueID UniqueID, bool IsNamedPipe,
71 bool InPCH) {
72 return const_cast<FileEntry &>(
73 *UniqueFiles.insert(FileEntry(UniqueID, IsNamedPipe, InPCH)).first);
Ted Kremenekd87eef82008-02-24 03:15:25 +000074 }
75
Chris Lattner966b25b2010-11-23 20:30:42 +000076 size_t size() const { return UniqueFiles.size(); }
Axel Naumann38179d92012-06-27 09:17:42 +000077
Axel Naumannb3074002012-07-10 16:50:27 +000078 void erase(const FileEntry *Entry) { UniqueFiles.erase(*Entry); }
Ted Kremenekd87eef82008-02-24 03:15:25 +000079};
80
Ted Kremenek5c04bd82009-01-28 00:27:31 +000081//===----------------------------------------------------------------------===//
82// Common logic.
83//===----------------------------------------------------------------------===//
Ted Kremenekd87eef82008-02-24 03:15:25 +000084
Ben Langmuirc8130a72014-02-20 21:59:23 +000085FileManager::FileManager(const FileSystemOptions &FSO,
86 IntrusiveRefCntPtr<vfs::FileSystem> FS)
87 : FS(FS), FileSystemOpts(FSO),
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000088 UniqueRealDirs(*new UniqueDirContainer()),
89 UniqueRealFiles(*new UniqueFileContainer()),
90 SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
Ted Kremenekd87eef82008-02-24 03:15:25 +000091 NumDirLookups = NumFileLookups = 0;
92 NumDirCacheMisses = NumFileCacheMisses = 0;
Ben Langmuirc8130a72014-02-20 21:59:23 +000093
94 // If the caller doesn't provide a virtual file system, just grab the real
95 // file system.
96 if (!FS)
97 this->FS = vfs::getRealFileSystem();
Ted Kremenekd87eef82008-02-24 03:15:25 +000098}
99
100FileManager::~FileManager() {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000101 delete &UniqueRealDirs;
102 delete &UniqueRealFiles;
Chris Lattner966b25b2010-11-23 20:30:42 +0000103 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
104 delete VirtualFileEntries[i];
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000105 for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
106 delete VirtualDirectoryEntries[i];
Ted Kremenekd87eef82008-02-24 03:15:25 +0000107}
108
Chris Lattner226efd32010-11-23 19:19:34 +0000109void FileManager::addStatCache(FileSystemStatCache *statCache,
110 bool AtBeginning) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000111 assert(statCache && "No stat cache provided?");
112 if (AtBeginning || StatCache.get() == 0) {
113 statCache->setNextStatCache(StatCache.take());
114 StatCache.reset(statCache);
115 return;
116 }
117
Chris Lattner226efd32010-11-23 19:19:34 +0000118 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000119 while (LastCache->getNextStatCache())
120 LastCache = LastCache->getNextStatCache();
121
122 LastCache->setNextStatCache(statCache);
123}
124
Chris Lattner226efd32010-11-23 19:19:34 +0000125void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000126 if (!statCache)
127 return;
128
129 if (StatCache.get() == statCache) {
130 // This is the first stat cache.
131 StatCache.reset(StatCache->takeNextStatCache());
132 return;
133 }
134
135 // Find the stat cache in the list.
Chris Lattner226efd32010-11-23 19:19:34 +0000136 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000137 while (PrevCache && PrevCache->getNextStatCache() != statCache)
138 PrevCache = PrevCache->getNextStatCache();
Chris Lattner9624b692010-11-23 20:50:22 +0000139
140 assert(PrevCache && "Stat cache not found for removal");
141 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000142}
143
Manuel Klimek3aad8552012-07-31 13:56:54 +0000144void FileManager::clearStatCaches() {
145 StatCache.reset(0);
146}
147
Douglas Gregor407e2122009-12-02 18:12:28 +0000148/// \brief Retrieve the directory that the given file name resides in.
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000149/// Filename can point to either a real file or a virtual file.
Douglas Gregor407e2122009-12-02 18:12:28 +0000150static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000151 StringRef Filename,
152 bool CacheFailure) {
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000153 if (Filename.empty())
154 return NULL;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000155
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000156 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
157 return NULL; // If Filename is a directory.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000158
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000159 StringRef DirName = llvm::sys::path::parent_path(Filename);
Chris Lattner0c0e8042010-11-21 09:50:16 +0000160 // Use the current directory if file has no path component.
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000161 if (DirName.empty())
162 DirName = ".";
Douglas Gregor407e2122009-12-02 18:12:28 +0000163
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000164 return FileMgr.getDirectory(DirName, CacheFailure);
Douglas Gregor407e2122009-12-02 18:12:28 +0000165}
166
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000167/// Add all ancestors of the given path (pointing to either a file or
168/// a directory) as virtual directories.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000169void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
170 StringRef DirName = llvm::sys::path::parent_path(Path);
Zhanyong Wanf3c0ff72011-02-11 21:25:35 +0000171 if (DirName.empty())
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000172 return;
173
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000174 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
175 SeenDirEntries.GetOrCreateValue(DirName);
176
177 // When caching a virtual directory, we always cache its ancestors
178 // at the same time. Therefore, if DirName is already in the cache,
179 // we don't need to recurse as its ancestors must also already be in
180 // the cache.
181 if (NamedDirEnt.getValue())
182 return;
183
184 // Add the virtual directory to the cache.
185 DirectoryEntry *UDE = new DirectoryEntry;
186 UDE->Name = NamedDirEnt.getKeyData();
187 NamedDirEnt.setValue(UDE);
188 VirtualDirectoryEntries.push_back(UDE);
189
190 // Recursively add the other ancestors.
191 addAncestorsAsVirtualDirs(DirName);
192}
193
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000194const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
195 bool CacheFailure) {
NAKAMURA Takumi8bd8ee72012-06-16 06:04:10 +0000196 // stat doesn't like trailing separators except for root directory.
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000197 // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
198 // (though it can strip '\\')
NAKAMURA Takumi8bd8ee72012-06-16 06:04:10 +0000199 if (DirName.size() > 1 &&
200 DirName != llvm::sys::path::root_path(DirName) &&
201 llvm::sys::path::is_separator(DirName.back()))
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000202 DirName = DirName.substr(0, DirName.size()-1);
Rafael Espindolaee305462013-07-29 15:47:24 +0000203#ifdef LLVM_ON_WIN32
204 // Fixing a problem with "clang C:test.c" on Windows.
205 // Stat("C:") does not recognize "C:" as a valid directory
206 std::string DirNameStr;
207 if (DirName.size() > 1 && DirName.back() == ':' &&
208 DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
209 DirNameStr = DirName.str() + '.';
210 DirName = DirNameStr;
211 }
212#endif
NAKAMURA Takumi32f1acf2011-11-17 06:16:05 +0000213
Chris Lattner22eb9722006-06-18 05:43:12 +0000214 ++NumDirLookups;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000215 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000216 SeenDirEntries.GetOrCreateValue(DirName);
Mike Stump11289f42009-09-09 15:08:12 +0000217
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000218 // See if there was already an entry in the map. Note that the map
219 // contains both virtual and real directories.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000220 if (NamedDirEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000221 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000222 ? 0 : NamedDirEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000223
Chris Lattner22eb9722006-06-18 05:43:12 +0000224 ++NumDirCacheMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000225
Chris Lattneraf653752006-10-30 03:06:54 +0000226 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000227 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump11289f42009-09-09 15:08:12 +0000228
Chris Lattner43fd42e2006-10-30 03:40:58 +0000229 // Get the null-terminated directory name as stored as the key of the
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000230 // SeenDirEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000231 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000232
Chris Lattneraf653752006-10-30 03:06:54 +0000233 // Check to see if the directory exists.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000234 FileData Data;
235 if (getStatValue(InterndDirName, Data, false, 0 /*directory lookup*/)) {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000236 // There's no real directory at the given path.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000237 if (!CacheFailure)
238 SeenDirEntries.erase(DirName);
Chris Lattner22eb9722006-06-18 05:43:12 +0000239 return 0;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000240 }
Ted Kremenekd87eef82008-02-24 03:15:25 +0000241
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000242 // It exists. See if we have already opened a directory with the
243 // same inode (this occurs on Unix-like systems when one dir is
244 // symlinked to another, for example) or the same path (on
245 // Windows).
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000246 DirectoryEntry &UDE =
247 UniqueRealDirs.getDirectory(Data.UniqueID);
Mike Stump11289f42009-09-09 15:08:12 +0000248
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000249 NamedDirEnt.setValue(&UDE);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000250 if (!UDE.getName()) {
251 // We don't have this directory yet, add it. We use the string
252 // key from the SeenDirEntries map as the string.
253 UDE.Name = InterndDirName;
254 }
Mike Stump11289f42009-09-09 15:08:12 +0000255
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000256 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000257}
258
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000259const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
260 bool CacheFailure) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000261 ++NumFileLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000262
Chris Lattner22eb9722006-06-18 05:43:12 +0000263 // See if there is already an entry in the map.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000264 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000265 SeenFileEntries.GetOrCreateValue(Filename);
Chris Lattner22eb9722006-06-18 05:43:12 +0000266
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000267 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000268 if (NamedFileEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000269 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000270 ? 0 : NamedFileEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000271
Chris Lattner22eb9722006-06-18 05:43:12 +0000272 ++NumFileCacheMisses;
273
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000274 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000275 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Chris Lattner22eb9722006-06-18 05:43:12 +0000276
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000277 // Get the null-terminated file name as stored as the key of the
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000278 // SeenFileEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000279 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattner966b25b2010-11-23 20:30:42 +0000281 // Look up the directory for the file. When looking up something like
282 // sys/foo.h we'll discover all of the search directories that have a 'sys'
283 // subdirectory. This will let us avoid having to waste time on known-to-fail
284 // searches when we go to find sys/bar.h, because all the search directories
285 // without a 'sys' subdir will get a cached failure result.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000286 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
287 CacheFailure);
288 if (DirInfo == 0) { // Directory doesn't exist, file can't exist.
289 if (!CacheFailure)
290 SeenFileEntries.erase(Filename);
291
Douglas Gregor407e2122009-12-02 18:12:28 +0000292 return 0;
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000293 }
294
Chris Lattner22eb9722006-06-18 05:43:12 +0000295 // FIXME: Use the directory info to prune this, before doing the stat syscall.
296 // FIXME: This will reduce the # syscalls.
Mike Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattner22eb9722006-06-18 05:43:12 +0000298 // Nope, there isn't. Check to see if the file exists.
Ben Langmuirc8130a72014-02-20 21:59:23 +0000299 vfs::File *F = 0;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000300 FileData Data;
Ben Langmuirc8130a72014-02-20 21:59:23 +0000301 if (getStatValue(InterndFileName, Data, true, openFile ? &F : 0)) {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000302 // There's no real file at the given path.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000303 if (!CacheFailure)
304 SeenFileEntries.erase(Filename);
305
Chris Lattner22eb9722006-06-18 05:43:12 +0000306 return 0;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000307 }
Mike Stump11289f42009-09-09 15:08:12 +0000308
Patrik Hagglundab01d4b2014-02-21 07:23:53 +0000309 assert((openFile || !F) && "undesired open file");
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000310
Ted Kremenekf4c38c92007-12-18 22:29:39 +0000311 // It exists. See if we have already opened a file with the same inode.
Chris Lattner22eb9722006-06-18 05:43:12 +0000312 // This occurs when one dir is symlinked to another, for example.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000313 FileEntry &UFE =
314 UniqueRealFiles.getFile(Data.UniqueID, Data.IsNamedPipe, Data.InPCH);
Mike Stump11289f42009-09-09 15:08:12 +0000315
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000316 NamedFileEnt.setValue(&UFE);
Chris Lattnerdd278432010-11-23 21:17:56 +0000317 if (UFE.getName()) { // Already have an entry with this inode, return it.
318 // If the stat process opened the file, close it to avoid a FD leak.
Ben Langmuirc8130a72014-02-20 21:59:23 +0000319 if (F)
320 delete F;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000321
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000322 return &UFE;
Chris Lattnerdd278432010-11-23 21:17:56 +0000323 }
Chris Lattner269c2322006-06-25 06:23:00 +0000324
Chris Lattner22eb9722006-06-18 05:43:12 +0000325 // Otherwise, we don't have this directory yet, add it.
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000326 // FIXME: Change the name to be a char* that points back to the
327 // 'SeenFileEntries' key.
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000328 UFE.Name = InterndFileName;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000329 UFE.Size = Data.Size;
330 UFE.ModTime = Data.ModTime;
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000331 UFE.Dir = DirInfo;
332 UFE.UID = NextFileUID++;
Ben Langmuirc8130a72014-02-20 21:59:23 +0000333 UFE.File.reset(F);
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000334 return &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000335}
336
Douglas Gregor407e2122009-12-02 18:12:28 +0000337const FileEntry *
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000338FileManager::getVirtualFile(StringRef Filename, off_t Size,
Chris Lattner5159f612010-11-23 08:35:12 +0000339 time_t ModificationTime) {
Douglas Gregor407e2122009-12-02 18:12:28 +0000340 ++NumFileLookups;
341
342 // See if there is already an entry in the map.
343 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000344 SeenFileEntries.GetOrCreateValue(Filename);
Douglas Gregor407e2122009-12-02 18:12:28 +0000345
346 // See if there is already an entry in the map.
Axel Naumann63fbaed2011-01-27 10:55:51 +0000347 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
348 return NamedFileEnt.getValue();
Douglas Gregor407e2122009-12-02 18:12:28 +0000349
350 ++NumFileCacheMisses;
351
352 // By default, initialize it to invalid.
353 NamedFileEnt.setValue(NON_EXISTENT_FILE);
354
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000355 addAncestorsAsVirtualDirs(Filename);
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000356 FileEntry *UFE = 0;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000357
358 // Now that all ancestors of Filename are in the cache, the
359 // following call is guaranteed to find the DirectoryEntry from the
360 // cache.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000361 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
362 /*CacheFailure=*/true);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000363 assert(DirInfo &&
364 "The directory of a virtual file should already be in the cache.");
Douglas Gregor407e2122009-12-02 18:12:28 +0000365
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000366 // Check to see if the file exists. If so, drop the virtual file
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000367 FileData Data;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000368 const char *InterndFileName = NamedFileEnt.getKeyData();
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000369 if (getStatValue(InterndFileName, Data, true, 0) == 0) {
370 Data.Size = Size;
371 Data.ModTime = ModificationTime;
372 UFE = &UniqueRealFiles.getFile(Data.UniqueID, Data.IsNamedPipe, Data.InPCH);
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000373
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000374 NamedFileEnt.setValue(UFE);
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000375
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000376 // If we had already opened this file, close it now so we don't
377 // leak the descriptor. We're not going to use the file
378 // descriptor anyway, since this is a virtual file.
Ben Langmuirc8130a72014-02-20 21:59:23 +0000379 if (UFE->File)
380 UFE->closeFile();
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000381
382 // If we already have an entry with this inode, return it.
383 if (UFE->getName())
384 return UFE;
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000385 }
386
387 if (!UFE) {
388 UFE = new FileEntry();
389 VirtualFileEntries.push_back(UFE);
390 NamedFileEnt.setValue(UFE);
391 }
Douglas Gregor407e2122009-12-02 18:12:28 +0000392
Chris Lattner9624b692010-11-23 20:50:22 +0000393 UFE->Name = InterndFileName;
Douglas Gregor407e2122009-12-02 18:12:28 +0000394 UFE->Size = Size;
395 UFE->ModTime = ModificationTime;
396 UFE->Dir = DirInfo;
397 UFE->UID = NextFileUID++;
Ben Langmuirc8130a72014-02-20 21:59:23 +0000398 UFE->File.reset();
Douglas Gregor407e2122009-12-02 18:12:28 +0000399 return UFE;
400}
401
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000402void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
403 StringRef pathRef(path.data(), path.size());
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000404
Anders Carlsson9ba8fb12011-03-14 01:13:54 +0000405 if (FileSystemOpts.WorkingDir.empty()
406 || llvm::sys::path::is_absolute(pathRef))
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000407 return;
408
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000409 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000410 llvm::sys::path::append(NewPath, pathRef);
Chris Lattner6e640992010-11-23 04:45:28 +0000411 path = NewPath;
412}
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000413
Chris Lattner6e640992010-11-23 04:45:28 +0000414llvm::MemoryBuffer *FileManager::
Argyrios Kyrtzidis6d7833f2012-07-11 20:59:04 +0000415getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
416 bool isVolatile) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000417 OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000418 llvm::error_code ec;
Argyrios Kyrtzidis669b0b12011-03-15 00:47:44 +0000419
Argyrios Kyrtzidis6d7833f2012-07-11 20:59:04 +0000420 uint64_t FileSize = Entry->getSize();
421 // If there's a high enough chance that the file have changed since we
422 // got its size, force a stat before opening it.
423 if (isVolatile)
424 FileSize = -1;
425
Argyrios Kyrtzidis669b0b12011-03-15 00:47:44 +0000426 const char *Filename = Entry->getName();
427 // If the file is already open, use the open file descriptor.
Ben Langmuirc8130a72014-02-20 21:59:23 +0000428 if (Entry->File) {
429 ec = Entry->File->getBuffer(Filename, Result, FileSize);
Argyrios Kyrtzidis669b0b12011-03-15 00:47:44 +0000430 if (ErrorStr)
431 *ErrorStr = ec.message();
Ben Langmuirc8130a72014-02-20 21:59:23 +0000432 Entry->closeFile();
Argyrios Kyrtzidis669b0b12011-03-15 00:47:44 +0000433 return Result.take();
434 }
435
436 // Otherwise, open the file.
437
Chris Lattner5ea7d072010-11-23 22:32:37 +0000438 if (FileSystemOpts.WorkingDir.empty()) {
Ben Langmuirc8130a72014-02-20 21:59:23 +0000439 ec = FS->getBufferForFile(Filename, Result, FileSize);
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000440 if (ec && ErrorStr)
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000441 *ErrorStr = ec.message();
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000442 return Result.take();
Chris Lattner5ea7d072010-11-23 22:32:37 +0000443 }
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000444
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000445 SmallString<128> FilePath(Entry->getName());
Anders Carlsson878b3e22011-03-07 01:28:33 +0000446 FixupRelativePath(FilePath);
Ben Langmuirc8130a72014-02-20 21:59:23 +0000447 ec = FS->getBufferForFile(FilePath.str(), Result, FileSize);
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000448 if (ec && ErrorStr)
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000449 *ErrorStr = ec.message();
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000450 return Result.take();
Chris Lattner26b5c192010-11-23 09:19:42 +0000451}
452
453llvm::MemoryBuffer *FileManager::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000454getBufferForFile(StringRef Filename, std::string *ErrorStr) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000455 OwningPtr<llvm::MemoryBuffer> Result;
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000456 llvm::error_code ec;
457 if (FileSystemOpts.WorkingDir.empty()) {
Ben Langmuirc8130a72014-02-20 21:59:23 +0000458 ec = FS->getBufferForFile(Filename, Result);
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000459 if (ec && ErrorStr)
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000460 *ErrorStr = ec.message();
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000461 return Result.take();
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000462 }
463
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000464 SmallString<128> FilePath(Filename);
Anders Carlsson878b3e22011-03-07 01:28:33 +0000465 FixupRelativePath(FilePath);
Ben Langmuirc8130a72014-02-20 21:59:23 +0000466 ec = FS->getBufferForFile(FilePath.c_str(), Result);
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000467 if (ec && ErrorStr)
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000468 *ErrorStr = ec.message();
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000469 return Result.take();
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000470}
471
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000472/// getStatValue - Get the 'stat' information for the specified path,
473/// using the cache to accelerate it if possible. This returns true
474/// if the path points to a virtual file or does not exist, or returns
475/// false if it's an existent real file. If FileDescriptor is NULL,
476/// do directory look-up instead of file look-up.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000477bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
Ben Langmuirc8130a72014-02-20 21:59:23 +0000478 vfs::File **F) {
Chris Lattner226efd32010-11-23 19:19:34 +0000479 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
480 // absolute!
Chris Lattner5769c3d2010-11-23 19:56:39 +0000481 if (FileSystemOpts.WorkingDir.empty())
Ben Langmuirc8130a72014-02-20 21:59:23 +0000482 return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000483
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000484 SmallString<128> FilePath(Path);
Anders Carlsson878b3e22011-03-07 01:28:33 +0000485 FixupRelativePath(FilePath);
Chris Lattner5769c3d2010-11-23 19:56:39 +0000486
Ben Langmuirc8130a72014-02-20 21:59:23 +0000487 return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F,
488 StatCache.get(), *FS);
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000489}
490
Rafael Espindolae4777f42013-07-29 18:22:23 +0000491bool FileManager::getNoncachedStatValue(StringRef Path,
Ben Langmuirc8130a72014-02-20 21:59:23 +0000492 vfs::Status &Result) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000493 SmallString<128> FilePath(Path);
Anders Carlsson5e368402011-03-18 19:23:19 +0000494 FixupRelativePath(FilePath);
495
Ben Langmuirc8130a72014-02-20 21:59:23 +0000496 llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str());
497 if (!S)
498 return true;
499 Result = *S;
500 return false;
Anders Carlsson5e368402011-03-18 19:23:19 +0000501}
502
Axel Naumannb3074002012-07-10 16:50:27 +0000503void FileManager::invalidateCache(const FileEntry *Entry) {
504 assert(Entry && "Cannot invalidate a NULL FileEntry");
Axel Naumann38179d92012-06-27 09:17:42 +0000505
506 SeenFileEntries.erase(Entry->getName());
Axel Naumannb3074002012-07-10 16:50:27 +0000507
508 // FileEntry invalidation should not block future optimizations in the file
509 // caches. Possible alternatives are cache truncation (invalidate last N) or
510 // invalidation of the whole cache.
511 UniqueRealFiles.erase(Entry);
Axel Naumann38179d92012-06-27 09:17:42 +0000512}
513
514
Douglas Gregor09b69892011-02-10 17:09:37 +0000515void FileManager::GetUniqueIDMapping(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000516 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
Douglas Gregor09b69892011-02-10 17:09:37 +0000517 UIDToFiles.clear();
518 UIDToFiles.resize(NextFileUID);
519
520 // Map file entries
521 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000522 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
Douglas Gregor09b69892011-02-10 17:09:37 +0000523 FE != FEEnd; ++FE)
524 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
525 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
526
527 // Map virtual file entries
Craig Topper2341c0d2013-07-04 03:08:24 +0000528 for (SmallVectorImpl<FileEntry *>::const_iterator
Douglas Gregor09b69892011-02-10 17:09:37 +0000529 VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
530 VFE != VFEEnd; ++VFE)
531 if (*VFE && *VFE != NON_EXISTENT_FILE)
532 UIDToFiles[(*VFE)->getUID()] = *VFE;
533}
Chris Lattner226efd32010-11-23 19:19:34 +0000534
Argyrios Kyrtzidis6eec06d2012-05-03 21:50:39 +0000535void FileManager::modifyFileEntry(FileEntry *File,
536 off_t Size, time_t ModificationTime) {
537 File->Size = Size;
538 File->ModTime = ModificationTime;
539}
540
Douglas Gregore00c8b22013-01-26 00:55:12 +0000541StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
542 // FIXME: use llvm::sys::fs::canonical() when it gets implemented
543#ifdef LLVM_ON_UNIX
544 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
545 = CanonicalDirNames.find(Dir);
546 if (Known != CanonicalDirNames.end())
547 return Known->second;
548
549 StringRef CanonicalName(Dir->getName());
550 char CanonicalNameBuf[PATH_MAX];
551 if (realpath(Dir->getName(), CanonicalNameBuf)) {
552 unsigned Len = strlen(CanonicalNameBuf);
553 char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1));
554 memcpy(Mem, CanonicalNameBuf, Len);
555 CanonicalName = StringRef(Mem, Len);
556 }
557
558 CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
559 return CanonicalName;
560#else
561 return StringRef(Dir->getName());
562#endif
563}
Chris Lattner226efd32010-11-23 19:19:34 +0000564
Chris Lattner22eb9722006-06-18 05:43:12 +0000565void FileManager::PrintStats() const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000566 llvm::errs() << "\n*** File Manager Stats:\n";
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000567 llvm::errs() << UniqueRealFiles.size() << " real files found, "
568 << UniqueRealDirs.size() << " real dirs found.\n";
569 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
570 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000571 llvm::errs() << NumDirLookups << " dir lookups, "
572 << NumDirCacheMisses << " dir cache misses.\n";
573 llvm::errs() << NumFileLookups << " file lookups, "
574 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000575
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000576 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Chris Lattner22eb9722006-06-18 05:43:12 +0000577}