blob: 565f8a61dee67310c3645987cff060b883757fe1 [file] [log] [blame]
Ted Kremenek8fbc88e2007-12-04 22:42:20 +00001///===--- FileManager.cpp - File System Probing and Caching ----------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the FileManager interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// TODO: This should index all interesting directories with dirent calls.
15// getdirentries ?
16// opendir/readdir_r/closedir ?
17//
18//===----------------------------------------------------------------------===//
19
20#include "clang/Basic/FileManager.h"
21#include "llvm/ADT/SmallString.h"
Chris Lattnerc070da42010-08-23 23:50:42 +000022#include "llvm/ADT/StringExtras.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000023#include "llvm/Support/raw_ostream.h"
Douglas Gregor4fed3f42009-04-27 18:38:38 +000024#include "llvm/System/Path.h"
Ted Kremenek6bb816a2008-02-24 03:15:25 +000025#include "llvm/Config/config.h"
Benjamin Kramer458fb102009-09-05 09:49:39 +000026#include <map>
27#include <set>
28#include <string>
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
31// FIXME: Enhance libsystem to support inode and other fields.
32#include <sys/stat.h>
33
Chris Lattnera8c11c62007-09-03 18:37:14 +000034#if defined(_MSC_VER)
Chris Lattner3102c832009-02-12 01:37:35 +000035#define S_ISDIR(s) (_S_IFDIR & s)
Chris Lattnera8c11c62007-09-03 18:37:14 +000036#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000037
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000038/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000039/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000040#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000041
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000042//===----------------------------------------------------------------------===//
43// Windows.
44//===----------------------------------------------------------------------===//
45
Ted Kremenek6bb816a2008-02-24 03:15:25 +000046#ifdef LLVM_ON_WIN32
47
48#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
49
50namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000051 static std::string GetFullPath(const char *relPath) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000052 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
53 assert(absPathStrPtr && "_fullpath() returned NULL!");
54
55 std::string absPath(absPathStrPtr);
56
57 free(absPathStrPtr);
58 return absPath;
59 }
60}
61
62class FileManager::UniqueDirContainer {
63 /// UniqueDirs - Cache from full path to existing directories/files.
64 ///
Mike Stump1eb44332009-09-09 15:08:12 +000065 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000066
67public:
68 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
69 std::string FullPath(GetFullPath(Name));
70 return UniqueDirs.GetOrCreateValue(
71 FullPath.c_str(),
72 FullPath.c_str() + FullPath.size()
73 ).getValue();
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Ted Kremenek6bb816a2008-02-24 03:15:25 +000076 size_t size() { return UniqueDirs.size(); }
77};
78
79class FileManager::UniqueFileContainer {
80 /// UniqueFiles - Cache from full path to existing directories/files.
81 ///
Ted Kremenek75368892009-01-28 01:01:07 +000082 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000083
84public:
85 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
86 std::string FullPath(GetFullPath(Name));
Chris Lattnerc070da42010-08-23 23:50:42 +000087
88 // LowercaseString because Windows filesystem is case insensitive.
89 FullPath = llvm::LowercaseString(FullPath);
Ted Kremenek6bb816a2008-02-24 03:15:25 +000090 return UniqueFiles.GetOrCreateValue(
91 FullPath.c_str(),
92 FullPath.c_str() + FullPath.size()
93 ).getValue();
94 }
95
96 size_t size() { return UniqueFiles.size(); }
97};
98
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000099//===----------------------------------------------------------------------===//
100// Unix-like Systems.
101//===----------------------------------------------------------------------===//
102
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000103#else
104
105#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
106
107class FileManager::UniqueDirContainer {
108 /// UniqueDirs - Cache from ID's to existing directories/files.
109 ///
Mike Stump1eb44332009-09-09 15:08:12 +0000110 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000111
112public:
113 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
114 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
115 }
116
117 size_t size() { return UniqueDirs.size(); }
118};
119
120class FileManager::UniqueFileContainer {
121 /// UniqueFiles - Cache from ID's to existing directories/files.
122 ///
123 std::set<FileEntry> UniqueFiles;
124
125public:
126 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
127 return
128 const_cast<FileEntry&>(
129 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000130 StatBuf.st_ino,
131 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000132 }
133
134 size_t size() { return UniqueFiles.size(); }
135};
136
137#endif
138
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000139//===----------------------------------------------------------------------===//
140// Common logic.
141//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000142
Ted Kremenek96438f32009-02-12 03:17:57 +0000143FileManager::FileManager()
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000144 : UniqueDirs(*new UniqueDirContainer),
145 UniqueFiles(*new UniqueFileContainer),
Ted Kremenek96438f32009-02-12 03:17:57 +0000146 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000147 NumDirLookups = NumFileLookups = 0;
148 NumDirCacheMisses = NumFileCacheMisses = 0;
149}
150
151FileManager::~FileManager() {
152 delete &UniqueDirs;
153 delete &UniqueFiles;
Douglas Gregor057e5672009-12-02 18:12:28 +0000154 for (llvm::SmallVectorImpl<FileEntry *>::iterator
155 V = VirtualFileEntries.begin(),
156 VEnd = VirtualFileEntries.end();
157 V != VEnd;
158 ++V)
159 delete *V;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000160}
161
Douglas Gregor52e71082009-10-16 18:18:30 +0000162void FileManager::addStatCache(StatSysCallCache *statCache, bool AtBeginning) {
163 assert(statCache && "No stat cache provided?");
164 if (AtBeginning || StatCache.get() == 0) {
165 statCache->setNextStatCache(StatCache.take());
166 StatCache.reset(statCache);
167 return;
168 }
169
170 StatSysCallCache *LastCache = StatCache.get();
171 while (LastCache->getNextStatCache())
172 LastCache = LastCache->getNextStatCache();
173
174 LastCache->setNextStatCache(statCache);
175}
176
177void FileManager::removeStatCache(StatSysCallCache *statCache) {
178 if (!statCache)
179 return;
180
181 if (StatCache.get() == statCache) {
182 // This is the first stat cache.
183 StatCache.reset(StatCache->takeNextStatCache());
184 return;
185 }
186
187 // Find the stat cache in the list.
188 StatSysCallCache *PrevCache = StatCache.get();
189 while (PrevCache && PrevCache->getNextStatCache() != statCache)
190 PrevCache = PrevCache->getNextStatCache();
191 if (PrevCache)
192 PrevCache->setNextStatCache(statCache->getNextStatCache());
193 else
194 assert(false && "Stat cache not found for removal");
195}
196
Douglas Gregor057e5672009-12-02 18:12:28 +0000197/// \brief Retrieve the directory that the given file name resides in.
198static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
199 const char *NameStart,
200 const char *NameEnd) {
201 // Figure out what directory it is in. If the string contains a / in it,
202 // strip off everything after it.
203 // FIXME: this logic should be in sys::Path.
204 const char *SlashPos = NameEnd-1;
205 while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
206 --SlashPos;
207 // Ignore duplicate //'s.
208 while (SlashPos > NameStart && IS_DIR_SEPARATOR_CHAR(SlashPos[-1]))
209 --SlashPos;
210
211 if (SlashPos < NameStart) {
212 // Use the current directory if file has no path component.
213 const char *Name = ".";
214 return FileMgr.getDirectory(Name, Name+1);
215 } else if (SlashPos == NameEnd-1)
216 return 0; // If filename ends with a /, it's a directory.
217 else
218 return FileMgr.getDirectory(NameStart, SlashPos);
219}
220
Reid Spencer5f016e22007-07-11 17:01:13 +0000221/// getDirectory - Lookup, cache, and verify the specified directory. This
222/// returns null if the directory doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000223///
Reid Spencer5f016e22007-07-11 17:01:13 +0000224const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
225 const char *NameEnd) {
John Thompson9a6ac542009-12-18 14:18:21 +0000226 // stat doesn't like trailing separators (at least on Windows).
227 if (((NameEnd - NameStart) > 1) &&
228 ((*(NameEnd - 1) == '/') || (*(NameEnd - 1) == '\\')))
229 NameEnd--;
230
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 ++NumDirLookups;
232 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
233 DirEntries.GetOrCreateValue(NameStart, NameEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 // See if there is already an entry in the map.
236 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000237 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000243 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 // Get the null-terminated directory name as stored as the key of the
246 // DirEntries map.
247 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 // Check to see if the directory exists.
250 struct stat StatBuf;
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000251 if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing.
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
253 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // It exists. See if we have already opened a directory with the same inode.
Mike Stump1eb44332009-09-09 15:08:12 +0000256 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000257 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 NamedDirEnt.setValue(&UDE);
260 if (UDE.getName()) // Already have an entry with this inode, return it.
261 return &UDE;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // Otherwise, we don't have this directory yet, add it. We use the string
264 // key from the DirEntries map as the string.
265 UDE.Name = InterndDirName;
266 return &UDE;
267}
268
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000269/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +0000270/// represent a filename that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000271#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +0000272
273/// getFile - Lookup, cache, and verify the specified file. This returns null
274/// if the file doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000275///
Reid Spencer5f016e22007-07-11 17:01:13 +0000276const FileEntry *FileManager::getFile(const char *NameStart,
277 const char *NameEnd) {
278 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // See if there is already an entry in the map.
281 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
282 FileEntries.GetOrCreateValue(NameStart, NameEnd);
283
284 // See if there is already an entry in the map.
285 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000286 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 ++NumFileCacheMisses;
290
291 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000292 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000293
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Get the null-terminated file name as stored as the key of the
296 // FileEntries map.
297 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Douglas Gregor057e5672009-12-02 18:12:28 +0000299 const DirectoryEntry *DirInfo
300 = getDirectoryFromFile(*this, NameStart, NameEnd);
301 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
302 return 0;
303
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 // FIXME: Use the directory info to prune this, before doing the stat syscall.
305 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // Nope, there isn't. Check to see if the file exists.
308 struct stat StatBuf;
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000309 //llvm::errs() << "STATING: " << Filename;
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000310 if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing.
311 S_ISDIR(StatBuf.st_mode)) { // A directory?
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 // If this file doesn't exist, we leave a null in FileEntries for this path.
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000313 //llvm::errs() << ": Not existing\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 return 0;
315 }
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000316 //llvm::errs() << ": exists\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenekbca6d122007-12-18 22:29:39 +0000318 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000320 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 NamedFileEnt.setValue(&UFE);
323 if (UFE.getName()) // Already have an entry with this inode, return it.
324 return &UFE;
325
326 // Otherwise, we don't have this directory yet, add it.
327 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
328 // key.
329 UFE.Name = InterndFileName;
330 UFE.Size = StatBuf.st_size;
331 UFE.ModTime = StatBuf.st_mtime;
332 UFE.Dir = DirInfo;
333 UFE.UID = NextFileUID++;
334 return &UFE;
335}
336
Douglas Gregor057e5672009-12-02 18:12:28 +0000337const FileEntry *
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000338FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
339 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000340 const char *NameStart = Filename.begin(), *NameEnd = Filename.end();
341
342 ++NumFileLookups;
343
344 // See if there is already an entry in the map.
345 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
346 FileEntries.GetOrCreateValue(NameStart, NameEnd);
347
348 // See if there is already an entry in the map.
349 if (NamedFileEnt.getValue())
350 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
351 ? 0 : NamedFileEnt.getValue();
352
353 ++NumFileCacheMisses;
354
355 // By default, initialize it to invalid.
356 NamedFileEnt.setValue(NON_EXISTENT_FILE);
357
358 const DirectoryEntry *DirInfo
359 = getDirectoryFromFile(*this, NameStart, NameEnd);
360 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
361 return 0;
362
363 FileEntry *UFE = new FileEntry();
364 VirtualFileEntries.push_back(UFE);
365 NamedFileEnt.setValue(UFE);
366
367 UFE->Name = NamedFileEnt.getKeyData();
368 UFE->Size = Size;
369 UFE->ModTime = ModificationTime;
370 UFE->Dir = DirInfo;
371 UFE->UID = NextFileUID++;
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000372
373 // If this virtual file resolves to a file, also map that file to the
374 // newly-created file entry.
375 const char *InterndFileName = NamedFileEnt.getKeyData();
376 struct stat StatBuf;
377 if (!stat_cached(InterndFileName, &StatBuf) &&
378 !S_ISDIR(StatBuf.st_mode)) {
379 llvm::sys::Path FilePath(InterndFileName);
380 FilePath.makeAbsolute();
381 FileEntries[FilePath.str()] = UFE;
382 }
383
Douglas Gregor057e5672009-12-02 18:12:28 +0000384 return UFE;
385}
386
Reid Spencer5f016e22007-07-11 17:01:13 +0000387void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000388 llvm::errs() << "\n*** File Manager Stats:\n";
389 llvm::errs() << UniqueFiles.size() << " files found, "
390 << UniqueDirs.size() << " dirs found.\n";
391 llvm::errs() << NumDirLookups << " dir lookups, "
392 << NumDirCacheMisses << " dir cache misses.\n";
393 llvm::errs() << NumFileLookups << " file lookups, "
394 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000396 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000397}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000398
399int MemorizeStatCalls::stat(const char *path, struct stat *buf) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000400 int result = StatSysCallCache::stat(path, buf);
401
Daniel Dunbar475ddb42009-12-11 00:27:20 +0000402 // Do not cache failed stats, it is easy to construct common inconsistent
403 // situations if we do, and they are not important for PCH performance (which
404 // currently only needs the stats to construct the initial FileManager
405 // entries).
406 if (result != 0)
407 return result;
408
409 // Cache file 'stat' results and directories with absolutely paths.
410 if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute())
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000411 StatCalls[path] = StatResult(result, *buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000412
413 return result;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000414}