blob: 9c5f1d59c194a1e6b00d084974a95202428bc0c4 [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 Lattnerd57a7ef2009-08-23 22:45:33 +000022#include "llvm/Support/raw_ostream.h"
Douglas Gregor4fed3f42009-04-27 18:38:38 +000023#include "llvm/System/Path.h"
Ted Kremenek6bb816a2008-02-24 03:15:25 +000024#include "llvm/Config/config.h"
Benjamin Kramer458fb102009-09-05 09:49:39 +000025#include <map>
26#include <set>
27#include <string>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
30// FIXME: Enhance libsystem to support inode and other fields.
31#include <sys/stat.h>
32
Chris Lattnera8c11c62007-09-03 18:37:14 +000033#if defined(_MSC_VER)
Chris Lattner3102c832009-02-12 01:37:35 +000034#define S_ISDIR(s) (_S_IFDIR & s)
Chris Lattnera8c11c62007-09-03 18:37:14 +000035#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000036
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000037/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000038/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000039#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000040
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000041//===----------------------------------------------------------------------===//
42// Windows.
43//===----------------------------------------------------------------------===//
44
Ted Kremenek6bb816a2008-02-24 03:15:25 +000045#ifdef LLVM_ON_WIN32
46
47#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
48
49namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000050 static std::string GetFullPath(const char *relPath) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000051 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
52 assert(absPathStrPtr && "_fullpath() returned NULL!");
53
54 std::string absPath(absPathStrPtr);
55
56 free(absPathStrPtr);
57 return absPath;
58 }
59}
60
61class FileManager::UniqueDirContainer {
62 /// UniqueDirs - Cache from full path to existing directories/files.
63 ///
Mike Stump1eb44332009-09-09 15:08:12 +000064 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000065
66public:
67 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
68 std::string FullPath(GetFullPath(Name));
69 return UniqueDirs.GetOrCreateValue(
70 FullPath.c_str(),
71 FullPath.c_str() + FullPath.size()
72 ).getValue();
73 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Ted Kremenek6bb816a2008-02-24 03:15:25 +000075 size_t size() { return UniqueDirs.size(); }
76};
77
78class FileManager::UniqueFileContainer {
79 /// UniqueFiles - Cache from full path to existing directories/files.
80 ///
Ted Kremenek75368892009-01-28 01:01:07 +000081 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000082
83public:
84 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
85 std::string FullPath(GetFullPath(Name));
86 return UniqueFiles.GetOrCreateValue(
87 FullPath.c_str(),
88 FullPath.c_str() + FullPath.size()
89 ).getValue();
90 }
91
92 size_t size() { return UniqueFiles.size(); }
93};
94
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000095//===----------------------------------------------------------------------===//
96// Unix-like Systems.
97//===----------------------------------------------------------------------===//
98
Ted Kremenek6bb816a2008-02-24 03:15:25 +000099#else
100
101#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
102
103class FileManager::UniqueDirContainer {
104 /// UniqueDirs - Cache from ID's to existing directories/files.
105 ///
Mike Stump1eb44332009-09-09 15:08:12 +0000106 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000107
108public:
109 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
110 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
111 }
112
113 size_t size() { return UniqueDirs.size(); }
114};
115
116class FileManager::UniqueFileContainer {
117 /// UniqueFiles - Cache from ID's to existing directories/files.
118 ///
119 std::set<FileEntry> UniqueFiles;
120
121public:
122 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
123 return
124 const_cast<FileEntry&>(
125 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000126 StatBuf.st_ino,
127 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000128 }
129
130 size_t size() { return UniqueFiles.size(); }
131};
132
133#endif
134
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000135//===----------------------------------------------------------------------===//
136// Common logic.
137//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000138
Ted Kremenek96438f32009-02-12 03:17:57 +0000139FileManager::FileManager()
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000140 : UniqueDirs(*new UniqueDirContainer),
141 UniqueFiles(*new UniqueFileContainer),
Ted Kremenek96438f32009-02-12 03:17:57 +0000142 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000143 NumDirLookups = NumFileLookups = 0;
144 NumDirCacheMisses = NumFileCacheMisses = 0;
145}
146
147FileManager::~FileManager() {
148 delete &UniqueDirs;
149 delete &UniqueFiles;
Douglas Gregor057e5672009-12-02 18:12:28 +0000150 for (llvm::SmallVectorImpl<FileEntry *>::iterator
151 V = VirtualFileEntries.begin(),
152 VEnd = VirtualFileEntries.end();
153 V != VEnd;
154 ++V)
155 delete *V;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000156}
157
Douglas Gregor52e71082009-10-16 18:18:30 +0000158void FileManager::addStatCache(StatSysCallCache *statCache, bool AtBeginning) {
159 assert(statCache && "No stat cache provided?");
160 if (AtBeginning || StatCache.get() == 0) {
161 statCache->setNextStatCache(StatCache.take());
162 StatCache.reset(statCache);
163 return;
164 }
165
166 StatSysCallCache *LastCache = StatCache.get();
167 while (LastCache->getNextStatCache())
168 LastCache = LastCache->getNextStatCache();
169
170 LastCache->setNextStatCache(statCache);
171}
172
173void FileManager::removeStatCache(StatSysCallCache *statCache) {
174 if (!statCache)
175 return;
176
177 if (StatCache.get() == statCache) {
178 // This is the first stat cache.
179 StatCache.reset(StatCache->takeNextStatCache());
180 return;
181 }
182
183 // Find the stat cache in the list.
184 StatSysCallCache *PrevCache = StatCache.get();
185 while (PrevCache && PrevCache->getNextStatCache() != statCache)
186 PrevCache = PrevCache->getNextStatCache();
187 if (PrevCache)
188 PrevCache->setNextStatCache(statCache->getNextStatCache());
189 else
190 assert(false && "Stat cache not found for removal");
191}
192
Douglas Gregor057e5672009-12-02 18:12:28 +0000193/// \brief Retrieve the directory that the given file name resides in.
194static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
195 const char *NameStart,
196 const char *NameEnd) {
197 // Figure out what directory it is in. If the string contains a / in it,
198 // strip off everything after it.
199 // FIXME: this logic should be in sys::Path.
200 const char *SlashPos = NameEnd-1;
201 while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
202 --SlashPos;
203 // Ignore duplicate //'s.
204 while (SlashPos > NameStart && IS_DIR_SEPARATOR_CHAR(SlashPos[-1]))
205 --SlashPos;
206
207 if (SlashPos < NameStart) {
208 // Use the current directory if file has no path component.
209 const char *Name = ".";
210 return FileMgr.getDirectory(Name, Name+1);
211 } else if (SlashPos == NameEnd-1)
212 return 0; // If filename ends with a /, it's a directory.
213 else
214 return FileMgr.getDirectory(NameStart, SlashPos);
215}
216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217/// getDirectory - Lookup, cache, and verify the specified directory. This
218/// returns null if the directory doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000219///
Reid Spencer5f016e22007-07-11 17:01:13 +0000220const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
221 const char *NameEnd) {
222 ++NumDirLookups;
223 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
224 DirEntries.GetOrCreateValue(NameStart, NameEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 // See if there is already an entry in the map.
227 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000228 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000234 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 // Get the null-terminated directory name as stored as the key of the
237 // DirEntries map.
238 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // Check to see if the directory exists.
241 struct stat StatBuf;
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000242 if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing.
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
244 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // It exists. See if we have already opened a directory with the same inode.
Mike Stump1eb44332009-09-09 15:08:12 +0000247 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000248 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 NamedDirEnt.setValue(&UDE);
251 if (UDE.getName()) // Already have an entry with this inode, return it.
252 return &UDE;
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 // Otherwise, we don't have this directory yet, add it. We use the string
255 // key from the DirEntries map as the string.
256 UDE.Name = InterndDirName;
257 return &UDE;
258}
259
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000260/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +0000261/// represent a filename that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000262#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +0000263
264/// getFile - Lookup, cache, and verify the specified file. This returns null
265/// if the file doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000266///
Reid Spencer5f016e22007-07-11 17:01:13 +0000267const FileEntry *FileManager::getFile(const char *NameStart,
268 const char *NameEnd) {
269 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 // See if there is already an entry in the map.
272 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
273 FileEntries.GetOrCreateValue(NameStart, NameEnd);
274
275 // See if there is already an entry in the map.
276 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000277 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 ++NumFileCacheMisses;
281
282 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000283 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000284
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 // Get the null-terminated file name as stored as the key of the
287 // FileEntries map.
288 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Douglas Gregor057e5672009-12-02 18:12:28 +0000290 const DirectoryEntry *DirInfo
291 = getDirectoryFromFile(*this, NameStart, NameEnd);
292 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
293 return 0;
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // FIXME: Use the directory info to prune this, before doing the stat syscall.
296 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 // Nope, there isn't. Check to see if the file exists.
299 struct stat StatBuf;
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000300 //llvm::errs() << "STATING: " << Filename;
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000301 if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing.
302 S_ISDIR(StatBuf.st_mode)) { // A directory?
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 // If this file doesn't exist, we leave a null in FileEntries for this path.
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000304 //llvm::errs() << ": Not existing\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 return 0;
306 }
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000307 //llvm::errs() << ": exists\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenekbca6d122007-12-18 22:29:39 +0000309 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000311 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 NamedFileEnt.setValue(&UFE);
314 if (UFE.getName()) // Already have an entry with this inode, return it.
315 return &UFE;
316
317 // Otherwise, we don't have this directory yet, add it.
318 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
319 // key.
320 UFE.Name = InterndFileName;
321 UFE.Size = StatBuf.st_size;
322 UFE.ModTime = StatBuf.st_mtime;
323 UFE.Dir = DirInfo;
324 UFE.UID = NextFileUID++;
325 return &UFE;
326}
327
Douglas Gregor057e5672009-12-02 18:12:28 +0000328const FileEntry *
329FileManager::getVirtualFile(const llvm::StringRef &Filename,
330 off_t Size, time_t ModificationTime) {
331 const char *NameStart = Filename.begin(), *NameEnd = Filename.end();
332
333 ++NumFileLookups;
334
335 // See if there is already an entry in the map.
336 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
337 FileEntries.GetOrCreateValue(NameStart, NameEnd);
338
339 // See if there is already an entry in the map.
340 if (NamedFileEnt.getValue())
341 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
342 ? 0 : NamedFileEnt.getValue();
343
344 ++NumFileCacheMisses;
345
346 // By default, initialize it to invalid.
347 NamedFileEnt.setValue(NON_EXISTENT_FILE);
348
349 const DirectoryEntry *DirInfo
350 = getDirectoryFromFile(*this, NameStart, NameEnd);
351 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
352 return 0;
353
354 FileEntry *UFE = new FileEntry();
355 VirtualFileEntries.push_back(UFE);
356 NamedFileEnt.setValue(UFE);
357
358 UFE->Name = NamedFileEnt.getKeyData();
359 UFE->Size = Size;
360 UFE->ModTime = ModificationTime;
361 UFE->Dir = DirInfo;
362 UFE->UID = NextFileUID++;
363 return UFE;
364}
365
Reid Spencer5f016e22007-07-11 17:01:13 +0000366void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000367 llvm::errs() << "\n*** File Manager Stats:\n";
368 llvm::errs() << UniqueFiles.size() << " files found, "
369 << UniqueDirs.size() << " dirs found.\n";
370 llvm::errs() << NumDirLookups << " dir lookups, "
371 << NumDirCacheMisses << " dir cache misses.\n";
372 llvm::errs() << NumFileLookups << " file lookups, "
373 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000375 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000377
378int MemorizeStatCalls::stat(const char *path, struct stat *buf) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000379 int result = StatSysCallCache::stat(path, buf);
380
Mike Stump1eb44332009-09-09 15:08:12 +0000381 if (result != 0) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000382 // Cache failed 'stat' results.
383 struct stat empty;
Chris Lattnerf1affe62009-09-18 04:51:01 +0000384 memset(&empty, 0, sizeof(empty));
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000385 StatCalls[path] = StatResult(result, empty);
386 }
387 else if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute()) {
388 // Cache file 'stat' results and directories with absolutely
389 // paths.
390 StatCalls[path] = StatResult(result, *buf);
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
393 return result;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000394}